rust-lang / rust-lang/rust-bindgen
Reason for PhantomData on incomplete arrays without lifetimes
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 5.3k
- Forks
- 829
- Avg merge
- 1d 1h
- Merged PRs (30d)
- 15
Description
Input C/C++ Header
struct Foo {
int length;
unsigned char data[0];
};
Bindgen Invocation
bindgen::Builder::default()
.header("input.h")
.generate()
.unwrap()
Actual Results
#[allow(non_snake_case)]
#[repr(C)]
pub struct Foo {
pub size: u32,
pub data: __IncompleteArrayField,
}
#[repr(C)]
#[derive(Default)]
pub struct __IncompleteArrayField<T>(::std::marker::PhantomData<T>, [T; 0]);
impl<T> __IncompleteArrayField<T> { /*....*/ }
Expected Results
This is borrowing the example from #1359.
In #1592 a zero-sized array was introduced to fix alignment issues for accessing incomplete arrays. My question is: Why does the PhantomData still get generated ? Shouldn't the zero-sized array + repr(C) already fix this ? Or could rustc still remove the whole thing without PhantomData ? Another obvious answer is obviously that it's just always generated and we don't need it for this case, as there are no lifetimes involved we might want to bind somewhere.
So this should be enough from my PoV:
/*..*/
pub struct __IncompleteArrayField([u8; 0]);
This is more a question about why and whether bindgen knows something I don't, so I maybe should adopt what bindgen does.
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
Start from the bindgen::Builder invocation using input.h and trace how the generated __IncompleteArrayField is represented. Compare the PhantomData and zero-sized [T; 0] forms for the incomplete-array case, then determine whether the issue should document the existing behavior or define a change with validation for generated bindings.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp, rust
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100