Skip to content

cargo xtask improvements #1741

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

Closed
wants to merge 2 commits into from
Closed
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
56 changes: 31 additions & 25 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,26 @@ 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) => 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 @@ -279,7 +280,12 @@ fn check_fields(fields: &Punctuated<Field, Comma>, src: &Path) -> Result<(), Err
}

/// List with allowed combinations of representations (see [`Repr`]).
const ALLOWED_REPRS: &[&[Repr]] = &[&[Repr::C], &[Repr::C, Repr::Packed], &[Repr::Transparent]];
const ALLOWED_REPRS: &[&[Repr]] = &[
&[Repr::C],
&[Repr::C, Repr::Packed],
&[Repr::Transparent],
&[Repr::Align(4), Repr::C],
];

fn check_type_attrs(attrs: &[Attribute], spanned: &dyn Spanned, src: &Path) -> Result<(), Error> {
let attrs = parse_attrs(attrs, src)?;
Expand All @@ -290,7 +296,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 +353,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 +487,7 @@ mod tests {
}
}
},
ErrorKind::ForbiddenRepr,
ErrorKind::ForbiddenRepr(vec![Repr::C]),
);
}

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

// Forbidden attr.
Expand Down
Loading