@@ -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 ) ]
109111enum 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 ) ]
119121enum 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