diff --git a/src/ir/comp.rs b/src/ir/comp.rs index 6c0563b18a..5e0b3ed62a 100644 --- a/src/ir/comp.rs +++ b/src/ir/comp.rs @@ -1078,8 +1078,14 @@ impl CompInfo { return None; } + // empty union case + if self.fields().is_empty() { + return None; + } + let mut max_size = 0; - let mut max_align = 0; + // Don't allow align(0) + let mut max_align = 1; for field in self.fields() { let field_layout = field.layout(ctx); diff --git a/tests/expectations/tests/empty-union.rs b/tests/expectations/tests/empty-union.rs new file mode 100644 index 0000000000..6cae993895 --- /dev/null +++ b/tests/expectations/tests/empty-union.rs @@ -0,0 +1,19 @@ +/* automatically generated by rust-bindgen */ + +#![allow( + dead_code, + non_snake_case, + non_camel_case_types, + non_upper_case_globals +)] + +#[repr(C)] +#[derive(Copy, Clone)] +pub union a__bindgen_ty_1 { + pub _address: u8, +} +impl Default for a__bindgen_ty_1 { + fn default() -> Self { + unsafe { ::std::mem::zeroed() } + } +} diff --git a/tests/expectations/tests/issue-493_1_0.rs b/tests/expectations/tests/issue-493_1_0.rs index 3c742e5def..3438d2b475 100644 --- a/tests/expectations/tests/issue-493_1_0.rs +++ b/tests/expectations/tests/issue-493_1_0.rs @@ -1,6 +1,11 @@ /* automatically generated by rust-bindgen */ -#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)] +#![allow( + dead_code, + non_snake_case, + non_camel_case_types, + non_upper_case_globals +)] #[repr(C)] pub struct __BindgenUnionField(::std::marker::PhantomData); @@ -91,17 +96,12 @@ impl Default for basic_string___short { } } #[repr(C)] -#[derive(Copy, Clone)] +#[derive(Debug, Default, Copy, Clone, Hash, PartialEq, Eq)] pub struct basic_string___ulx { pub __lx: __BindgenUnionField, pub __lxx: __BindgenUnionField, pub bindgen_union_field: [u8; 0usize], } -impl Default for basic_string___ulx { - fn default() -> Self { - unsafe { ::std::mem::zeroed() } - } -} pub const basic_string___n_words: basic_string__bindgen_ty_2 = basic_string__bindgen_ty_2::__n_words; #[repr(i32)] @@ -120,25 +120,15 @@ impl Default for basic_string___raw { } } #[repr(C)] -#[derive(Copy, Clone)] +#[derive(Debug, Default, Copy, Clone, Hash, PartialEq, Eq)] pub struct basic_string___rep { pub __bindgen_anon_1: basic_string___rep__bindgen_ty_1, } #[repr(C)] -#[derive(Copy, Clone)] +#[derive(Debug, Default, Copy, Clone, Hash, PartialEq, Eq)] pub struct basic_string___rep__bindgen_ty_1 { pub __l: __BindgenUnionField, pub __s: __BindgenUnionField, pub __r: __BindgenUnionField, pub bindgen_union_field: [u8; 0usize], } -impl Default for basic_string___rep__bindgen_ty_1 { - fn default() -> Self { - unsafe { ::std::mem::zeroed() } - } -} -impl Default for basic_string___rep { - fn default() -> Self { - unsafe { ::std::mem::zeroed() } - } -} diff --git a/tests/expectations/tests/transform-op.rs b/tests/expectations/tests/transform-op.rs index c3273ca2d3..4093b65f25 100644 --- a/tests/expectations/tests/transform-op.rs +++ b/tests/expectations/tests/transform-op.rs @@ -63,7 +63,7 @@ impl Default for StylePoint { } } #[repr(C)] -#[derive(Copy, Clone)] +#[derive(Debug, Default, Copy, Clone)] pub struct StyleFoo { pub __bindgen_anon_1: __BindgenUnionField, pub foo: __BindgenUnionField>, @@ -125,13 +125,8 @@ impl Default for StyleFoo__bindgen_ty_1 { unsafe { ::std::mem::zeroed() } } } -impl Default for StyleFoo { - fn default() -> Self { - unsafe { ::std::mem::zeroed() } - } -} #[repr(C)] -#[derive(Copy, Clone)] +#[derive(Debug, Copy, Clone)] pub struct StyleBar { pub tag: StyleBar_Tag, pub __bindgen_anon_1: StyleBar__bindgen_ty_1, @@ -178,7 +173,7 @@ impl Default for StyleBar_StyleBar3_Body { } } #[repr(C)] -#[derive(Copy, Clone)] +#[derive(Debug, Default, Copy, Clone)] pub struct StyleBar__bindgen_ty_1 { pub bar1: __BindgenUnionField>, pub bar2: __BindgenUnionField>, @@ -186,11 +181,6 @@ pub struct StyleBar__bindgen_ty_1 { pub bindgen_union_field: [u8; 0usize], pub _phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell>, } -impl Default for StyleBar__bindgen_ty_1 { - fn default() -> Self { - unsafe { ::std::mem::zeroed() } - } -} impl Default for StyleBar { fn default() -> Self { unsafe { ::std::mem::zeroed() } diff --git a/tests/headers/empty-union.hpp b/tests/headers/empty-union.hpp new file mode 100644 index 0000000000..3b067e39da --- /dev/null +++ b/tests/headers/empty-union.hpp @@ -0,0 +1,5 @@ +// bindgen-flags: --opaque-type "*" + +template class a { + union {}; +};