Skip to content

Commit

Permalink
Force muliple repr hint to be hard error
Browse files Browse the repository at this point in the history
The consideraton is this will be unsafe in our environmnt. In rust, it
is possible that users would allow that error, thinking it shouldn't
lead to unsafe behavior.

Signed-off-by: Ahmed Abdelraoof <[email protected]>
  • Loading branch information
oddcoder committed Aug 16, 2024
1 parent 02c564f commit 28cea8d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
17 changes: 13 additions & 4 deletions safe-discriminant-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,21 @@ fn get_enum_repr_prim(attrs: &[Attribute], error_span: Span) -> Result<Path> {
if !attr.path().is_ident("repr") {
continue;
}
let _ = attr.parse_nested_meta(|meta| {
if is_prim(&meta.path) {
prim = Some(meta.path);
attr.parse_nested_meta(|meta| {
if !is_prim(&meta.path) {
return Ok(());
}
if prim.is_some() {
return Err(Error::new(
// TODO join meta.path span with prim span
// but the function is currently nightly only
meta.path.span(),
"conflicting representation hints",
));
}
prim = Some(meta.path);
Ok(())
});
})?;
}
match prim {
Some(prim) => Ok(prim),
Expand Down
6 changes: 6 additions & 0 deletions safe-discriminant/tests/fail/multi_repr.stderr
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
error: conflicting representation hints
--> tests/fail/multi_repr.rs:4:15
|
4 | #[repr(C, u8, i8)]
| ^^

error[E0566]: conflicting representation hints
--> tests/fail/multi_repr.rs:4:8
|
Expand Down

0 comments on commit 28cea8d

Please sign in to comment.