Skip to content

Commit c8bc986

Browse files
committed
Derive from any other trait only when deriving from Copy
It's impossible to #[derive] from any other trait when not deriving from Copy when using the newest Rust nightly. Any attempt to do that results in the following error: error: `#[derive]` can't be used on a `#[repr(packed)]` struct that does not derive Copy (error E0133) Fixes: rust-lang#2083 Signed-off-by: Michal Rostecki <[email protected]>
1 parent ee6ff69 commit c8bc986

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+482
-126
lines changed

src/codegen/mod.rs

+25-24
Original file line numberDiff line numberDiff line change
@@ -113,14 +113,6 @@ bitflags! {
113113
fn derives_of_item(item: &Item, ctx: &BindgenContext) -> DerivableTraits {
114114
let mut derivable_traits = DerivableTraits::empty();
115115

116-
if item.can_derive_debug(ctx) && !item.annotations().disallow_debug() {
117-
derivable_traits |= DerivableTraits::DEBUG;
118-
}
119-
120-
if item.can_derive_default(ctx) && !item.annotations().disallow_default() {
121-
derivable_traits |= DerivableTraits::DEFAULT;
122-
}
123-
124116
let all_template_params = item.all_template_params(ctx);
125117

126118
if item.can_derive_copy(ctx) && !item.annotations().disallow_copy() {
@@ -137,26 +129,36 @@ fn derives_of_item(item: &Item, ctx: &BindgenContext) -> DerivableTraits {
137129
// It's not hard to fix though.
138130
derivable_traits |= DerivableTraits::CLONE;
139131
}
140-
}
141132

142-
if item.can_derive_hash(ctx) {
143-
derivable_traits |= DerivableTraits::HASH;
144-
}
133+
if item.can_derive_debug(ctx) && !item.annotations().disallow_debug() {
134+
derivable_traits |= DerivableTraits::DEBUG;
135+
}
145136

146-
if item.can_derive_partialord(ctx) {
147-
derivable_traits |= DerivableTraits::PARTIAL_ORD;
148-
}
137+
if item.can_derive_default(ctx) &&
138+
!item.annotations().disallow_default()
139+
{
140+
derivable_traits |= DerivableTraits::DEFAULT;
141+
}
149142

150-
if item.can_derive_ord(ctx) {
151-
derivable_traits |= DerivableTraits::ORD;
152-
}
143+
if item.can_derive_hash(ctx) {
144+
derivable_traits |= DerivableTraits::HASH;
145+
}
153146

154-
if item.can_derive_partialeq(ctx) {
155-
derivable_traits |= DerivableTraits::PARTIAL_EQ;
156-
}
147+
if item.can_derive_partialord(ctx) {
148+
derivable_traits |= DerivableTraits::PARTIAL_ORD;
149+
}
157150

158-
if item.can_derive_eq(ctx) {
159-
derivable_traits |= DerivableTraits::EQ;
151+
if item.can_derive_ord(ctx) {
152+
derivable_traits |= DerivableTraits::ORD;
153+
}
154+
155+
if item.can_derive_partialeq(ctx) {
156+
derivable_traits |= DerivableTraits::PARTIAL_EQ;
157+
}
158+
159+
if item.can_derive_eq(ctx) {
160+
derivable_traits |= DerivableTraits::EQ;
161+
}
160162
}
161163

162164
derivable_traits
@@ -4554,7 +4556,6 @@ pub mod utils {
45544556

45554557
let incomplete_array_decl = quote! {
45564558
#[repr(C)]
4557-
#[derive(Default)]
45584559
pub struct __IncompleteArrayField<T>(
45594560
::#prefix::marker::PhantomData<T>, [T; 0]);
45604561
};

tests/expectations/tests/allowlisted_item_references_no_copy.rs

+18-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
)]
77

88
#[repr(C)]
9-
#[derive(Debug, Default)]
109
pub struct NoCopy {
1110
pub _address: u8,
1211
}
@@ -23,8 +22,16 @@ fn bindgen_test_layout_NoCopy() {
2322
concat!("Alignment of ", stringify!(NoCopy))
2423
);
2524
}
25+
impl Default for NoCopy {
26+
fn default() -> Self {
27+
let mut s = ::std::mem::MaybeUninit::<Self>::uninit();
28+
unsafe {
29+
::std::ptr::write_bytes(s.as_mut_ptr(), 0, 1);
30+
s.assume_init()
31+
}
32+
}
33+
}
2634
#[repr(C)]
27-
#[derive(Debug, Default)]
2835
pub struct AllowlistMe {
2936
pub a: NoCopy,
3037
}
@@ -53,3 +60,12 @@ fn bindgen_test_layout_AllowlistMe() {
5360
)
5461
);
5562
}
63+
impl Default for AllowlistMe {
64+
fn default() -> Self {
65+
let mut s = ::std::mem::MaybeUninit::<Self>::uninit();
66+
unsafe {
67+
::std::ptr::write_bytes(s.as_mut_ptr(), 0, 1);
68+
s.assume_init()
69+
}
70+
}
71+
}

