diff --git a/bindgen-tests/tests/expectations/tests/union_with_zero_sized_array.rs b/bindgen-tests/tests/expectations/tests/union_with_zero_sized_array.rs new file mode 100644 index 0000000000..aa23fcd734 --- /dev/null +++ b/bindgen-tests/tests/expectations/tests/union_with_zero_sized_array.rs @@ -0,0 +1,68 @@ +#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)] +#[repr(C)] +pub struct __BindgenUnionField(::std::marker::PhantomData); +impl __BindgenUnionField { + #[inline] + pub const fn new() -> Self { + __BindgenUnionField(::std::marker::PhantomData) + } + #[inline] + pub unsafe fn as_ref(&self) -> &T { + ::std::mem::transmute(self) + } + #[inline] + pub unsafe fn as_mut(&mut self) -> &mut T { + ::std::mem::transmute(self) + } +} +impl ::std::default::Default for __BindgenUnionField { + #[inline] + fn default() -> Self { + Self::new() + } +} +impl ::std::clone::Clone for __BindgenUnionField { + #[inline] + fn clone(&self) -> Self { + *self + } +} +impl ::std::marker::Copy for __BindgenUnionField {} +impl ::std::fmt::Debug for __BindgenUnionField { + fn fmt(&self, fmt: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result { + fmt.write_str("__BindgenUnionField") + } +} +impl ::std::hash::Hash for __BindgenUnionField { + fn hash(&self, _state: &mut H) {} +} +impl ::std::cmp::PartialEq for __BindgenUnionField { + fn eq(&self, _other: &__BindgenUnionField) -> bool { + true + } +} +impl ::std::cmp::Eq for __BindgenUnionField {} +#[repr(C)] +pub struct U { + pub d0: __BindgenUnionField<[::std::os::raw::c_char; 0usize]>, + pub d1: __BindgenUnionField<[::std::os::raw::c_char; 1usize]>, + pub d2: __BindgenUnionField<[::std::os::raw::c_char; 2usize]>, + pub bindgen_union_field: [u8; 2usize], +} +#[allow(clippy::unnecessary_operation, clippy::identity_op)] +const _: () = { + ["Size of U"][::std::mem::size_of::() - 2usize]; + ["Alignment of U"][::std::mem::align_of::() - 1usize]; + ["Offset of field: U::d0"][::std::mem::offset_of!(U, d0) - 0usize]; + ["Offset of field: U::d1"][::std::mem::offset_of!(U, d1) - 0usize]; + ["Offset of field: U::d2"][::std::mem::offset_of!(U, d2) - 0usize]; +}; +impl Default for U { + fn default() -> Self { + let mut s = ::std::mem::MaybeUninit::::uninit(); + unsafe { + ::std::ptr::write_bytes(s.as_mut_ptr(), 0, 1); + s.assume_init() + } + } +} diff --git a/bindgen-tests/tests/headers/union_with_zero_sized_array.h b/bindgen-tests/tests/headers/union_with_zero_sized_array.h new file mode 100644 index 0000000000..ace745f26c --- /dev/null +++ b/bindgen-tests/tests/headers/union_with_zero_sized_array.h @@ -0,0 +1,5 @@ +union U { + char d0[0]; + char d1[1]; + char d2[2]; +}; diff --git a/bindgen/codegen/struct_layout.rs b/bindgen/codegen/struct_layout.rs index f7f66373fb..0d97b8cc20 100644 --- a/bindgen/codegen/struct_layout.rs +++ b/bindgen/codegen/struct_layout.rs @@ -266,7 +266,12 @@ impl<'a> StructLayoutTracker<'a> { } }; - self.latest_offset += field_layout.size; + if !is_union { + self.latest_offset += field_layout.size; + } else { + self.latest_offset = + cmp::max(self.latest_offset, field_layout.size); + } self.latest_field_layout = Some(field_layout); self.max_field_align = cmp::max(self.max_field_align, field_layout.align);