Skip to content

Commit

Permalink
Emit a proper error if bitflags enum is generic
Browse files Browse the repository at this point in the history
This probably hasn't been hit by anyone but I noticed it in the AST,
so...
  • Loading branch information
meithecatte committed Jun 5, 2024
1 parent f3bb174 commit 561fe5e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
5 changes: 5 additions & 0 deletions enumflags_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,11 @@ fn gen_enumflags(ast: &mut DeriveInput, default: Vec<Ident>) -> Result<TokenStre
}
};

if ast.generics.lt_token.is_some() || ast.generics.where_clause.is_some() {
return Err(syn::Error::new_spanned(&ast.generics,
"bitflags cannot be generic"));
}

let repr = extract_repr(&ast.attrs)?
.ok_or_else(|| syn::Error::new_spanned(ident,
"repr attribute missing. Add #[repr(u64)] or a similar attribute to specify the size of the bitfield."))?;
Expand Down
7 changes: 7 additions & 0 deletions test_suite/ui/with_generics.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#[enumflags2::bitflags]
#[repr(u8)]
enum Foo<A> {
Bar,
}

fn main() {}
14 changes: 14 additions & 0 deletions test_suite/ui/with_generics.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
error: bitflags cannot be generic
--> ui/with_generics.rs:3:9
|
3 | enum Foo<A> {
| ^^^

error[E0392]: type parameter `A` is never used
--> ui/with_generics.rs:3:10
|
3 | enum Foo<A> {
| ^ unused type parameter
|
= help: consider removing `A`, referring to it in a field, or using a marker such as `PhantomData`
= help: if you intended `A` to be a const parameter, use `const A: /* Type */` instead

0 comments on commit 561fe5e

Please sign in to comment.