tests/expectations/tests/class.rs

+36-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
)]
77

88
#[repr(C)]
9-
#[derive(Default)]
109
pub struct __IncompleteArrayField<T>(::std::marker::PhantomData<T>, [T; 0]);
1110
impl<T> __IncompleteArrayField<T> {
1211
#[inline]
@@ -190,6 +189,15 @@ fn bindgen_test_layout_C_with_zero_length_array_2() {
190189
)
191190
);
192191
}
192+
impl Default for C_with_zero_length_array_2 {
193+
fn default() -> Self {
194+
let mut s = ::std::mem::MaybeUninit::<Self>::uninit();
195+
unsafe {
196+
::std::ptr::write_bytes(s.as_mut_ptr(), 0, 1);
197+
s.assume_init()
198+
}
199+
}
200+
}
193201
#[repr(C)]
194202
pub struct C_with_incomplete_array {
195203
pub a: ::std::os::raw::c_int,
@@ -219,7 +227,6 @@ impl Default for C_with_incomplete_array {
219227
}
220228
}
221229
#[repr(C)]
222-
#[derive(Debug, Default)]
223230
pub struct C_with_incomplete_array_2 {
224231
pub a: ::std::os::raw::c_int,
225232
pub incomplete_array: __IncompleteArrayField<::std::os::raw::c_char>,
@@ -237,6 +244,15 @@ fn bindgen_test_layout_C_with_incomplete_array_2() {
237244
concat!("Alignment of ", stringify!(C_with_incomplete_array_2))
238245
);
239246
}
247+
impl Default for C_with_incomplete_array_2 {
248+
fn default() -> Self {
249+
let mut s = ::std::mem::MaybeUninit::<Self>::uninit();
250+
unsafe {
251+
::std::ptr::write_bytes(s.as_mut_ptr(), 0, 1);
252+
s.assume_init()
253+
}
254+
}
255+
}
240256
#[repr(C)]
241257
pub struct C_with_zero_length_array_and_incomplete_array {
242258
pub a: ::std::os::raw::c_int,
@@ -273,7 +289,6 @@ impl Default for C_with_zero_length_array_and_incomplete_array {
273289
}
274290
}
275291
#[repr(C)]
276-
#[derive(Debug, Default)]
277292
pub struct C_with_zero_length_array_and_incomplete_array_2 {
278293
pub a: ::std::os::raw::c_int,
279294
pub zero_length_array: __IncompleteArrayField<::std::os::raw::c_char>,
@@ -300,8 +315,16 @@ fn bindgen_test_layout_C_with_zero_length_array_and_incomplete_array_2() {
300315
)
301316
);
302317
}
318+
impl Default for C_with_zero_length_array_and_incomplete_array_2 {
319+
fn default() -> Self {
320+
let mut s = ::std::mem::MaybeUninit::<Self>::uninit();
321+
unsafe {
322+
::std::ptr::write_bytes(s.as_mut_ptr(), 0, 1);
323+
s.assume_init()
324+
}
325+
}
326+
}
303327
#[repr(C)]
304-
#[derive(Debug, Default, Hash, PartialOrd, Ord, PartialEq, Eq)]
305328
pub struct WithDtor {
306329
pub b: ::std::os::raw::c_int,
307330
}
@@ -328,6 +351,15 @@ fn bindgen_test_layout_WithDtor() {
328351
)
329352
);
330353
}
354+
impl Default for WithDtor {
355+
fn default() -> Self {
356+
let mut s = ::std::mem::MaybeUninit::<Self>::uninit();
357+
unsafe {
358+
::std::ptr::write_bytes(s.as_mut_ptr(), 0, 1);
359+
s.assume_init()
360+
}
361+
}
362+
}
331363
#[repr(C)]
332364
pub struct IncompleteArrayNonCopiable {
333365
pub whatever: *mut ::std::os::raw::c_void,

tests/expectations/tests/class_1_0.rs

+36-5
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
)]
77

