@@ -10,7 +10,7 @@ use std::vec;
1010
1111use rustc_abi:: ExternAbi ;
1212use rustc_ast:: util:: parser:: { self , ExprPrecedence , Fixity } ;
13- use rustc_ast:: { AttrStyle , DUMMY_NODE_ID , DelimArgs } ;
13+ use rustc_ast:: { DUMMY_NODE_ID , DelimArgs } ;
1414use rustc_ast_pretty:: pp:: Breaks :: { Consistent , Inconsistent } ;
1515use rustc_ast_pretty:: pp:: { self , BoxMarker , Breaks } ;
1616use rustc_ast_pretty:: pprust:: state:: MacHeader ;
@@ -81,32 +81,24 @@ impl<'a> State<'a> {
8181 }
8282
8383 fn precedence ( & self , expr : & hir:: Expr < ' _ > ) -> ExprPrecedence {
84- let for_each_attr = |id : HirId , callback : & mut dyn FnMut ( & hir:: Attribute ) | {
85- self . attrs ( id) . iter ( ) . for_each ( callback) ;
86- } ;
87- expr. precedence ( & for_each_attr)
88- }
89-
90- fn print_attrs_as_inner ( & mut self , attrs : & [ hir:: Attribute ] ) {
91- self . print_either_attributes ( attrs, ast:: AttrStyle :: Inner )
92- }
93-
94- fn print_attrs_as_outer ( & mut self , attrs : & [ hir:: Attribute ] ) {
95- self . print_either_attributes ( attrs, ast:: AttrStyle :: Outer )
84+ let has_attr = |id : HirId | !self . attrs ( id) . is_empty ( ) ;
85+ expr. precedence ( & has_attr)
9686 }
9787
98- fn print_either_attributes ( & mut self , attrs : & [ hir:: Attribute ] , style : ast :: AttrStyle ) {
88+ fn print_attrs ( & mut self , attrs : & [ hir:: Attribute ] ) {
9989 if attrs. is_empty ( ) {
10090 return ;
10191 }
10292
10393 for attr in attrs {
104- self . print_attribute_inline ( attr, style ) ;
94+ self . print_attribute_as_style ( attr, ast :: AttrStyle :: Outer ) ;
10595 }
10696 self . hardbreak_if_not_bol ( ) ;
10797 }
10898
109- fn print_attribute_inline ( & mut self , attr : & hir:: Attribute , style : AttrStyle ) {
99+ /// Print a single attribute as if it has style `style`, disregarding the
100+ /// actual style of the attribute.
101+ fn print_attribute_as_style ( & mut self , attr : & hir:: Attribute , style : ast:: AttrStyle ) {
110102 match & attr {
111103 hir:: Attribute :: Unparsed ( unparsed) => {
112104 self . maybe_print_comment ( unparsed. span . lo ( ) ) ;
@@ -118,14 +110,17 @@ impl<'a> State<'a> {
118110 self . word ( "]" ) ;
119111 self . hardbreak ( )
120112 }
121- hir:: Attribute :: Parsed ( AttributeKind :: DocComment { style , kind, comment, .. } ) => {
113+ hir:: Attribute :: Parsed ( AttributeKind :: DocComment { kind, comment, .. } ) => {
122114 self . word ( rustc_ast_pretty:: pprust:: state:: doc_comment_to_string (
123- * kind, * style, * comment,
115+ * kind, style, * comment,
124116 ) ) ;
125117 self . hardbreak ( )
126118 }
127119 hir:: Attribute :: Parsed ( pa) => {
128- self . word ( "#[attr = " ) ;
120+ match style {
121+ ast:: AttrStyle :: Inner => self . word ( "#![attr = " ) ,
122+ ast:: AttrStyle :: Outer => self . word ( "#[attr = " ) ,
123+ }
129124 pa. print_attribute ( self ) ;
130125 self . word ( "]" ) ;
131126 self . hardbreak ( )
@@ -281,10 +276,17 @@ pub fn print_crate<'a>(
281276 ann,
282277 } ;
283278
279+ // Print all attributes, regardless of actual style, as inner attributes
280+ // since this is the crate root with nothing above it to print outer
281+ // attributes.
282+ for attr in s. attrs ( hir:: CRATE_HIR_ID ) {
283+ s. print_attribute_as_style ( attr, ast:: AttrStyle :: Inner ) ;
284+ }
285+
284286 // When printing the AST, we sometimes need to inject `#[no_std]` here.
285287 // Since you can't compile the HIR, it's not necessary.
286288
287- s. print_mod ( krate, ( * attrs ) ( hir :: CRATE_HIR_ID ) ) ;
289+ s. print_mod ( krate) ;
288290 s. print_remaining_comments ( ) ;
289291 s. s . eof ( )
290292}
@@ -299,7 +301,7 @@ where
299301}
300302
301303pub fn attribute_to_string ( ann : & dyn PpAnn , attr : & hir:: Attribute ) -> String {
302- to_string ( ann, |s| s. print_attribute_inline ( attr, AttrStyle :: Outer ) )
304+ to_string ( ann, |s| s. print_attribute_as_style ( attr, ast :: AttrStyle :: Outer ) )
303305}
304306
305307pub fn ty_to_string ( ann : & dyn PpAnn , ty : & hir:: Ty < ' _ > ) -> String {
@@ -361,8 +363,7 @@ impl<'a> State<'a> {
361363 self . commasep_cmnt ( b, exprs, |s, e| s. print_expr ( e) , |e| e. span ) ;
362364 }
363365
364- fn print_mod ( & mut self , _mod : & hir:: Mod < ' _ > , attrs : & [ hir:: Attribute ] ) {
365- self . print_attrs_as_inner ( attrs) ;
366+ fn print_mod ( & mut self , _mod : & hir:: Mod < ' _ > ) {
366367 for & item_id in _mod. item_ids {
367368 self . ann . nested ( self , Nested :: Item ( item_id) ) ;
368369 }
@@ -479,7 +480,7 @@ impl<'a> State<'a> {
479480 fn print_foreign_item ( & mut self , item : & hir:: ForeignItem < ' _ > ) {
480481 self . hardbreak_if_not_bol ( ) ;
481482 self . maybe_print_comment ( item. span . lo ( ) ) ;
482- self . print_attrs_as_outer ( self . attrs ( item. hir_id ( ) ) ) ;
483+ self . print_attrs ( self . attrs ( item. hir_id ( ) ) ) ;
483484 match item. kind {
484485 hir:: ForeignItemKind :: Fn ( sig, arg_idents, generics) => {
485486 let ( cb, ib) = self . head ( "" ) ;
@@ -565,7 +566,7 @@ impl<'a> State<'a> {
565566 self . hardbreak_if_not_bol ( ) ;
566567 self . maybe_print_comment ( item. span . lo ( ) ) ;
567568 let attrs = self . attrs ( item. hir_id ( ) ) ;
568- self . print_attrs_as_outer ( attrs) ;
569+ self . print_attrs ( attrs) ;
569570 self . ann . pre ( self , AnnNode :: Item ( item) ) ;
570571 match item. kind {
571572 hir:: ItemKind :: ExternCrate ( orig_name, ident) => {
@@ -647,14 +648,13 @@ impl<'a> State<'a> {
647648 self . print_ident ( ident) ;
648649 self . nbsp ( ) ;
649650 self . bopen ( ib) ;
650- self . print_mod ( mod_, attrs ) ;
651+ self . print_mod ( mod_) ;
651652 self . bclose ( item. span , cb) ;
652653 }
653654 hir:: ItemKind :: ForeignMod { abi, items } => {
654655 let ( cb, ib) = self . head ( "extern" ) ;
655656 self . word_nbsp ( abi. to_string ( ) ) ;
656657 self . bopen ( ib) ;
657- self . print_attrs_as_inner ( self . attrs ( item. hir_id ( ) ) ) ;
658658 for item in items {
659659 self . ann . nested ( self , Nested :: ForeignItem ( item. id ) ) ;
660660 }
@@ -731,7 +731,6 @@ impl<'a> State<'a> {
731731
732732 self . space ( ) ;
733733 self . bopen ( ib) ;
734- self . print_attrs_as_inner ( attrs) ;
735734 for impl_item in items {
736735 self . ann . nested ( self , Nested :: ImplItem ( impl_item. id ) ) ;
737736 }
@@ -822,7 +821,7 @@ impl<'a> State<'a> {
822821 for v in variants {
823822 self . space_if_not_bol ( ) ;
824823 self . maybe_print_comment ( v. span . lo ( ) ) ;
825- self . print_attrs_as_outer ( self . attrs ( v. hir_id ) ) ;
824+ self . print_attrs ( self . attrs ( v. hir_id ) ) ;
826825 let ib = self . ibox ( INDENT_UNIT ) ;
827826 self . print_variant ( v) ;
828827 self . word ( "," ) ;
@@ -857,7 +856,7 @@ impl<'a> State<'a> {
857856 self . popen ( ) ;
858857 self . commasep ( Inconsistent , struct_def. fields ( ) , |s, field| {
859858 s. maybe_print_comment ( field. span . lo ( ) ) ;
860- s. print_attrs_as_outer ( s. attrs ( field. hir_id ) ) ;
859+ s. print_attrs ( s. attrs ( field. hir_id ) ) ;
861860 s. print_type ( field. ty ) ;
862861 } ) ;
863862 self . pclose ( ) ;
@@ -878,7 +877,7 @@ impl<'a> State<'a> {
878877 for field in struct_def. fields ( ) {
879878 self . hardbreak_if_not_bol ( ) ;
880879 self . maybe_print_comment ( field. span . lo ( ) ) ;
881- self . print_attrs_as_outer ( self . attrs ( field. hir_id ) ) ;
880+ self . print_attrs ( self . attrs ( field. hir_id ) ) ;
882881 self . print_ident ( field. ident ) ;
883882 self . word_nbsp ( ":" ) ;
884883 self . print_type ( field. ty ) ;
@@ -916,7 +915,7 @@ impl<'a> State<'a> {
916915 self . ann . pre ( self , AnnNode :: SubItem ( ti. hir_id ( ) ) ) ;
917916 self . hardbreak_if_not_bol ( ) ;
918917 self . maybe_print_comment ( ti. span . lo ( ) ) ;
919- self . print_attrs_as_outer ( self . attrs ( ti. hir_id ( ) ) ) ;
918+ self . print_attrs ( self . attrs ( ti. hir_id ( ) ) ) ;
920919 match ti. kind {
921920 hir:: TraitItemKind :: Const ( ty, default) => {
922921 self . print_associated_const ( ti. ident , ti. generics , ty, default) ;
@@ -944,7 +943,7 @@ impl<'a> State<'a> {
944943 self . ann . pre ( self , AnnNode :: SubItem ( ii. hir_id ( ) ) ) ;
945944 self . hardbreak_if_not_bol ( ) ;
946945 self . maybe_print_comment ( ii. span . lo ( ) ) ;
947- self . print_attrs_as_outer ( self . attrs ( ii. hir_id ( ) ) ) ;
946+ self . print_attrs ( self . attrs ( ii. hir_id ( ) ) ) ;
948947
949948 match ii. kind {
950949 hir:: ImplItemKind :: Const ( ty, expr) => {
@@ -1028,27 +1027,16 @@ impl<'a> State<'a> {
10281027 }
10291028
10301029 fn print_block ( & mut self , blk : & hir:: Block < ' _ > , cb : BoxMarker , ib : BoxMarker ) {
1031- self . print_block_with_attrs ( blk, & [ ] , cb , ib)
1030+ self . print_block_maybe_unclosed ( blk, Some ( cb ) , ib)
10321031 }
10331032
10341033 fn print_block_unclosed ( & mut self , blk : & hir:: Block < ' _ > , ib : BoxMarker ) {
1035- self . print_block_maybe_unclosed ( blk, & [ ] , None , ib)
1036- }
1037-
1038- fn print_block_with_attrs (
1039- & mut self ,
1040- blk : & hir:: Block < ' _ > ,
1041- attrs : & [ hir:: Attribute ] ,
1042- cb : BoxMarker ,
1043- ib : BoxMarker ,
1044- ) {
1045- self . print_block_maybe_unclosed ( blk, attrs, Some ( cb) , ib)
1034+ self . print_block_maybe_unclosed ( blk, None , ib)
10461035 }
10471036
10481037 fn print_block_maybe_unclosed (
10491038 & mut self ,
10501039 blk : & hir:: Block < ' _ > ,
1051- attrs : & [ hir:: Attribute ] ,
10521040 cb : Option < BoxMarker > ,
10531041 ib : BoxMarker ,
10541042 ) {
@@ -1060,8 +1048,6 @@ impl<'a> State<'a> {
10601048 self . ann . pre ( self , AnnNode :: Block ( blk) ) ;
10611049 self . bopen ( ib) ;
10621050
1063- self . print_attrs_as_inner ( attrs) ;
1064-
10651051 for st in blk. stmts {
10661052 self . print_stmt ( st) ;
10671053 }
@@ -1251,7 +1237,7 @@ impl<'a> State<'a> {
12511237
12521238 fn print_expr_field ( & mut self , field : & hir:: ExprField < ' _ > ) {
12531239 let cb = self . cbox ( INDENT_UNIT ) ;
1254- self . print_attrs_as_outer ( self . attrs ( field. hir_id ) ) ;
1240+ self . print_attrs ( self . attrs ( field. hir_id ) ) ;
12551241 if !field. is_shorthand {
12561242 self . print_ident ( field. ident ) ;
12571243 self . word_space ( ":" ) ;
@@ -1451,7 +1437,7 @@ impl<'a> State<'a> {
14511437
14521438 fn print_expr ( & mut self , expr : & hir:: Expr < ' _ > ) {
14531439 self . maybe_print_comment ( expr. span . lo ( ) ) ;
1454- self . print_attrs_as_outer ( self . attrs ( expr. hir_id ) ) ;
1440+ self . print_attrs ( self . attrs ( expr. hir_id ) ) ;
14551441 let ib = self . ibox ( INDENT_UNIT ) ;
14561442 self . ann . pre ( self , AnnNode :: Expr ( expr) ) ;
14571443 match expr. kind {
@@ -2076,7 +2062,7 @@ impl<'a> State<'a> {
20762062 self . space ( ) ;
20772063 }
20782064 let cb = self . cbox ( INDENT_UNIT ) ;
2079- self . print_attrs_as_outer ( self . attrs ( field. hir_id ) ) ;
2065+ self . print_attrs ( self . attrs ( field. hir_id ) ) ;
20802066 if !field. is_shorthand {
20812067 self . print_ident ( field. ident ) ;
20822068 self . word_nbsp ( ":" ) ;
@@ -2086,7 +2072,7 @@ impl<'a> State<'a> {
20862072 }
20872073
20882074 fn print_param ( & mut self , arg : & hir:: Param < ' _ > ) {
2089- self . print_attrs_as_outer ( self . attrs ( arg. hir_id ) ) ;
2075+ self . print_attrs ( self . attrs ( arg. hir_id ) ) ;
20902076 self . print_pat ( arg. pat ) ;
20912077 }
20922078
@@ -2121,7 +2107,7 @@ impl<'a> State<'a> {
21212107 let cb = self . cbox ( INDENT_UNIT ) ;
21222108 self . ann . pre ( self , AnnNode :: Arm ( arm) ) ;
21232109 let ib = self . ibox ( 0 ) ;
2124- self . print_attrs_as_outer ( self . attrs ( arm. hir_id ) ) ;
2110+ self . print_attrs ( self . attrs ( arm. hir_id ) ) ;
21252111 self . print_pat ( arm. pat ) ;
21262112 self . space ( ) ;
21272113 if let Some ( ref g) = arm. guard {
@@ -2409,7 +2395,7 @@ impl<'a> State<'a> {
24092395 }
24102396
24112397 fn print_where_predicate ( & mut self , predicate : & hir:: WherePredicate < ' _ > ) {
2412- self . print_attrs_as_outer ( self . attrs ( predicate. hir_id ) ) ;
2398+ self . print_attrs ( self . attrs ( predicate. hir_id ) ) ;
24132399 match * predicate. kind {
24142400 hir:: WherePredicateKind :: BoundPredicate ( hir:: WhereBoundPredicate {
24152401 bound_generic_params,
0 commit comments