Skip to content

Commit 561fe5e

Browse files
committed
Emit a proper error if bitflags enum is generic
This probably hasn't been hit by anyone but I noticed it in the AST, so...
1 parent f3bb174 commit 561fe5e

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

enumflags_derive/src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,11 @@ fn gen_enumflags(ast: &mut DeriveInput, default: Vec<Ident>) -> Result<TokenStre
258258
}
259259
};
260260

261+
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"));
264+
}
265+
261266
let repr = extract_repr(&ast.attrs)?
262267
.ok_or_else(|| syn::Error::new_spanned(ident,
263268
"repr attribute missing. Add #[repr(u64)] or a similar attribute to specify the size of the bitfield."))?;

test_suite/ui/with_generics.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#[enumflags2::bitflags]
2+
#[repr(u8)]
3+
enum Foo<A> {
4+
Bar,
5+
}
6+
7+
fn main() {}

test_suite/ui/with_generics.stderr

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error: bitflags cannot be generic
2+
--> ui/with_generics.rs:3:9
3+
|
4+
3 | enum Foo<A> {
5+
| ^^^
6+
7+
error[E0392]: type parameter `A` is never used
8+
--> ui/with_generics.rs:3:10
9+
|
10+
3 | enum Foo<A> {
11+
| ^ unused type parameter
12+
|
13+
= help: consider removing `A`, referring to it in a field, or using a marker such as `PhantomData`
14+
= help: if you intended `A` to be a const parameter, use `const A: /* Type */` instead

0 commit comments

Comments
 (0)