Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reason for PhantomData on incomplete arrays without lifetimes #2434

Open
0xpr03 opened this issue Mar 6, 2023 · 0 comments
Open

Reason for PhantomData on incomplete arrays without lifetimes #2434

0xpr03 opened this issue Mar 6, 2023 · 0 comments

Comments

@0xpr03
Copy link

0xpr03 commented Mar 6, 2023

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.

@0xpr03 0xpr03 changed the title Reason for PhantomData on incomplete Arrays without lifetimes Reason for PhantomData on incomplete arrays without lifetimes Mar 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant