@@ -33,6 +33,7 @@ enum ErrorKind {
33
33
ForbiddenType ,
34
34
MalformedAttrs ,
35
35
MissingPub ,
36
+ MissingRepr ,
36
37
MissingUnsafe ,
37
38
UnderscoreField ,
38
39
UnknownRepr ,
@@ -51,6 +52,7 @@ impl Display for ErrorKind {
51
52
Self :: ForbiddenType => "forbidden type" ,
52
53
Self :: MalformedAttrs => "malformed attribute contents" ,
53
54
Self :: MissingPub => "missing pub" ,
55
+ Self :: MissingRepr => "missing repr" ,
54
56
Self :: MissingUnsafe => "missing unsafe" ,
55
57
Self :: UnderscoreField => "field name starts with `_`" ,
56
58
Self :: UnknownRepr => "unknown repr" ,
@@ -107,17 +109,18 @@ fn is_pub(vis: &Visibility) -> bool {
107
109
}
108
110
109
111
/// 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 ) ]
111
113
enum Repr {
112
114
Align ( usize ) ,
113
115
C ,
114
116
Packed ,
117
+ Rust ,
115
118
Transparent ,
116
119
}
117
120
118
121
/// A restricted view of `Attribute`, limited to just the attributes that are
119
122
/// expected in `uefi-raw`.
120
- #[ derive( Clone , Copy ) ]
123
+ #[ derive( Debug , Clone , Copy ) ]
121
124
enum ParsedAttr {
122
125
Derive ,
123
126
Doc ,
@@ -139,8 +142,10 @@ fn parse_attrs(attrs: &[Attribute], src: &Path) -> Result<Vec<ParsedAttr>, Error
139
142
attr. parse_nested_meta ( |meta| {
140
143
if meta. path . is_ident ( "C" ) {
141
144
va. push ( ParsedAttr :: Repr ( Repr :: C ) ) ;
142
- } else if meta. path . is_ident ( "packed " ) {
145
+ } else if meta. path . is_ident ( "Rust " ) {
143
146
va. push ( ParsedAttr :: Repr ( Repr :: Packed ) ) ;
147
+ } else if meta. path . is_ident ( "packed" ) {
148
+ va. push ( ParsedAttr :: Repr ( Repr :: Rust ) ) ;
144
149
} else if meta. path . is_ident ( "transparent" ) {
145
150
va. push ( ParsedAttr :: Repr ( Repr :: Transparent ) ) ;
146
151
} else if meta. path . is_ident ( "align" ) {
@@ -261,7 +266,9 @@ fn check_type_attrs(attrs: &[Attribute], spanned: &dyn Spanned, src: &Path) -> R
261
266
262
267
let allowed_reprs: & [ & [ Repr ] ] = & [ & [ Repr :: C ] , & [ Repr :: C , Repr :: Packed ] , & [ Repr :: Transparent ] ] ;
263
268
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 ( ) ) {
265
272
Ok ( ( ) )
266
273
} else {
267
274
Err ( Error :: new ( ErrorKind :: ForbiddenRepr , src, spanned) )
@@ -410,6 +417,7 @@ mod tests {
410
417
Path :: new ( "test" )
411
418
}
412
419
420
+ #[ track_caller]
413
421
fn check_item_err ( item : Item , expected_error : ErrorKind ) {
414
422
assert_eq ! ( check_item( & item, src( ) ) . unwrap_err( ) . kind, expected_error) ;
415
423
}
@@ -547,9 +555,20 @@ mod tests {
547
555
ErrorKind :: UnderscoreField ,
548
556
) ;
549
557
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
+
550
568
// Forbidden `repr`.
551
569
check_item_err (
552
570
parse_quote ! {
571
+ #[ repr( Rust ) ]
553
572
pub struct S {
554
573
pub f: u32 ,
555
574
}
@@ -625,7 +644,7 @@ mod tests {
625
644
pub f: u32 ,
626
645
}
627
646
} ,
628
- ErrorKind :: ForbiddenRepr ,
647
+ ErrorKind :: MissingRepr ,
629
648
) ;
630
649
}
631
650
}
0 commit comments