diff options
author | Patrick Marks <pmarks@gmail.com> | 2019-07-23 13:09:45 -0700 |
---|---|---|
committer | Emilio Cobos Álvarez <emilio@crisal.io> | 2019-07-23 22:09:45 +0200 |
commit | 9fe3e909bea3cc24759148f27d03772a479c6ecf (patch) | |
tree | 66a3fb524323eaee49dc36aeb080070cf1b9f101 | |
parent | 281b62e07786915a040f8a3b4352b8c7b96132cf (diff) |
Don't emit #[repr(align(0))] for empty unions (#1595)
-rw-r--r-- | src/ir/comp.rs | 8 | ||||
-rw-r--r-- | tests/expectations/tests/empty-union.rs | 19 | ||||
-rw-r--r-- | tests/expectations/tests/issue-493_1_0.rs | 28 | ||||
-rw-r--r-- | tests/expectations/tests/transform-op.rs | 16 | ||||
-rw-r--r-- | tests/headers/empty-union.hpp | 5 |
5 files changed, 43 insertions, 33 deletions
diff --git a/src/ir/comp.rs b/src/ir/comp.rs index 6c0563b1..5e0b3ed6 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 00000000..6cae9938 --- /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 3c742e5d..3438d2b4 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<T>(::std::marker::PhantomData<T>); @@ -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<basic_string___long>, pub __lxx: __BindgenUnionField<basic_string___short>, 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<basic_string___long>, pub __s: __BindgenUnionField<basic_string___short>, pub __r: __BindgenUnionField<basic_string___raw>, 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 c3273ca2..4093b65f 100644 --- a/tests/expectations/tests/transform-op.rs +++ b/tests/expectations/tests/transform-op.rs @@ -63,7 +63,7 @@ impl<T> Default for StylePoint<T> { } } #[repr(C)] -#[derive(Copy, Clone)] +#[derive(Debug, Default, Copy, Clone)] pub struct StyleFoo<T> { pub __bindgen_anon_1: __BindgenUnionField<StyleFoo__bindgen_ty_1>, pub foo: __BindgenUnionField<StyleFoo_Foo_Body<T>>, @@ -125,13 +125,8 @@ impl Default for StyleFoo__bindgen_ty_1 { unsafe { ::std::mem::zeroed() } } } -impl<T> Default for StyleFoo<T> { - fn default() -> Self { - unsafe { ::std::mem::zeroed() } - } -} #[repr(C)] -#[derive(Copy, Clone)] +#[derive(Debug, Copy, Clone)] pub struct StyleBar<T> { pub tag: StyleBar_Tag, pub __bindgen_anon_1: StyleBar__bindgen_ty_1<T>, @@ -178,7 +173,7 @@ impl<T> Default for StyleBar_StyleBar3_Body<T> { } } #[repr(C)] -#[derive(Copy, Clone)] +#[derive(Debug, Default, Copy, Clone)] pub struct StyleBar__bindgen_ty_1<T> { pub bar1: __BindgenUnionField<StyleBar_StyleBar1_Body<T>>, pub bar2: __BindgenUnionField<StyleBar_StyleBar2_Body<T>>, @@ -186,11 +181,6 @@ pub struct StyleBar__bindgen_ty_1<T> { pub bindgen_union_field: [u8; 0usize], pub _phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell<T>>, } -impl<T> Default for StyleBar__bindgen_ty_1<T> { - fn default() -> Self { - unsafe { ::std::mem::zeroed() } - } -} impl<T> Default for StyleBar<T> { 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 00000000..3b067e39 --- /dev/null +++ b/tests/headers/empty-union.hpp @@ -0,0 +1,5 @@ +// bindgen-flags: --opaque-type "*" + +template <int> class a { + union {}; +}; |