Skip to content

Commit

Permalink
Don't emit #[repr(align(0))] for empty unions (#1595)
Browse files Browse the repository at this point in the history
  • Loading branch information
pmarks authored and emilio committed Jul 23, 2019
1 parent 281b62e commit 9fe3e90
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 33 deletions.
8 changes: 7 additions & 1 deletion src/ir/comp.rs
Expand Up @@ -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);

Expand Down
19 changes: 19 additions & 0 deletions 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() }
}
}
28 changes: 9 additions & 19 deletions 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>);
Expand Down Expand Up @@ -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)]
Expand All @@ -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() }
}
}
16 changes: 3 additions & 13 deletions tests/expectations/tests/transform-op.rs
Expand Up @@ -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>>,
Expand Down Expand Up @@ -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>,
Expand Down Expand Up @@ -178,19 +173,14 @@ 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>>,
pub bar3: __BindgenUnionField<StyleBar_StyleBar3_Body<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() }
Expand Down
5 changes: 5 additions & 0 deletions tests/headers/empty-union.hpp
@@ -0,0 +1,5 @@
// bindgen-flags: --opaque-type "*"

template <int> class a {
union {};
};

0 comments on commit 9fe3e90

Please sign in to comment.