@@ -5,7 +5,6 @@ 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
7
use rustc_middle:: bug;
8
- use rustc_middle:: hir:: map as hir_map;
9
8
use rustc_middle:: mir:: { write_mir_graphviz, write_mir_pretty} ;
10
9
use rustc_middle:: ty:: { self , TyCtxt } ;
11
10
use rustc_session:: config:: { OutFileName , PpHirMode , PpMode , PpSourceMode } ;
@@ -20,87 +19,10 @@ pub use self::PpMode::*;
20
19
pub use self :: PpSourceMode :: * ;
21
20
use crate :: abort_on_err;
22
21
23
- // This slightly awkward construction is to allow for each PpMode to
24
- // choose whether it needs to do analyses (which can consume the
25
- // Session) and then pass through the session (now attached to the
26
- // analysis results) on to the chosen pretty-printer, along with the
27
- // `&PpAnn` object.
28
- //
29
- // Note that since the `&AstPrinterSupport` is freshly constructed on each
30
- // call, it would not make sense to try to attach the lifetime of `self`
31
- // to the lifetime of the `&PrinterObject`.
32
-
33
- /// Constructs an `AstPrinterSupport` object and passes it to `f`.
34
- fn call_with_pp_support_ast < ' tcx , A , F > (
35
- ppmode : & PpSourceMode ,
36
- sess : & ' tcx Session ,
37
- tcx : Option < TyCtxt < ' tcx > > ,
38
- f : F ,
39
- ) -> A
40
- where
41
- F : FnOnce ( & dyn AstPrinterSupport ) -> A ,
42
- {
43
- match * ppmode {
44
- Normal | Expanded => {
45
- let annotation = NoAnn { sess, tcx } ;
46
- f ( & annotation)
47
- }
48
- Identified | ExpandedIdentified => {
49
- let annotation = IdentifiedAnnotation { sess, tcx } ;
50
- f ( & annotation)
51
- }
52
- ExpandedHygiene => {
53
- let annotation = HygieneAnnotation { sess } ;
54
- f ( & annotation)
55
- }
56
- }
57
- }
58
-
59
- fn call_with_pp_support_hir < A , F > ( ppmode : & PpHirMode , tcx : TyCtxt < ' _ > , f : F ) -> A
60
- where
61
- F : FnOnce ( & dyn HirPrinterSupport , hir_map:: Map < ' _ > ) -> A ,
62
- {
63
- match * ppmode {
64
- PpHirMode :: Normal => {
65
- let annotation = NoAnn { sess : tcx. sess , tcx : Some ( tcx) } ;
66
- f ( & annotation, tcx. hir ( ) )
67
- }
68
- PpHirMode :: Identified => {
69
- let annotation = IdentifiedAnnotation { sess : tcx. sess , tcx : Some ( tcx) } ;
70
- f ( & annotation, tcx. hir ( ) )
71
- }
72
- PpHirMode :: Typed => {
73
- abort_on_err ( tcx. analysis ( ( ) ) , tcx. sess ) ;
74
-
75
- let annotation = TypedAnnotation { tcx, maybe_typeck_results : Cell :: new ( None ) } ;
76
- tcx. dep_graph . with_ignore ( || f ( & annotation, tcx. hir ( ) ) )
77
- }
78
- }
79
- }
80
-
81
- trait Sess {
82
- /// Provides a uniform interface for re-extracting a reference to a
83
- /// `Session`.
84
- fn sess ( & self ) -> & Session ;
85
- }
86
-
87
- trait AstPrinterSupport : pprust_ast:: PpAnn + Sess { }
88
- trait HirPrinterSupport : pprust_hir:: PpAnn + Sess { }
89
-
90
22
struct NoAnn < ' hir > {
91
- sess : & ' hir Session ,
92
23
tcx : Option < TyCtxt < ' hir > > ,
93
24
}
94
25
95
- impl < ' hir > Sess for NoAnn < ' hir > {
96
- fn sess ( & self ) -> & Session {
97
- self . sess
98
- }
99
- }
100
-
101
- impl < ' tcx > AstPrinterSupport for NoAnn < ' tcx > { }
102
- impl < ' hir > HirPrinterSupport for NoAnn < ' hir > { }
103
-
104
26
impl < ' hir > pprust_ast:: PpAnn for NoAnn < ' hir > { }
105
27
impl < ' hir > pprust_hir:: PpAnn for NoAnn < ' hir > {
106
28
fn nested ( & self , state : & mut pprust_hir:: State < ' _ > , nested : pprust_hir:: Nested ) {
@@ -111,18 +33,9 @@ impl<'hir> pprust_hir::PpAnn for NoAnn<'hir> {
111
33
}
112
34
113
35
struct IdentifiedAnnotation < ' hir > {
114
- sess : & ' hir Session ,
115
36
tcx : Option < TyCtxt < ' hir > > ,
116
37
}
117
38
118
- impl < ' hir > Sess for IdentifiedAnnotation < ' hir > {
119
- fn sess ( & self ) -> & Session {
120
- self . sess
121
- }
122
- }
123
-
124
- impl < ' hir > AstPrinterSupport for IdentifiedAnnotation < ' hir > { }
125
-
126
39
impl < ' hir > pprust_ast:: PpAnn for IdentifiedAnnotation < ' hir > {
127
40
fn pre ( & self , s : & mut pprust_ast:: State < ' _ > , node : pprust_ast:: AnnNode < ' _ > ) {
128
41
if let pprust_ast:: AnnNode :: Expr ( _) = node {
@@ -161,8 +74,6 @@ impl<'hir> pprust_ast::PpAnn for IdentifiedAnnotation<'hir> {
161
74
}
162
75
}
163
76
164
- impl < ' hir > HirPrinterSupport for IdentifiedAnnotation < ' hir > { }
165
-
166
77
impl < ' hir > pprust_hir:: PpAnn for IdentifiedAnnotation < ' hir > {
167
78
fn nested ( & self , state : & mut pprust_hir:: State < ' _ > , nested : pprust_hir:: Nested ) {
168
79
if let Some ( ref tcx) = self . tcx {
@@ -212,14 +123,6 @@ struct HygieneAnnotation<'a> {
212
123
sess : & ' a Session ,
213
124
}
214
125
215
- impl < ' a > Sess for HygieneAnnotation < ' a > {
216
- fn sess ( & self ) -> & Session {
217
- self . sess
218
- }
219
- }
220
-
221
- impl < ' a > AstPrinterSupport for HygieneAnnotation < ' a > { }
222
-
223
126
impl < ' a > pprust_ast:: PpAnn for HygieneAnnotation < ' a > {
224
127
fn post ( & self , s : & mut pprust_ast:: State < ' _ > , node : pprust_ast:: AnnNode < ' _ > ) {
225
128
match node {
@@ -247,14 +150,6 @@ struct TypedAnnotation<'tcx> {
247
150
maybe_typeck_results : Cell < Option < & ' tcx ty:: TypeckResults < ' tcx > > > ,
248
151
}
249
152
250
- impl < ' tcx > Sess for TypedAnnotation < ' tcx > {
251
- fn sess ( & self ) -> & Session {
252
- self . tcx . sess
253
- }
254
- }
255
-
256
- impl < ' tcx > HirPrinterSupport for TypedAnnotation < ' tcx > { }
257
-
258
153
impl < ' tcx > pprust_hir:: PpAnn for TypedAnnotation < ' tcx > {
259
154
fn nested ( & self , state : & mut pprust_hir:: State < ' _ > , nested : pprust_hir:: Nested ) {
260
155
let old_maybe_typeck_results = self . maybe_typeck_results . get ( ) ;
@@ -320,7 +215,7 @@ pub enum PrintExtra<'tcx> {
320
215
impl < ' tcx > PrintExtra < ' tcx > {
321
216
fn with_krate < F , R > ( & self , f : F ) -> R
322
217
where
323
- F : FnOnce ( & ast:: Crate ) -> R
218
+ F : FnOnce ( & ast:: Crate ) -> R ,
324
219
{
325
220
match self {
326
221
PrintExtra :: AfterParsing { krate, .. } => f ( krate) ,
@@ -345,23 +240,26 @@ pub fn print<'tcx>(sess: &Session, ppm: PpMode, ex: PrintExtra<'tcx>) {
345
240
346
241
let out = match ppm {
347
242
Source ( s) => {
348
- // Silently ignores an identified node.
349
- call_with_pp_support_ast ( & s, sess, None , move |annotation| {
350
- debug ! ( "pretty printing source code {:?}" , s) ;
351
- let sess = annotation. sess ( ) ;
352
- let parse = & sess. parse_sess ;
353
- let is_expanded = ppm. needs_ast_map ( ) ;
354
- ex. with_krate ( |krate|
355
- pprust_ast:: print_crate (
356
- sess. source_map ( ) ,
357
- krate,
358
- src_name,
359
- src,
360
- annotation,
361
- is_expanded,
362
- parse. edition ,
363
- & sess. parse_sess . attr_id_generator ,
364
- )
243
+ debug ! ( "pretty printing source code {:?}" , s) ;
244
+ let annotation: Box < dyn pprust_ast:: PpAnn > = match s {
245
+ Normal => Box :: new ( NoAnn { tcx : None } ) ,
246
+ Expanded => Box :: new ( NoAnn { tcx : Some ( ex. tcx ( ) ) } ) ,
247
+ Identified => Box :: new ( IdentifiedAnnotation { tcx : None } ) ,
248
+ ExpandedIdentified => Box :: new ( IdentifiedAnnotation { tcx : Some ( ex. tcx ( ) ) } ) ,
249
+ ExpandedHygiene => Box :: new ( HygieneAnnotation { sess } ) ,
250
+ } ;
251
+ let parse = & sess. parse_sess ;
252
+ let is_expanded = ppm. needs_ast_map ( ) ;
253
+ ex. with_krate ( |krate| {
254
+ pprust_ast:: print_crate (
255
+ sess. source_map ( ) ,
256
+ krate,
257
+ src_name,
258
+ src,
259
+ & * annotation,
260
+ is_expanded,
261
+ parse. edition ,
262
+ & sess. parse_sess . attr_id_generator ,
365
263
)
366
264
} )
367
265
}
@@ -373,13 +271,38 @@ pub fn print<'tcx>(sess: &Session, ppm: PpMode, ex: PrintExtra<'tcx>) {
373
271
debug ! ( "pretty-printing expanded AST" ) ;
374
272
format ! ( "{:#?}" , ex. tcx( ) . resolver_for_lowering( ( ) ) . borrow( ) . 1 )
375
273
}
376
- Hir ( s) => call_with_pp_support_hir ( & s , ex . tcx ( ) , move |annotation , hir_map| {
274
+ Hir ( s) => {
377
275
debug ! ( "pretty printing HIR {:?}" , s) ;
378
- let sess = annotation. sess ( ) ;
379
- let sm = sess. source_map ( ) ;
380
- let attrs = |id| hir_map. attrs ( id) ;
381
- pprust_hir:: print_crate ( sm, hir_map. root_module ( ) , src_name, src, & attrs, annotation)
382
- } ) ,
276
+ let tcx = ex. tcx ( ) ;
277
+ let f = |annotation : & dyn pprust_hir:: PpAnn | {
278
+ let sm = sess. source_map ( ) ;
279
+ let hir_map = tcx. hir ( ) ;
280
+ let attrs = |id| hir_map. attrs ( id) ;
281
+ pprust_hir:: print_crate (
282
+ sm,
283
+ hir_map. root_module ( ) ,
284
+ src_name,
285
+ src,
286
+ & attrs,
287
+ annotation,
288
+ )
289
+ } ;
290
+ match s {
291
+ PpHirMode :: Normal => {
292
+ let annotation = NoAnn { tcx : Some ( tcx) } ;
293
+ f ( & annotation)
294
+ }
295
+ PpHirMode :: Identified => {
296
+ let annotation = IdentifiedAnnotation { tcx : Some ( tcx) } ;
297
+ f ( & annotation)
298
+ }
299
+ PpHirMode :: Typed => {
300
+ abort_on_err ( tcx. analysis ( ( ) ) , tcx. sess ) ;
301
+ let annotation = TypedAnnotation { tcx, maybe_typeck_results : Cell :: new ( None ) } ;
302
+ tcx. dep_graph . with_ignore ( || f ( & annotation) )
303
+ }
304
+ }
305
+ }
383
306
HirTree => {
384
307
debug ! ( "pretty printing HIR tree" ) ;
385
308
format ! ( "{:#?}" , ex. tcx( ) . hir( ) . krate( ) )
0 commit comments