Skip to content

Reason for PhantomData on incomplete arrays without lifetimes #2434

@0xpr03

Description

@0xpr03

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.

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