From a79b404c9dc08aed6dbe7bf07c20647666e9f65d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emilio=20Cobos=20=C3=81lvarez?= Date: Mon, 22 Jan 2018 10:11:21 +0100 Subject: [PATCH] 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 --- src/codegen/mod.rs | 11 ++++------ .../expectations/tests/bitfield-enum-basic.rs | 10 ++++----- .../expectations/tests/constify-all-enums.rs | 4 +--- .../tests/constify-module-enums-basic.rs | 4 +--- .../tests/constify-module-enums-namespace.rs | 4 +--- .../constify-module-enums-shadow-name.rs | 4 +--- .../constify-module-enums-simple-alias.rs | 4 +--- ...onstify-module-enums-simple-nonamespace.rs | 4 +--- .../tests/constify-module-enums-types.rs | 12 +++++------ tests/expectations/tests/empty-enum.rs | 12 +++++------ .../expectations/tests/enum_explicit_type.rs | 5 +++++ .../tests/enum_explicit_type_constants.rs | 21 +++++++++++++++++++ tests/expectations/tests/prepend_enum_name.rs | 4 +--- tests/headers/enum_explicit_type.hpp | 4 ++++ .../headers/enum_explicit_type_constants.hpp | 5 +++++ 15 files changed, 61 insertions(+), 47 deletions(-) create mode 100644 tests/expectations/tests/enum_explicit_type_constants.rs create mode 100644 tests/headers/enum_explicit_type_constants.hpp diff --git a/src/codegen/mod.rs b/src/codegen/mod.rs index 5641b84d30..43a2ec0982 100644 --- a/src/codegen/mod.rs +++ b/src/codegen/mod.rs @@ -2486,13 +2486,10 @@ impl CodeGenerator for Enum { }); } - let repr = - self.repr() - .and_then(|repr| repr.try_to_rust_ty_or_opaque(ctx, &()).ok()) - .unwrap_or_else(|| { - let repr_name = ctx.rust_ident_raw(repr_name); - quote! { #repr_name } - }); + let repr = { + let repr_name = ctx.rust_ident_raw(repr_name); + quote! { #repr_name } + }; let mut builder = EnumBuilder::new( &name, diff --git a/tests/expectations/tests/bitfield-enum-basic.rs b/tests/expectations/tests/bitfield-enum-basic.rs index 84362d1232..091dc70be8 100644 --- a/tests/expectations/tests/bitfield-enum-basic.rs +++ b/tests/expectations/tests/bitfield-enum-basic.rs @@ -1,9 +1,7 @@ /* automatically generated by rust-bindgen */ - #![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)] - pub const Foo_Bar: Foo = Foo(2); pub const Foo_Baz: Foo = Foo(4); pub const Foo_Duplicated: Foo = Foo(4); @@ -36,7 +34,7 @@ impl ::std::ops::BitAndAssign for Foo { } #[repr(C)] #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] -pub struct Foo(pub ::std::os::raw::c_int); +pub struct Foo(pub i32); pub const Buz_Bar: Buz = Buz(2); pub const Buz_Baz: Buz = Buz(4); pub const Buz_Duplicated: Buz = Buz(4); @@ -69,7 +67,7 @@ impl ::std::ops::BitAndAssign for Buz { } #[repr(C)] #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] -pub struct Buz(pub ::std::os::raw::c_schar); +pub struct Buz(pub i8); pub const NS_FOO: _bindgen_ty_1 = _bindgen_ty_1(1); pub const NS_BAR: _bindgen_ty_1 = _bindgen_ty_1(2); impl ::std::ops::BitOr<_bindgen_ty_1> for _bindgen_ty_1 { @@ -100,7 +98,7 @@ impl ::std::ops::BitAndAssign for _bindgen_ty_1 { } #[repr(C)] #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] -pub struct _bindgen_ty_1(pub ::std::os::raw::c_uint); +pub struct _bindgen_ty_1(pub u32); #[repr(C)] #[derive(Debug, Default, Copy, Clone)] pub struct Dummy { @@ -136,7 +134,7 @@ impl ::std::ops::BitAndAssign for Dummy__bindgen_ty_1 { } #[repr(C)] #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] -pub struct Dummy__bindgen_ty_1(pub ::std::os::raw::c_uint); +pub struct Dummy__bindgen_ty_1(pub u32); #[test] fn bindgen_test_layout_Dummy() { assert_eq!( diff --git a/tests/expectations/tests/constify-all-enums.rs b/tests/expectations/tests/constify-all-enums.rs index 1d518b54aa..b2e1e974ea 100644 --- a/tests/expectations/tests/constify-all-enums.rs +++ b/tests/expectations/tests/constify-all-enums.rs @@ -1,13 +1,11 @@ /* automatically generated by rust-bindgen */ - #![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)] - pub const foo_THIS: foo = 0; pub const foo_SHOULD_BE: foo = 1; pub const foo_A_CONSTANT: foo = 2; -pub type foo = ::std::os::raw::c_uint; +pub type foo = u32; #[repr(C)] #[derive(Debug, Copy, Clone)] pub struct bar { diff --git a/tests/expectations/tests/constify-module-enums-basic.rs b/tests/expectations/tests/constify-module-enums-basic.rs index f8f45f18f2..342e5ba525 100644 --- a/tests/expectations/tests/constify-module-enums-basic.rs +++ b/tests/expectations/tests/constify-module-enums-basic.rs @@ -1,11 +1,9 @@ /* automatically generated by rust-bindgen */ - #![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)] - pub mod foo { - pub type Type = ::std::os::raw::c_uint; + pub type Type = u32; pub const THIS: Type = 0; pub const SHOULD_BE: Type = 1; pub const A_CONSTANT: Type = 2; diff --git a/tests/expectations/tests/constify-module-enums-namespace.rs b/tests/expectations/tests/constify-module-enums-namespace.rs index a69044e23b..7f1c134d3c 100644 --- a/tests/expectations/tests/constify-module-enums-namespace.rs +++ b/tests/expectations/tests/constify-module-enums-namespace.rs @@ -1,9 +1,7 @@ /* automatically generated by rust-bindgen */ - #![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)] - #[allow(non_snake_case, non_camel_case_types, non_upper_case_globals)] pub mod root { #[allow(unused_imports)] @@ -15,7 +13,7 @@ pub mod root { #[allow(unused_imports)] use self::super::super::super::root; pub mod foo { - pub type Type = ::std::os::raw::c_uint; + pub type Type = u32; pub const THIS: Type = 0; pub const SHOULD_BE: Type = 1; pub const A_CONSTANT: Type = 2; diff --git a/tests/expectations/tests/constify-module-enums-shadow-name.rs b/tests/expectations/tests/constify-module-enums-shadow-name.rs index b7da1cc3fa..9642871745 100644 --- a/tests/expectations/tests/constify-module-enums-shadow-name.rs +++ b/tests/expectations/tests/constify-module-enums-shadow-name.rs @@ -1,11 +1,9 @@ /* automatically generated by rust-bindgen */ - #![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)] - pub mod foo { - pub type Type = ::std::os::raw::c_uint; + pub type Type = u32; pub const Type: Type = 0; pub const Type_: Type = 1; pub const Type1: Type = 2; diff --git a/tests/expectations/tests/constify-module-enums-simple-alias.rs b/tests/expectations/tests/constify-module-enums-simple-alias.rs index 89b3ead907..70c881b9f4 100644 --- a/tests/expectations/tests/constify-module-enums-simple-alias.rs +++ b/tests/expectations/tests/constify-module-enums-simple-alias.rs @@ -1,11 +1,9 @@ /* automatically generated by rust-bindgen */ - #![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)] - pub mod Foo { - pub type Type = ::std::os::raw::c_int; + pub type Type = i32; pub const Variant1: Type = 0; pub const Variant2: Type = 1; pub const Variant3: Type = 2; diff --git a/tests/expectations/tests/constify-module-enums-simple-nonamespace.rs b/tests/expectations/tests/constify-module-enums-simple-nonamespace.rs index b85edbae65..ee15b57be5 100644 --- a/tests/expectations/tests/constify-module-enums-simple-nonamespace.rs +++ b/tests/expectations/tests/constify-module-enums-simple-nonamespace.rs @@ -1,11 +1,9 @@ /* automatically generated by rust-bindgen */ - #![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)] - pub mod one_Foo { - pub type Type = ::std::os::raw::c_int; + pub type Type = i32; pub const Variant1: Type = 0; pub const Variant2: Type = 1; } diff --git a/tests/expectations/tests/constify-module-enums-types.rs b/tests/expectations/tests/constify-module-enums-types.rs index 8849cc3870..155839be7c 100644 --- a/tests/expectations/tests/constify-module-enums-types.rs +++ b/tests/expectations/tests/constify-module-enums-types.rs @@ -1,11 +1,9 @@ /* automatically generated by rust-bindgen */ - #![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)] - pub mod foo { - pub type Type = ::std::os::raw::c_uint; + pub type Type = u32; pub const THIS: Type = 0; pub const SHOULD_BE: Type = 1; pub const A_CONSTANT: Type = 2; @@ -13,20 +11,20 @@ pub mod foo { pub const AND_ALSO_THIS: Type = 42; } pub mod anon_enum { - pub type Type = ::std::os::raw::c_uint; + pub type Type = u32; pub const Variant1: Type = 0; pub const Variant2: Type = 1; pub const Variant3: Type = 2; } pub mod ns1_foo { - pub type Type = ::std::os::raw::c_uint; + pub type Type = u32; pub const THIS: Type = 0; pub const SHOULD_BE: Type = 1; pub const A_CONSTANT: Type = 2; pub const ALSO_THIS: Type = 42; } pub mod ns2_Foo { - pub type Type = ::std::os::raw::c_int; + pub type Type = i32; pub const Variant1: Type = 0; pub const Variant2: Type = 1; } @@ -202,7 +200,7 @@ impl Default for Baz { } } pub mod one_Foo { - pub type Type = ::std::os::raw::c_int; + pub type Type = i32; pub const Variant1: Type = 0; pub const Variant2: Type = 1; } diff --git a/tests/expectations/tests/empty-enum.rs b/tests/expectations/tests/empty-enum.rs index 99ffc27ede..473a508ad4 100644 --- a/tests/expectations/tests/empty-enum.rs +++ b/tests/expectations/tests/empty-enum.rs @@ -2,30 +2,30 @@ #![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)] -pub type EmptyConstified = ::std::os::raw::c_uint; +pub type EmptyConstified = u32; #[repr(u32)] #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] pub enum EmptyRustified { __bindgen_cannot_repr_c_on_empty_enum = 0, } pub mod EmptyModule { - pub type Type = ::std::os::raw::c_uint; + pub type Type = u32; } #[repr(i8)] #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] pub enum EmptyClassRustified { __bindgen_cannot_repr_c_on_empty_enum = 0, } -pub type EmptyClassConstified = ::std::os::raw::c_char; +pub type EmptyClassConstified = i8; pub mod EmptyClassModule { - pub type Type = ::std::os::raw::c_char; + pub type Type = i8; } #[repr(i8)] #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] pub enum ForwardClassRustified { __bindgen_cannot_repr_c_on_empty_enum = 0, } -pub type ForwardClassConstified = ::std::os::raw::c_char; +pub type ForwardClassConstified = i8; pub mod ForwardClassModule { - pub type Type = ::std::os::raw::c_char; + pub type Type = i8; } diff --git a/tests/expectations/tests/enum_explicit_type.rs b/tests/expectations/tests/enum_explicit_type.rs index 17f135b3af..a5ec4a57b7 100644 --- a/tests/expectations/tests/enum_explicit_type.rs +++ b/tests/expectations/tests/enum_explicit_type.rs @@ -35,3 +35,8 @@ pub enum MuchLongLong { pub enum MuchULongLong { MuchHigh = 4294967296, } +#[repr(u8)] +#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] +pub enum BoolEnumsAreFun { + Value = 1, +} diff --git a/tests/expectations/tests/enum_explicit_type_constants.rs b/tests/expectations/tests/enum_explicit_type_constants.rs new file mode 100644 index 0000000000..cd1a922903 --- /dev/null +++ b/tests/expectations/tests/enum_explicit_type_constants.rs @@ -0,0 +1,21 @@ +/* automatically generated by rust-bindgen */ + +#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)] + +pub const Foo_Bar: Foo = 0; +pub const Foo_Qux: Foo = 1; +pub type Foo = u8; +pub const Neg_MinusOne: Neg = -1; +pub const Neg_One: Neg = 1; +pub type Neg = i8; +pub const Bigger_Much: Bigger = 255; +pub const Bigger_Larger: Bigger = 256; +pub type Bigger = u16; +pub const MuchLong_MuchLow: MuchLong = -4294967296; +pub type MuchLong = i64; +pub const MuchLongLong_I64_MIN: MuchLongLong = -9223372036854775808; +pub type MuchLongLong = i64; +pub const MuchULongLong_MuchHigh: MuchULongLong = 4294967296; +pub type MuchULongLong = u64; +pub const BoolEnumsAreFun_Value: BoolEnumsAreFun = 1; +pub type BoolEnumsAreFun = u8; diff --git a/tests/expectations/tests/prepend_enum_name.rs b/tests/expectations/tests/prepend_enum_name.rs index c409e54a2a..96c7b617ae 100644 --- a/tests/expectations/tests/prepend_enum_name.rs +++ b/tests/expectations/tests/prepend_enum_name.rs @@ -1,9 +1,7 @@ /* automatically generated by rust-bindgen */ - #![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)] - pub const FOO_BAR: foo = 0; pub const FOO_BAZ: foo = 1; -pub type foo = ::std::os::raw::c_uint; +pub type foo = u32; diff --git a/tests/headers/enum_explicit_type.hpp b/tests/headers/enum_explicit_type.hpp index e611de74bf..3cb931386f 100644 --- a/tests/headers/enum_explicit_type.hpp +++ b/tests/headers/enum_explicit_type.hpp @@ -26,3 +26,7 @@ enum MuchLongLong: long long { enum MuchULongLong: unsigned long long { MuchHigh = 4294967296, }; + +enum BoolEnumsAreFun: bool { + Value = true, +}; diff --git a/tests/headers/enum_explicit_type_constants.hpp b/tests/headers/enum_explicit_type_constants.hpp new file mode 100644 index 0000000000..7deab3699a --- /dev/null +++ b/tests/headers/enum_explicit_type_constants.hpp @@ -0,0 +1,5 @@ +// bindgen-flags: -- -std=c++11 +// +// This test is much like enum_explicit_type, but without --rustified-enum. + +#include "enum_explicit_type.hpp"