Skip to content

Commit 2ec959f

Browse files
committed
extend MIR dump with detailed, extra information
1 parent e5dc4ba commit 2ec959f

File tree

2 files changed

+104
-24
lines changed

2 files changed

+104
-24
lines changed

src/librustc_mir/util/pretty.rs

Lines changed: 96 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
use rustc::hir;
1212
use rustc::hir::def_id::{DefId, LOCAL_CRATE};
1313
use rustc::mir::*;
14-
use rustc::ty::TyCtxt;
14+
use rustc::mir::visit::Visitor;
15+
use rustc::ty::{self, TyCtxt};
1516
use rustc::ty::item_path;
1617
use rustc_data_structures::fx::FxHashMap;
1718
use rustc_data_structures::indexed_vec::Idx;
@@ -125,14 +126,7 @@ fn dump_matched_mir_node<'a, 'gcx, 'tcx, F>(
125126
F: FnMut(PassWhere, &mut Write) -> io::Result<()>,
126127
{
127128
let _: io::Result<()> = do catch {
128-
let mut file = create_dump_file(
129-
tcx,
130-
"mir",
131-
pass_num,
132-
pass_name,
133-
disambiguator,
134-
source,
135-
)?;
129+
let mut file = create_dump_file(tcx, "mir", pass_num, pass_name, disambiguator, source)?;
136130
writeln!(file, "// MIR for `{}`", node_path)?;
137131
writeln!(file, "// source = {:?}", source)?;
138132
writeln!(file, "// pass_name = {}", pass_name)?;
@@ -148,15 +142,9 @@ fn dump_matched_mir_node<'a, 'gcx, 'tcx, F>(
148142
};
149143

150144
if tcx.sess.opts.debugging_opts.dump_mir_graphviz {
151-
let _: io::Result<()> = do catch {
152-
let mut file = create_dump_file(
153-
tcx,
154-
"dot",
155-
pass_num,
156-
pass_name,
157-
disambiguator,
158-
source,
159-
)?;
145+
let _: io::Result<()> = do catch {
146+
let mut file =
147+
create_dump_file(tcx, "dot", pass_num, pass_name, disambiguator, source)?;
160148
write_mir_fn_graphviz(tcx, source.def_id, mir, &mut file)?;
161149
Ok(())
162150
};
@@ -297,10 +285,10 @@ where
297285
}
298286

299287
/// Write out a human-readable textual representation for the given basic block.
300-
pub fn write_basic_block<F>(
301-
tcx: TyCtxt,
288+
pub fn write_basic_block<'cx, 'gcx, 'tcx, F>(
289+
tcx: TyCtxt<'cx, 'gcx, 'tcx>,
302290
block: BasicBlock,
303-
mir: &Mir,
291+
mir: &Mir<'tcx>,
304292
extra_data: &mut F,
305293
w: &mut Write,
306294
) -> io::Result<()>
@@ -330,6 +318,11 @@ where
330318
comment(tcx, statement.source_info),
331319
A = ALIGN,
332320
)?;
321+
322+
write_extra(tcx, w, |visitor| {
323+
visitor.visit_statement(current_location.block, statement, current_location);
324+
})?;
325+
333326
extra_data(PassWhere::AfterLocation(current_location), w)?;
334327

335328
current_location.statement_index += 1;
@@ -346,11 +339,93 @@ where
346339
comment(tcx, data.terminator().source_info),
347340
A = ALIGN,
348341
)?;
342+
343+
write_extra(tcx, w, |visitor| {
344+
visitor.visit_terminator(current_location.block, data.terminator(), current_location);
345+
})?;
346+
349347
extra_data(PassWhere::AfterLocation(current_location), w)?;
350348

351349
writeln!(w, "{}}}", INDENT)
352350
}
353351

