Skip to content

C variable length arrays (VLAs) get imported incorrectly #3219

Open
@tothambrus11

Description

@tothambrus11

The following C code declares a prototype of a function taking a variable length array argument:

int vla(int w, int xs[][3][w][5]);

Currently, it is imported as:

unsafe extern "C" {
    pub fn vla(
        w: ::std::os::raw::c_int,
        xs: *mut *mut *mut [::std::os::raw::c_int; 5usize],
    ) -> ::std::os::raw::c_int;
}

So the parameter type is transformed into a pointer to a pointer to a pointer to a sized array. This is not correct, because the pointer we need to pass into the function should point to the first element of the whole multidimensional array instead of pointing to some other pointers.

I guess this is very to represent in Rust's type system. It's not possible to compose code like this:
*mut [VLA<[c_int; 5usize]>; 3usize]
because the outer sized array needs to know the size of its element at compile time.

Swift's approach is to just import VLAs as opaque pointers, and gives the responsibility of filling it with data in the right size and layout to the programmer.

Do you have any other ideas for asking for VLAs in Rust that's any better than an opaque pointer?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions