Skip to content

Commit a2409da

Browse files
author
bors-servo
authored
Auto merge of #1232 - emilio:enum-colon-bool, r=pepyakin
codegen: Try to reasonably handle enum : bool. Just use the repr name we generate, since we generate constants for that. It's not worth trying to guess the actual type to use IMO. Bindings lose a bit of portability I guess, but that's really a lost bet already, so instead of special-casing bool and map constants, let's use a consistent representation everywhere. Fixes #1145
2 parents 8fa15cc + a79b404 commit a2409da

15 files changed

+61
-47
lines changed

src/codegen/mod.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -2486,13 +2486,10 @@ impl CodeGenerator for Enum {
24862486
});
24872487
}
24882488

2489-
let repr =
2490-
self.repr()
2491-
.and_then(|repr| repr.try_to_rust_ty_or_opaque(ctx, &()).ok())
2492-
.unwrap_or_else(|| {
2493-
let repr_name = ctx.rust_ident_raw(repr_name);
2494-
quote! { #repr_name }
2495-
});
2489+
let repr = {
2490+
let repr_name = ctx.rust_ident_raw(repr_name);
2491+
quote! { #repr_name }
2492+
};
24962493

24972494
let mut builder = EnumBuilder::new(
24982495
&name,

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

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
/* automatically generated by rust-bindgen */
22

3-
43
#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]
54

6-
75
pub const Foo_Bar: Foo = Foo(2);
86
pub const Foo_Baz: Foo = Foo(4);
97
pub const Foo_Duplicated: Foo = Foo(4);
@@ -36,7 +34,7 @@ impl ::std::ops::BitAndAssign for Foo {
3634
}
3735
#[repr(C)]
3836
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
39-
pub struct Foo(pub ::std::os::raw::c_int);
37+
pub struct Foo(pub i32);
4038
pub const Buz_Bar: Buz = Buz(2);
4139
pub const Buz_Baz: Buz = Buz(4);
4240
pub const Buz_Duplicated: Buz = Buz(4);
@@ -69,7 +67,7 @@ impl ::std::ops::BitAndAssign for Buz {
6967
}
7068
#[repr(C)]
7169
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
72-
pub struct Buz(pub ::std::os::raw::c_schar);
70+
pub struct Buz(pub i8);
7371
pub const NS_FOO: _bindgen_ty_1 = _bindgen_ty_1(1);
7472
pub const NS_BAR: _bindgen_ty_1 = _bindgen_ty_1(2);
7573
impl ::std::ops::BitOr<_bindgen_ty_1> for _bindgen_ty_1 {
@@ -100,7 +98,7 @@ impl ::std::ops::BitAndAssign for _bindgen_ty_1 {
10098
}
10199
#[repr(C)]
102100
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
103-
pub struct _bindgen_ty_1(pub ::std::os::raw::c_uint);
101+
pub struct _bindgen_ty_1(pub u32);
104102
#[repr(C)]
105103
#[derive(Debug, Default, Copy, Clone)]
106104
pub struct Dummy {
@@ -136,7 +134,7 @@ impl ::std::ops::BitAndAssign for Dummy__bindgen_ty_1 {
136134
}
137135
#[repr(C)]
138136
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
139-
pub struct Dummy__bindgen_ty_1(pub ::std::os::raw::c_uint);
137+
pub struct Dummy__bindgen_ty_1(pub u32);
140138
#[test]
141139
fn bindgen_test_layout_Dummy() {
142140
assert_eq!(

tests/expectations/tests/constify-all-enums.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
/* automatically generated by rust-bindgen */
22

3-
43
#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]
54

6-
75
pub const foo_THIS: foo = 0;
86
pub const foo_SHOULD_BE: foo = 1;
97
pub const foo_A_CONSTANT: foo = 2;
10-
pub type foo = ::std::os::raw::c_uint;
8+
pub type foo = u32;
119
#[repr(C)]
1210
#[derive(Debug, Copy, Clone)]
1311
pub struct bar {

tests/expectations/tests/constify-module-enums-basic.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
/* automatically generated by rust-bindgen */
22

3-
43
#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]
54

6-
75
pub mod foo {
8-
pub type Type = ::std::os::raw::c_uint;
6+
pub type Type = u32;
97
pub const THIS: Type = 0;
108
pub const SHOULD_BE: Type = 1;
119
pub const A_CONSTANT: Type = 2;

tests/expectations/tests/constify-module-enums-namespace.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
/* automatically generated by rust-bindgen */
22

3-
43
#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]
54

6-
75
#[allow(non_snake_case, non_camel_case_types, non_upper_case_globals)]
86
pub mod root {
97
#[allow(unused_imports)]
@@ -15,7 +13,7 @@ pub mod root {
1513
#[allow(unused_imports)]
1614
use self::super::super::super::root;
1715
pub mod foo {
18-
pub type Type = ::std::os::raw::c_uint;
16+
pub type Type = u32;
1917
pub const THIS: Type = 0;
2018
pub const SHOULD_BE: Type = 1;
2119
pub const A_CONSTANT: Type = 2;

tests/expectations/tests/constify-module-enums-shadow-name.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
/* automatically generated by rust-bindgen */
22

3-
43
#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]
54

6-
75
pub mod foo {
8-
pub type Type = ::std::os::raw::c_uint;
6+
pub type Type = u32;
97
pub const Type: Type = 0;
108
pub const Type_: Type = 1;
119
pub const Type1: Type = 2;

tests/expectations/tests/constify-module-enums-simple-alias.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
/* automatically generated by rust-bindgen */
22

3-
43
#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]
54

6-
75
pub mod Foo {
8-
pub type Type = ::std::os::raw::c_int;
6+
pub type Type = i32;
97
pub const Variant1: Type = 0;
108
pub const Variant2: Type = 1;
119
pub const Variant3: Type = 2;

tests/expectations/tests/constify-module-enums-simple-nonamespace.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
/* automatically generated by rust-bindgen */
22

3-
43
#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]
54

6-
75
pub mod one_Foo {
8-
pub type Type = ::std::os::raw::c_int;
6+
pub type Type = i32;
97
pub const Variant1: Type = 0;
108
pub const Variant2: Type = 1;
119
}

tests/expectations/tests/constify-module-enums-types.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,30 @@
11
/* automatically generated by rust-bindgen */
22

3-
43
#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]
54

6-
75
pub mod foo {
8-
pub type Type = ::std::os::raw::c_uint;
6+
pub type Type = u32;
97
pub const THIS: Type = 0;
108
pub const SHOULD_BE: Type = 1;
119
pub const A_CONSTANT: Type = 2;
1210
pub const ALSO_THIS: Type = 42;
1311
pub const AND_ALSO_THIS: Type = 42;
1412
}
1513
pub mod anon_enum {
16-
pub type Type = ::std::os::raw::c_uint;
14+
pub type Type = u32;
1715
pub const Variant1: Type = 0;
1816
pub const Variant2: Type = 1;
1917
pub const Variant3: Type = 2;
2018
}
2119
pub mod ns1_foo {
22-
pub type Type = ::std::os::raw::c_uint;
20+
pub type Type = u32;
2321
pub const THIS: Type = 0;
2422
pub const SHOULD_BE: Type = 1;
2523
pub const A_CONSTANT: Type = 2;
2624
pub const ALSO_THIS: Type = 42;
2725
}
2826
pub mod ns2_Foo {
29-
pub type Type = ::std::os::raw::c_int;
27+
pub type Type = i32;
3028
pub const Variant1: Type = 0;
3129
pub const Variant2: Type = 1;
3230
}
@@ -202,7 +200,7 @@ impl Default for Baz {
202200
}
203201
}
204202
pub mod one_Foo {
205-
pub type Type = ::std::os::raw::c_int;
203+
pub type Type = i32;
206204
pub const Variant1: Type = 0;
207205
pub const Variant2: Type = 1;
208206
}

tests/expectations/tests/empty-enum.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,30 @@
22

33
#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]
44

5-
pub type EmptyConstified = ::std::os::raw::c_uint;
5+
pub type EmptyConstified = u32;
66
#[repr(u32)]
77
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
88
pub enum EmptyRustified {
99
__bindgen_cannot_repr_c_on_empty_enum = 0,
1010
}
1111
pub mod EmptyModule {
12-
pub type Type = ::std::os::raw::c_uint;
12+
pub type Type = u32;
1313
}
1414
#[repr(i8)]
1515
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
1616
pub enum EmptyClassRustified {
1717
__bindgen_cannot_repr_c_on_empty_enum = 0,
1818
}
19-
pub type EmptyClassConstified = ::std::os::raw::c_char;
19+
pub type EmptyClassConstified = i8;
2020
pub mod EmptyClassModule {
21-
pub type Type = ::std::os::raw::c_char;
21+
pub type Type = i8;
2222
}
2323
#[repr(i8)]
2424
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
2525
pub enum ForwardClassRustified {
2626
__bindgen_cannot_repr_c_on_empty_enum = 0,
2727
}
28-
pub type ForwardClassConstified = ::std::os::raw::c_char;
28+
pub type ForwardClassConstified = i8;
2929
pub mod ForwardClassModule {
30-
pub type Type = ::std::os::raw::c_char;
30+
pub type Type = i8;
3131
}

tests/expectations/tests/enum_explicit_type.rs

+5
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,8 @@ pub enum MuchLongLong {
3535
pub enum MuchULongLong {
3636
MuchHigh = 4294967296,
3737
}
38+
#[repr(u8)]
39+
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
40+
pub enum BoolEnumsAreFun {
41+
Value = 1,
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/* automatically generated by rust-bindgen */
2+
3+
#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]
4+
5+
pub const Foo_Bar: Foo = 0;
6+
pub const Foo_Qux: Foo = 1;
7+
pub type Foo = u8;
8+
pub const Neg_MinusOne: Neg = -1;
9+
pub const Neg_One: Neg = 1;
10+
pub type Neg = i8;
11+
pub const Bigger_Much: Bigger = 255;
12+
pub const Bigger_Larger: Bigger = 256;
13+
pub type Bigger = u16;
14+
pub const MuchLong_MuchLow: MuchLong = -4294967296;
15+
pub type MuchLong = i64;
16+
pub const MuchLongLong_I64_MIN: MuchLongLong = -9223372036854775808;
17+
pub type MuchLongLong = i64;
18+
pub const MuchULongLong_MuchHigh: MuchULongLong = 4294967296;
19+
pub type MuchULongLong = u64;
20+
pub const BoolEnumsAreFun_Value: BoolEnumsAreFun = 1;
21+
pub type BoolEnumsAreFun = u8;
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
/* automatically generated by rust-bindgen */
22

3-
43
#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]
54

6-
75
pub const FOO_BAR: foo = 0;
86
pub const FOO_BAZ: foo = 1;
9-
pub type foo = ::std::os::raw::c_uint;
7+
pub type foo = u32;

tests/headers/enum_explicit_type.hpp

+4
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,7 @@ enum MuchLongLong: long long {
2626
enum MuchULongLong: unsigned long long {
2727
MuchHigh = 4294967296,
2828
};
29+
30+
enum BoolEnumsAreFun: bool {
31+
Value = true,
32+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// bindgen-flags: -- -std=c++11
2+
//
3+
// This test is much like enum_explicit_type, but without --rustified-enum.
4+
5+
#include "enum_explicit_type.hpp"

0 commit comments

Comments
 (0)