Skip to content

Commit

Permalink
codegen: Allow to not derive Debug on enums.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
emilio committed Oct 16, 2020
1 parent e096149 commit a467d3e
Show file tree
Hide file tree
Showing 64 changed files with 183 additions and 120 deletions.
22 changes: 11 additions & 11 deletions src/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2840,17 +2840,17 @@ impl CodeGenerator for Enum {
}

if !variation.is_const() {
let mut derives =
vec!["Debug", "Copy", "Clone", "PartialEq", "Eq", "Hash"];

if item.can_derive_partialord(ctx) {
derives.push("PartialOrd");
}

if item.can_derive_ord(ctx) {
derives.push("Ord");
}

let mut derives = derives_of_item(item, ctx);
// For backwards compat, enums always derive Clone/Eq/PartialEq/Hash, even
// if we don't generate those by default.
derives.insert(
DerivableTraits::CLONE |
DerivableTraits::COPY |
DerivableTraits::HASH |
DerivableTraits::PARTIAL_EQ |
DerivableTraits::EQ,
);
let derives: Vec<_> = derives.into();
attrs.push(attributes::derives(&derives));
}

Expand Down
4 changes: 2 additions & 2 deletions tests/expectations/tests/anon_enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub struct Test {
}
pub const Test_T_NONE: Test__bindgen_ty_1 = Test__bindgen_ty_1::T_NONE;
#[repr(u32)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
pub enum Test__bindgen_ty_1 {
T_NONE = 0,
}
Expand Down Expand Up @@ -41,7 +41,7 @@ fn bindgen_test_layout_Test() {
);
}
#[repr(u32)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
pub enum Baz {
Foo = 0,
Bar = 1,
Expand Down
4 changes: 2 additions & 2 deletions tests/expectations/tests/anon_enum_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub const DataType_fmt: DataType__bindgen_ty_1 =
pub const DataType_type_: DataType__bindgen_ty_1 =
DataType__bindgen_ty_1::generic_type;
#[repr(i32)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
pub enum DataType__bindgen_ty_1 {
generic_type = 0,
}
Expand All @@ -37,7 +37,7 @@ pub struct Foo {
pub const Foo_Bar: Foo__bindgen_ty_1 = Foo__bindgen_ty_1::Bar;
pub const Foo_Baz: Foo__bindgen_ty_1 = Foo__bindgen_ty_1::Bar;
#[repr(u32)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
pub enum Foo__bindgen_ty_1 {
Bar = 0,
}
Expand Down
2 changes: 1 addition & 1 deletion tests/expectations/tests/anon_enum_whitelist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
pub const NODE_FLAG_FOO: _bindgen_ty_1 = _bindgen_ty_1::NODE_FLAG_FOO;
pub const NODE_FLAG_BAR: _bindgen_ty_1 = _bindgen_ty_1::NODE_FLAG_BAR;
#[repr(u32)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
pub enum _bindgen_ty_1 {
NODE_FLAG_FOO = 0,
NODE_FLAG_BAR = 1,
Expand Down
2 changes: 1 addition & 1 deletion tests/expectations/tests/anon_union.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ impl TErrorResult_UnionState {
TErrorResult_UnionState::HasMessage;
}
#[repr(i32)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
pub enum TErrorResult_UnionState {
HasMessage = 0,
}
Expand Down
2 changes: 1 addition & 1 deletion tests/expectations/tests/anon_union_1_0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ pub struct TErrorResult {
pub const TErrorResult_UnionState_HasException: TErrorResult_UnionState =
TErrorResult_UnionState::HasMessage;
#[repr(i32)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
pub enum TErrorResult_UnionState {
HasMessage = 0,
}
Expand Down
8 changes: 4 additions & 4 deletions tests/expectations/tests/bitfield-enum-basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl ::std::ops::BitAndAssign for Foo {
}
}
#[repr(transparent)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
pub struct Foo(pub ::std::os::raw::c_int);
impl Buz {
pub const Bar: Buz = Buz(2);
Expand Down Expand Up @@ -85,7 +85,7 @@ impl ::std::ops::BitAndAssign for Buz {
}
}
#[repr(transparent)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
pub struct Buz(pub ::std::os::raw::c_schar);
pub const NS_FOO: _bindgen_ty_1 = _bindgen_ty_1(1);
pub const NS_BAR: _bindgen_ty_1 = _bindgen_ty_1(2);
Expand Down Expand Up @@ -116,7 +116,7 @@ impl ::std::ops::BitAndAssign for _bindgen_ty_1 {
}
}
#[repr(transparent)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
pub struct _bindgen_ty_1(pub ::std::os::raw::c_uint);
#[repr(C)]
#[derive(Debug, Default, Copy, Clone)]
Expand Down Expand Up @@ -152,7 +152,7 @@ impl ::std::ops::BitAndAssign for Dummy__bindgen_ty_1 {
}
}
#[repr(transparent)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
pub struct Dummy__bindgen_ty_1(pub ::std::os::raw::c_uint);
#[test]
fn bindgen_test_layout_Dummy() {
Expand Down
2 changes: 1 addition & 1 deletion tests/expectations/tests/bitfield-enum-repr-c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,5 @@ impl ::std::ops::BitAndAssign for Foo {
}
}
#[repr(C)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
pub struct Foo(pub ::std::os::raw::c_int);
2 changes: 1 addition & 1 deletion tests/expectations/tests/bitfield-enum-repr-transparent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,5 @@ impl ::std::ops::BitAndAssign for Foo {
}
}
#[repr(transparent)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
pub struct Foo(pub ::std::os::raw::c_int);
2 changes: 1 addition & 1 deletion tests/expectations/tests/bitfield_align_2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ where
}
}
#[repr(u32)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
pub enum MyEnum {
ONE = 0,
TWO = 1,
Expand Down
2 changes: 1 addition & 1 deletion tests/expectations/tests/class_with_inner_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ fn bindgen_test_layout_B() {
);
}
#[repr(i32)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
pub enum StepSyntax {
Keyword = 0,
FunctionalWithoutKeyword = 1,
Expand Down
2 changes: 1 addition & 1 deletion tests/expectations/tests/class_with_inner_struct_1_0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ impl Clone for B {
}
}
#[repr(i32)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
pub enum StepSyntax {
Keyword = 0,
FunctionalWithoutKeyword = 1,
Expand Down
4 changes: 2 additions & 2 deletions tests/expectations/tests/const_enum_unnamed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
pub const FOO_BAR: _bindgen_ty_1 = _bindgen_ty_1::FOO_BAR;
pub const FOO_BAZ: _bindgen_ty_1 = _bindgen_ty_1::FOO_BAZ;
#[repr(u32)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
pub enum _bindgen_ty_1 {
FOO_BAR = 0,
FOO_BAZ = 1,
Expand All @@ -20,7 +20,7 @@ pub struct Foo {
}
pub const Foo_FOO_BAR: Foo__bindgen_ty_1 = Foo__bindgen_ty_1::FOO_BAR;
#[repr(u32)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
pub enum Foo__bindgen_ty_1 {
FOO_BAR = 10,
}
Expand Down
2 changes: 1 addition & 1 deletion tests/expectations/tests/constify-enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ impl nsCSSPropertyID {
nsCSSPropertyID::eCSSPropertyAlias_aa;
}
#[repr(u32)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
pub enum nsCSSPropertyID {
eCSSProperty_a = 0,
eCSSProperty_b = 1,
Expand Down
2 changes: 1 addition & 1 deletion tests/expectations/tests/dupe-enum-variant-in-namespace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub mod root {
pub const Foo3: root::foo::Bar = Bar::Foo2;
}
#[repr(u32)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
pub enum Bar {
Foo = 0,
Foo2 = 1,
Expand Down
6 changes: 3 additions & 3 deletions tests/expectations/tests/empty-enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@

pub type EmptyConstified = ::std::os::raw::c_uint;
#[repr(u32)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
pub enum EmptyRustified {
__bindgen_cannot_repr_c_on_empty_enum = 0,
}
pub mod EmptyModule {
pub type Type = ::std::os::raw::c_uint;
}
#[repr(i8)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
pub enum EmptyClassRustified {
__bindgen_cannot_repr_c_on_empty_enum = 0,
}
Expand All @@ -24,7 +24,7 @@ pub mod EmptyClassModule {
pub type Type = ::std::os::raw::c_char;
}
#[repr(i8)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
pub enum ForwardClassRustified {
__bindgen_cannot_repr_c_on_empty_enum = 0,
}
Expand Down
40 changes: 38 additions & 2 deletions tests/expectations/tests/enum-default-bitfield.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ impl ::std::ops::BitAndAssign for foo__bindgen_ty_1 {
}
}
#[repr(transparent)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
pub struct foo__bindgen_ty_1(pub ::std::os::raw::c_uint);
#[test]
fn bindgen_test_layout_foo() {
Expand Down Expand Up @@ -102,10 +102,46 @@ impl ::std::ops::BitAndAssign for Foo {
}
}
#[repr(transparent)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
pub struct Foo(pub ::std::os::raw::c_uint);
pub mod Neg {
pub type Type = ::std::os::raw::c_int;
pub const MinusOne: Type = -1;
pub const One: Type = 1;
}
impl NoDebug {
pub const NoDebug1: NoDebug = NoDebug(0);
}
impl NoDebug {
pub const NoDebug2: NoDebug = NoDebug(1);
}
impl ::std::ops::BitOr<NoDebug> for NoDebug {
type Output = Self;
#[inline]
fn bitor(self, other: Self) -> Self {
NoDebug(self.0 | other.0)
}
}
impl ::std::ops::BitOrAssign for NoDebug {
#[inline]
fn bitor_assign(&mut self, rhs: NoDebug) {
self.0 |= rhs.0;
}
}
impl ::std::ops::BitAnd<NoDebug> for NoDebug {
type Output = Self;
#[inline]
fn bitand(self, other: Self) -> Self {
NoDebug(self.0 & other.0)
}
}
impl ::std::ops::BitAndAssign for NoDebug {
#[inline]
fn bitand_assign(&mut self, rhs: NoDebug) {
self.0 &= rhs.0;
}
}
#[repr(transparent)]
/// <div rustbindgen nodebug></div>
#[derive(Copy, Clone, Hash, PartialEq, Eq)]
pub struct NoDebug(pub ::std::os::raw::c_uint);
4 changes: 4 additions & 0 deletions tests/expectations/tests/enum-default-consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,7 @@ pub mod Neg {
pub const MinusOne: Type = -1;
pub const One: Type = 1;
}
pub const NoDebug_NoDebug1: NoDebug = 0;
pub const NoDebug_NoDebug2: NoDebug = 1;
/// <div rustbindgen nodebug></div>
pub type NoDebug = ::std::os::raw::c_uint;
6 changes: 6 additions & 0 deletions tests/expectations/tests/enum-default-module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,9 @@ pub mod Neg {
pub const MinusOne: Type = -1;
pub const One: Type = 1;
}
pub mod NoDebug {
/// <div rustbindgen nodebug></div>
pub type Type = ::std::os::raw::c_uint;
pub const NoDebug1: Type = 0;
pub const NoDebug2: Type = 1;
}
11 changes: 9 additions & 2 deletions tests/expectations/tests/enum-default-rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub struct foo {
pub const foo_FOO_A: foo__bindgen_ty_1 = foo__bindgen_ty_1::FOO_A;
pub const foo_FOO_B: foo__bindgen_ty_1 = foo__bindgen_ty_1::FOO_B;
#[repr(u32)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
pub enum foo__bindgen_ty_1 {
FOO_A = 0,
FOO_B = 1,
Expand Down Expand Up @@ -47,7 +47,7 @@ impl Default for foo {
}
}
#[repr(u32)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
pub enum Foo {
Bar = 0,
Qux = 1,
Expand All @@ -57,3 +57,10 @@ pub mod Neg {
pub const MinusOne: Type = -1;
pub const One: Type = 1;
}
#[repr(u32)]
/// <div rustbindgen nodebug></div>
#[derive(Copy, Clone, Hash, PartialEq, Eq)]
pub enum NoDebug {
NoDebug1 = 0,
NoDebug2 = 1,
}
2 changes: 1 addition & 1 deletion tests/expectations/tests/enum-doc-bitfield.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,5 @@ impl ::std::ops::BitAndAssign for B {
}
#[repr(transparent)]
/// Document enum
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
pub struct B(pub ::std::os::raw::c_uint);
2 changes: 1 addition & 1 deletion tests/expectations/tests/enum-doc-rusty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

#[repr(u32)]
/// Document enum
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
pub enum B {
/// Document field with three slashes
VAR_A = 0,
Expand Down
2 changes: 1 addition & 1 deletion tests/expectations/tests/enum-undefault.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
)]

#[repr(u32)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
pub enum Foo {
Bar = 0,
Qux = 1,
Expand Down
4 changes: 4 additions & 0 deletions tests/expectations/tests/enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,7 @@ pub type Foo = ::std::os::raw::c_uint;
pub const Neg_MinusOne: Neg = -1;
pub const Neg_One: Neg = 1;
pub type Neg = ::std::os::raw::c_int;
pub const NoDebug_NoDebug1: NoDebug = 0;
pub const NoDebug_NoDebug2: NoDebug = 1;
/// <div rustbindgen nodebug></div>
pub type NoDebug = ::std::os::raw::c_uint;
2 changes: 1 addition & 1 deletion tests/expectations/tests/enum_alias.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
)]

#[repr(u8)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
pub enum Bar {
VAL = 0,
}
2 changes: 1 addition & 1 deletion tests/expectations/tests/enum_and_vtable_mangling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
pub const match_: _bindgen_ty_1 = _bindgen_ty_1::match_;
pub const whatever_else: _bindgen_ty_1 = _bindgen_ty_1::whatever_else;
#[repr(u32)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
pub enum _bindgen_ty_1 {
match_ = 0,
whatever_else = 1,
Expand Down
2 changes: 1 addition & 1 deletion tests/expectations/tests/enum_dupe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ impl Foo {
pub const Dupe: Foo = Foo::Bar;
}
#[repr(u32)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
pub enum Foo {
Bar = 1,
}

0 comments on commit a467d3e

Please sign in to comment.