diff --git a/CHANGELOG.md b/CHANGELOG.md index 1c5fca5ad5..4ab8200986 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -144,6 +144,17 @@ Released YYYY/MM/DD -------------------------------------------------------------------------------- +# 0.55.1 + +Released 2020/08/24. + +## Fixed + + * Fixed a regression where anonymous enums referenced by members or such won't + generate valid Rust code. (#1882). + +-------------------------------------------------------------------------------- + # 0.55.0 Released 2020/08/23. diff --git a/Cargo.lock b/Cargo.lock index 25e878c0a0..b4a5544398 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -27,7 +27,7 @@ dependencies = [ [[package]] name = "bindgen" -version = "0.55.0" +version = "0.55.1" dependencies = [ "bitflags 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "cexpr 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/Cargo.toml b/Cargo.toml index 41616e78be..7ab43a21dc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,7 +14,7 @@ readme = "README.md" repository = "https://github.com/rust-lang/rust-bindgen" documentation = "https://docs.rs/bindgen" homepage = "https://rust-lang.github.io/rust-bindgen/" -version = "0.55.0" +version = "0.55.1" edition = "2018" build = "build.rs" diff --git a/src/codegen/mod.rs b/src/codegen/mod.rs index 60e0e5929f..1ad413ffe2 100644 --- a/src/codegen/mod.rs +++ b/src/codegen/mod.rs @@ -2478,7 +2478,6 @@ impl<'a> EnumBuilder<'a> { repr: proc_macro2::TokenStream, enum_variation: EnumVariation, enum_codegen_depth: usize, - is_ty_named: bool, ) -> Self { let ident = Ident::new(name, Span::call_site()); @@ -2507,12 +2506,10 @@ impl<'a> EnumBuilder<'a> { EnumVariation::Consts => { let mut variants = Vec::new(); - if is_ty_named { - variants.push(quote! { - #( #attrs )* - pub type #ident = #repr; - }); - } + variants.push(quote! { + #( #attrs )* + pub type #ident = #repr; + }); EnumBuilder::Consts { repr, @@ -2901,7 +2898,6 @@ impl CodeGenerator for Enum { repr, variation, item.codegen_depth(ctx), - enum_ty.name().is_some(), ); // A map where we keep a value -> variant relation. diff --git a/tests/expectations/tests/enum-default-bitfield.rs b/tests/expectations/tests/enum-default-bitfield.rs index ebf5caa462..86a0449983 100644 --- a/tests/expectations/tests/enum-default-bitfield.rs +++ b/tests/expectations/tests/enum-default-bitfield.rs @@ -5,6 +5,70 @@ non_upper_case_globals )] +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct foo { + pub member: foo__bindgen_ty_1, +} +pub const foo_FOO_A: foo__bindgen_ty_1 = foo__bindgen_ty_1(0); +pub const foo_FOO_B: foo__bindgen_ty_1 = foo__bindgen_ty_1(1); +impl ::std::ops::BitOr for foo__bindgen_ty_1 { + type Output = Self; + #[inline] + fn bitor(self, other: Self) -> Self { + foo__bindgen_ty_1(self.0 | other.0) + } +} +impl ::std::ops::BitOrAssign for foo__bindgen_ty_1 { + #[inline] + fn bitor_assign(&mut self, rhs: foo__bindgen_ty_1) { + self.0 |= rhs.0; + } +} +impl ::std::ops::BitAnd for foo__bindgen_ty_1 { + type Output = Self; + #[inline] + fn bitand(self, other: Self) -> Self { + foo__bindgen_ty_1(self.0 & other.0) + } +} +impl ::std::ops::BitAndAssign for foo__bindgen_ty_1 { + #[inline] + fn bitand_assign(&mut self, rhs: foo__bindgen_ty_1) { + self.0 &= rhs.0; + } +} +#[repr(transparent)] +#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] +pub struct foo__bindgen_ty_1(pub ::std::os::raw::c_uint); +#[test] +fn bindgen_test_layout_foo() { + assert_eq!( + ::std::mem::size_of::(), + 4usize, + concat!("Size of: ", stringify!(foo)) + ); + assert_eq!( + ::std::mem::align_of::(), + 4usize, + concat!("Alignment of ", stringify!(foo)) + ); + assert_eq!( + unsafe { &(*(::std::ptr::null::())).member as *const _ as usize }, + 0usize, + concat!( + "Offset of field: ", + stringify!(foo), + "::", + stringify!(member) + ) + ); +} +impl Default for foo { + fn default() -> Self { + unsafe { ::std::mem::zeroed() } + } +} impl Foo { pub const Bar: Foo = Foo(0); } diff --git a/tests/expectations/tests/enum-default-consts.rs b/tests/expectations/tests/enum-default-consts.rs index ab49663063..1f086791b9 100644 --- a/tests/expectations/tests/enum-default-consts.rs +++ b/tests/expectations/tests/enum-default-consts.rs @@ -5,6 +5,42 @@ non_upper_case_globals )] +#[repr(C)] +#[derive(Debug, Copy, Clone)] +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 type foo__bindgen_ty_1 = ::std::os::raw::c_uint; +#[test] +fn bindgen_test_layout_foo() { + assert_eq!( + ::std::mem::size_of::(), + 4usize, + concat!("Size of: ", stringify!(foo)) + ); + assert_eq!( + ::std::mem::align_of::(), + 4usize, + concat!("Alignment of ", stringify!(foo)) + ); + assert_eq!( + unsafe { &(*(::std::ptr::null::())).member as *const _ as usize }, + 0usize, + concat!( + "Offset of field: ", + stringify!(foo), + "::", + stringify!(member) + ) + ); +} +impl Default for foo { + fn default() -> Self { + unsafe { ::std::mem::zeroed() } + } +} pub const Foo_Bar: Foo = 0; pub const Foo_Qux: Foo = 1; pub type Foo = ::std::os::raw::c_uint; diff --git a/tests/expectations/tests/enum-default-module.rs b/tests/expectations/tests/enum-default-module.rs index a81e6ad023..7eb63a95db 100644 --- a/tests/expectations/tests/enum-default-module.rs +++ b/tests/expectations/tests/enum-default-module.rs @@ -5,6 +5,44 @@ non_upper_case_globals )] +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct foo { + pub member: foo__bindgen_ty_1::Type, +} +pub mod foo__bindgen_ty_1 { + pub type Type = ::std::os::raw::c_uint; + pub const FOO_A: Type = 0; + pub const FOO_B: Type = 1; +} +#[test] +fn bindgen_test_layout_foo() { + assert_eq!( + ::std::mem::size_of::(), + 4usize, + concat!("Size of: ", stringify!(foo)) + ); + assert_eq!( + ::std::mem::align_of::(), + 4usize, + concat!("Alignment of ", stringify!(foo)) + ); + assert_eq!( + unsafe { &(*(::std::ptr::null::())).member as *const _ as usize }, + 0usize, + concat!( + "Offset of field: ", + stringify!(foo), + "::", + stringify!(member) + ) + ); +} +impl Default for foo { + fn default() -> Self { + unsafe { ::std::mem::zeroed() } + } +} pub mod Foo { pub type Type = ::std::os::raw::c_uint; pub const Bar: Type = 0; diff --git a/tests/expectations/tests/enum-default-rust.rs b/tests/expectations/tests/enum-default-rust.rs index b497a5a3f2..788de139d8 100644 --- a/tests/expectations/tests/enum-default-rust.rs +++ b/tests/expectations/tests/enum-default-rust.rs @@ -5,6 +5,47 @@ non_upper_case_globals )] +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct foo { + pub member: foo__bindgen_ty_1, +} +pub const foo_FOO_A: foo__bindgen_ty_1 = foo__bindgen_ty_1::FOO_A; +pub const foo_FOO_B: foo__bindgen_ty_1 = foo__bindgen_ty_1::FOO_B; +#[repr(u32)] +#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] +pub enum foo__bindgen_ty_1 { + FOO_A = 0, + FOO_B = 1, +} +#[test] +fn bindgen_test_layout_foo() { + assert_eq!( + ::std::mem::size_of::(), + 4usize, + concat!("Size of: ", stringify!(foo)) + ); + assert_eq!( + ::std::mem::align_of::(), + 4usize, + concat!("Alignment of ", stringify!(foo)) + ); + assert_eq!( + unsafe { &(*(::std::ptr::null::())).member as *const _ as usize }, + 0usize, + concat!( + "Offset of field: ", + stringify!(foo), + "::", + stringify!(member) + ) + ); +} +impl Default for foo { + fn default() -> Self { + unsafe { ::std::mem::zeroed() } + } +} #[repr(u32)] #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] pub enum Foo { diff --git a/tests/expectations/tests/enum.rs b/tests/expectations/tests/enum.rs index 27a1f41bad..3a1bffcb40 100644 --- a/tests/expectations/tests/enum.rs +++ b/tests/expectations/tests/enum.rs @@ -5,15 +5,45 @@ non_upper_case_globals )] -#[repr(u32)] -#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] -pub enum Foo { - Bar = 0, - Qux = 1, +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct foo { + pub member: foo__bindgen_ty_1, } -#[repr(i32)] -#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] -pub enum Neg { - MinusOne = -1, - One = 1, +pub const foo_FOO_A: ::std::os::raw::c_uint = 0; +pub const foo_FOO_B: ::std::os::raw::c_uint = 1; +pub type foo__bindgen_ty_1 = ::std::os::raw::c_uint; +#[test] +fn bindgen_test_layout_foo() { + assert_eq!( + ::std::mem::size_of::(), + 4usize, + concat!("Size of: ", stringify!(foo)) + ); + assert_eq!( + ::std::mem::align_of::(), + 4usize, + concat!("Alignment of ", stringify!(foo)) + ); + assert_eq!( + unsafe { &(*(::std::ptr::null::())).member as *const _ as usize }, + 0usize, + concat!( + "Offset of field: ", + stringify!(foo), + "::", + stringify!(member) + ) + ); } +impl Default for foo { + fn default() -> Self { + unsafe { ::std::mem::zeroed() } + } +} +pub const Foo_Bar: Foo = 0; +pub const Foo_Qux: Foo = 1; +pub type Foo = ::std::os::raw::c_uint; +pub const Neg_MinusOne: Neg = -1; +pub const Neg_One: Neg = 1; +pub type Neg = ::std::os::raw::c_int; diff --git a/tests/expectations/tests/enum_explicit_type_constants.rs b/tests/expectations/tests/enum_explicit_type_constants.rs index 9406c7f2d6..030d64bedc 100644 --- a/tests/expectations/tests/enum_explicit_type_constants.rs +++ b/tests/expectations/tests/enum_explicit_type_constants.rs @@ -27,3 +27,4 @@ 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 type _bindgen_ty_1 = ::std::os::raw::c_uchar; diff --git a/tests/expectations/tests/issue-1025-unknown-enum-repr.rs b/tests/expectations/tests/issue-1025-unknown-enum-repr.rs index 1516c0a95e..c42e167fba 100644 --- a/tests/expectations/tests/issue-1025-unknown-enum-repr.rs +++ b/tests/expectations/tests/issue-1025-unknown-enum-repr.rs @@ -10,3 +10,4 @@ pub struct a { pub _address: u8, } +pub type a__bindgen_ty_1 = i32; diff --git a/tests/headers/enum-default-bitfield.h b/tests/headers/enum-default-bitfield.h index be2841329b..5f3cb95468 100644 --- a/tests/headers/enum-default-bitfield.h +++ b/tests/headers/enum-default-bitfield.h @@ -1,11 +1,3 @@ // bindgen-flags: --default-enum-style=bitfield --constified-enum-module=Neg -enum Foo { - Bar = 0, - Qux -}; - -enum Neg { - MinusOne = -1, - One = 1, -}; +#include "enum.h" diff --git a/tests/headers/enum-default-consts.h b/tests/headers/enum-default-consts.h index 448c3030a8..233d2718a4 100644 --- a/tests/headers/enum-default-consts.h +++ b/tests/headers/enum-default-consts.h @@ -1,11 +1,3 @@ // bindgen-flags: --default-enum-style=consts --constified-enum-module=Neg -enum Foo { - Bar = 0, - Qux -}; - -enum Neg { - MinusOne = -1, - One = 1, -}; +#include "enum.h" diff --git a/tests/headers/enum-default-module.h b/tests/headers/enum-default-module.h index a2f3d7adfd..8c2ec1bca4 100644 --- a/tests/headers/enum-default-module.h +++ b/tests/headers/enum-default-module.h @@ -1,11 +1,3 @@ // bindgen-flags: --default-enum-style=moduleconsts --constified-enum-module=Neg -enum Foo { - Bar = 0, - Qux -}; - -enum Neg { - MinusOne = -1, - One = 1, -}; +#include "enum.h" diff --git a/tests/headers/enum-default-rust.h b/tests/headers/enum-default-rust.h index 282c62f3ff..7fd2999fe0 100644 --- a/tests/headers/enum-default-rust.h +++ b/tests/headers/enum-default-rust.h @@ -1,11 +1,3 @@ // bindgen-flags: --default-enum-style=rust --constified-enum-module=Neg -enum Foo { - Bar = 0, - Qux -}; - -enum Neg { - MinusOne = -1, - One = 1, -}; +#include "enum.h" diff --git a/tests/headers/enum.h b/tests/headers/enum.h index f04c213e4f..901b805814 100644 --- a/tests/headers/enum.h +++ b/tests/headers/enum.h @@ -1,4 +1,12 @@ -// bindgen-flags: --rustified-enum ".*" +// A few tests for enum-related issues that should be tested with all the enum +// representations. + +struct foo { + enum { + FOO_A, + FOO_B, + } member; +}; enum Foo { Bar = 0,