Skip to content

Commit 94f3f6e

Browse files
author
bors-servo
authored
Auto merge of rust-lang#1277 - tmfink:issue-1198-bitfield-enum, r=emilio
Handle bitfield enum pattern aliasing The previous fix for issue rust-lang#1198 was incomplete.
2 parents 22041e1 + c0c1dca commit 94f3f6e

6 files changed

+117
-7
lines changed

src/codegen/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -2479,10 +2479,10 @@ impl CodeGenerator for Enum {
24792479

24802480
// ModuleConsts has higher precedence before Rust in order to avoid problems with
24812481
// overlapping match patterns
2482-
let variation = if self.is_bitfield(ctx, item) {
2483-
EnumVariation::Bitfield
2484-
} else if self.is_constified_enum_module(ctx, item) {
2482+
let variation = if self.is_constified_enum_module(ctx, item) {
24852483
EnumVariation::ModuleConsts
2484+
} else if self.is_bitfield(ctx, item) {
2485+
EnumVariation::Bitfield
24862486
} else if self.is_rustified_enum(ctx, item) {
24872487
EnumVariation::Rust
24882488
} else {

src/lib.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -161,14 +161,14 @@ impl Default for CodegenConfig {
161161
/// Bindgen can map C/C++ enums into Rust in different ways. The way bindgen maps enums depends on
162162
/// the pattern passed to several methods:
163163
///
164-
/// 1. [`bitfield_enum()`](#method.bitfield_enum)
165-
/// 2. [`constified_enum_module()`](#method.constified_enum_module)
164+
/// 1. [`constified_enum_module()`](#method.constified_enum_module)
165+
/// 2. [`bitfield_enum()`](#method.bitfield_enum)
166166
/// 3. [`rustified_enum()`](#method.rustified_enum)
167167
///
168168
/// For each C enum, bindgen tries to match the pattern in the following order:
169169
///
170-
/// 1. Bitfield enum
171-
/// 2. Constified enum module
170+
/// 1. Constified enum module
171+
/// 2. Bitfield enum
172172
/// 3. Rustified enum
173173
///
174174
/// If none of the above patterns match, then bindgen will generate a set of Rust constants.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/* automatically generated by rust-bindgen */
2+
3+
#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]
4+
5+
pub const MyDupeEnum_A: MyDupeEnum = MyDupeEnum(0);
6+
pub const MyDupeEnum_A_alias: MyDupeEnum = MyDupeEnum(0);
7+
pub const MyDupeEnum_B: MyDupeEnum = MyDupeEnum(1);
8+
impl ::std::ops::BitOr<MyDupeEnum> for MyDupeEnum {
9+
type Output = Self;
10+
#[inline]
11+
fn bitor(self, other: Self) -> Self {
12+
MyDupeEnum(self.0 | other.0)
13+
}
14+
}
15+
impl ::std::ops::BitOrAssign for MyDupeEnum {
16+
#[inline]
17+
fn bitor_assign(&mut self, rhs: MyDupeEnum) {
18+
self.0 |= rhs.0;
19+
}
20+
}
21+
impl ::std::ops::BitAnd<MyDupeEnum> for MyDupeEnum {
22+
type Output = Self;
23+
#[inline]
24+
fn bitand(self, other: Self) -> Self {
25+
MyDupeEnum(self.0 & other.0)
26+
}
27+
}
28+
impl ::std::ops::BitAndAssign for MyDupeEnum {
29+
#[inline]
30+
fn bitand_assign(&mut self, rhs: MyDupeEnum) {
31+
self.0 &= rhs.0;
32+
}
33+
}
34+
#[repr(C)]
35+
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
36+
pub struct MyDupeEnum(pub u32);
37+
pub const MyOtherDupeEnum_C: MyOtherDupeEnum = MyOtherDupeEnum(0);
38+
pub const MyOtherDupeEnum_C_alias: MyOtherDupeEnum = MyOtherDupeEnum(0);
39+
pub const MyOtherDupeEnum_D: MyOtherDupeEnum = MyOtherDupeEnum(1);
40+
impl ::std::ops::BitOr<MyOtherDupeEnum> for MyOtherDupeEnum {
41+
type Output = Self;
42+
#[inline]
43+
fn bitor(self, other: Self) -> Self {
44+
MyOtherDupeEnum(self.0 | other.0)
45+
}
46+
}
47+
impl ::std::ops::BitOrAssign for MyOtherDupeEnum {
48+
#[inline]
49+
fn bitor_assign(&mut self, rhs: MyOtherDupeEnum) {
50+
self.0 |= rhs.0;
51+
}
52+
}
53+
impl ::std::ops::BitAnd<MyOtherDupeEnum> for MyOtherDupeEnum {
54+
type Output = Self;
55+
#[inline]
56+
fn bitand(self, other: Self) -> Self {
57+
MyOtherDupeEnum(self.0 & other.0)
58+
}
59+
}
60+
impl ::std::ops::BitAndAssign for MyOtherDupeEnum {
61+
#[inline]
62+
fn bitand_assign(&mut self, rhs: MyOtherDupeEnum) {
63+
self.0 &= rhs.0;
64+
}
65+
}
66+
#[repr(C)]
67+
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
68+
pub struct MyOtherDupeEnum(pub u32);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/* automatically generated by rust-bindgen */
2+
3+
#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]
4+
5+
pub mod MyDupeEnum {
6+
pub type Type = u32;
7+
pub const A: Type = 0;
8+
pub const A_alias: Type = 0;
9+
pub const B: Type = 1;
10+
}
11+
pub mod MyOtherDupeEnum {
12+
pub type Type = u32;
13+
pub const C: Type = 0;
14+
pub const C_alias: Type = 0;
15+
pub const D: Type = 1;
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// bindgen-flags: --rustified-enum '.*' --bitfield-enum '.*'
2+
3+
typedef enum MyDupeEnum {
4+
A = 0,
5+
A_alias = 0,
6+
B,
7+
} MyDupeEnum;
8+
9+
enum MyOtherDupeEnum {
10+
C = 0,
11+
C_alias = 0,
12+
D,
13+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// bindgen-flags: --rustified-enum '.*' --constified-enum-module '.*' --bitfield-enum '.*'
2+
3+
typedef enum MyDupeEnum {
4+
A = 0,
5+
A_alias = 0,
6+
B,
7+
} MyDupeEnum;
8+
9+
enum MyOtherDupeEnum {
10+
C = 0,
11+
C_alias = 0,
12+
D,
13+
};

0 commit comments

Comments
 (0)