Skip to content

Commit 2611778

Browse files
committed
xtask/uefi-raw: fix check-raw
1 parent 6f9d1b0 commit 2611778

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

uefi-raw/src/lib.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ pub type VirtualAddress = u64;
7171
/// The provided [`Boolean`] can't be converted to [`bool`] as it is neither
7272
/// `0` nor `1`.
7373
#[derive(Debug, Copy, Clone, PartialEq, Ord, PartialOrd, Eq)]
74-
pub struct InvalidBooleanError(u8);
74+
#[repr(transparent)]
75+
pub struct InvalidBooleanError(pub u8);
7576

7677
impl Display for InvalidBooleanError {
7778
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
@@ -89,7 +90,7 @@ impl Error for InvalidBooleanError {}
8990
/// respectively [`TryFrom`] implementations.
9091
#[derive(Copy, Clone, Debug, Default, PartialEq, Ord, PartialOrd, Eq, Hash)]
9192
#[repr(transparent)]
92-
pub struct Boolean(u8);
93+
pub struct Boolean(pub u8);
9394

9495
impl Boolean {
9596
/// [`Boolean`] representing `true`.

xtask/src/check_raw.rs

+22-4
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ enum ErrorKind {
3131
ForbiddenType,
3232
MalformedAttrs,
3333
MissingPub,
34+
MissingRepr,
3435
MissingUnsafe,
3536
UnderscoreField,
3637
UnknownRepr,
@@ -49,6 +50,7 @@ impl Display for ErrorKind {
4950
Self::ForbiddenType => "forbidden type",
5051
Self::MalformedAttrs => "malformed attribute contents",
5152
Self::MissingPub => "missing pub",
53+
Self::MissingRepr => "missing repr",
5254
Self::MissingUnsafe => "missing unsafe",
5355
Self::UnderscoreField => "field name starts with `_`",
5456
Self::UnknownRepr => "unknown repr",
@@ -105,7 +107,7 @@ fn is_pub(vis: &Visibility) -> bool {
105107
}
106108

107109
/// Type repr. A type may have more than one of these (e.g. both `C` and `Packed`).
108-
#[derive(Clone, Copy, Eq, PartialEq, Ord, PartialOrd)]
110+
#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd)]
109111
enum Repr {
110112
Align(usize),
111113
C,
@@ -115,7 +117,7 @@ enum Repr {
115117

116118
/// A restricted view of `Attribute`, limited to just the attributes that are
117119
/// expected in `uefi-raw`.
118-
#[derive(Clone, Copy)]
120+
#[derive(Debug, Clone, Copy)]
119121
enum ParsedAttr {
120122
Derive,
121123
Doc,
@@ -137,6 +139,8 @@ fn parse_attrs(attrs: &[Attribute], src: &Path) -> Result<Vec<ParsedAttr>, Error
137139
attr.parse_nested_meta(|meta| {
138140
if meta.path.is_ident("C") {
139141
va.push(ParsedAttr::Repr(Repr::C));
142+
} else if meta.path.is_ident("Rust") {
143+
va.push(ParsedAttr::Repr(Repr::Packed));
140144
} else if meta.path.is_ident("packed") {
141145
va.push(ParsedAttr::Repr(Repr::Packed));
142146
} else if meta.path.is_ident("transparent") {
@@ -259,7 +263,9 @@ fn check_type_attrs(attrs: &[Attribute], spanned: &dyn Spanned, src: &Path) -> R
259263

260264
let allowed_reprs: &[&[Repr]] = &[&[Repr::C], &[Repr::C, Repr::Packed], &[Repr::Transparent]];
261265

262-
if allowed_reprs.contains(&reprs.as_slice()) {
266+
if reprs.is_empty() {
267+
Err(Error::new(ErrorKind::MissingRepr, src, spanned))
268+
} else if allowed_reprs.contains(&reprs.as_slice()) {
263269
Ok(())
264270
} else {
265271
Err(Error::new(ErrorKind::ForbiddenRepr, src, spanned))
@@ -408,6 +414,7 @@ mod tests {
408414
Path::new("test")
409415
}
410416

417+
#[track_caller]
411418
fn check_item_err(item: Item, expected_error: ErrorKind) {
412419
assert_eq!(check_item(&item, src()).unwrap_err().kind, expected_error);
413420
}
@@ -545,9 +552,20 @@ mod tests {
545552
ErrorKind::UnderscoreField,
546553
);
547554

555+
// Missing `repr`.
556+
check_item_err(
557+
parse_quote! {
558+
pub struct S {
559+
pub f: u32,
560+
}
561+
},
562+
ErrorKind::MissingRepr,
563+
);
564+
548565
// Forbidden `repr`.
549566
check_item_err(
550567
parse_quote! {
568+
#[repr(Rust)]
551569
pub struct S {
552570
pub f: u32,
553571
}
@@ -623,7 +641,7 @@ mod tests {
623641
pub f: u32,
624642
}
625643
},
626-
ErrorKind::ForbiddenRepr,
644+
ErrorKind::MissingRepr,
627645
);
628646
}
629647
}

0 commit comments

Comments
 (0)