Skip to content

xtask: Improve error message for enums in uefi-raw #1660

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 9, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 38 additions & 5 deletions xtask/src/check_raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,28 @@ use syn::{
};
use walkdir::WalkDir;

/// Type of an `Item`.
#[derive(Debug, Eq, PartialEq)]
enum ItemKind {
Enum,
Other,
}

impl From<&Item> for ItemKind {
fn from(item: &Item) -> Self {
match item {
Item::Enum(_) => Self::Enum,
_ => Self::Other,
}
}
}

/// All possible validation error kinds.
#[derive(Debug, Eq, PartialEq)]
enum ErrorKind {
ForbiddenAbi,
ForbiddenAttr,
ForbiddenItemKind,
ForbiddenItemKind(ItemKind),
ForbiddenRepr,
ForbiddenType,
MalformedAttrs,
Expand All @@ -47,7 +63,9 @@ impl Display for ErrorKind {
match self {
Self::ForbiddenAbi => "forbidden ABI",
Self::ForbiddenAttr => "forbidden attribute",
Self::ForbiddenItemKind => "forbidden type of item",
Self::ForbiddenItemKind(ItemKind::Enum) =>
"forbidden use of enum; use the `newtype_enum!` macro instead",
Self::ForbiddenItemKind(_) => "forbidden type of item",
Self::ForbiddenRepr => "forbidden repr",
Self::ForbiddenType => "forbidden type",
Self::MalformedAttrs => "malformed attribute contents",
Expand Down Expand Up @@ -364,7 +382,11 @@ fn check_item(item: &Item, src: &Path) -> Result<(), Error> {
// Allow.
}
item => {
return Err(Error::new(ErrorKind::ForbiddenItemKind, src, item));
return Err(Error::new(
ErrorKind::ForbiddenItemKind(item.into()),
src,
item,
));
}
}

Expand Down Expand Up @@ -424,15 +446,26 @@ mod tests {
}

#[test]
fn test_invalid_item() {
fn test_invalid_item_enum() {
// Rust enums are not allowed.
check_item_err(
parse_quote! {
pub enum E {
A
}
},
ErrorKind::ForbiddenItemKind,
ErrorKind::ForbiddenItemKind(ItemKind::Enum),
);
}

#[test]
fn test_invalid_item_other() {
// Top-level functions are not allowed.
check_item_err(
parse_quote! {
pub fn x() {}
},
ErrorKind::ForbiddenItemKind(ItemKind::Other),
);
}

Expand Down
Loading