bytecodealliance / bytecodealliance/wit-bindgen

Imported functions with a string or list argument should use a const pointer at ABI level (Rust, C++, C, ?)

Open
#1,545 3 comments 0 reactions 0 assignees View on GitHub
gen-cpp gen-rust
Dominant language
Rust
Stars
1.5k
Forks
286
Avg merge
6h 32m
Merged PRs (30d)
19

Description

If I take a look into the generated Rust code for the `tests/runtime/lists` test:
```Rust
pub fn empty_list_param(a: &[u8]) -> () {
unsafe {
let vec0 = a;
let ptr0 = vec0.as_ptr().cast::();
let len0 = vec0.len();
#[cfg(target_arch = "wasm32")]
#[link(wasm_import_module = "test:lists/to-test")]
unsafe extern "C" {
#[link_name = "empty-list-param"]
fn wit_import1(_: *mut u8, _: usize);
}
#[cfg(not(target_arch = "wasm32"))]
unsafe extern "C" fn wit_import1(_: *mut u8, _: usize) {
unreachable!()
}
wit_import1(ptr0.cast_mut(), len0);
}
}
```
The lowered function could be declared as receiving a const pointer (the host won't free or modify the list. This is also required by the immutable slice argument), this would remove the alarming `.cast_mut()` call.

(I came to this conclusion while looking into const correctness in the C++ backend)

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.