Skip to content

Commit a467d3e

Browse files
committed
codegen: Allow to not derive Debug on enums.
Fixes #1899. This code predated all the derive machinery, and always hardcoded its derives. We could avoid hard-coding the other traits, but those seem usually-useful, so leave them there for backwards compat for now.
1 parent e096149 commit a467d3e

Some content is hidden

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

64 files changed

+183
-120
lines changed

src/codegen/mod.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2840,17 +2840,17 @@ impl CodeGenerator for Enum {
28402840
}
28412841

28422842
if !variation.is_const() {
2843-
let mut derives =
2844-
vec!["Debug", "Copy", "Clone", "PartialEq", "Eq", "Hash"];
2845-
2846-
if item.can_derive_partialord(ctx) {
2847-
derives.push("PartialOrd");
2848-
}
2849-
2850-
if item.can_derive_ord(ctx) {
2851-
derives.push("Ord");
2852-
}
2853-
2843+
let mut derives = derives_of_item(item, ctx);
2844+
// For backwards compat, enums always derive Clone/Eq/PartialEq/Hash, even
2845+
// if we don't generate those by default.
2846+
derives.insert(
2847+
DerivableTraits::CLONE |
2848+
DerivableTraits::COPY |
2849+
DerivableTraits::HASH |
2850+
DerivableTraits::PARTIAL_EQ |
2851+
DerivableTraits::EQ,
2852+
);
2853+
let derives: Vec<_> = derives.into();
28542854
attrs.push(attributes::derives(&derives));
28552855
}
28562856

tests/expectations/tests/anon_enum.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub struct Test {
1313
}
1414
pub const Test_T_NONE: Test__bindgen_ty_1 = Test__bindgen_ty_1::T_NONE;
1515
#[repr(u32)]
16-
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
16+
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
1717
pub enum Test__bindgen_ty_1 {
1818
T_NONE = 0,
1919
}
@@ -41,7 +41,7 @@ fn bindgen_test_layout_Test() {
4141
);
4242
}
4343
#[repr(u32)]
44-
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
44+
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
4545
pub enum Baz {
4646
Foo = 0,
4747
Bar = 1,

tests/expectations/tests/anon_enum_trait.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ pub const DataType_fmt: DataType__bindgen_ty_1 =
2525
pub const DataType_type_: DataType__bindgen_ty_1 =
2626
DataType__bindgen_ty_1::generic_type;
2727
#[repr(i32)]
28-
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
28+
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
2929
pub enum DataType__bindgen_ty_1 {
3030
generic_type = 0,
3131
}
@@ -37,7 +37,7 @@ pub struct Foo {
3737
pub const Foo_Bar: Foo__bindgen_ty_1 = Foo__bindgen_ty_1::Bar;
3838
pub const Foo_Baz: Foo__bindgen_ty_1 = Foo__bindgen_ty_1::Bar;
3939
#[repr(u32)]
40-
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
40+
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
4141
pub enum Foo__bindgen_ty_1 {
4242
Bar = 0,
4343
}

tests/expectations/tests/anon_enum_whitelist.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
pub const NODE_FLAG_FOO: _bindgen_ty_1 = _bindgen_ty_1::NODE_FLAG_FOO;
99
pub const NODE_FLAG_BAR: _bindgen_ty_1 = _bindgen_ty_1::NODE_FLAG_BAR;
1010
#[repr(u32)]
11-
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
11+
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
1212
pub enum _bindgen_ty_1 {
1313
NODE_FLAG_FOO = 0,
1414
NODE_FLAG_BAR = 1,

tests/expectations/tests/anon_union.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ impl TErrorResult_UnionState {
1717
TErrorResult_UnionState::HasMessage;
1818
}
1919
#[repr(i32)]
20-
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
20+
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
2121
pub enum TErrorResult_UnionState {
2222
HasMessage = 0,
2323
}

tests/expectations/tests/anon_union_1_0.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ pub struct TErrorResult {
5959
pub const TErrorResult_UnionState_HasException: TErrorResult_UnionState =
6060
TErrorResult_UnionState::HasMessage;
6161
#[repr(i32)]
62-
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
62+
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
6363
pub enum TErrorResult_UnionState {
6464
HasMessage = 0,
6565
}

tests/expectations/tests/bitfield-enum-basic.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ impl ::std::ops::BitAndAssign for Foo {
4444
}
4545
}
4646
#[repr(transparent)]
47-
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
47+
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
4848
pub struct Foo(pub ::std::os::raw::c_int);
4949
impl Buz {
5050
pub const Bar: Buz = Buz(2);
@@ -85,7 +85,7 @@ impl ::std::ops::BitAndAssign for Buz {
8585
}
8686
}
8787
#[repr(transparent)]
88-
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
88+
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
8989
pub struct Buz(pub ::std::os::raw::c_schar);
9090
pub const NS_FOO: _bindgen_ty_1 = _bindgen_ty_1(1);
9191
pub const NS_BAR: _bindgen_ty_1 = _bindgen_ty_1(2);
@@ -116,7 +116,7 @@ impl ::std::ops::BitAndAssign for _bindgen_ty_1 {
116116
}
117117
}
118118
#[repr(transparent)]
119-
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
119+
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
120120
pub struct _bindgen_ty_1(pub ::std::os::raw::c_uint);
121121
#[repr(C)]
122122
#[derive(Debug, Default, Copy, Clone)]
@@ -152,7 +152,7 @@ impl ::std::ops::BitAndAssign for Dummy__bindgen_ty_1 {
152152
}
153153
}
154154
#[repr(transparent)]
155-
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
155+
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
156156
pub struct Dummy__bindgen_ty_1(pub ::std::os::raw::c_uint);
157157
#[test]
158158
fn bindgen_test_layout_Dummy() {

tests/expectations/tests/bitfield-enum-repr-c.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,5 @@ impl ::std::ops::BitAndAssign for Foo {
4444
}
4545
}
4646
#[repr(C)]
47-
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
47+
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
4848
pub struct Foo(pub ::std::os::raw::c_int);

tests/expectations/tests/bitfield-enum-repr-transparent.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,5 @@ impl ::std::ops::BitAndAssign for Foo {
4444
}
4545
}
4646
#[repr(transparent)]
47-
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
47+
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
4848
pub struct Foo(pub ::std::os::raw::c_int);

