@@ -10,7 +10,7 @@ use std::vec;
10
10
11
11
use rustc_abi:: ExternAbi ;
12
12
use rustc_ast:: util:: parser:: { self , ExprPrecedence , Fixity } ;
13
- use rustc_ast:: { AttrStyle , DUMMY_NODE_ID , DelimArgs } ;
13
+ use rustc_ast:: { DUMMY_NODE_ID , DelimArgs } ;
14
14
use rustc_ast_pretty:: pp:: Breaks :: { Consistent , Inconsistent } ;
15
15
use rustc_ast_pretty:: pp:: { self , BoxMarker , Breaks } ;
16
16
use rustc_ast_pretty:: pprust:: state:: MacHeader ;
@@ -81,32 +81,24 @@ impl<'a> State<'a> {
81
81
}
82
82
83
83
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)
96
86
}
97
87
98
- fn print_either_attributes ( & mut self , attrs : & [ hir:: Attribute ] , style : ast :: AttrStyle ) {
88
+ fn print_attrs ( & mut self , attrs : & [ hir:: Attribute ] ) {
99
89
if attrs. is_empty ( ) {
100
90
return ;
101
91
}
102
92
103
93
for attr in attrs {
104
- self . print_attribute_inline ( attr, style ) ;
94
+ self . print_attribute_as_style ( attr, ast :: AttrStyle :: Outer ) ;
105
95
}
106
96
self . hardbreak_if_not_bol ( ) ;
107
97
}
108
98
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 ) {
110
102
match & attr {
111
103
hir:: Attribute :: Unparsed ( unparsed) => {
112
104
self . maybe_print_comment ( unparsed. span . lo ( ) ) ;
@@ -118,14 +110,17 @@ impl<'a> State<'a> {
118
110
self . word ( "]" ) ;
119
111
self . hardbreak ( )
120
112
}
121
- hir:: Attribute :: Parsed ( AttributeKind :: DocComment { style , kind, comment, .. } ) => {
113
+ hir:: Attribute :: Parsed ( AttributeKind :: DocComment { kind, comment, .. } ) => {
122
114
self . word ( rustc_ast_pretty:: pprust:: state:: doc_comment_to_string (
123
- * kind, * style, * comment,
115
+ * kind, style, * comment,
124
116
) ) ;
125
117
self . hardbreak ( )
126
118
}
127
119
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
+ }
129
124
pa. print_attribute ( self ) ;
130
125
self . word ( "]" ) ;
131
126
self . hardbreak ( )
@@ -281,10 +276,17 @@ pub fn print_crate<'a>(
281
276
ann,
282
277
} ;
283
278
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
+
284
286
// When printing the AST, we sometimes need to inject `#[no_std]` here.
285
287
// Since you can't compile the HIR, it's not necessary.
286
288
287
- s. print_mod ( krate, ( * attrs ) ( hir :: CRATE_HIR_ID ) ) ;
289
+ s. print_mod ( krate) ;
288
290
s. print_remaining_comments ( ) ;
289
291
s. s . eof ( )
290
292
}
@@ -299,7 +301,7 @@ where
299
301
}
300
302
301
303
pub 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 ) )
303
305
}
304
306
305
307
pub fn ty_to_string ( ann : & dyn PpAnn , ty : & hir:: Ty < ' _ > ) -> String {
@@ -361,8 +363,7 @@ impl<'a> State<'a> {
361
363
self . commasep_cmnt ( b, exprs, |s, e| s. print_expr ( e) , |e| e. span ) ;
362
364
}
363
365
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 < ' _ > ) {
366
367
for & item_id in _mod. item_ids {
367
368
self . ann . nested ( self , Nested :: Item ( item_id) ) ;
368
369
}
@@ -479,7 +480,7 @@ impl<'a> State<'a> {
479
480
fn print_foreign_item ( & mut self , item : & hir:: ForeignItem < ' _ > ) {
480
481
self . hardbreak_if_not_bol ( ) ;
481
482
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 ( ) ) ) ;
483
484
match item. kind {
484
485
hir:: ForeignItemKind :: Fn ( sig, arg_idents, generics) => {
485
486
let ( cb, ib) = self . head ( "" ) ;
@@ -565,7 +566,7 @@ impl<'a> State<'a> {
565
566
self . hardbreak_if_not_bol ( ) ;
566
567
self . maybe_print_comment ( item. span . lo ( ) ) ;
567
568
let attrs = self . attrs ( item. hir_id ( ) ) ;
568
- self . print_attrs_as_outer ( attrs) ;
569
+ self . print_attrs ( attrs) ;
569
570
self . ann . pre ( self , AnnNode :: Item ( item) ) ;
570
571
match item. kind {
571
572
hir:: ItemKind :: ExternCrate ( orig_name, ident) => {
@@ -647,14 +648,13 @@ impl<'a> State<'a> {
647
648
self . print_ident ( ident) ;
648
649
self . nbsp ( ) ;
649
650
self . bopen ( ib) ;
650
- self . print_mod ( mod_, attrs ) ;
651
+ self . print_mod ( mod_) ;
651
652
self . bclose ( item. span , cb) ;
652
653
}
653
654
hir:: ItemKind :: ForeignMod { abi, items } => {
654
655
let ( cb, ib) = self . head ( "extern" ) ;
655
656
self . word_nbsp ( abi. to_string ( ) ) ;
656
657
self . bopen ( ib) ;
657
- self . print_attrs_as_inner ( self . attrs ( item. hir_id ( ) ) ) ;
658
658
for item in items {
659
659
self . ann . nested ( self , Nested :: ForeignItem ( item. id ) ) ;
660
660
}
@@ -731,7 +731,6 @@ impl<'a> State<'a> {
731
731
732
732
self . space ( ) ;
733
733
self . bopen ( ib) ;
734
- self . print_attrs_as_inner ( attrs) ;
735
734
for impl_item in items {
736
735
self . ann . nested ( self , Nested :: ImplItem ( impl_item. id ) ) ;
737
736
}
@@ -822,7 +821,7 @@ impl<'a> State<'a> {
822
821
for v in variants {
823
822
self . space_if_not_bol ( ) ;
824
823
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 ) ) ;
826
825
let ib = self . ibox ( INDENT_UNIT ) ;
827
826
self . print_variant ( v) ;
828
827
self . word ( "," ) ;
@@ -857,7 +856,7 @@ impl<'a> State<'a> {
857
856
self . popen ( ) ;
858
857
self . commasep ( Inconsistent , struct_def. fields ( ) , |s, field| {
859
858
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 ) ) ;
861
860
s. print_type ( field. ty ) ;
862
861
} ) ;
863
862
self . pclose ( ) ;
@@ -878,7 +877,7 @@ impl<'a> State<'a> {
878
877
for field in struct_def. fields ( ) {
879
878
self . hardbreak_if_not_bol ( ) ;
880
879
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 ) ) ;
882
881
self . print_ident ( field. ident ) ;
883
882
self . word_nbsp ( ":" ) ;
884
883
self . print_type ( field. ty ) ;
@@ -916,7 +915,7 @@ impl<'a> State<'a> {
916
915
self . ann . pre ( self , AnnNode :: SubItem ( ti. hir_id ( ) ) ) ;
917
916
self . hardbreak_if_not_bol ( ) ;
918
917
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 ( ) ) ) ;
920
919
match ti. kind {
921
920
hir:: TraitItemKind :: Const ( ty, default) => {
922
921
self . print_associated_const ( ti. ident , ti. generics , ty, default) ;
@@ -944,7 +943,7 @@ impl<'a> State<'a> {
944
943
self . ann . pre ( self , AnnNode :: SubItem ( ii. hir_id ( ) ) ) ;
945
944
self . hardbreak_if_not_bol ( ) ;
946
945
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 ( ) ) ) ;
948
947
949
948
match ii. kind {
950
949
hir:: ImplItemKind :: Const ( ty, expr) => {
@@ -1028,27 +1027,16 @@ impl<'a> State<'a> {
1028
1027
}
1029
1028
1030
1029
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)
1032
1031
}
1033
1032
1034
1033
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)
1046
1035
}
1047
1036
1048
1037
fn print_block_maybe_unclosed (
1049
1038
& mut self ,
1050
1039
blk : & hir:: Block < ' _ > ,
1051
- attrs : & [ hir:: Attribute ] ,
1052
1040
cb : Option < BoxMarker > ,
1053
1041
ib : BoxMarker ,
1054
1042
) {
@@ -1060,8 +1048,6 @@ impl<'a> State<'a> {
1060
1048
self . ann . pre ( self , AnnNode :: Block ( blk) ) ;
1061
1049
self . bopen ( ib) ;
1062
1050
1063
- self . print_attrs_as_inner ( attrs) ;
1064
-
1065
1051
for st in blk. stmts {
1066
1052
self . print_stmt ( st) ;
1067
1053
}
@@ -1251,7 +1237,7 @@ impl<'a> State<'a> {
1251
1237
1252
1238
fn print_expr_field ( & mut self , field : & hir:: ExprField < ' _ > ) {
1253
1239
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 ) ) ;
1255
1241
if !field. is_shorthand {
1256
1242
self . print_ident ( field. ident ) ;
1257
1243
self . word_space ( ":" ) ;
@@ -1451,7 +1437,7 @@ impl<'a> State<'a> {
1451
1437
1452
1438
fn print_expr ( & mut self , expr : & hir:: Expr < ' _ > ) {
1453
1439
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 ) ) ;
1455
1441
let ib = self . ibox ( INDENT_UNIT ) ;
1456
1442
self . ann . pre ( self , AnnNode :: Expr ( expr) ) ;
1457
1443
match expr. kind {
@@ -2076,7 +2062,7 @@ impl<'a> State<'a> {
2076
2062
self . space ( ) ;
2077
2063
}
2078
2064
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 ) ) ;
2080
2066
if !field. is_shorthand {
2081
2067
self . print_ident ( field. ident ) ;
2082
2068
self . word_nbsp ( ":" ) ;
@@ -2086,7 +2072,7 @@ impl<'a> State<'a> {
2086
2072
}
2087
2073
2088
2074
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 ) ) ;
2090
2076
self . print_pat ( arg. pat ) ;
2091
2077
}
2092
2078
@@ -2121,7 +2107,7 @@ impl<'a> State<'a> {
2121
2107
let cb = self . cbox ( INDENT_UNIT ) ;
2122
2108
self . ann . pre ( self , AnnNode :: Arm ( arm) ) ;
2123
2109
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 ) ) ;
2125
2111
self . print_pat ( arm. pat ) ;
2126
2112
self . space ( ) ;
2127
2113
if let Some ( ref g) = arm. guard {
@@ -2409,7 +2395,7 @@ impl<'a> State<'a> {
2409
2395
}
2410
2396
2411
2397
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 ) ) ;
2413
2399
match * predicate. kind {
2414
2400
hir:: WherePredicateKind :: BoundPredicate ( hir:: WhereBoundPredicate {
2415
2401
bound_generic_params,
0 commit comments