Skip to content

Commit 841fd4c

Browse files
XrXremilio
authored andcommitted
Use common type alias for anonymous enums in consts mode
Previously, anonymous enums generated a type alias but did not use it. For example the following: ```C enum { ZERO, ONE = 4999, }; ``` Generated this: ```Rust /* automatically generated by rust-bindgen 0.59.2 */ pub const ZERO: ::std::os::raw::c_uint = 0; pub const ONE: ::std::os::raw::c_uint = 4999; pub type _bindgen_ty_1 = ::std::os::raw::c_uint; ``` For use cases where humans look at bindgen's Rust output this was a little strange since it's a deviation from how the Rust output for named enums is organized, where all constants share the same type using the type alias. The unused type alias also triggered the dead_code lint. Change to use the generated type alias.
1 parent c27578f commit 841fd4c

File tree

5 files changed

+11
-15
lines changed

5 files changed

+11
-15
lines changed

src/codegen/mod.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -2625,7 +2625,6 @@ enum EnumBuilder<'a> {
26252625
is_bitfield: bool,
26262626
},
26272627
Consts {
2628-
repr: proc_macro2::TokenStream,
26292628
variants: Vec<proc_macro2::TokenStream>,
26302629
codegen_depth: usize,
26312630
},
@@ -2696,7 +2695,6 @@ impl<'a> EnumBuilder<'a> {
26962695
});
26972696

26982697
EnumBuilder::Consts {
2699-
repr,
27002698
variants,
27012699
codegen_depth: enum_codegen_depth,
27022700
}
@@ -2800,20 +2798,18 @@ impl<'a> EnumBuilder<'a> {
28002798
self
28012799
}
28022800

2803-
EnumBuilder::Consts { ref repr, .. } => {
2801+
EnumBuilder::Consts { .. } => {
28042802
let constant_name = match mangling_prefix {
28052803
Some(prefix) => {
28062804
Cow::Owned(format!("{}_{}", prefix, variant_name))
28072805
}
28082806
None => variant_name,
28092807
};
28102808

2811-
let ty = if is_ty_named { &rust_ty } else { repr };
2812-
28132809
let ident = ctx.rust_ident(constant_name);
28142810
result.push(quote! {
28152811
#doc
2816-
pub const #ident : #ty = #expr ;
2812+
pub const #ident : #rust_ty = #expr ;
28172813
});
28182814

28192815
self

tests/expectations/tests/enum-default-consts.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
pub struct foo {
1111
pub member: foo__bindgen_ty_1,
1212
}
13-
pub const foo_FOO_A: ::std::os::raw::c_uint = 0;
14-
pub const foo_FOO_B: ::std::os::raw::c_uint = 1;
13+
pub const foo_FOO_A: foo__bindgen_ty_1 = 0;
14+
pub const foo_FOO_B: foo__bindgen_ty_1 = 1;
1515
pub type foo__bindgen_ty_1 = ::std::os::raw::c_uint;
1616
#[test]
1717
fn bindgen_test_layout_foo() {
@@ -60,4 +60,4 @@ pub type NoDebug = ::std::os::raw::c_uint;
6060
pub const Debug_Debug1: Debug = 0;
6161
pub const Debug_Debug2: Debug = 1;
6262
/// <div rustbindgen derive="Debug"></div>
63-
pub type Debug = ::std::os::raw::c_uint;
63+
pub type Debug = ::std::os::raw::c_uint;

tests/expectations/tests/enum.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
pub struct foo {
1111
pub member: foo__bindgen_ty_1,
1212
}
13-
pub const foo_FOO_A: ::std::os::raw::c_uint = 0;
14-
pub const foo_FOO_B: ::std::os::raw::c_uint = 1;
13+
pub const foo_FOO_A: foo__bindgen_ty_1 = 0;
14+
pub const foo_FOO_B: foo__bindgen_ty_1 = 1;
1515
pub type foo__bindgen_ty_1 = ::std::os::raw::c_uint;
1616
#[test]
1717
fn bindgen_test_layout_foo() {
@@ -58,4 +58,4 @@ pub type NoDebug = ::std::os::raw::c_uint;
5858
pub const Debug_Debug1: Debug = 0;
5959
pub const Debug_Debug2: Debug = 1;
6060
/// <div rustbindgen derive="Debug"></div>
61-
pub type Debug = ::std::os::raw::c_uint;
61+
pub type Debug = ::std::os::raw::c_uint;

tests/expectations/tests/enum_explicit_type_constants.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,6 @@ pub type BoolEnumsAreFun = bool;
2626
pub type MyType = bool;
2727
pub const BoolEnumsAreFun2_Value2: BoolEnumsAreFun2 = true;
2828
pub type BoolEnumsAreFun2 = MyType;
29-
pub const AnonymousVariantOne: ::std::os::raw::c_uchar = 0;
30-
pub const AnonymousVariantTwo: ::std::os::raw::c_uchar = 1;
29+
pub const AnonymousVariantOne: _bindgen_ty_1 = 0;
30+
pub const AnonymousVariantTwo: _bindgen_ty_1 = 1;
3131
pub type _bindgen_ty_1 = ::std::os::raw::c_uchar;

tests/expectations/tests/parsecb-anonymous-enum-variant-rename.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55
non_upper_case_globals
66
)]
77

8-
pub const RENAMED_MyVal: ::std::os::raw::c_uint = 0;
8+
pub const RENAMED_MyVal: _bindgen_ty_1 = 0;
99
pub type _bindgen_ty_1 = ::std::os::raw::c_uint;

0 commit comments

Comments
 (0)