Skip to content

Commit c5bf934

Browse files
Merge pull request #1742 from rust-osdev/improve-cargo-xtask-v2
xtask: improved error output for "wrong" repr
2 parents db40d3d + b5919ff commit c5bf934

File tree

1 file changed

+35
-23
lines changed

1 file changed

+35
-23
lines changed

xtask/src/check_raw.rs

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ enum ErrorKind {
4545
ForbiddenAbi,
4646
ForbiddenAttr,
4747
ForbiddenItemKind(ItemKind),
48-
ForbiddenRepr,
48+
ForbiddenRepr(Vec<Repr>),
4949
ForbiddenType,
5050
MalformedAttrs,
5151
MissingPub,
@@ -57,25 +57,37 @@ enum ErrorKind {
5757

5858
impl Display for ErrorKind {
5959
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
60-
write!(
61-
f,
62-
"{}",
63-
match self {
64-
Self::ForbiddenAbi => "forbidden ABI",
65-
Self::ForbiddenAttr => "forbidden attribute",
66-
Self::ForbiddenItemKind(ItemKind::Enum) =>
67-
"forbidden use of enum; use the `newtype_enum!` macro instead",
68-
Self::ForbiddenItemKind(_) => "forbidden type of item",
69-
Self::ForbiddenRepr => "forbidden repr",
70-
Self::ForbiddenType => "forbidden type",
71-
Self::MalformedAttrs => "malformed attribute contents",
72-
Self::MissingPub => "missing pub",
73-
Self::MissingRepr => "missing repr",
74-
Self::MissingUnsafe => "missing unsafe",
75-
Self::UnderscoreField => "field name starts with `_`",
76-
Self::UnknownRepr => "unknown repr",
60+
match self {
61+
Self::ForbiddenAbi => write!(f, "forbidden ABI"),
62+
Self::ForbiddenAttr => write!(f, "forbidden attribute"),
63+
Self::ForbiddenItemKind(ItemKind::Enum) => write!(
64+
f,
65+
"forbidden use of enum; use the `newtype_enum!` macro instead"
66+
),
67+
Self::ForbiddenItemKind(_) => write!(f, "forbidden type of item"),
68+
Self::ForbiddenRepr(reprs) => {
69+
assert!(!reprs.is_empty());
70+
if reprs.len() == 1 {
71+
write!(
72+
f,
73+
"the following repr attribute is forbidden: {:?}",
74+
reprs[0]
75+
)
76+
} else {
77+
write!(
78+
f,
79+
"the following combination of repr attributes is forbidden: {reprs:?}"
80+
)
81+
}
7782
}
78-
)
83+
Self::ForbiddenType => write!(f, "forbidden type"),
84+
Self::MalformedAttrs => write!(f, "malformed attribute contents"),
85+
Self::MissingPub => write!(f, "missing pub"),
86+
Self::MissingRepr => write!(f, "missing repr"),
87+
Self::MissingUnsafe => write!(f, "missing unsafe"),
88+
Self::UnderscoreField => write!(f, "field name starts with `_`"),
89+
Self::UnknownRepr => write!(f, "unknown repr"),
90+
}
7991
}
8092
}
8193

@@ -290,7 +302,7 @@ fn check_type_attrs(attrs: &[Attribute], spanned: &dyn Spanned, src: &Path) -> R
290302
} else if ALLOWED_REPRS.contains(&reprs.as_slice()) {
291303
Ok(())
292304
} else {
293-
Err(Error::new(ErrorKind::ForbiddenRepr, src, spanned))
305+
Err(Error::new(ErrorKind::ForbiddenRepr(reprs), src, spanned))
294306
}
295307
}
296308

@@ -347,7 +359,7 @@ fn check_macro(item: &ItemMacro, src: &Path) -> Result<(), Error> {
347359
let reprs = get_reprs(&attrs);
348360
let allowed_reprs: &[&[Repr]] = &[&[Repr::Transparent]];
349361
if !allowed_reprs.contains(&reprs.as_slice()) {
350-
return Err(Error::new(ErrorKind::ForbiddenRepr, src, mac));
362+
return Err(Error::new(ErrorKind::ForbiddenRepr(reprs), src, mac));
351363
}
352364
}
353365

@@ -481,7 +493,7 @@ mod tests {
481493
}
482494
}
483495
},
484-
ErrorKind::ForbiddenRepr,
496+
ErrorKind::ForbiddenRepr(vec![Repr::C]),
485497
);
486498
}
487499

@@ -613,7 +625,7 @@ mod tests {
613625
pub f: u32,
614626
}
615627
},
616-
ErrorKind::ForbiddenRepr,
628+
ErrorKind::ForbiddenRepr(vec![Repr::Rust]),
617629
);
618630

619631
// Forbidden attr.

0 commit comments

Comments
 (0)