Skip to content

Commit 28cea8d

Browse files
author
Ahmed Abdelraoof
committed
Force muliple repr hint to be hard error
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 <ahmed.abdelraoof@huawei.com>
1 parent 02c564f commit 28cea8d

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

safe-discriminant-derive/src/lib.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,21 @@ fn get_enum_repr_prim(attrs: &[Attribute], error_span: Span) -> Result<Path> {
2626
if !attr.path().is_ident("repr") {
2727
continue;
2828
}
29-
let _ = attr.parse_nested_meta(|meta| {
30-
if is_prim(&meta.path) {
31-
prim = Some(meta.path);
29+
attr.parse_nested_meta(|meta| {
30+
if !is_prim(&meta.path) {
31+
return Ok(());
3232
}
33+
if prim.is_some() {
34+
return Err(Error::new(
35+
// TODO join meta.path span with prim span
36+
// but the function is currently nightly only
37+
meta.path.span(),
38+
"conflicting representation hints",
39+
));
40+
}
41+
prim = Some(meta.path);
3342
Ok(())
34-
});
43+
})?;
3544
}
3645
match prim {
3746
Some(prim) => Ok(prim),

safe-discriminant/tests/fail/multi_repr.stderr

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
error: conflicting representation hints
2+
--> tests/fail/multi_repr.rs:4:15
3+
|
4+
4 | #[repr(C, u8, i8)]
5+
| ^^
6+
17
error[E0566]: conflicting representation hints
28
--> tests/fail/multi_repr.rs:4:8
39
|

0 commit comments

Comments
 (0)