rust-bindgen
rust-bindgen copied to clipboard
Struct/union forward-declaration in a field definition gets misinterpreted as a definition
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],
}
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.