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.
This commit is contained in:
Alan Wu
2022-04-13 18:55:49 -04:00
committed by Emilio Cobos Álvarez
parent 0955c5ded7
commit b05ab16208
5 changed files with 11 additions and 15 deletions
+2 -6
View File
@@ -2625,7 +2625,6 @@ enum EnumBuilder<'a> {
is_bitfield: bool,
},
Consts {
repr: proc_macro2::TokenStream,
variants: Vec<proc_macro2::TokenStream>,
codegen_depth: usize,
},
@@ -2696,7 +2695,6 @@ impl<'a> EnumBuilder<'a> {
});
EnumBuilder::Consts {
repr,
variants,
codegen_depth: enum_codegen_depth,
}
@@ -2800,7 +2798,7 @@ impl<'a> EnumBuilder<'a> {
self
}
EnumBuilder::Consts { ref repr, .. } => {
EnumBuilder::Consts { .. } => {
let constant_name = match mangling_prefix {
Some(prefix) => {
Cow::Owned(format!("{}_{}", prefix, variant_name))
@@ -2808,12 +2806,10 @@ impl<'a> EnumBuilder<'a> {
None => variant_name,
};
let ty = if is_ty_named { &rust_ty } else { repr };
let ident = ctx.rust_ident(constant_name);
result.push(quote! {
#doc
pub const #ident : #ty = #expr ;
pub const #ident : #rust_ty = #expr ;
});
self
@@ -10,8 +10,8 @@
pub struct foo {
pub member: foo__bindgen_ty_1,
}
pub const foo_FOO_A: ::std::os::raw::c_uint = 0;
pub const foo_FOO_B: ::std::os::raw::c_uint = 1;
pub const foo_FOO_A: foo__bindgen_ty_1 = 0;
pub const foo_FOO_B: foo__bindgen_ty_1 = 1;
pub type foo__bindgen_ty_1 = ::std::os::raw::c_uint;
#[test]
fn bindgen_test_layout_foo() {
@@ -60,4 +60,4 @@ pub type NoDebug = ::std::os::raw::c_uint;
pub const Debug_Debug1: Debug = 0;
pub const Debug_Debug2: Debug = 1;
/// <div rustbindgen derive="Debug"></div>
pub type Debug = ::std::os::raw::c_uint;
pub type Debug = ::std::os::raw::c_uint;
+3 -3
View File
@@ -10,8 +10,8 @@
pub struct foo {
pub member: foo__bindgen_ty_1,
}
pub const foo_FOO_A: ::std::os::raw::c_uint = 0;
pub const foo_FOO_B: ::std::os::raw::c_uint = 1;
pub const foo_FOO_A: foo__bindgen_ty_1 = 0;
pub const foo_FOO_B: foo__bindgen_ty_1 = 1;
pub type foo__bindgen_ty_1 = ::std::os::raw::c_uint;
#[test]
fn bindgen_test_layout_foo() {
@@ -58,4 +58,4 @@ pub type NoDebug = ::std::os::raw::c_uint;
pub const Debug_Debug1: Debug = 0;
pub const Debug_Debug2: Debug = 1;
/// <div rustbindgen derive="Debug"></div>
pub type Debug = ::std::os::raw::c_uint;
pub type Debug = ::std::os::raw::c_uint;
@@ -26,6 +26,6 @@ pub type BoolEnumsAreFun = bool;
pub type MyType = bool;
pub const BoolEnumsAreFun2_Value2: BoolEnumsAreFun2 = true;
pub type BoolEnumsAreFun2 = MyType;
pub const AnonymousVariantOne: ::std::os::raw::c_uchar = 0;
pub const AnonymousVariantTwo: ::std::os::raw::c_uchar = 1;
pub const AnonymousVariantOne: _bindgen_ty_1 = 0;
pub const AnonymousVariantTwo: _bindgen_ty_1 = 1;
pub type _bindgen_ty_1 = ::std::os::raw::c_uchar;
@@ -5,5 +5,5 @@
non_upper_case_globals
)]
pub const RENAMED_MyVal: ::std::os::raw::c_uint = 0;
pub const RENAMED_MyVal: _bindgen_ty_1 = 0;
pub type _bindgen_ty_1 = ::std::os::raw::c_uint;