Skip to content

Commit

Permalink
handle empty transparent struct in a better way
Browse files Browse the repository at this point in the history
  • Loading branch information
scovich committed Aug 12, 2024
1 parent f6d956d commit 0f4f3c3
Showing 1 changed file with 12 additions and 14 deletions.
26 changes: 12 additions & 14 deletions src/bindgen/ir/structure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,18 @@ impl Struct {
has_tag_field: bool,
is_enum_variant_body: bool,
alignment: Option<ReprAlign>,
is_transparent: bool,
mut is_transparent: bool,
cfg: Option<Cfg>,
annotations: AnnotationSet,
documentation: Documentation,
) -> Self {
// WARNING: The rust compiler rejects a `#[repr(transparent)]` struct with 2+ NZST fields, but
// accepts an empty struct (see https://github.com/rust-lang/rust/issues/129029). We must
// not emit a typedef in that case, because there is no underlying type.
if is_transparent && fields.len() != 1 {
error!("Illegal empty transparent struct {}", &path);
is_transparent = false;
}
let export_name = path.name().to_owned();
Self {
path,
Expand All @@ -152,19 +159,10 @@ impl Struct {

/// Attempts to convert this struct to a typedef (only works for transparent structs).
pub fn as_typedef(&self) -> Option<Typedef> {
if self.is_transparent {
// NOTE: The rust compiler rejects a `#[repr(transparent)]` struct with 2+ NZST fields,
// but accepts an empty struct (see https://github.com/rust-lang/rust/issues/129029).
// Don't emit the typedef in that case.
if let Some(field) = self.fields.first() {
return Some(Typedef::new_from_struct_field(self, field));
}
error!(
"Cannot convert empty transparent struct {} to typedef",
self.name()
);
match self.fields.first() {
Some(field) if self.is_transparent => Some(Typedef::new_from_struct_field(self, field)),
_ => None,
}
None
}

pub fn add_monomorphs(&self, library: &Library, out: &mut Monomorphs) {
Expand Down Expand Up @@ -288,7 +286,7 @@ impl Item for Struct {
}

fn collect_declaration_types(&self, resolver: &mut DeclarationTypeResolver) {
if self.is_transparent && self.fields.len() == 1 {
if self.is_transparent {
resolver.add_none(&self.path);
} else {
resolver.add_struct(&self.path);
Expand Down

0 comments on commit 0f4f3c3

Please sign in to comment.