Skip to content

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

@cpetig

Description

@cpetig

If I take a look into the generated Rust code for the tests/runtime/lists test:

pub fn empty_list_param(a: &[u8]) -> () {
    unsafe {
        let vec0 = a;
        let ptr0 = vec0.as_ptr().cast::<u8>();
        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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    gen-cppRelated to the C++ code generatorgen-rustRelated to bindings for Rust-compiled-to-WebAssembly

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions