Skip to content

Commit a5c07b1

Browse files
committed
Merge print_* functions.
The handling of the `PpMode` variants is currently spread across three functions: `print_after_parsing`, `print_after_hir_lowering`, and `print_with_analysis`. Each one handles some of the variants. This split is primarily because `print_after_parsing` has slightly different arguments to the other two. This commit changes the structure. It merges the three functions into a single `print` function, and encapsulates the different arguments in a new enum `PrintExtra`. Benefits: - The code is a little shorter. - All the `PpMode` variants are handled in a single `match`, with no need for `unreachable!` arms. - It enables the trait removal in the subsequent commit by reducing the number of `call_with_pp_support_ast` call sites from two to one.
1 parent 7bcb9af commit a5c07b1

File tree

2 files changed

+51
-70
lines changed

2 files changed

+51
-70
lines changed

compiler/rustc_driver_impl/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ fn run_compiler(
393393
if ppm.needs_ast_map() {
394394
queries.global_ctxt()?.enter(|tcx| {
395395
tcx.ensure().early_lint_checks(());
396-
pretty::print_after_hir_lowering(tcx, *ppm);
396+
pretty::print(sess, *ppm, pretty::PrintExtra::NeedsAstMap { tcx });
397397
Ok(())
398398
})?;
399399

@@ -402,7 +402,7 @@ fn run_compiler(
402402
queries.global_ctxt()?.enter(|tcx| tcx.output_filenames(()));
403403
} else {
404404
let krate = queries.parse()?.steal();
405-
pretty::print_after_parsing(sess, &krate, *ppm);
405+
pretty::print(sess, *ppm, pretty::PrintExtra::AfterParsing { krate });
406406
}
407407
trace!("finished pretty-printing");
408408
return early_exit();

compiler/rustc_driver_impl/src/pretty.rs

Lines changed: 49 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
33
use rustc_ast as ast;
44
use rustc_ast_pretty::pprust as pprust_ast;
5-
use rustc_errors::ErrorGuaranteed;
65
use rustc_hir as hir;
76
use rustc_hir_pretty as pprust_hir;
87
use rustc_middle::hir::map as hir_map;
@@ -310,106 +309,92 @@ fn write_or_print(out: &str, sess: &Session) {
310309
sess.io.output_file.as_ref().unwrap_or(&OutFileName::Stdout).overwrite(out, sess);
311310
}
312311

313-
pub fn print_after_parsing(sess: &Session, krate: &ast::Crate, ppm: PpMode) {
314-
let (src, src_name) = get_source(sess);
312+
// Extra data for pretty-printing, the form of which depends on what kind of
313+
// pretty-printing we are doing.
314+
pub enum PrintExtra<'tcx> {
315+
AfterParsing { krate: ast::Crate },
316+
NeedsAstMap { tcx: TyCtxt<'tcx> },
317+
}
315318

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-
})
319+
impl<'tcx> PrintExtra<'tcx> {
320+
fn with_krate<F, R>(&self, f: F) -> R
321+
where
322+
F: FnOnce(&ast::Crate) -> R
323+
{
324+
match self {
325+
PrintExtra::AfterParsing { krate, .. } => f(krate),
326+
PrintExtra::NeedsAstMap { tcx } => f(&tcx.resolver_for_lowering(()).borrow().1),
334327
}
335-
AstTree => {
336-
debug!("pretty printing AST tree");
337-
format!("{krate:#?}")
338-
}
339-
_ => unreachable!(),
340-
};
328+
}
341329

342-
write_or_print(&out, sess);
330+
fn tcx(&self) -> TyCtxt<'tcx> {
331+
match self {
332+
PrintExtra::AfterParsing { .. } => panic!("PrintExtra::tcx"),
333+
PrintExtra::NeedsAstMap { tcx } => *tcx,
334+
}
335+
}
343336
}
344337

