Skip to content

Commit 7b99b25

Browse files
committed
Fix bug when enum matched multiple types
If an enum matched a pattern for rustified enum and constified module enum, then rust code would fail to compile with "ambiguous associated type" error. We fix the error by giving constified module enum "higher precedence". Fixes issue rust-lang#1198
1 parent f36f4e3 commit 7b99b25

File tree

5 files changed

+65
-2
lines changed

5 files changed

+65
-2
lines changed

src/codegen/mod.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -2461,12 +2461,14 @@ impl CodeGenerator for Enum {
24612461
}
24622462
};
24632463

2464+
// ModuleConsts has higher precedence before Rust in order to avoid problems with
2465+
// overlapping match patterns
24642466
let variation = if self.is_bitfield(ctx, item) {
24652467
EnumVariation::Bitfield
2466-
} else if self.is_rustified_enum(ctx, item) {
2467-
EnumVariation::Rust
24682468
} else if self.is_constified_enum_module(ctx, item) {
24692469
EnumVariation::ModuleConsts
2470+
} else if self.is_rustified_enum(ctx, item) {
2471+
EnumVariation::Rust
24702472
} else {
24712473
// We generate consts by default
24722474
EnumVariation::Consts
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)