@@ -913,7 +913,7 @@ fn maybe_from_hir_attr(
913913 {
914914 Attribute :: DocHidden
915915 } else {
916- Attribute :: Other ( rustc_hir_pretty :: attribute_to_string ( & tcx, attr) )
916+ other_attr ( tcx, attr)
917917 } ) ;
918918 }
919919 } ;
@@ -925,22 +925,59 @@ fn maybe_from_hir_attr(
925925 AK :: MustUse { reason, span : _ } => {
926926 Attribute :: MustUse { reason : reason. map ( |s| s. to_string ( ) ) }
927927 }
928- AK :: Repr { .. } => Attribute :: Repr ( repr ( item_id. as_def_id ( ) . expect ( "reason" ) , tcx) ) ,
928+ AK :: Repr { .. } => repr_attr (
929+ tcx,
930+ item_id. as_def_id ( ) . expect ( "all items that could have #[repr] have a DefId" ) ,
931+ ) ,
929932 AK :: NoMangle ( _) => Attribute :: NoMangle ,
930933
931- _ => Attribute :: Other ( rustc_hir_pretty :: attribute_to_string ( & tcx, attr) ) ,
934+ _ => other_attr ( tcx, attr) ,
932935 } )
933936}
934937
935- fn repr ( def_id : DefId , tcx : TyCtxt < ' _ > ) -> AttributeRepr {
938+ fn other_attr ( tcx : TyCtxt < ' _ > , attr : & hir:: Attribute ) -> Attribute {
939+ let mut s = rustc_hir_pretty:: attribute_to_string ( & tcx, attr) ;
940+ assert_eq ! ( s. pop( ) , Some ( '\n' ) ) ;
941+ Attribute :: Other ( s)
942+ }
943+
944+ fn repr_attr ( tcx : TyCtxt < ' _ > , def_id : DefId ) -> Attribute {
936945 let repr = tcx. adt_def ( def_id) . repr ( ) ;
937946
938947 // FIXME: This is wildly insufficient
939- if repr. c ( ) {
940- AttributeRepr :: C
948+ let kind = if repr. c ( ) {
949+ ReprKind :: C
941950 } else if repr. transparent ( ) {
942- AttributeRepr :: Transparent
951+ ReprKind :: Transparent
952+ } else if repr. simd ( ) {
953+ ReprKind :: Simd
943954 } else {
944- AttributeRepr :: Rust
945- }
955+ ReprKind :: Rust
956+ } ;
957+
958+ let align = repr. align . map ( |a| a. bytes ( ) ) ;
959+ let packed = repr. pack . map ( |p| p. bytes ( ) ) ;
960+ let int = repr. int . map ( format_integer_type) ;
961+
962+ Attribute :: Repr ( AttributeRepr { kind, align, packed, int } )
963+ }
964+
965+ fn format_integer_type ( it : rustc_abi:: IntegerType ) -> String {
966+ use rustc_abi:: Integer :: * ;
967+ use rustc_abi:: IntegerType :: * ;
968+ match it {
969+ Pointer ( true ) => "isize" ,
970+ Pointer ( false ) => "usize" ,
971+ Fixed ( I8 , true ) => "i8" ,
972+ Fixed ( I8 , false ) => "u8" ,
973+ Fixed ( I16 , true ) => "i16" ,
974+ Fixed ( I16 , false ) => "u16" ,
975+ Fixed ( I32 , true ) => "i32" ,
976+ Fixed ( I32 , false ) => "u32" ,
977+ Fixed ( I64 , true ) => "i64" ,
978+ Fixed ( I64 , false ) => "u64" ,
979+ Fixed ( I128 , true ) => "i128" ,
980+ Fixed ( I128 , false ) => "u128" ,
981+ }
982+ . to_owned ( )
946983}
0 commit comments