@@ -4,7 +4,6 @@ use rustc_ast as ast;
4
4
use rustc_ast_pretty:: pprust as pprust_ast;
5
5
use rustc_hir as hir;
6
6
use rustc_hir_pretty as pprust_hir;
7
- use rustc_middle:: hir:: map as hir_map;
8
7
use rustc_middle:: mir:: { write_mir_graphviz, write_mir_pretty} ;
9
8
use rustc_middle:: ty:: { self , TyCtxt } ;
10
9
use rustc_session:: config:: { OutFileName , PpHirMode , PpMode , PpSourceMode } ;
@@ -19,87 +18,10 @@ pub use self::PpMode::*;
19
18
pub use self :: PpSourceMode :: * ;
20
19
use crate :: abort_on_err;
21
20
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
-
89
21
struct NoAnn < ' hir > {
90
- sess : & ' hir Session ,
91
22
tcx : Option < TyCtxt < ' hir > > ,
92
23
}
93
24
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
-
103
25
impl < ' hir > pprust_ast:: PpAnn for NoAnn < ' hir > { }
104
26
impl < ' hir > pprust_hir:: PpAnn for NoAnn < ' hir > {
105
27
fn nested ( & self , state : & mut pprust_hir:: State < ' _ > , nested : pprust_hir:: Nested ) {
@@ -110,18 +32,9 @@ impl<'hir> pprust_hir::PpAnn for NoAnn<'hir> {
110
32
}
111
33
112
34
struct IdentifiedAnnotation < ' hir > {
113
- sess : & ' hir Session ,
114
35
tcx : Option < TyCtxt < ' hir > > ,
115
36
}
116
37
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
-
125
38
impl < ' hir > pprust_ast:: PpAnn for IdentifiedAnnotation < ' hir > {
126
39
fn pre ( & self , s : & mut pprust_ast:: State < ' _ > , node : pprust_ast:: AnnNode < ' _ > ) {
127
40
if let pprust_ast:: AnnNode :: Expr ( _) = node {
@@ -160,8 +73,6 @@ impl<'hir> pprust_ast::PpAnn for IdentifiedAnnotation<'hir> {
160
73
}
161
74
}
162
75
163
- impl < ' hir > HirPrinterSupport for IdentifiedAnnotation < ' hir > { }
164
-
165
76
impl < ' hir > pprust_hir:: PpAnn for IdentifiedAnnotation < ' hir > {
166
77
fn nested ( & self , state : & mut pprust_hir:: State < ' _ > , nested : pprust_hir:: Nested ) {
167
78
if let Some ( ref tcx) = self . tcx {
@@ -211,14 +122,6 @@ struct HygieneAnnotation<'a> {
211
122
sess : & ' a Session ,
212
123
}
213
124
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
-
222
125
impl < ' a > pprust_ast:: PpAnn for HygieneAnnotation < ' a > {
223
126
fn post ( & self , s : & mut pprust_ast:: State < ' _ > , node : pprust_ast:: AnnNode < ' _ > ) {
224
127
match node {
@@ -246,14 +149,6 @@ struct TypedAnnotation<'tcx> {
246
149
maybe_typeck_results : Cell < Option < & ' tcx ty:: TypeckResults < ' tcx > > > ,
247
150
}
248
151
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
-
257
152
impl < ' tcx > pprust_hir:: PpAnn for TypedAnnotation < ' tcx > {
258
153
fn nested ( & self , state : & mut pprust_hir:: State < ' _ > , nested : pprust_hir:: Nested ) {
259
154
let old_maybe_typeck_results = self . maybe_typeck_results . get ( ) ;
@@ -319,7 +214,7 @@ pub enum PrintExtra<'tcx> {
319
214
impl < ' tcx > PrintExtra < ' tcx > {
320
215
fn with_krate < F , R > ( & self , f : F ) -> R
321
216
where
322
- F : FnOnce ( & ast:: Crate ) -> R
217
+ F : FnOnce ( & ast:: Crate ) -> R ,
323
218
{
324
219
match self {
325
220
PrintExtra :: AfterParsing { krate, .. } => f ( krate) ,
@@ -344,23 +239,26 @@ pub fn print<'tcx>(sess: &Session, ppm: PpMode, ex: PrintExtra<'tcx>) {
344
239
345
240
let out = match ppm {
346
241
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 ,
364
262
)
365
263
} )
366
264
}
@@ -372,13 +270,38 @@ pub fn print<'tcx>(sess: &Session, ppm: PpMode, ex: PrintExtra<'tcx>) {
372
270
debug ! ( "pretty-printing expanded AST" ) ;
373
271
format ! ( "{:#?}" , ex. tcx( ) . resolver_for_lowering( ( ) ) . borrow( ) . 1 )
374
272
}
375
- Hir ( s) => call_with_pp_support_hir ( & s , ex . tcx ( ) , move |annotation , hir_map| {
273
+ Hir ( s) => {
376
274
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
+ }
382
305
HirTree => {
383
306
debug ! ( "pretty printing HIR tree" ) ;
384
307
format ! ( "{:#?}" , ex. tcx( ) . hir( ) . krate( ) )
0 commit comments