352+
/// After we print the main statement, we sometimes dump extra
353+
/// information. There's often a lot of little things "nuzzled up" in
354+
/// a statement.
355+
fn write_extra<'cx, 'gcx, 'tcx, F>(
356+
tcx: TyCtxt<'cx, 'gcx, 'tcx>,
357+
write: &mut Write,
358+
mut visit_op: F,
359+
) -> io::Result<()>
360+
where F: FnMut(&mut ExtraComments<'cx, 'gcx, 'tcx>)
361+
{
362+
let mut extra_comments = ExtraComments {
363+
_tcx: tcx,
364+
comments: vec![],
365+
};
366+
visit_op(&mut extra_comments);
367+
for comment in extra_comments.comments {
368+
writeln!(write, "{:A$} // {}", "", comment, A = ALIGN)?;
369+
}
370+
Ok(())
371+
}
372+
373+
struct ExtraComments<'cx, 'gcx: 'tcx, 'tcx: 'cx> {
374+
_tcx: TyCtxt<'cx, 'gcx, 'tcx>, // don't need it now, but bet we will soon
375+
comments: Vec<String>,
376+
}
377+
378+
impl<'cx, 'gcx, 'tcx> ExtraComments<'cx, 'gcx, 'tcx> {
379+
fn push(&mut self, lines: &str) {
380+
for line in lines.split("\n") {
381+
self.comments.push(line.to_string());
382+
}
383+
}
384+
}
385+
386+
impl<'cx, 'gcx, 'tcx> Visitor<'tcx> for ExtraComments<'cx, 'gcx, 'tcx> {
387+
fn visit_constant(&mut self, constant: &Constant<'tcx>, location: Location) {
388+
self.super_constant(constant, location);
389+
let Constant { span, ty, literal } = constant;
390+
self.push(&format!("mir::Constant"));
391+
self.push(&format!("└ span: {:?}", span));
392+
self.push(&format!("└ ty: {:?}", ty));
393+
self.push(&format!("└ literal: {:?}", literal));
394+
}
395+
396+
fn visit_const(&mut self, constant: &&'tcx ty::Const<'tcx>, _: Location) {
397+
self.super_const(constant);
398+
let ty::Const { ty, val } = constant;
399+
self.push(&format!("ty::Const"));
400+
self.push(&format!("└ ty: {:?}", ty));
401+
self.push(&format!("└ val: {:?}", val));
402+
}
403+
404+
fn visit_rvalue(&mut self, rvalue: &Rvalue<'tcx>, location: Location) {
405+
self.super_rvalue(rvalue, location);
406+
match rvalue {
407+
Rvalue::Aggregate(kind, _) => match **kind {
408+
AggregateKind::Closure(def_id, substs) => {
409+
self.push(&format!("closure"));
410+
self.push(&format!("└ def_id: {:?}", def_id));
411+
self.push(&format!("└ substs: {:#?}", substs));
412+
}
413+
414+
AggregateKind::Generator(def_id, substs, interior) => {
415+
self.push(&format!("generator"));
416+
self.push(&format!("└ def_id: {:?}", def_id));
417+
self.push(&format!("└ substs: {:#?}", substs));
418+
self.push(&format!("└ interior: {:?}", interior));
419+
}
420+
421+
_ => {}
422+
},
423+
424+
_ => {}
425+
}
426+
}
427+
}
428+
354429
fn comment(tcx: TyCtxt, SourceInfo { span, scope }: SourceInfo) -> String {
355430
format!(
356431
"scope {} at {}",

src/tools/compiletest/src/runtest.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2535,7 +2535,10 @@ impl<'test> TestCx<'test> {
25352535
let mut dumped_file = fs::File::open(output_file.clone()).unwrap();
25362536
let mut dumped_string = String::new();
25372537
dumped_file.read_to_string(&mut dumped_string).unwrap();
2538-
let mut dumped_lines = dumped_string.lines().filter(|l| !l.is_empty());
2538+
let mut dumped_lines = dumped_string
2539+
.lines()
2540+
.map(|l| nocomment_mir_line(l))
2541+
.filter(|l| !l.is_empty());
25392542
let mut expected_lines = expected_content
25402543
.iter()
25412544
.filter(|&l| {
@@ -2573,7 +2576,7 @@ impl<'test> TestCx<'test> {
25732576
.join("\n");
25742577
panic!(
25752578
"Did not find expected line, error: {}\n\
2576-
Actual Line: {:?}\n\
2579+
Expected Line: {:?}\n\
25772580
Expected:\n{}\n\
25782581
Actual:\n{}",
25792582
extra_msg,
@@ -2599,7 +2602,9 @@ impl<'test> TestCx<'test> {
25992602
error(
26002603
expected_line,
26012604
format!(
2602-
"Mismatch in lines\nCurrnt block: {}\nExpected Line: {:?}",
2605+
"Mismatch in lines\n\
2606+
Current block: {}\n\
2607+
Actual Line: {:?}",
26032608
start_block_line.unwrap_or("None"),
26042609
dumped_line
26052610
),

0 commit comments

Comments
 (0)