From 271eeb0782d34942267ceabcf5f1cf118f0f5842 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emilio=20Cobos=20=C3=81lvarez?= Date: Mon, 26 Jul 2021 00:22:05 +0200 Subject: [PATCH] codegen: Don't check large_arrays to generate bitfield ctors because they use the Default trait. Fixes #2082 --- src/codegen/mod.rs | 8 +- tests/expectations/tests/timex.rs | 149 +++++++++++++++++++++++++++++- tests/headers/timex.h | 2 - 3 files changed, 152 insertions(+), 7 deletions(-) diff --git a/src/codegen/mod.rs b/src/codegen/mod.rs index d49d3248b4..0f3337ac5d 100644 --- a/src/codegen/mod.rs +++ b/src/codegen/mod.rs @@ -1502,9 +1502,11 @@ impl<'a> FieldCodegen<'a> for BitfieldUnit { let mut ctor_impl = quote! {}; // We cannot generate any constructor if the underlying storage can't - // implement AsRef<[u8]> / AsMut<[u8]> / etc. - let mut generate_ctor = layout.size <= RUST_DERIVE_IN_ARRAY_LIMIT || - ctx.options().rust_features().larger_arrays; + // implement AsRef<[u8]> / AsMut<[u8]> / etc, or can't derive Default. + // + // We don't check `larger_arrays` here because Default does still have + // the 32 items limitation. + let mut generate_ctor = layout.size <= RUST_DERIVE_IN_ARRAY_LIMIT; let mut access_spec = !fields_should_be_private; for bf in self.bitfields() { diff --git a/tests/expectations/tests/timex.rs b/tests/expectations/tests/timex.rs index 72ada37ad3..b2a84d8462 100644 --- a/tests/expectations/tests/timex.rs +++ b/tests/expectations/tests/timex.rs @@ -92,7 +92,7 @@ where } } #[repr(C)] -#[derive(Copy, Clone)] +#[derive(Debug, Copy, Clone)] pub struct timex { pub tai: ::std::os::raw::c_int, pub _bitfield_align_1: [u8; 0], @@ -131,7 +131,7 @@ impl Default for timex { } } #[repr(C)] -#[derive(Copy, Clone)] +#[derive(Debug, Copy, Clone)] pub struct timex_named { pub tai: ::std::os::raw::c_int, pub _bitfield_align_1: [u32; 0], @@ -171,3 +171,148 @@ impl Default for timex_named { } } } +impl timex_named { + #[inline] + pub fn a(&self) -> ::std::os::raw::c_int { + unsafe { + ::std::mem::transmute(self._bitfield_1.get(0usize, 32u8) as u32) + } + } + #[inline] + pub fn set_a(&mut self, val: ::std::os::raw::c_int) { + unsafe { + let val: u32 = ::std::mem::transmute(val); + self._bitfield_1.set(0usize, 32u8, val as u64) + } + } + #[inline] + pub fn b(&self) -> ::std::os::raw::c_int { + unsafe { + ::std::mem::transmute(self._bitfield_1.get(32usize, 32u8) as u32) + } + } + #[inline] + pub fn set_b(&mut self, val: ::std::os::raw::c_int) { + unsafe { + let val: u32 = ::std::mem::transmute(val); + self._bitfield_1.set(32usize, 32u8, val as u64) + } + } + #[inline] + pub fn c(&self) -> ::std::os::raw::c_int { + unsafe { + ::std::mem::transmute(self._bitfield_1.get(64usize, 32u8) as u32) + } + } + #[inline] + pub fn set_c(&mut self, val: ::std::os::raw::c_int) { + unsafe { + let val: u32 = ::std::mem::transmute(val); + self._bitfield_1.set(64usize, 32u8, val as u64) + } + } + #[inline] + pub fn d(&self) -> ::std::os::raw::c_int { + unsafe { + ::std::mem::transmute(self._bitfield_1.get(96usize, 32u8) as u32) + } + } + #[inline] + pub fn set_d(&mut self, val: ::std::os::raw::c_int) { + unsafe { + let val: u32 = ::std::mem::transmute(val); + self._bitfield_1.set(96usize, 32u8, val as u64) + } + } + #[inline] + pub fn e(&self) -> ::std::os::raw::c_int { + unsafe { + ::std::mem::transmute(self._bitfield_1.get(128usize, 32u8) as u32) + } + } + #[inline] + pub fn set_e(&mut self, val: ::std::os::raw::c_int) { + unsafe { + let val: u32 = ::std::mem::transmute(val); + self._bitfield_1.set(128usize, 32u8, val as u64) + } + } + #[inline] + pub fn f(&self) -> ::std::os::raw::c_int { + unsafe { + ::std::mem::transmute(self._bitfield_1.get(160usize, 32u8) as u32) + } + } + #[inline] + pub fn set_f(&mut self, val: ::std::os::raw::c_int) { + unsafe { + let val: u32 = ::std::mem::transmute(val); + self._bitfield_1.set(160usize, 32u8, val as u64) + } + } + #[inline] + pub fn g(&self) -> ::std::os::raw::c_int { + unsafe { + ::std::mem::transmute(self._bitfield_1.get(192usize, 32u8) as u32) + } + } + #[inline] + pub fn set_g(&mut self, val: ::std::os::raw::c_int) { + unsafe { + let val: u32 = ::std::mem::transmute(val); + self._bitfield_1.set(192usize, 32u8, val as u64) + } + } + #[inline] + pub fn h(&self) -> ::std::os::raw::c_int { + unsafe { + ::std::mem::transmute(self._bitfield_1.get(224usize, 32u8) as u32) + } + } + #[inline] + pub fn set_h(&mut self, val: ::std::os::raw::c_int) { + unsafe { + let val: u32 = ::std::mem::transmute(val); + self._bitfield_1.set(224usize, 32u8, val as u64) + } + } + #[inline] + pub fn i(&self) -> ::std::os::raw::c_int { + unsafe { + ::std::mem::transmute(self._bitfield_1.get(256usize, 32u8) as u32) + } + } + #[inline] + pub fn set_i(&mut self, val: ::std::os::raw::c_int) { + unsafe { + let val: u32 = ::std::mem::transmute(val); + self._bitfield_1.set(256usize, 32u8, val as u64) + } + } + #[inline] + pub fn j(&self) -> ::std::os::raw::c_int { + unsafe { + ::std::mem::transmute(self._bitfield_1.get(288usize, 32u8) as u32) + } + } + #[inline] + pub fn set_j(&mut self, val: ::std::os::raw::c_int) { + unsafe { + let val: u32 = ::std::mem::transmute(val); + self._bitfield_1.set(288usize, 32u8, val as u64) + } + } + #[inline] + pub fn k(&self) -> ::std::os::raw::c_int { + unsafe { + ::std::mem::transmute(self._bitfield_1.get(320usize, 32u8) as u32) + } + } + #[inline] + pub fn set_k(&mut self, val: ::std::os::raw::c_int) { + unsafe { + let val: u32 = ::std::mem::transmute(val); + self._bitfield_1.set(320usize, 32u8, val as u64) + } + } +} diff --git a/tests/headers/timex.h b/tests/headers/timex.h index 099be61f24..1add26cabe 100644 --- a/tests/headers/timex.h +++ b/tests/headers/timex.h @@ -1,5 +1,3 @@ -// bindgen-flags: --rust-target 1.40 - struct timex { int tai;