rust-lang / rust-lang/rust-bindgen
C variable length arrays (VLAs) get imported incorrectly
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 5.3k
- Forks
- 829
- Avg merge
- 1d 1h
- Merged PRs (30d)
- 15
Description
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?
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
No source file or test is named. Start with the C declaration and generated Rust binding shown in the issue, then trace how bindgen represents multidimensional VLA parameters. Done means the generated parameter preserves the whole array's contiguous layout and the behavior is covered by a regression case for this declaration.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- c, rust
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100