rust-bindgen icon indicating copy to clipboard operation
rust-bindgen copied to clipboard

Struct/union forward-declaration in a field definition gets misinterpreted as a definition

Open 1c7718e7 opened this issue 2 years ago • 1 comments

Input C/C++ Header

//union forward_declared; <-- uncommenting this prevents the bug from manifesting
struct some_struct {
	union forward_declared *ptr_field;
};

Bindgen Invocation

$ RUST_BACKTRACE=1 bindgen input.h

Actual Results

panicked at bindgen/codegen/mod.rs:2148:17:
Unable to get layout information?

Expected Results

"union forward_declared" should be codegen'd into an opaque struct:

pub struct forward_declared {
    _unused: [u8; 0],
}

1c7718e7 avatar Dec 04 '23 12:12 1c7718e7

The exact same bug happens with struct forward_declared instead of union, but the effects are more subtle:

A

struct forward_declared;
struct some_struct { struct forward_declared *ptr_field; };

B

struct some_struct { struct forward_declared *ptr_field; };

Both cases run without a bindgen panic, but B codegens forward_declared as a 1-byte-sized empty struct instead of an opaque struct:

pub struct forward_declared {
    pub _address: u8, // <-- wrong, should be a non-pub _unused instead
}

I think the reason is that CompInfo::from_ty() receives a location of kind CXCursor_FieldDecl and misinterprets that as a struct/union definition with no fields.

1c7718e7 avatar Dec 04 '23 12:12 1c7718e7