Skip to content

Commit

Permalink
Use a whitelist of recognized integer reprs
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Oct 30, 2023
1 parent b466791 commit 748970b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 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
6 changes: 2 additions & 4 deletions tests/ui/repr_c.stderr
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
error[E0412]: cannot find type `C` in this scope
error: unsupported repr for serde_repr enum
--> tests/ui/repr_c.rs:4:8
|
3 | #[derive(Serialize_repr)]
| -------------- similarly named type parameter `S` defined here
4 | #[repr(C)]
| ^ help: a type parameter with a similar name exists: `S`
| ^

0 comments on commit 748970b

Please sign in to comment.