Skip to content

Commit 4eaca74

Browse files
committed
Bitfield enums use #[repr(transparent)] on Rust 1.28+
Fixes rust-lang#1474.
1 parent 651605f commit 4eaca74

7 files changed

+133
-2
lines changed

src/codegen/mod.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -2573,7 +2573,11 @@ impl CodeGenerator for Enum {
25732573
if variation.is_rust() {
25742574
attrs.push(attributes::repr(repr_name));
25752575
} else if variation.is_bitfield() {
2576-
attrs.push(attributes::repr("C"));
2576+
if ctx.options().rust_features.repr_transparent {
2577+
attrs.push(attributes::repr("transparent"));
2578+
} else {
2579+
attrs.push(attributes::repr("C"));
2580+
}
25772581
}
25782582

25792583
if let Some(comment) = item.comment(ctx) {

src/features.rs

+6
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ macro_rules! rust_target_base {
100100
=> Stable_1_26 => 1.26;
101101
/// Rust stable 1.27
102102
=> Stable_1_27 => 1.27;
103+
/// Rust stable 1.28
104+
=> Stable_1_28 => 1.28;
103105
/// Nightly rust
104106
=> Nightly => nightly;
105107
);
@@ -184,6 +186,10 @@ rust_feature_def!(
184186
/// `must_use` attribute on functions ([PR](https://github.com./rust-lang/rust/pull/48925))
185187
=> must_use_function;
186188
}
189+
Stable_1_28 {
190+
/// repr(transparent) ([PR](https://github.com./rust-lang/rust/pull/51562))
191+
=> repr_transparent;
192+
}
187193
Nightly {
188194
/// `thiscall` calling convention ([Tracking issue](https://github.com./rust-lang/rust/issues/42202))
189195
=> thiscall_abi;

tests/expectations/tests/bitfield-enum-basic.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
/* automatically generated by rust-bindgen */
22

3-
#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]
3+
#![allow(
4+
dead_code,
5+
non_snake_case,
6+
non_camel_case_types,
7+
non_upper_case_globals
8+
)]
49

510
impl Foo {
611
pub const Bar: Foo = Foo(2);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/* automatically generated by rust-bindgen */
2+
3+
#![allow(
4+
dead_code,
5+
non_snake_case,
6+
non_camel_case_types,
7+
non_upper_case_globals
8+
)]
9+
10+
impl Foo {
11+
pub const Bar: Foo = Foo(2);
12+
}
13+
impl Foo {
14+
pub const Baz: Foo = Foo(4);
15+
}
16+
impl Foo {
17+
pub const Duplicated: Foo = Foo(4);
18+
}
19+
impl Foo {
20+
pub const Negative: Foo = Foo(-3);
21+
}
22+
impl ::std::ops::BitOr<Foo> for Foo {
23+
type Output = Self;
24+
#[inline]
25+
fn bitor(self, other: Self) -> Self {
26+
Foo(self.0 | other.0)
27+
}
28+
}
29+
impl ::std::ops::BitOrAssign for Foo {
30+
#[inline]
31+
fn bitor_assign(&mut self, rhs: Foo) {
32+
self.0 |= rhs.0;
33+
}
34+
}
35+
impl ::std::ops::BitAnd<Foo> for Foo {
36+
type Output = Self;
37+
#[inline]
38+
fn bitand(self, other: Self) -> Self {
39+
Foo(self.0 & other.0)
40+
}
41+
}
42+
impl ::std::ops::BitAndAssign for Foo {
43+
#[inline]
44+
fn bitand_assign(&mut self, rhs: Foo) {
45+
self.0 &= rhs.0;
46+
}
47+
}
48+
#[repr(C)]
49+
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
50+
pub struct Foo(pub i32);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/* automatically generated by rust-bindgen */
2+
3+
#![allow(
4+
dead_code,
5+
non_snake_case,
6+
non_camel_case_types,
7+
non_upper_case_globals
8+
)]
9+
10+
impl Foo {
11+
pub const Bar: Foo = Foo(2);
12+
}
13+
impl Foo {
14+
pub const Baz: Foo = Foo(4);
15+
}
16+
impl Foo {
17+
pub const Duplicated: Foo = Foo(4);
18+
}
19+
impl Foo {
20+
pub const Negative: Foo = Foo(-3);
21+
}
22+
impl ::std::ops::BitOr<Foo> for Foo {
23+
type Output = Self;
24+
#[inline]
25+
fn bitor(self, other: Self) -> Self {
26+
Foo(self.0 | other.0)
27+
}
28+
}
29+
impl ::std::ops::BitOrAssign for Foo {
30+
#[inline]
31+
fn bitor_assign(&mut self, rhs: Foo) {
32+
self.0 |= rhs.0;
33+
}
34+
}
35+
impl ::std::ops::BitAnd<Foo> for Foo {
36+
type Output = Self;
37+
#[inline]
38+
fn bitand(self, other: Self) -> Self {
39+
Foo(self.0 & other.0)
40+
}
41+
}
42+
impl ::std::ops::BitAndAssign for Foo {
43+
#[inline]
44+
fn bitand_assign(&mut self, rhs: Foo) {
45+
self.0 &= rhs.0;
46+
}
47+
}
48+
#[repr(transparent)]
49+
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
50+
pub struct Foo(pub i32);
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// bindgen-flags: --bitfield-enum "Foo" --rust-target 1.27 -- -std=c++11
2+
3+
enum Foo {
4+
Bar = 1 << 1,
5+
Baz = 1 << 2,
6+
Duplicated = 1 << 2,
7+
Negative = -3,
8+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// bindgen-flags: --bitfield-enum "Foo" --rust-target 1.28 -- -std=c++11
2+
3+
enum Foo {
4+
Bar = 1 << 1,
5+
Baz = 1 << 2,
6+
Duplicated = 1 << 2,
7+
Negative = -3,
8+
};

0 commit comments

Comments
 (0)