345-
pub fn print_after_hir_lowering<'tcx>(tcx: TyCtxt<'tcx>, ppm: PpMode) {
338+
pub fn print<'tcx>(sess: &Session, ppm: PpMode, ex: PrintExtra<'tcx>) {
346339
if ppm.needs_analysis() {
347-
abort_on_err(print_with_analysis(tcx, ppm), tcx.sess);
348-
return;
340+
abort_on_err(ex.tcx().analysis(()), sess);
349341
}
350342

351-
let (src, src_name) = get_source(tcx.sess);
343+
let (src, src_name) = get_source(sess);
352344

353345
let out = match ppm {
354346
Source(s) => {
355347
// Silently ignores an identified node.
356-
call_with_pp_support_ast(&s, tcx.sess, Some(tcx), move |annotation| {
348+
call_with_pp_support_ast(&s, sess, None, move |annotation| {
357349
debug!("pretty printing source code {:?}", s);
358350
let sess = annotation.sess();
359351
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,
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+
)
369364
)
370365
})
371366
}
372-
367+
AstTree => {
368+
debug!("pretty printing AST tree");
369+
ex.with_krate(|krate| format!("{krate:#?}"))
370+
}
373371
AstTreeExpanded => {
374372
debug!("pretty-printing expanded AST");
375-
format!("{:#?}", tcx.resolver_for_lowering(()).borrow().1)
373+
format!("{:#?}", ex.tcx().resolver_for_lowering(()).borrow().1)
376374
}
377-
378-
Hir(s) => call_with_pp_support_hir(&s, tcx, move |annotation, hir_map| {
375+
Hir(s) => call_with_pp_support_hir(&s, ex.tcx(), move |annotation, hir_map| {
379376
debug!("pretty printing HIR {:?}", s);
380377
let sess = annotation.sess();
381378
let sm = sess.source_map();
382379
let attrs = |id| hir_map.attrs(id);
383380
pprust_hir::print_crate(sm, hir_map.root_module(), src_name, src, &attrs, annotation)
384381
}),
385-
386382
HirTree => {
387383
debug!("pretty printing HIR tree");
388-
format!("{:#?}", tcx.hir().krate())
384+
format!("{:#?}", ex.tcx().hir().krate())
389385
}
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 {
400386
Mir => {
401387
let mut out = Vec::new();
402-
write_mir_pretty(tcx, None, &mut out).unwrap();
388+
write_mir_pretty(ex.tcx(), None, &mut out).unwrap();
403389
String::from_utf8(out).unwrap()
404390
}
405-
406391
MirCFG => {
407392
let mut out = Vec::new();
408-
write_mir_graphviz(tcx, None, &mut out).unwrap();
393+
write_mir_graphviz(ex.tcx(), None, &mut out).unwrap();
409394
String::from_utf8(out).unwrap()
410395
}
411-
412396
ThirTree => {
397+
let tcx = ex.tcx();
413398
let mut out = String::new();
414399
abort_on_err(rustc_hir_analysis::check_crate(tcx), tcx.sess);
415400
debug!("pretty printing THIR tree");
@@ -418,8 +403,8 @@ fn print_with_analysis(tcx: TyCtxt<'_>, ppm: PpMode) -> Result<(), ErrorGuarante
418403
}
419404
out
420405
}
421-
422406
ThirFlat => {
407+
let tcx = ex.tcx();
423408
let mut out = String::new();
424409
abort_on_err(rustc_hir_analysis::check_crate(tcx), tcx.sess);
425410
debug!("pretty printing THIR flat");
@@ -428,11 +413,7 @@ fn print_with_analysis(tcx: TyCtxt<'_>, ppm: PpMode) -> Result<(), ErrorGuarante
428413
}
429414
out
430415
}
431-
432-
_ => unreachable!(),
433416
};
434417

435-
write_or_print(&out, tcx.sess);
436-
437-
Ok(())
418+
write_or_print(&out, sess);
438419
}

0 commit comments

Comments
 (0)