5555 }
5656 }
5757}
58+
5859fn call_with_pp_support_hir < A , F > ( ppmode : & PpHirMode , tcx : TyCtxt < ' _ > , f : F ) -> A
5960where
60- F : FnOnce ( & dyn HirPrinterSupport < ' _ > , hir_map:: Map < ' _ > ) -> A ,
61+ F : FnOnce ( & dyn HirPrinterSupport , hir_map:: Map < ' _ > ) -> A ,
6162{
6263 match * ppmode {
6364 PpHirMode :: Normal => {
@@ -77,54 +78,28 @@ where
7778 }
7879}
7980
80- trait AstPrinterSupport : pprust_ast :: PpAnn {
81+ trait Sess {
8182 /// Provides a uniform interface for re-extracting a reference to a
82- /// `Session` from a value that now owns it .
83+ /// `Session`.
8384 fn sess ( & self ) -> & Session ;
84-
85- /// Produces the pretty-print annotation object.
86- ///
87- /// (Rust does not yet support upcasting from a trait object to
88- /// an object for one of its supertraits.)
89- fn pp_ann ( & self ) -> & dyn pprust_ast:: PpAnn ;
9085}
9186
92- trait HirPrinterSupport < ' hir > : pprust_hir:: PpAnn {
93- /// Provides a uniform interface for re-extracting a reference to a
94- /// `Session` from a value that now owns it.
95- fn sess ( & self ) -> & Session ;
96-
97- /// Produces the pretty-print annotation object.
98- ///
99- /// (Rust does not yet support upcasting from a trait object to
100- /// an object for one of its supertraits.)
101- fn pp_ann ( & self ) -> & dyn pprust_hir:: PpAnn ;
102- }
87+ trait AstPrinterSupport : pprust_ast:: PpAnn + Sess { }
88+ trait HirPrinterSupport : pprust_hir:: PpAnn + Sess { }
10389
10490struct NoAnn < ' hir > {
10591 sess : & ' hir Session ,
10692 tcx : Option < TyCtxt < ' hir > > ,
10793}
10894
109- impl < ' hir > AstPrinterSupport for NoAnn < ' hir > {
95+ impl < ' hir > Sess for NoAnn < ' hir > {
11096 fn sess ( & self ) -> & Session {
11197 self . sess
11298 }
113-
114- fn pp_ann ( & self ) -> & dyn pprust_ast:: PpAnn {
115- self
116- }
11799}
118100
119- impl < ' hir > HirPrinterSupport < ' hir > for NoAnn < ' hir > {
120- fn sess ( & self ) -> & Session {
121- self . sess
122- }
123-
124- fn pp_ann ( & self ) -> & dyn pprust_hir:: PpAnn {
125- self
126- }
127- }
101+ impl < ' tcx > AstPrinterSupport for NoAnn < ' tcx > { }
102+ impl < ' hir > HirPrinterSupport for NoAnn < ' hir > { }
128103
129104impl < ' hir > pprust_ast:: PpAnn for NoAnn < ' hir > { }
130105impl < ' hir > pprust_hir:: PpAnn for NoAnn < ' hir > {
@@ -140,22 +115,21 @@ struct IdentifiedAnnotation<'hir> {
140115 tcx : Option < TyCtxt < ' hir > > ,
141116}
142117
143- impl < ' hir > AstPrinterSupport for IdentifiedAnnotation < ' hir > {
118+ impl < ' hir > Sess for IdentifiedAnnotation < ' hir > {
144119 fn sess ( & self ) -> & Session {
145120 self . sess
146121 }
147-
148- fn pp_ann ( & self ) -> & dyn pprust_ast:: PpAnn {
149- self
150- }
151122}
152123
124+ impl < ' hir > AstPrinterSupport for IdentifiedAnnotation < ' hir > { }
125+
153126impl < ' hir > pprust_ast:: PpAnn for IdentifiedAnnotation < ' hir > {
154127 fn pre ( & self , s : & mut pprust_ast:: State < ' _ > , node : pprust_ast:: AnnNode < ' _ > ) {
155128 if let pprust_ast:: AnnNode :: Expr ( _) = node {
156129 s. popen ( ) ;
157130 }
158131 }
132+
159133 fn post ( & self , s : & mut pprust_ast:: State < ' _ > , node : pprust_ast:: AnnNode < ' _ > ) {
160134 match node {
161135 pprust_ast:: AnnNode :: Crate ( _)
@@ -187,27 +161,21 @@ impl<'hir> pprust_ast::PpAnn for IdentifiedAnnotation<'hir> {
187161 }
188162}
189163
190- impl < ' hir > HirPrinterSupport < ' hir > for IdentifiedAnnotation < ' hir > {
191- fn sess ( & self ) -> & Session {
192- self . sess
193- }
194-
195- fn pp_ann ( & self ) -> & dyn pprust_hir:: PpAnn {
196- self
197- }
198- }
164+ impl < ' hir > HirPrinterSupport for IdentifiedAnnotation < ' hir > { }
199165
200166impl < ' hir > pprust_hir:: PpAnn for IdentifiedAnnotation < ' hir > {
201167 fn nested ( & self , state : & mut pprust_hir:: State < ' _ > , nested : pprust_hir:: Nested ) {
202168 if let Some ( ref tcx) = self . tcx {
203169 pprust_hir:: PpAnn :: nested ( & ( & tcx. hir ( ) as & dyn hir:: intravisit:: Map < ' _ > ) , state, nested)
204170 }
205171 }
172+
206173 fn pre ( & self , s : & mut pprust_hir:: State < ' _ > , node : pprust_hir:: AnnNode < ' _ > ) {
207174 if let pprust_hir:: AnnNode :: Expr ( _) = node {
208175 s. popen ( ) ;
209176 }
210177 }
178+
211179 fn post ( & self , s : & mut pprust_hir:: State < ' _ > , node : pprust_hir:: AnnNode < ' _ > ) {
212180 match node {
213181 pprust_hir:: AnnNode :: Name ( _) => { }
@@ -244,16 +212,14 @@ struct HygieneAnnotation<'a> {
244212 sess : & ' a Session ,
245213}
246214
247- impl < ' a > AstPrinterSupport for HygieneAnnotation < ' a > {
215+ impl < ' a > Sess for HygieneAnnotation < ' a > {
248216 fn sess ( & self ) -> & Session {
249217 self . sess
250218 }
251-
252- fn pp_ann ( & self ) -> & dyn pprust_ast:: PpAnn {
253- self
254- }
255219}
256220
221+ impl < ' a > AstPrinterSupport for HygieneAnnotation < ' a > { }
222+
257223impl < ' a > pprust_ast:: PpAnn for HygieneAnnotation < ' a > {
258224 fn post ( & self , s : & mut pprust_ast:: State < ' _ > , node : pprust_ast:: AnnNode < ' _ > ) {
259225 match node {
@@ -281,16 +247,14 @@ struct TypedAnnotation<'tcx> {
281247 maybe_typeck_results : Cell < Option < & ' tcx ty:: TypeckResults < ' tcx > > > ,
282248}
283249
284- impl < ' tcx > HirPrinterSupport < ' tcx > for TypedAnnotation < ' tcx > {
250+ impl < ' tcx > Sess for TypedAnnotation < ' tcx > {
285251 fn sess ( & self ) -> & Session {
286252 self . tcx . sess
287253 }
288-
289- fn pp_ann ( & self ) -> & dyn pprust_hir:: PpAnn {
290- self
291- }
292254}
293255
256+ impl < ' tcx > HirPrinterSupport for TypedAnnotation < ' tcx > { }
257+
294258impl < ' tcx > pprust_hir:: PpAnn for TypedAnnotation < ' tcx > {
295259 fn nested ( & self , state : & mut pprust_hir:: State < ' _ > , nested : pprust_hir:: Nested ) {
296260 let old_maybe_typeck_results = self . maybe_typeck_results . get ( ) ;
@@ -301,11 +265,13 @@ impl<'tcx> pprust_hir::PpAnn for TypedAnnotation<'tcx> {
301265 pprust_hir:: PpAnn :: nested ( pp_ann, state, nested) ;
302266 self . maybe_typeck_results . set ( old_maybe_typeck_results) ;
303267 }
268+
304269 fn pre ( & self , s : & mut pprust_hir:: State < ' _ > , node : pprust_hir:: AnnNode < ' _ > ) {
305270 if let pprust_hir:: AnnNode :: Expr ( _) = node {
306271 s. popen ( ) ;
307272 }
308273 }
274+
309275 fn post ( & self , s : & mut pprust_hir:: State < ' _ > , node : pprust_hir:: AnnNode < ' _ > ) {
310276 if let pprust_hir:: AnnNode :: Expr ( expr) = node {
311277 let typeck_results = self . maybe_typeck_results . get ( ) . or_else ( || {
@@ -359,7 +325,7 @@ pub fn print_after_parsing(sess: &Session, krate: &ast::Crate, ppm: PpMode) {
359325 krate,
360326 src_name,
361327 src,
362- annotation. pp_ann ( ) ,
328+ annotation,
363329 false ,
364330 parse. edition ,
365331 & sess. parse_sess . attr_id_generator ,
@@ -396,7 +362,7 @@ pub fn print_after_hir_lowering<'tcx>(tcx: TyCtxt<'tcx>, ppm: PpMode) {
396362 & tcx. resolver_for_lowering ( ( ) ) . borrow ( ) . 1 ,
397363 src_name,
398364 src,
399- annotation. pp_ann ( ) ,
365+ annotation,
400366 true ,
401367 parse. edition ,
402368 & sess. parse_sess . attr_id_generator ,
@@ -414,14 +380,7 @@ pub fn print_after_hir_lowering<'tcx>(tcx: TyCtxt<'tcx>, ppm: PpMode) {
414380 let sess = annotation. sess ( ) ;
415381 let sm = sess. source_map ( ) ;
416382 let attrs = |id| hir_map. attrs ( id) ;
417- pprust_hir:: print_crate (
418- sm,
419- hir_map. root_module ( ) ,
420- src_name,
421- src,
422- & attrs,
423- annotation. pp_ann ( ) ,
424- )
383+ pprust_hir:: print_crate ( sm, hir_map. root_module ( ) , src_name, src, & attrs, annotation)
425384 } ) ,
426385
427386 HirTree => {
0 commit comments