Skip to content

xtask: improved error output for "wrong" repr #1742

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
Aug 16, 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
58 changes: 35 additions & 23 deletions xtask/src/check_raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ enum ErrorKind {
ForbiddenAbi,
ForbiddenAttr,
ForbiddenItemKind(ItemKind),
ForbiddenRepr,
ForbiddenRepr(Vec<Repr>),
ForbiddenType,
MalformedAttrs,
MissingPub,
Expand All @@ -57,25 +57,37 @@ enum ErrorKind {

impl Display for ErrorKind {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(
f,
"{}",
match self {
Self::ForbiddenAbi => "forbidden ABI",
Self::ForbiddenAttr => "forbidden attribute",
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",
Self::MissingPub => "missing pub",
Self::MissingRepr => "missing repr",
Self::MissingUnsafe => "missing unsafe",
Self::UnderscoreField => "field name starts with `_`",
Self::UnknownRepr => "unknown repr",
match self {
Self::ForbiddenAbi => write!(f, "forbidden ABI"),
Self::ForbiddenAttr => write!(f, "forbidden attribute"),
Self::ForbiddenItemKind(ItemKind::Enum) => write!(
f,
"forbidden use of enum; use the `newtype_enum!` macro instead"
),
Self::ForbiddenItemKind(_) => write!(f, "forbidden type of item"),
Self::ForbiddenRepr(reprs) => {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all arms are the same as before, except for this arm

assert!(!reprs.is_empty());
if reprs.len() == 1 {
write!(
f,
"the following repr attribute is forbidden: {:?}",
reprs[0]
)
} else {
write!(
f,
"the following combination of repr attributes is forbidden: {reprs:?}"
)
}
}
)
Self::ForbiddenType => write!(f, "forbidden type"),
Self::MalformedAttrs => write!(f, "malformed attribute contents"),
Self::MissingPub => write!(f, "missing pub"),
Self::MissingRepr => write!(f, "missing repr"),
Self::MissingUnsafe => write!(f, "missing unsafe"),
Self::UnderscoreField => write!(f, "field name starts with `_`"),
Self::UnknownRepr => write!(f, "unknown repr"),
}
}
}

Expand Down Expand Up @@ -290,7 +302,7 @@ fn check_type_attrs(attrs: &[Attribute], spanned: &dyn Spanned, src: &Path) -> R
} else if ALLOWED_REPRS.contains(&reprs.as_slice()) {
Ok(())
} else {
Err(Error::new(ErrorKind::ForbiddenRepr, src, spanned))
Err(Error::new(ErrorKind::ForbiddenRepr(reprs), src, spanned))
}
}

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

Expand Down Expand Up @@ -481,7 +493,7 @@ mod tests {
}
}
},
ErrorKind::ForbiddenRepr,
ErrorKind::ForbiddenRepr(vec![Repr::C]),
);
}

Expand Down Expand Up @@ -613,7 +625,7 @@ mod tests {
pub f: u32,
}
},
ErrorKind::ForbiddenRepr,
ErrorKind::ForbiddenRepr(vec![Repr::Rust]),
);

// Forbidden attr.
Expand Down
Loading