@@ -82,6 +82,7 @@ impl PpAnn for &dyn rustc_hir::intravisit::Map<'_> {
8282pub struct State < ' a > {
8383 pub s : pp:: Printer ,
8484 comments : Option < Comments < ' a > > ,
85+ attrs : & ' a hir:: HirIdVec < & ' a [ ast:: Attribute ] > ,
8586 ann : & ' a ( dyn PpAnn + ' a ) ,
8687}
8788
@@ -163,7 +164,7 @@ pub fn print_crate<'a>(
163164 input : String ,
164165 ann : & ' a dyn PpAnn ,
165166) -> String {
166- let mut s = State :: new_from_input ( sm, filename, input, ann) ;
167+ let mut s = State :: new_from_input ( sm, filename, input, & krate . attrs , ann) ;
167168
168169 // When printing the AST, we sometimes need to inject `#[no_std]` here.
169170 // Since you can't compile the HIR, it's not necessary.
@@ -178,17 +179,28 @@ impl<'a> State<'a> {
178179 sm : & ' a SourceMap ,
179180 filename : FileName ,
180181 input : String ,
182+ attrs : & ' a hir:: HirIdVec < & [ ast:: Attribute ] > ,
181183 ann : & ' a dyn PpAnn ,
182184 ) -> State < ' a > {
183- State { s : pp:: mk_printer ( ) , comments : Some ( Comments :: new ( sm, filename, input) ) , ann }
185+ State {
186+ s : pp:: mk_printer ( ) ,
187+ comments : Some ( Comments :: new ( sm, filename, input) ) ,
188+ attrs,
189+ ann,
190+ }
191+ }
192+
193+ fn attrs ( & self , id : hir:: HirId ) -> & ' a [ ast:: Attribute ] {
194+ self . attrs . get ( id) . map_or ( & [ ] , |la| * la)
184195 }
185196}
186197
187198pub fn to_string < F > ( ann : & dyn PpAnn , f : F ) -> String
188199where
189200 F : FnOnce ( & mut State < ' _ > ) ,
190201{
191- let mut printer = State { s : pp:: mk_printer ( ) , comments : None , ann } ;
202+ let mut printer =
203+ State { s : pp:: mk_printer ( ) , comments : None , attrs : & hir:: HirIdVec :: default ( ) , ann } ;
192204 f ( & mut printer) ;
193205 printer. s . eof ( )
194206}
@@ -2027,13 +2039,13 @@ impl<'a> State<'a> {
20272039 pub fn print_arm ( & mut self , arm : & hir:: Arm < ' _ > ) {
20282040 // I have no idea why this check is necessary, but here it
20292041 // is :(
2030- if arm . attrs . is_empty ( ) {
2042+ if self . attrs ( arm . hir_id ) . is_empty ( ) {
20312043 self . s . space ( ) ;
20322044 }
20332045 self . cbox ( INDENT_UNIT ) ;
20342046 self . ann . pre ( self , AnnNode :: Arm ( arm) ) ;
20352047 self . ibox ( 0 ) ;
2036- self . print_outer_attributes ( & arm . attrs ) ;
2048+ self . print_outer_attributes ( & self . attrs ( arm . hir_id ) ) ;
20372049 self . print_pat ( & arm. pat ) ;
20382050 self . s . space ( ) ;
20392051 if let Some ( ref g) = arm. guard {
0 commit comments