Skip to content

Commit c68c341

Browse files
Use fully-qualified path to refer to the associated type
Prevent "ambiguous associated type" if one of the variants is named `Numeric`.
1 parent cc09d89 commit c68c341

File tree

2 files changed

+29
-12
lines changed

2 files changed

+29
-12
lines changed

enumflags_derive/src/lib.rs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use syn::{
99
parse::{Parse, ParseStream},
1010
parse_macro_input,
1111
spanned::Spanned,
12-
Expr, Ident, DeriveInput, Data, Token, Variant,
12+
Data, DeriveInput, Expr, Ident, Token, Variant,
1313
};
1414

1515
struct Flag<'a> {
@@ -249,18 +249,24 @@ fn gen_enumflags(ast: &mut DeriveInput, default: Vec<Ident>) -> Result<TokenStre
249249
let ast_variants = match &mut ast.data {
250250
Data::Enum(ref mut data) => &mut data.variants,
251251
Data::Struct(data) => {
252-
return Err(syn::Error::new_spanned(&data.struct_token,
253-
"expected enum for #[bitflags], found struct"));
252+
return Err(syn::Error::new_spanned(
253+
&data.struct_token,
254+
"expected enum for #[bitflags], found struct",
255+
));
254256
}
255257
Data::Union(data) => {
256-
return Err(syn::Error::new_spanned(&data.union_token,
257-
"expected enum for #[bitflags], found union"));
258+
return Err(syn::Error::new_spanned(
259+
&data.union_token,
260+
"expected enum for #[bitflags], found union",
261+
));
258262
}
259263
};
260264

261265
if ast.generics.lt_token.is_some() || ast.generics.where_clause.is_some() {
262-
return Err(syn::Error::new_spanned(&ast.generics,
263-
"bitflags cannot be generic"));
266+
return Err(syn::Error::new_spanned(
267+
&ast.generics,
268+
"bitflags cannot be generic",
269+
));
264270
}
265271

266272
let repr = extract_repr(&ast.attrs)?
@@ -334,18 +340,18 @@ fn gen_enumflags(ast: &mut DeriveInput, default: Vec<Ident>) -> Result<TokenStre
334340
unsafe impl ::enumflags2::_internal::RawBitFlags for #ident {
335341
type Numeric = #repr;
336342

337-
const EMPTY: Self::Numeric = 0;
343+
const EMPTY: <Self as ::enumflags2::_internal::RawBitFlags>::Numeric = 0;
338344

339-
const DEFAULT: Self::Numeric =
345+
const DEFAULT: <Self as ::enumflags2::_internal::RawBitFlags>::Numeric =
340346
0 #(| (Self::#default as #repr))*;
341347

342-
const ALL_BITS: Self::Numeric =
348+
const ALL_BITS: <Self as ::enumflags2::_internal::RawBitFlags>::Numeric =
343349
0 #(| (Self::#variant_names as #repr))*;
344350

345351
const BITFLAGS_TYPE_NAME : &'static str =
346352
concat!("BitFlags<", stringify!(#ident), ">");
347353

348-
fn bits(self) -> Self::Numeric {
354+
fn bits(self) -> <Self as ::enumflags2::_internal::RawBitFlags>::Numeric {
349355
self as #repr
350356
}
351357
}

test_suite/common.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ enum Default6 {
2929
D = 1 << 3,
3030
}
3131

32+
#[bitflags]
33+
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
34+
#[repr(u8)]
35+
enum AssociatedTypeNameConflict {
36+
Stringy = 1 << 0,
37+
Numeric = 1 << 1,
38+
}
39+
3240
#[test]
3341
fn test_ctors() {
3442
use enumflags2::BitFlags;
@@ -109,7 +117,10 @@ fn iterator() {
109117
];
110118

111119
for &(bitflag, expected) in tests {
112-
assert!(bitflag.iter().zip(expected.iter().copied()).all(|(a, b)| a == b));
120+
assert!(bitflag
121+
.iter()
122+
.zip(expected.iter().copied())
123+
.all(|(a, b)| a == b));
113124
// If cloned, the iterator will yield the same elements.
114125
let it = bitflag.iter();
115126
assert!(it.clone().zip(it).all(|(a, b)| a == b));

0 commit comments

Comments
 (0)