Skip to content

Commit

Permalink
codegen: Try to reasonably handle enum : bool.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
emilio committed Jan 22, 2018
1 parent 8fa15cc commit a79b404
Show file tree
Hide file tree
Showing 15 changed files with 61 additions and 47 deletions.
11 changes: 4 additions & 7 deletions src/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
10 changes: 4 additions & 6 deletions tests/expectations/tests/bitfield-enum-basic.rs
Original file line number Diff line number Diff line change
@@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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!(
Expand Down
4 changes: 1 addition & 3 deletions tests/expectations/tests/constify-all-enums.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
4 changes: 1 addition & 3 deletions tests/expectations/tests/constify-module-enums-basic.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
4 changes: 1 addition & 3 deletions tests/expectations/tests/constify-module-enums-namespace.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
Expand Down
12 changes: 5 additions & 7 deletions tests/expectations/tests/constify-module-enums-types.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,30 @@
/* 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;
pub const ALSO_THIS: Type = 42;
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;
}
Expand Down Expand Up @@ -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;
}
Expand Down
12 changes: 6 additions & 6 deletions tests/expectations/tests/empty-enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
5 changes: 5 additions & 0 deletions tests/expectations/tests/enum_explicit_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
21 changes: 21 additions & 0 deletions tests/expectations/tests/enum_explicit_type_constants.rs
Original file line number Diff line number Diff line change
@@ -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;
4 changes: 1 addition & 3 deletions tests/expectations/tests/prepend_enum_name.rs
Original file line number Diff line number Diff line change
@@ -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;
4 changes: 4 additions & 0 deletions tests/headers/enum_explicit_type.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,7 @@ enum MuchLongLong: long long {
enum MuchULongLong: unsigned long long {
MuchHigh = 4294967296,
};

enum BoolEnumsAreFun: bool {
Value = true,
};
5 changes: 5 additions & 0 deletions tests/headers/enum_explicit_type_constants.hpp
Original file line number Diff line number Diff line change
@@ -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"

0 comments on commit a79b404

Please sign in to comment.