22
33use rustc_ast as ast;
44use rustc_ast_pretty:: pprust as pprust_ast;
5- use rustc_errors:: ErrorGuaranteed ;
65use rustc_hir as hir;
76use rustc_hir_pretty as pprust_hir;
7+ use rustc_middle:: bug;
88use rustc_middle:: hir:: map as hir_map;
99use rustc_middle:: mir:: { write_mir_graphviz, write_mir_pretty} ;
1010use rustc_middle:: ty:: { self , TyCtxt } ;
@@ -310,106 +310,92 @@ fn write_or_print(out: &str, sess: &Session) {
310310 sess. io . output_file . as_ref ( ) . unwrap_or ( & OutFileName :: Stdout ) . overwrite ( out, sess) ;
311311}
312312
313- pub fn print_after_parsing ( sess : & Session , krate : & ast:: Crate , ppm : PpMode ) {
314- let ( src, src_name) = get_source ( sess) ;
313+ // Extra data for pretty-printing, the form of which depends on what kind of
314+ // pretty-printing we are doing.
315+ pub enum PrintExtra < ' tcx > {
316+ AfterParsing { krate : ast:: Crate } ,
317+ NeedsAstMap { tcx : TyCtxt < ' tcx > } ,
318+ }
315319
316- let out = match ppm {
317- Source ( s) => {
318- // Silently ignores an identified node.
319- call_with_pp_support_ast ( & s, sess, None , move |annotation| {
320- debug ! ( "pretty printing source code {:?}" , s) ;
321- let sess = annotation. sess ( ) ;
322- let parse = & sess. parse_sess ;
323- pprust_ast:: print_crate (
324- sess. source_map ( ) ,
325- krate,
326- src_name,
327- src,
328- annotation,
329- false ,
330- parse. edition ,
331- & sess. parse_sess . attr_id_generator ,
332- )
333- } )
320+ impl < ' tcx > PrintExtra < ' tcx > {
321+ fn with_krate < F , R > ( & self , f : F ) -> R
322+ where
323+ F : FnOnce ( & ast:: Crate ) -> R
324+ {
325+ match self {
326+ PrintExtra :: AfterParsing { krate, .. } => f ( krate) ,
327+ PrintExtra :: NeedsAstMap { tcx } => f ( & tcx. resolver_for_lowering ( ( ) ) . borrow ( ) . 1 ) ,
334328 }
335- AstTree => {
336- debug ! ( "pretty printing AST tree" ) ;
337- format ! ( "{krate:#?}" )
338- }
339- _ => unreachable ! ( ) ,
340- } ;
329+ }
341330
342- write_or_print ( & out, sess) ;
331+ fn tcx ( & self ) -> TyCtxt < ' tcx > {
332+ match self {
333+ PrintExtra :: AfterParsing { .. } => bug ! ( "PrintExtra::tcx" ) ,
334+ PrintExtra :: NeedsAstMap { tcx } => * tcx,
335+ }
336+ }
343337}
344338
345- pub fn print_after_hir_lowering < ' tcx > ( tcx : TyCtxt < ' tcx > , ppm : PpMode ) {
339+ pub fn print < ' tcx > ( sess : & Session , ppm : PpMode , ex : PrintExtra < ' tcx > ) {
346340 if ppm. needs_analysis ( ) {
347- abort_on_err ( print_with_analysis ( tcx, ppm) , tcx. sess ) ;
348- return ;
341+ abort_on_err ( ex. tcx ( ) . analysis ( ( ) ) , sess) ;
349342 }
350343
351- let ( src, src_name) = get_source ( tcx . sess ) ;
344+ let ( src, src_name) = get_source ( sess) ;
352345
353346 let out = match ppm {
354347 Source ( s) => {
355348 // Silently ignores an identified node.
356- call_with_pp_support_ast ( & s, tcx . sess , Some ( tcx ) , move |annotation| {
349+ call_with_pp_support_ast ( & s, sess, None , move |annotation| {
357350 debug ! ( "pretty printing source code {:?}" , s) ;
358351 let sess = annotation. sess ( ) ;
359352 let parse = & sess. parse_sess ;
360- pprust_ast:: print_crate (
361- sess. source_map ( ) ,
362- & tcx. resolver_for_lowering ( ( ) ) . borrow ( ) . 1 ,
363- src_name,
364- src,
365- annotation,
366- true ,
367- parse. edition ,
368- & sess. parse_sess . attr_id_generator ,
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+ )
369365 )
370366 } )
371367 }
372-
368+ AstTree => {
369+ debug ! ( "pretty printing AST tree" ) ;
370+ ex. with_krate ( |krate| format ! ( "{krate:#?}" ) )
371+ }
373372 AstTreeExpanded => {
374373 debug ! ( "pretty-printing expanded AST" ) ;
375- format ! ( "{:#?}" , tcx. resolver_for_lowering( ( ) ) . borrow( ) . 1 )
374+ format ! ( "{:#?}" , ex . tcx( ) . resolver_for_lowering( ( ) ) . borrow( ) . 1 )
376375 }
377-
378- Hir ( s) => call_with_pp_support_hir ( & s, tcx, move |annotation, hir_map| {
376+ Hir ( s) => call_with_pp_support_hir ( & s, ex. tcx ( ) , move |annotation, hir_map| {
379377 debug ! ( "pretty printing HIR {:?}" , s) ;
380378 let sess = annotation. sess ( ) ;
381379 let sm = sess. source_map ( ) ;
382380 let attrs = |id| hir_map. attrs ( id) ;
383381 pprust_hir:: print_crate ( sm, hir_map. root_module ( ) , src_name, src, & attrs, annotation)
384382 } ) ,
385-
386383 HirTree => {
387384 debug ! ( "pretty printing HIR tree" ) ;
388- format ! ( "{:#?}" , tcx. hir( ) . krate( ) )
385+ format ! ( "{:#?}" , ex . tcx( ) . hir( ) . krate( ) )
389386 }
390-
391- _ => unreachable ! ( ) ,
392- } ;
393-
394- write_or_print ( & out, tcx. sess ) ;
395- }
396-
397- fn print_with_analysis ( tcx : TyCtxt < ' _ > , ppm : PpMode ) -> Result < ( ) , ErrorGuaranteed > {
398- tcx. analysis ( ( ) ) ?;
399- let out = match ppm {
400387 Mir => {
401388 let mut out = Vec :: new ( ) ;
402- write_mir_pretty ( tcx, None , & mut out) . unwrap ( ) ;
389+ write_mir_pretty ( ex . tcx ( ) , None , & mut out) . unwrap ( ) ;
403390 String :: from_utf8 ( out) . unwrap ( )
404391 }
405-
406392 MirCFG => {
407393 let mut out = Vec :: new ( ) ;
408- write_mir_graphviz ( tcx, None , & mut out) . unwrap ( ) ;
394+ write_mir_graphviz ( ex . tcx ( ) , None , & mut out) . unwrap ( ) ;
409395 String :: from_utf8 ( out) . unwrap ( )
410396 }
411-
412397 ThirTree => {
398+ let tcx = ex. tcx ( ) ;
413399 let mut out = String :: new ( ) ;
414400 abort_on_err ( rustc_hir_analysis:: check_crate ( tcx) , tcx. sess ) ;
415401 debug ! ( "pretty printing THIR tree" ) ;
@@ -418,8 +404,8 @@ fn print_with_analysis(tcx: TyCtxt<'_>, ppm: PpMode) -> Result<(), ErrorGuarante
418404 }
419405 out
420406 }
421-
422407 ThirFlat => {
408+ let tcx = ex. tcx ( ) ;
423409 let mut out = String :: new ( ) ;
424410 abort_on_err ( rustc_hir_analysis:: check_crate ( tcx) , tcx. sess ) ;
425411 debug ! ( "pretty printing THIR flat" ) ;
@@ -428,11 +414,7 @@ fn print_with_analysis(tcx: TyCtxt<'_>, ppm: PpMode) -> Result<(), ErrorGuarante
428414 }
429415 out
430416 }
431-
432- _ => unreachable ! ( ) ,
433417 } ;
434418
435- write_or_print ( & out, tcx. sess ) ;
436-
437- Ok ( ( ) )
419+ write_or_print ( & out, sess) ;
438420}
0 commit comments