@@ -31,6 +31,7 @@ enum ErrorKind {
31
31
ForbiddenType ,
32
32
MalformedAttrs ,
33
33
MissingPub ,
34
+ MissingRepr ,
34
35
MissingUnsafe ,
35
36
UnderscoreField ,
36
37
UnknownRepr ,
@@ -49,6 +50,7 @@ impl Display for ErrorKind {
49
50
Self :: ForbiddenType => "forbidden type" ,
50
51
Self :: MalformedAttrs => "malformed attribute contents" ,
51
52
Self :: MissingPub => "missing pub" ,
53
+ Self :: MissingRepr => "missing repr" ,
52
54
Self :: MissingUnsafe => "missing unsafe" ,
53
55
Self :: UnderscoreField => "field name starts with `_`" ,
54
56
Self :: UnknownRepr => "unknown repr" ,
@@ -105,7 +107,7 @@ fn is_pub(vis: &Visibility) -> bool {
105
107
}
106
108
107
109
/// 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 ) ]
109
111
enum Repr {
110
112
Align ( usize ) ,
111
113
C ,
@@ -115,7 +117,7 @@ enum Repr {
115
117
116
118
/// A restricted view of `Attribute`, limited to just the attributes that are
117
119
/// expected in `uefi-raw`.
118
- #[ derive( Clone , Copy ) ]
120
+ #[ derive( Debug , Clone , Copy ) ]
119
121
enum ParsedAttr {
120
122
Derive ,
121
123
Doc ,
@@ -137,6 +139,8 @@ fn parse_attrs(attrs: &[Attribute], src: &Path) -> Result<Vec<ParsedAttr>, Error
137
139
attr. parse_nested_meta ( |meta| {
138
140
if meta. path . is_ident ( "C" ) {
139
141
va. push ( ParsedAttr :: Repr ( Repr :: C ) ) ;
142
+ } else if meta. path . is_ident ( "Rust" ) {
143
+ va. push ( ParsedAttr :: Repr ( Repr :: Packed ) ) ;
140
144
} else if meta. path . is_ident ( "packed" ) {
141
145
va. push ( ParsedAttr :: Repr ( Repr :: Packed ) ) ;
142
146
} else if meta. path . is_ident ( "transparent" ) {
@@ -259,7 +263,9 @@ fn check_type_attrs(attrs: &[Attribute], spanned: &dyn Spanned, src: &Path) -> R
259
263
260
264
let allowed_reprs: & [ & [ Repr ] ] = & [ & [ Repr :: C ] , & [ Repr :: C , Repr :: Packed ] , & [ Repr :: Transparent ] ] ;
261
265
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 ( ) ) {
263
269
Ok ( ( ) )
264
270
} else {
265
271
Err ( Error :: new ( ErrorKind :: ForbiddenRepr , src, spanned) )
@@ -408,6 +414,7 @@ mod tests {
408
414
Path :: new ( "test" )
409
415
}
410
416
417
+ #[ track_caller]
411
418
fn check_item_err ( item : Item , expected_error : ErrorKind ) {
412
419
assert_eq ! ( check_item( & item, src( ) ) . unwrap_err( ) . kind, expected_error) ;
413
420
}
@@ -545,9 +552,20 @@ mod tests {
545
552
ErrorKind :: UnderscoreField ,
546
553
) ;
547
554
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
+
548
565
// Forbidden `repr`.
549
566
check_item_err (
550
567
parse_quote ! {
568
+ #[ repr( Rust ) ]
551
569
pub struct S {
552
570
pub f: u32 ,
553
571
}
@@ -623,7 +641,7 @@ mod tests {
623
641
pub f: u32 ,
624
642
}
625
643
} ,
626
- ErrorKind :: ForbiddenRepr ,
644
+ ErrorKind :: MissingRepr ,
627
645
) ;
628
646
}
629
647
}
0 commit comments