tests/expectations/tests/bitfield_align_2.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ where
9393
}
9494
}
9595
#[repr(u32)]
96-
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
96+
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
9797
pub enum MyEnum {
9898
ONE = 0,
9999
TWO = 1,

tests/expectations/tests/class_with_inner_struct.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ fn bindgen_test_layout_B() {
229229
);
230230
}
231231
#[repr(i32)]
232-
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
232+
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
233233
pub enum StepSyntax {
234234
Keyword = 0,
235235
FunctionalWithoutKeyword = 1,

tests/expectations/tests/class_with_inner_struct_1_0.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ impl Clone for B {
287287
}
288288
}
289289
#[repr(i32)]
290-
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
290+
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
291291
pub enum StepSyntax {
292292
Keyword = 0,
293293
FunctionalWithoutKeyword = 1,

tests/expectations/tests/const_enum_unnamed.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
pub const FOO_BAR: _bindgen_ty_1 = _bindgen_ty_1::FOO_BAR;
99
pub const FOO_BAZ: _bindgen_ty_1 = _bindgen_ty_1::FOO_BAZ;
1010
#[repr(u32)]
11-
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
11+
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
1212
pub enum _bindgen_ty_1 {
1313
FOO_BAR = 0,
1414
FOO_BAZ = 1,
@@ -20,7 +20,7 @@ pub struct Foo {
2020
}
2121
pub const Foo_FOO_BAR: Foo__bindgen_ty_1 = Foo__bindgen_ty_1::FOO_BAR;
2222
#[repr(u32)]
23-
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
23+
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
2424
pub enum Foo__bindgen_ty_1 {
2525
FOO_BAR = 10,
2626
}

tests/expectations/tests/constify-enum.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ impl nsCSSPropertyID {
1313
nsCSSPropertyID::eCSSPropertyAlias_aa;
1414
}
1515
#[repr(u32)]
16-
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
16+
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
1717
pub enum nsCSSPropertyID {
1818
eCSSProperty_a = 0,
1919
eCSSProperty_b = 1,

tests/expectations/tests/dupe-enum-variant-in-namespace.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub mod root {
1919
pub const Foo3: root::foo::Bar = Bar::Foo2;
2020
}
2121
#[repr(u32)]
22-
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
22+
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
2323
pub enum Bar {
2424
Foo = 0,
2525
Foo2 = 1,

tests/expectations/tests/empty-enum.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@
77

88
pub type EmptyConstified = ::std::os::raw::c_uint;
99
#[repr(u32)]
10-
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
10+
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
1111
pub enum EmptyRustified {
1212
__bindgen_cannot_repr_c_on_empty_enum = 0,
1313
}
1414
pub mod EmptyModule {
1515
pub type Type = ::std::os::raw::c_uint;
1616
}
1717
#[repr(i8)]
18-
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
18+
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
1919
pub enum EmptyClassRustified {
2020
__bindgen_cannot_repr_c_on_empty_enum = 0,
2121
}
@@ -24,7 +24,7 @@ pub mod EmptyClassModule {
2424
pub type Type = ::std::os::raw::c_char;
2525
}
2626
#[repr(i8)]
27-
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
27+
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
2828
pub enum ForwardClassRustified {
2929
__bindgen_cannot_repr_c_on_empty_enum = 0,
3030
}

tests/expectations/tests/enum-default-bitfield.rs

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ impl ::std::ops::BitAndAssign for foo__bindgen_ty_1 {
3939
}
4040
}
4141
#[repr(transparent)]
42-
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
42+
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
4343
pub struct foo__bindgen_ty_1(pub ::std::os::raw::c_uint);
4444
#[test]
4545
fn bindgen_test_layout_foo() {
@@ -102,10 +102,46 @@ impl ::std::ops::BitAndAssign for Foo {
102102
}
103103
}
104104
#[repr(transparent)]
105-
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
105+
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
106106
pub struct Foo(pub ::std::os::raw::c_uint);
107107
pub mod Neg {
108108
pub type Type = ::std::os::raw::c_int;
109109
pub const MinusOne: Type = -1;
110110
pub const One: Type = 1;
111111
}
112+
impl NoDebug {
113+
pub const NoDebug1: NoDebug = NoDebug(0);
114+
}
115+
impl NoDebug {
116+
pub const NoDebug2: NoDebug = NoDebug(1);
117+
}
118+
impl ::std::ops::BitOr<NoDebug> for NoDebug {
119+
type Output = Self;
120+
#[inline]
121+
fn bitor(self, other: Self) -> Self {
122+
NoDebug(self.0 | other.0)
123+
}
124+
}
125+
impl ::std::ops::BitOrAssign for NoDebug {
126+
#[inline]
127+
fn bitor_assign(&mut self, rhs: NoDebug) {
128+
self.0 |= rhs.0;
129+
}
130+
}
131+
impl ::std::ops::BitAnd<NoDebug> for NoDebug {
132+
type Output = Self;
133+
#[inline]
134+
fn bitand(self, other: Self) -> Self {
135+
NoDebug(self.0 & other.0)
136+
}
137+
}
138+
impl ::std::ops::BitAndAssign for NoDebug {
139+
#[inline]
140+
fn bitand_assign(&mut self, rhs: NoDebug) {
141+
self.0 &= rhs.0;
142+
}
143+
}
144+
#[repr(transparent)]
145+
/// <div rustbindgen nodebug></div>
146+
#[derive(Copy, Clone, Hash, PartialEq, Eq)]
147+
pub struct NoDebug(pub ::std::os::raw::c_uint);

tests/expectations/tests/enum-default-consts.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,7 @@ pub mod Neg {
4949
pub const MinusOne: Type = -1;
5050
pub const One: Type = 1;
5151
}
52+
pub const NoDebug_NoDebug1: NoDebug = 0;
53+
pub const NoDebug_NoDebug2: NoDebug = 1;
54+
/// <div rustbindgen nodebug></div>
55+
pub type NoDebug = ::std::os::raw::c_uint;

tests/expectations/tests/enum-default-module.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,9 @@ pub mod Neg {
5353
pub const MinusOne: Type = -1;
5454
pub const One: Type = 1;
5555
}
56+
pub mod NoDebug {
57+
/// <div rustbindgen nodebug></div>
58+
pub type Type = ::std::os::raw::c_uint;
59+
pub const NoDebug1: Type = 0;
60+
pub const NoDebug2: Type = 1;
61+
}

tests/expectations/tests/enum-default-rust.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub struct foo {
1313
pub const foo_FOO_A: foo__bindgen_ty_1 = foo__bindgen_ty_1::FOO_A;
1414
pub const foo_FOO_B: foo__bindgen_ty_1 = foo__bindgen_ty_1::FOO_B;
1515
#[repr(u32)]
16-
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
16+
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
1717
pub enum foo__bindgen_ty_1 {
1818
FOO_A = 0,
1919
FOO_B = 1,
@@ -47,7 +47,7 @@ impl Default for foo {
4747
}
4848
}
4949
#[repr(u32)]
50-
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
50+
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
5151
pub enum Foo {
5252
Bar = 0,
5353
Qux = 1,
@@ -57,3 +57,10 @@ pub mod Neg {
5757
pub const MinusOne: Type = -1;
5858
pub const One: Type = 1;
5959
}
60+
#[repr(u32)]
61+
/// <div rustbindgen nodebug></div>
62+
#[derive(Copy, Clone, Hash, PartialEq, Eq)]
63+
pub enum NoDebug {
64+
NoDebug1 = 0,
65+
NoDebug2 = 1,
66+
}

tests/expectations/tests/enum-doc-bitfield.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,5 @@ impl ::std::ops::BitAndAssign for B {
6060
}
6161
#[repr(transparent)]
6262
/// Document enum
63-
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
63+
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
6464
pub struct B(pub ::std::os::raw::c_uint);

tests/expectations/tests/enum-doc-rusty.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
#[repr(u32)]
99
/// Document enum
10-
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
10+
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
1111
pub enum B {
1212
/// Document field with three slashes
1313
VAR_A = 0,

tests/expectations/tests/enum-undefault.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
)]
77

88
#[repr(u32)]
9-
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
9+
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
1010
pub enum Foo {
1111
Bar = 0,
1212
Qux = 1,

tests/expectations/tests/enum.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,7 @@ pub type Foo = ::std::os::raw::c_uint;
4747
pub const Neg_MinusOne: Neg = -1;
4848
pub const Neg_One: Neg = 1;
4949
pub type Neg = ::std::os::raw::c_int;
50+
pub const NoDebug_NoDebug1: NoDebug = 0;
51+
pub const NoDebug_NoDebug2: NoDebug = 1;
52+
/// <div rustbindgen nodebug></div>
53+
pub type NoDebug = ::std::os::raw::c_uint;

tests/expectations/tests/enum_alias.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
)]
77

88
#[repr(u8)]
9-
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
9+
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
1010
pub enum Bar {
1111
VAL = 0,
1212
}

tests/expectations/tests/enum_and_vtable_mangling.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
pub const match_: _bindgen_ty_1 = _bindgen_ty_1::match_;
99
pub const whatever_else: _bindgen_ty_1 = _bindgen_ty_1::whatever_else;
1010
#[repr(u32)]
11-
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
11+
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
1212
pub enum _bindgen_ty_1 {
1313
match_ = 0,
1414
whatever_else = 1,

tests/expectations/tests/enum_dupe.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ impl Foo {
99
pub const Dupe: Foo = Foo::Bar;
1010
}
1111
#[repr(u32)]
12-
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
12+
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
1313
pub enum Foo {
1414
Bar = 1,
1515
}

0 commit comments

Comments
 (0)