Skip to content

Commit 6b4718a

Browse files
committed
Always add repr(C) to rustified enums
If we don't, then eddyb's recent layout optimizations will do unexpected things to them.
1 parent d9950d4 commit 6b4718a

Some content is hidden

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

42 files changed

+273
-68
lines changed

src/codegen/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2185,6 +2185,7 @@ impl<'a> EnumBuilder<'a> {
21852185
EnumVariation::Rust => {
21862186
let mut tokens = quote! {
21872187
#( #attrs )*
2188+
#[repr(C)]
21882189
pub enum #ident
21892190
};
21902191
tokens.append("{");

tests/expectations/tests/anon_enum.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ pub struct Test {
1313
pub const Test_T_NONE: Test__bindgen_ty_1 = Test__bindgen_ty_1::T_NONE;
1414
#[repr(u32)]
1515
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
16+
#[repr(C)]
1617
pub enum Test__bindgen_ty_1 {
1718
T_NONE = 0,
1819
}
@@ -41,6 +42,7 @@ fn bindgen_test_layout_Test() {
4142
}
4243
#[repr(u32)]
4344
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
45+
#[repr(C)]
4446
pub enum Baz {
4547
Foo = 0,
4648
Bar = 1,

tests/expectations/tests/anon_enum_trait.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ pub const DataType_fmt: DataType__bindgen_ty_1 = DataType__bindgen_ty_1::generic
2020
pub const DataType_type_: DataType__bindgen_ty_1 = DataType__bindgen_ty_1::generic_type;
2121
#[repr(i32)]
2222
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
23+
#[repr(C)]
2324
pub enum DataType__bindgen_ty_1 {
2425
generic_type = 0,
2526
}
@@ -32,6 +33,7 @@ pub const Foo_Bar: Foo__bindgen_ty_1 = Foo__bindgen_ty_1::Bar;
3233
pub const Foo_Baz: Foo__bindgen_ty_1 = Foo__bindgen_ty_1::Bar;
3334
#[repr(u32)]
3435
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
36+
#[repr(C)]
3537
pub enum Foo__bindgen_ty_1 {
3638
Bar = 0,
3739
}

tests/expectations/tests/anon_enum_whitelist.rs

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

tests/expectations/tests/anon_union.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ pub const TErrorResult_UnionState_HasException: TErrorResult_UnionState =
1515
TErrorResult_UnionState::HasMessage;
1616
#[repr(i32)]
1717
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
18+
#[repr(C)]
1819
pub enum TErrorResult_UnionState {
1920
HasMessage = 0,
2021
}

tests/expectations/tests/anon_union_1_0.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ pub const TErrorResult_UnionState_HasException: TErrorResult_UnionState =
5959
TErrorResult_UnionState::HasMessage;
6060
#[repr(i32)]
6161
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
62+
#[repr(C)]
6263
pub enum TErrorResult_UnionState {
6364
HasMessage = 0,
6465
}

tests/expectations/tests/bitfield_align_2.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ where
8585
}
8686
#[repr(u32)]
8787
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
88+
#[repr(C)]
8889
pub enum MyEnum {
8990
ONE = 0,
9091
TWO = 1,

tests/expectations/tests/class_with_inner_struct.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ fn bindgen_test_layout_B() {
215215
}
216216
#[repr(i32)]
217217
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
218+
#[repr(C)]
218219
pub enum StepSyntax {
219220
Keyword = 0,
220221
FunctionalWithoutKeyword = 1,

tests/expectations/tests/class_with_inner_struct_1_0.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,7 @@ impl Clone for B {
273273
}
274274
#[repr(i32)]
275275
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
276+
#[repr(C)]
276277
pub enum StepSyntax {
277278
Keyword = 0,
278279
FunctionalWithoutKeyword = 1,

tests/expectations/tests/const_enum_unnamed.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ pub const FOO_BAR: _bindgen_ty_1 = _bindgen_ty_1::FOO_BAR;
88
pub const FOO_BAZ: _bindgen_ty_1 = _bindgen_ty_1::FOO_BAZ;
99
#[repr(u32)]
1010
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
11+
#[repr(C)]
1112
pub enum _bindgen_ty_1 {
1213
FOO_BAR = 0,
1314
FOO_BAZ = 1,
@@ -20,6 +21,7 @@ pub struct Foo {
2021
pub const Foo_FOO_BAR: Foo__bindgen_ty_1 = Foo__bindgen_ty_1::FOO_BAR;
2122
#[repr(u32)]
2223
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
24+
#[repr(C)]
2325
pub enum Foo__bindgen_ty_1 {
2426
FOO_BAR = 10,
2527
}

tests/expectations/tests/constify-enum.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ pub const nsCSSPropertyID_eCSSProperty_COUNT: nsCSSPropertyID =
1010
nsCSSPropertyID::eCSSPropertyAlias_aa;
1111
#[repr(u32)]
1212
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
13+
#[repr(C)]
1314
pub enum nsCSSPropertyID {
1415
eCSSProperty_a = 0,
1516
eCSSProperty_b = 1,

tests/expectations/tests/enum.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66

77
#[repr(u32)]
88
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
9+
#[repr(C)]
910
pub enum Foo {
1011
Bar = 0,
1112
Qux = 1,
1213
}
1314
#[repr(i32)]
1415
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
16+
#[repr(C)]
1517
pub enum Neg {
1618
MinusOne = -1,
1719
One = 1,

tests/expectations/tests/enum_alias.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#[repr(u8)]
88
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
9+
#[repr(C)]
910
pub enum Bar {
1011
VAL = 0,
1112
}

tests/expectations/tests/enum_and_vtable_mangling.rs

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

tests/expectations/tests/enum_dupe.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
pub const Foo_Dupe: Foo = Foo::Bar;
88
#[repr(u32)]
99
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
10+
#[repr(C)]
1011
pub enum Foo {
1112
Bar = 1,
1213
}

tests/expectations/tests/enum_explicit_type.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,34 +6,40 @@
66

77
#[repr(u8)]
88
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
9+
#[repr(C)]
910
pub enum Foo {
1011
Bar = 0,
1112
Qux = 1,
1213
}
1314
#[repr(i8)]
1415
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
16+
#[repr(C)]
1517
pub enum Neg {
1618
MinusOne = -1,
1719
One = 1,
1820
}
1921
#[repr(u16)]
2022
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
23+
#[repr(C)]
2124
pub enum Bigger {
2225
Much = 255,
2326
Larger = 256,
2427
}
2528
#[repr(i64)]
2629
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
30+
#[repr(C)]
2731
pub enum MuchLong {
2832
MuchLow = -4294967296,
2933
}
3034
#[repr(i64)]
3135
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
36+
#[repr(C)]
3237
pub enum MuchLongLong {
3338
I64_MIN = -9223372036854775808,
3439
}
3540
#[repr(u64)]
3641
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
42+
#[repr(C)]
3743
pub enum MuchULongLong {
3844
MuchHigh = 4294967296,
3945
}

tests/expectations/tests/enum_in_template_with_typedef.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ pub const std_fbstring_core_Category_Bar: std_fbstring_core_Category =
1414
std_fbstring_core_Category::Foo;
1515
#[repr(u8)]
1616
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
17+
#[repr(C)]
1718
pub enum std_fbstring_core_Category {
1819
Foo = 0,
1920
}

tests/expectations/tests/enum_negative.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#[repr(i32)]
88
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
9+
#[repr(C)]
910
pub enum Foo {
1011
Bar = -2,
1112
Qux = 1,

tests/expectations/tests/enum_packed.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,21 @@
66

77
#[repr(u8)]
88
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
9+
#[repr(C)]
910
pub enum Foo {
1011
Bar = 0,
1112
Qux = 1,
1213
}
1314
#[repr(i8)]
1415
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
16+
#[repr(C)]
1517
pub enum Neg {
1618
MinusOne = -1,
1719
One = 1,
1820
}
1921
#[repr(u16)]
2022
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
23+
#[repr(C)]
2124
pub enum Bigger {
2225
Much = 255,
2326
Larger = 256,

tests/expectations/tests/forward-enum-decl.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#[repr(i32)]
88
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
9+
#[repr(C)]
910
pub enum CSSPseudoClassType {
1011
empty = 0,
1112
link = 1,

tests/expectations/tests/func_ptr_in_struct.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55

66

77
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
8-
pub enum baz {}
8+
#[repr(C)]
9+
pub enum baz {
10+
__bindgen_cannot_repr_c_on_empty_enum = 0,
11+
}
912
#[repr(C)]
1013
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
1114
pub struct Foo {

tests/expectations/tests/issue-372.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ pub mod root {
7878
}
7979
#[repr(u32)]
8080
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
81+
#[repr(C)]
8182
pub enum n {
8283
o = 0,
8384
p = 1,

tests/expectations/tests/issue-410.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,8 @@ pub mod root {
4141
}
4242
}
4343
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
44-
pub enum JSWhyMagic {}
44+
#[repr(C)]
45+
pub enum JSWhyMagic {
46+
__bindgen_cannot_repr_c_on_empty_enum = 0,
47+
}
4548
}

tests/expectations/tests/issue-493.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ pub const basic_string___min_cap: basic_string__bindgen_ty_1 =
7171
basic_string__bindgen_ty_1::__min_cap;
7272
#[repr(i32)]
7373
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
74+
#[repr(C)]
7475
pub enum basic_string__bindgen_ty_1 {
7576
__min_cap = 0,
7677
}
@@ -110,6 +111,7 @@ pub const basic_string___n_words: basic_string__bindgen_ty_2 =
110111
basic_string__bindgen_ty_2::__n_words;
111112
#[repr(i32)]
112113
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
114+
#[repr(C)]
113115
pub enum basic_string__bindgen_ty_2 {
114116
__n_words = 0,
115117
}

tests/expectations/tests/issue-493_1_0.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ pub const basic_string___min_cap: basic_string__bindgen_ty_1 =
7171
basic_string__bindgen_ty_1::__min_cap;
7272
#[repr(i32)]
7373
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
74+
#[repr(C)]
7475
pub enum basic_string__bindgen_ty_1 {
7576
__min_cap = 0,
7677
}
@@ -108,6 +109,7 @@ pub const basic_string___n_words: basic_string__bindgen_ty_2 =
108109
basic_string__bindgen_ty_2::__n_words;
109110
#[repr(i32)]
110111
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
112+
#[repr(C)]
111113
pub enum basic_string__bindgen_ty_2 {
112114
__n_words = 0,
113115
}

tests/expectations/tests/issue-569-non-type-template-params-causing-layout-test-failures.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ pub const ENUM_VARIANT_1: _bindgen_ty_1 = _bindgen_ty_1::ENUM_VARIANT_1;
88
pub const ENUM_VARIANT_2: _bindgen_ty_1 = _bindgen_ty_1::ENUM_VARIANT_2;
99
#[repr(u32)]
1010
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
11+
#[repr(C)]
1112
pub enum _bindgen_ty_1 {
1213
ENUM_VARIANT_1 = 0,
1314
ENUM_VARIANT_2 = 1,

tests/expectations/tests/issue-888-enum-var-decl-jump.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,8 @@ pub mod root {
3535
}
3636
}
3737
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
38-
pub enum a {}
38+
#[repr(C)]
39+
pub enum a {
40+
__bindgen_cannot_repr_c_on_empty_enum = 0,
41+
}
3942
}

tests/expectations/tests/jsval_layout_opaque.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ pub const JSVAL_PAYLOAD_MASK: ::std::os::raw::c_ulonglong = 140737488355327;
8888
pub const JSVAL_TAG_MASK: ::std::os::raw::c_longlong = -140737488355328;
8989
#[repr(u8)]
9090
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
91+
#[repr(C)]
9192
pub enum JSValueType {
9293
JSVAL_TYPE_DOUBLE = 0,
9394
JSVAL_TYPE_INT32 = 1,
@@ -103,6 +104,7 @@ pub enum JSValueType {
103104
}
104105
#[repr(u32)]
105106
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
107+
#[repr(C)]
106108
pub enum JSValueTag {
107109
JSVAL_TAG_MAX_DOUBLE = 131056,
108110
JSVAL_TAG_INT32 = 131057,
@@ -116,6 +118,7 @@ pub enum JSValueTag {
116118
}
117119
#[repr(u64)]
118120
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
121+
#[repr(C)]
119122
pub enum JSValueShiftedTag {
120123
JSVAL_SHIFTED_TAG_MAX_DOUBLE = 18444492278190833663,
121124
JSVAL_SHIFTED_TAG_INT32 = 18444633011384221696,
@@ -129,6 +132,7 @@ pub enum JSValueShiftedTag {
129132
}
130133
#[repr(u32)]
131134
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
135+
#[repr(C)]
132136
pub enum JSWhyMagic {
133137
JS_ELEMENTS_HOLE = 0,
134138
JS_NO_ITER_VALUE = 1,

tests/expectations/tests/jsval_layout_opaque_1_0.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ pub const JSVAL_PAYLOAD_MASK: ::std::os::raw::c_ulonglong = 140737488355327;
131131
pub const JSVAL_TAG_MASK: ::std::os::raw::c_longlong = -140737488355328;
132132
#[repr(u8)]
133133
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
134+
#[repr(C)]
134135
pub enum JSValueType {
135136
JSVAL_TYPE_DOUBLE = 0,
136137
JSVAL_TYPE_INT32 = 1,
@@ -146,6 +147,7 @@ pub enum JSValueType {
146147
}
147148
#[repr(u32)]
148149
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
150+
#[repr(C)]
149151
pub enum JSValueTag {
150152
JSVAL_TAG_MAX_DOUBLE = 131056,
151153
JSVAL_TAG_INT32 = 131057,
@@ -159,6 +161,7 @@ pub enum JSValueTag {
159161
}
160162
#[repr(u64)]
161163
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
164+
#[repr(C)]
162165
pub enum JSValueShiftedTag {
163166
JSVAL_SHIFTED_TAG_MAX_DOUBLE = 18444492278190833663,
164167
JSVAL_SHIFTED_TAG_INT32 = 18444633011384221696,
@@ -172,6 +175,7 @@ pub enum JSValueShiftedTag {
172175
}
173176
#[repr(u32)]
174177
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
178+
#[repr(C)]
175179
pub enum JSWhyMagic {
176180
JS_ELEMENTS_HOLE = 0,
177181
JS_NO_ITER_VALUE = 1,

tests/expectations/tests/layout_array_too_long.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ pub const IP_MIN_FRAG_NUM: _bindgen_ty_1 = _bindgen_ty_1::IP_MIN_FRAG_NUM;
1212
pub const IP_MAX_FRAG_NUM: _bindgen_ty_1 = _bindgen_ty_1::IP_MAX_FRAG_NUM;
1313
#[repr(u32)]
1414
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
15+
#[repr(C)]
1516
pub enum _bindgen_ty_1 {
1617
IP_LAST_FRAG_IDX = 0,
1718
IP_FIRST_FRAG_IDX = 1,

0 commit comments

Comments
 (0)