@@ -4,7 +4,6 @@ use rustc_ast as ast;
44use rustc_ast_pretty:: pprust as pprust_ast;
55use rustc_hir as hir;
66use rustc_hir_pretty as pprust_hir;
7- use rustc_middle:: hir:: map as hir_map;
87use rustc_middle:: mir:: { write_mir_graphviz, write_mir_pretty} ;
98use rustc_middle:: ty:: { self , TyCtxt } ;
109use rustc_session:: config:: { OutFileName , PpHirMode , PpMode , PpSourceMode } ;
@@ -19,87 +18,10 @@ pub use self::PpMode::*;
1918pub use self :: PpSourceMode :: * ;
2019use crate :: abort_on_err;
2120
22- // This slightly awkward construction is to allow for each PpMode to
23- // choose whether it needs to do analyses (which can consume the
24- // Session) and then pass through the session (now attached to the
25- // analysis results) on to the chosen pretty-printer, along with the
26- // `&PpAnn` object.
27- //
28- // Note that since the `&AstPrinterSupport` is freshly constructed on each
29- // call, it would not make sense to try to attach the lifetime of `self`
30- // to the lifetime of the `&PrinterObject`.
31-
32- /// Constructs an `AstPrinterSupport` object and passes it to `f`.
33- fn call_with_pp_support_ast < ' tcx , A , F > (
34- ppmode : & PpSourceMode ,
35- sess : & ' tcx Session ,
36- tcx : Option < TyCtxt < ' tcx > > ,
37- f : F ,
38- ) -> A
39- where
40- F : FnOnce ( & dyn AstPrinterSupport ) -> A ,
41- {
42- match * ppmode {
43- Normal | Expanded => {
44- let annotation = NoAnn { sess, tcx } ;
45- f ( & annotation)
46- }
47- Identified | ExpandedIdentified => {
48- let annotation = IdentifiedAnnotation { sess, tcx } ;
49- f ( & annotation)
50- }
51- ExpandedHygiene => {
52- let annotation = HygieneAnnotation { sess } ;
53- f ( & annotation)
54- }
55- }
56- }
57-
58- fn call_with_pp_support_hir < A , F > ( ppmode : & PpHirMode , tcx : TyCtxt < ' _ > , f : F ) -> A
59- where
60- F : FnOnce ( & dyn HirPrinterSupport , hir_map:: Map < ' _ > ) -> A ,
61- {
62- match * ppmode {
63- PpHirMode :: Normal => {
64- let annotation = NoAnn { sess : tcx. sess , tcx : Some ( tcx) } ;
65- f ( & annotation, tcx. hir ( ) )
66- }
67- PpHirMode :: Identified => {
68- let annotation = IdentifiedAnnotation { sess : tcx. sess , tcx : Some ( tcx) } ;
69- f ( & annotation, tcx. hir ( ) )
70- }
71- PpHirMode :: Typed => {
72- abort_on_err ( tcx. analysis ( ( ) ) , tcx. sess ) ;
73-
74- let annotation = TypedAnnotation { tcx, maybe_typeck_results : Cell :: new ( None ) } ;
75- tcx. dep_graph . with_ignore ( || f ( & annotation, tcx. hir ( ) ) )
76- }
77- }
78- }
79-
80- trait Sess {
81- /// Provides a uniform interface for re-extracting a reference to a
82- /// `Session`.
83- fn sess ( & self ) -> & Session ;
84- }
85-
86- trait AstPrinterSupport : pprust_ast:: PpAnn + Sess { }
87- trait HirPrinterSupport : pprust_hir:: PpAnn + Sess { }
88-
8921struct NoAnn < ' hir > {
90- sess : & ' hir Session ,
9122 tcx : Option < TyCtxt < ' hir > > ,
9223}
9324
94- impl < ' hir > Sess for NoAnn < ' hir > {
95- fn sess ( & self ) -> & Session {
96- self . sess
97- }
98- }
99-
100- impl < ' tcx > AstPrinterSupport for NoAnn < ' tcx > { }
101- impl < ' hir > HirPrinterSupport for NoAnn < ' hir > { }
102-
10325impl < ' hir > pprust_ast:: PpAnn for NoAnn < ' hir > { }
10426impl < ' hir > pprust_hir:: PpAnn for NoAnn < ' hir > {
10527 fn nested ( & self , state : & mut pprust_hir:: State < ' _ > , nested : pprust_hir:: Nested ) {
@@ -110,18 +32,9 @@ impl<'hir> pprust_hir::PpAnn for NoAnn<'hir> {
11032}
11133
11234struct IdentifiedAnnotation < ' hir > {
113- sess : & ' hir Session ,
11435 tcx : Option < TyCtxt < ' hir > > ,
11536}
11637
117- impl < ' hir > Sess for IdentifiedAnnotation < ' hir > {
118- fn sess ( & self ) -> & Session {
119- self . sess
120- }
121- }
122-
123- impl < ' hir > AstPrinterSupport for IdentifiedAnnotation < ' hir > { }
124-
12538impl < ' hir > pprust_ast:: PpAnn for IdentifiedAnnotation < ' hir > {
12639 fn pre ( & self , s : & mut pprust_ast:: State < ' _ > , node : pprust_ast:: AnnNode < ' _ > ) {
12740 if let pprust_ast:: AnnNode :: Expr ( _) = node {
@@ -160,8 +73,6 @@ impl<'hir> pprust_ast::PpAnn for IdentifiedAnnotation<'hir> {
16073 }
16174}
16275
163- impl < ' hir > HirPrinterSupport for IdentifiedAnnotation < ' hir > { }
164-
16576impl < ' hir > pprust_hir:: PpAnn for IdentifiedAnnotation < ' hir > {
16677 fn nested ( & self , state : & mut pprust_hir:: State < ' _ > , nested : pprust_hir:: Nested ) {
16778 if let Some ( ref tcx) = self . tcx {
@@ -211,14 +122,6 @@ struct HygieneAnnotation<'a> {
211122 sess : & ' a Session ,
212123}
213124
214- impl < ' a > Sess for HygieneAnnotation < ' a > {
215- fn sess ( & self ) -> & Session {
216- self . sess
217- }
218- }
219-
220- impl < ' a > AstPrinterSupport for HygieneAnnotation < ' a > { }
221-
222125impl < ' a > pprust_ast:: PpAnn for HygieneAnnotation < ' a > {
223126 fn post ( & self , s : & mut pprust_ast:: State < ' _ > , node : pprust_ast:: AnnNode < ' _ > ) {
224127 match node {
@@ -246,14 +149,6 @@ struct TypedAnnotation<'tcx> {
246149 maybe_typeck_results : Cell < Option < & ' tcx ty:: TypeckResults < ' tcx > > > ,
247150}
248151
249- impl < ' tcx > Sess for TypedAnnotation < ' tcx > {
250- fn sess ( & self ) -> & Session {
251- self . tcx . sess
252- }
253- }
254-
255- impl < ' tcx > HirPrinterSupport for TypedAnnotation < ' tcx > { }
256-
257152impl < ' tcx > pprust_hir:: PpAnn for TypedAnnotation < ' tcx > {
258153 fn nested ( & self , state : & mut pprust_hir:: State < ' _ > , nested : pprust_hir:: Nested ) {
259154 let old_maybe_typeck_results = self . maybe_typeck_results . get ( ) ;
@@ -319,7 +214,7 @@ pub enum PrintExtra<'tcx> {
319214impl < ' tcx > PrintExtra < ' tcx > {
320215 fn with_krate < F , R > ( & self , f : F ) -> R
321216 where
322- F : FnOnce ( & ast:: Crate ) -> R
217+ F : FnOnce ( & ast:: Crate ) -> R ,
323218 {
324219 match self {
325220 PrintExtra :: AfterParsing { krate, .. } => f ( krate) ,
@@ -344,23 +239,26 @@ pub fn print<'tcx>(sess: &Session, ppm: PpMode, ex: PrintExtra<'tcx>) {
344239
345240 let out = match ppm {
346241 Source ( s) => {
347- // Silently ignores an identified node.
348- call_with_pp_support_ast ( & s, sess, None , move |annotation| {
349- debug ! ( "pretty printing source code {:?}" , s) ;
350- let sess = annotation. sess ( ) ;
351- let parse = & sess. parse_sess ;
352- let is_expanded = ppm. needs_ast_map ( ) ;
353- ex. with_krate ( |krate|
354- pprust_ast:: print_crate (
355- sess. source_map ( ) ,
356- krate,
357- src_name,
358- src,
359- annotation,
360- is_expanded,
361- parse. edition ,
362- & sess. parse_sess . attr_id_generator ,
363- )
242+ debug ! ( "pretty printing source code {:?}" , s) ;
243+ let annotation: Box < dyn pprust_ast:: PpAnn > = match s {
244+ Normal => Box :: new ( NoAnn { tcx : None } ) ,
245+ Expanded => Box :: new ( NoAnn { tcx : Some ( ex. tcx ( ) ) } ) ,
246+ Identified => Box :: new ( IdentifiedAnnotation { tcx : None } ) ,
247+ ExpandedIdentified => Box :: new ( IdentifiedAnnotation { tcx : Some ( ex. tcx ( ) ) } ) ,
248+ ExpandedHygiene => Box :: new ( HygieneAnnotation { sess } ) ,
249+ } ;
250+ let parse = & sess. parse_sess ;
251+ let is_expanded = ppm. needs_ast_map ( ) ;
252+ ex. with_krate ( |krate| {
253+ pprust_ast:: print_crate (
254+ sess. source_map ( ) ,
255+ krate,
256+ src_name,
257+ src,
258+ & * annotation,
259+ is_expanded,
260+ parse. edition ,
261+ & sess. parse_sess . attr_id_generator ,
364262 )
365263 } )
366264 }
@@ -372,13 +270,38 @@ pub fn print<'tcx>(sess: &Session, ppm: PpMode, ex: PrintExtra<'tcx>) {
372270 debug ! ( "pretty-printing expanded AST" ) ;
373271 format ! ( "{:#?}" , ex. tcx( ) . resolver_for_lowering( ( ) ) . borrow( ) . 1 )
374272 }
375- Hir ( s) => call_with_pp_support_hir ( & s , ex . tcx ( ) , move |annotation , hir_map| {
273+ Hir ( s) => {
376274 debug ! ( "pretty printing HIR {:?}" , s) ;
377- let sess = annotation. sess ( ) ;
378- let sm = sess. source_map ( ) ;
379- let attrs = |id| hir_map. attrs ( id) ;
380- pprust_hir:: print_crate ( sm, hir_map. root_module ( ) , src_name, src, & attrs, annotation)
381- } ) ,
275+ let tcx = ex. tcx ( ) ;
276+ let f = |annotation : & dyn pprust_hir:: PpAnn | {
277+ let sm = sess. source_map ( ) ;
278+ let hir_map = tcx. hir ( ) ;
279+ let attrs = |id| hir_map. attrs ( id) ;
280+ pprust_hir:: print_crate (
281+ sm,
282+ hir_map. root_module ( ) ,
283+ src_name,
284+ src,
285+ & attrs,
286+ annotation,
287+ )
288+ } ;
289+ match s {
290+ PpHirMode :: Normal => {
291+ let annotation = NoAnn { tcx : Some ( tcx) } ;
292+ f ( & annotation)
293+ }
294+ PpHirMode :: Identified => {
295+ let annotation = IdentifiedAnnotation { tcx : Some ( tcx) } ;
296+ f ( & annotation)
297+ }
298+ PpHirMode :: Typed => {
299+ abort_on_err ( tcx. analysis ( ( ) ) , tcx. sess ) ;
300+ let annotation = TypedAnnotation { tcx, maybe_typeck_results : Cell :: new ( None ) } ;
301+ tcx. dep_graph . with_ignore ( || f ( & annotation) )
302+ }
303+ }
304+ }
382305 HirTree => {
383306 debug ! ( "pretty printing HIR tree" ) ;
384307 format ! ( "{:#?}" , ex. tcx( ) . hir( ) . krate( ) )
0 commit comments