Skip to content

Commit 22041e1

Browse files
author
bors-servo
authored
Auto merge of #1272 - tmfink:issue-1198, r=emilio
Declare precedence on enum types Fixes issue #1198 where an enum matches the pattern for multiple enum types, such as constified module enum AND rustified enum. Documents precedence in `Builder` doc comment.
2 parents 9a0edb8 + 8c9b3ab commit 22041e1

6 files changed

+82
-2
lines changed

src/codegen/mod.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -2477,12 +2477,14 @@ impl CodeGenerator for Enum {
24772477
}
24782478
};
24792479

2480+
// ModuleConsts has higher precedence before Rust in order to avoid problems with
2481+
// overlapping match patterns
24802482
let variation = if self.is_bitfield(ctx, item) {
24812483
EnumVariation::Bitfield
2482-
} else if self.is_rustified_enum(ctx, item) {
2483-
EnumVariation::Rust
24842484
} else if self.is_constified_enum_module(ctx, item) {
24852485
EnumVariation::ModuleConsts
2486+
} else if self.is_rustified_enum(ctx, item) {
2487+
EnumVariation::Rust
24862488
} else {
24872489
// We generate consts by default
24882490
EnumVariation::Consts

src/lib.rs

+17
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,23 @@ impl Default for CodegenConfig {
155155
/// // Write the generated bindings to an output file.
156156
/// bindings.write_to_file("path/to/output.rs")?;
157157
/// ```
158+
///
159+
/// # Enums
160+
///
161+
/// Bindgen can map C/C++ enums into Rust in different ways. The way bindgen maps enums depends on
162+
/// the pattern passed to several methods:
163+
///
164+
/// 1. [`bitfield_enum()`](#method.bitfield_enum)
165+
/// 2. [`constified_enum_module()`](#method.constified_enum_module)
166+
/// 3. [`rustified_enum()`](#method.rustified_enum)
167+
///
168+
/// For each C enum, bindgen tries to match the pattern in the following order:
169+
///
170+
/// 1. Bitfield enum
171+
/// 2. Constified enum module
172+
/// 3. Rustified enum
173+
///
174+
/// If none of the above patterns match, then bindgen will generate a set of Rust constants.
158175
#[derive(Debug, Default)]
159176
pub struct Builder {
160177
options: BindgenOptions,
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,18 @@
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_alias: MyDupeEnum = MyDupeEnum::A;
6+
#[repr(u32)]
7+
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
8+
pub enum MyDupeEnum {
9+
A = 0,
10+
B = 1,
11+
}
12+
pub const MyOtherDupeEnum_C_alias: MyOtherDupeEnum = MyOtherDupeEnum::C;
13+
#[repr(u32)]
14+
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
15+
pub enum MyOtherDupeEnum {
16+
C = 0,
17+
D = 1,
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// bindgen-flags: --rustified-enum '.*' --constified-enum-module '.*'
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+
};
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// bindgen-flags: --rustified-enum '.*'
2+
3+
4+
typedef enum MyDupeEnum {
5+
A = 0,
6+
A_alias = 0,
7+
B,
8+
} MyDupeEnum;
9+
10+
enum MyOtherDupeEnum {
11+
C = 0,
12+
C_alias = 0,
13+
D,
14+
};

0 commit comments

Comments
 (0)