Skip to content

Commit

Permalink
Merge pull request #24 from dtolnay/parserepr
Browse files Browse the repository at this point in the history
Use a whitelist of recognized integer reprs
  • Loading branch information
dtolnay authored Oct 30, 2023
2 parents ed317c7 + 748970b commit 8a6b642
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,18 @@ impl Parse for Input {
let mut repr = None;
for attr in derive_input.attrs {
if attr.path().is_ident("repr") {
if let Meta::List(_) = &attr.meta {
let ty: Ident = attr.parse_args()?;
repr = Some(ty);
break;
if let Meta::List(meta) = &attr.meta {
meta.parse_nested_meta(|meta| {
const RECOGNIZED: &[&str] = &[
"u8", "u16", "u32", "u64", "u128", "usize", "i8", "i16", "i32", "i64",
"i128", "isize",
];
if RECOGNIZED.iter().any(|int| meta.path.is_ident(int)) {
repr = Some(meta.path.get_ident().unwrap().clone());
return Ok(());
}
Err(meta.error("unsupported repr for serde_repr enum"))
})?;
}
}
}
Expand Down
12 changes: 12 additions & 0 deletions tests/ui/repr_c.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use serde_repr::Serialize_repr;

#[derive(Serialize_repr)]
#[repr(C)]
enum SmallPrime {
Two = 2,
Three = 3,
Five = 5,
Seven = 7,
}

fn main() {}
5 changes: 5 additions & 0 deletions tests/ui/repr_c.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
error: unsupported repr for serde_repr enum
--> tests/ui/repr_c.rs:4:8
|
4 | #[repr(C)]
| ^

0 comments on commit 8a6b642

Please sign in to comment.