88
#[repr(C)]
9-
#[derive(Default)]
109
pub struct __IncompleteArrayField<T>(::std::marker::PhantomData<T>, [T; 0]);
1110
impl<T> __IncompleteArrayField<T> {
1211
#[inline]
@@ -199,7 +198,6 @@ impl Default for C_with_zero_length_array {
199198
}
200199
}
201200
#[repr(C)]
202-
#[derive(Debug, Default)]
203201
pub struct C_with_zero_length_array_2 {
204202
pub a: ::std::os::raw::c_int,
205203
pub zero_length_array: __IncompleteArrayField<::std::os::raw::c_char>,
@@ -243,6 +241,15 @@ fn bindgen_test_layout_C_with_zero_length_array_2() {
243241
)
244242
);
245243
}
244+
impl Default for C_with_zero_length_array_2 {
245+
fn default() -> Self {
246+
unsafe {
247+
let mut s: Self = ::std::mem::uninitialized();
248+
::std::ptr::write_bytes(&mut s, 0, 1);
249+
s
250+
}
251+
}
252+
}
246253
#[repr(C)]
247254
pub struct C_with_incomplete_array {
248255
pub a: ::std::os::raw::c_int,
@@ -272,7 +279,6 @@ impl Default for C_with_incomplete_array {
272279
}
273280
}
274281
#[repr(C)]
275-
#[derive(Debug, Default)]
276282
pub struct C_with_incomplete_array_2 {
277283
pub a: ::std::os::raw::c_int,
278284
pub incomplete_array: __IncompleteArrayField<::std::os::raw::c_char>,
@@ -290,6 +296,15 @@ fn bindgen_test_layout_C_with_incomplete_array_2() {
290296
concat!("Alignment of ", stringify!(C_with_incomplete_array_2))
291297
);
292298
}
299+
impl Default for C_with_incomplete_array_2 {
300+
fn default() -> Self {
301+
unsafe {
302+
let mut s: Self = ::std::mem::uninitialized();
303+
::std::ptr::write_bytes(&mut s, 0, 1);
304+
s
305+
}
306+
}
307+
}
293308
#[repr(C)]
294309
pub struct C_with_zero_length_array_and_incomplete_array {
295310
pub a: ::std::os::raw::c_int,
@@ -326,7 +341,6 @@ impl Default for C_with_zero_length_array_and_incomplete_array {
326341
}
327342
}
328343
#[repr(C)]
329-
#[derive(Debug, Default)]
330344
pub struct C_with_zero_length_array_and_incomplete_array_2 {
331345
pub a: ::std::os::raw::c_int,
332346
pub zero_length_array: __IncompleteArrayField<::std::os::raw::c_char>,
@@ -353,8 +367,16 @@ fn bindgen_test_layout_C_with_zero_length_array_and_incomplete_array_2() {
353367
)
354368
);
355369
}
370+
impl Default for C_with_zero_length_array_and_incomplete_array_2 {
371+
fn default() -> Self {
372+
unsafe {
373+
let mut s: Self = ::std::mem::uninitialized();
374+
::std::ptr::write_bytes(&mut s, 0, 1);
375+
s
376+
}
377+
}
378+
}
356379
#[repr(C)]
357-
#[derive(Debug, Default, Hash, PartialEq, Eq)]
358380
pub struct WithDtor {
359381
pub b: ::std::os::raw::c_int,
360382
}
@@ -381,6 +403,15 @@ fn bindgen_test_layout_WithDtor() {
381403
)
382404
);
383405
}
406+
impl Default for WithDtor {
407+
fn default() -> Self {
408+
unsafe {
409+
let mut s: Self = ::std::mem::uninitialized();
410+
::std::ptr::write_bytes(&mut s, 0, 1);
411+
s
412+
}
413+
}
414+
}
384415
#[repr(C)]
385416
pub struct IncompleteArrayNonCopiable {
386417
pub whatever: *mut ::std::os::raw::c_void,

tests/expectations/tests/class_with_dtor.rs

-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
)]
77

88
#[repr(C)]
9-
#[derive(Debug, Hash, PartialEq, Eq)]
109
pub struct HandleWithDtor<T> {
1110
pub ptr: *mut T,
1211
pub _phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell<T>>,
@@ -22,7 +21,6 @@ impl<T> Default for HandleWithDtor<T> {
2221
}
2322
pub type HandleValue = HandleWithDtor<::std::os::raw::c_int>;
2423
#[repr(C)]
25-
#[derive(Debug, Hash, PartialEq, Eq)]
2624
pub struct WithoutDtor {
2725
pub shouldBeWithDtor: HandleValue,
2826
}

tests/expectations/tests/crtp.rs

+18-2
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,19 @@ fn bindgen_test_layout_Derived() {
2929
);
3030
}
3131
#[repr(C)]
32-
#[derive(Debug, Default)]
3332
pub struct BaseWithDestructor {
3433
pub _address: u8,
3534
}
35+
impl Default for BaseWithDestructor {
36+
fn default() -> Self {
37+
let mut s = ::std::mem::MaybeUninit::<Self>::uninit();
38+
unsafe {
39+
::std::ptr::write_bytes(s.as_mut_ptr(), 0, 1);
40+
s.assume_init()
41+
}
42+
}
43+
}
3644
#[repr(C)]
37-
#[derive(Debug, Default)]
3845
pub struct DerivedFromBaseWithDestructor {
3946
pub _address: u8,
4047
}
@@ -51,6 +58,15 @@ fn bindgen_test_layout_DerivedFromBaseWithDestructor() {
5158
concat!("Alignment of ", stringify!(DerivedFromBaseWithDestructor))
5259
);
5360
}
61+
impl Default for DerivedFromBaseWithDestructor {
62+
fn default() -> Self {
63+
let mut s = ::std::mem::MaybeUninit::<Self>::uninit();
64+
unsafe {
65+
::std::ptr::write_bytes(s.as_mut_ptr(), 0, 1);
66+
s.assume_init()
67+
}
68+
}
69+
}
5470
#[test]
5571
fn __bindgen_test_layout_Base_open0_Derived_close0_instantiation() {
5672
assert_eq!(

0 commit comments

Comments
 (0)