Skip to content

Commit 31d9a36

Browse files
committed
xtask/uefi-raw: improve check-raw error messages
This helps to better identify why the check-raw step fails.
1 parent 2273da4 commit 31d9a36

File tree

1 file changed

+24
-5
lines changed

1 file changed

+24
-5
lines changed

xtask/src/check_raw.rs

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ enum ErrorKind {
3333
ForbiddenType,
3434
MalformedAttrs,
3535
MissingPub,
36+
MissingRepr,
3637
MissingUnsafe,
3738
UnderscoreField,
3839
UnknownRepr,
@@ -51,6 +52,7 @@ impl Display for ErrorKind {
5152
Self::ForbiddenType => "forbidden type",
5253
Self::MalformedAttrs => "malformed attribute contents",
5354
Self::MissingPub => "missing pub",
55+
Self::MissingRepr => "missing repr",
5456
Self::MissingUnsafe => "missing unsafe",
5557
Self::UnderscoreField => "field name starts with `_`",
5658
Self::UnknownRepr => "unknown repr",
@@ -107,17 +109,18 @@ fn is_pub(vis: &Visibility) -> bool {
107109
}
108110

109111
/// Type repr. A type may have more than one of these (e.g. both `C` and `Packed`).
110-
#[derive(Clone, Copy, Eq, PartialEq, Ord, PartialOrd)]
112+
#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd)]
111113
enum Repr {
112114
Align(usize),
113115
C,
114116
Packed,
117+
Rust,
115118
Transparent,
116119
}
117120

118121
/// A restricted view of `Attribute`, limited to just the attributes that are
119122
/// expected in `uefi-raw`.
120-
#[derive(Clone, Copy)]
123+
#[derive(Debug, Clone, Copy)]
121124
enum ParsedAttr {
122125
Derive,
123126
Doc,
@@ -139,8 +142,10 @@ fn parse_attrs(attrs: &[Attribute], src: &Path) -> Result<Vec<ParsedAttr>, Error
139142
attr.parse_nested_meta(|meta| {
140143
if meta.path.is_ident("C") {
141144
va.push(ParsedAttr::Repr(Repr::C));
142-
} else if meta.path.is_ident("packed") {
145+
} else if meta.path.is_ident("Rust") {
143146
va.push(ParsedAttr::Repr(Repr::Packed));
147+
} else if meta.path.is_ident("packed") {
148+
va.push(ParsedAttr::Repr(Repr::Rust));
144149
} else if meta.path.is_ident("transparent") {
145150
va.push(ParsedAttr::Repr(Repr::Transparent));
146151
} else if meta.path.is_ident("align") {
@@ -261,7 +266,9 @@ fn check_type_attrs(attrs: &[Attribute], spanned: &dyn Spanned, src: &Path) -> R
261266

262267
let allowed_reprs: &[&[Repr]] = &[&[Repr::C], &[Repr::C, Repr::Packed], &[Repr::Transparent]];
263268

264-
if allowed_reprs.contains(&reprs.as_slice()) {
269+
if reprs.is_empty() {
270+
Err(Error::new(ErrorKind::MissingRepr, src, spanned))
271+
} else if allowed_reprs.contains(&reprs.as_slice()) {
265272
Ok(())
266273
} else {
267274
Err(Error::new(ErrorKind::ForbiddenRepr, src, spanned))
@@ -410,6 +417,7 @@ mod tests {
410417
Path::new("test")
411418
}
412419

420+
#[track_caller]
413421
fn check_item_err(item: Item, expected_error: ErrorKind) {
414422
assert_eq!(check_item(&item, src()).unwrap_err().kind, expected_error);
415423
}
@@ -547,9 +555,20 @@ mod tests {
547555
ErrorKind::UnderscoreField,
548556
);
549557

558+
// Missing `repr`.
559+
check_item_err(
560+
parse_quote! {
561+
pub struct S {
562+
pub f: u32,
563+
}
564+
},
565+
ErrorKind::MissingRepr,
566+
);
567+
550568
// Forbidden `repr`.
551569
check_item_err(
552570
parse_quote! {
571+
#[repr(Rust)]
553572
pub struct S {
554573
pub f: u32,
555574
}
@@ -625,7 +644,7 @@ mod tests {
625644
pub f: u32,
626645
}
627646
},
628-
ErrorKind::ForbiddenRepr,
647+
ErrorKind::MissingRepr,
629648
);
630649
}
631650
}

0 commit comments

Comments
 (0)