Skip to content

Commit 1b46b77

Browse files
authored
Unrolled build for #144780
Rollup merge of #144780 - bjorn3:mir_build_debug, r=matthewjasper Add a method to dump MIR in the middle of MIR building This makes it easier to debug issues with MIR building by inserting dump_for_debugging calls around the suspected code responsible for the bad MIR.
2 parents 6ba0ce4 + 695473a commit 1b46b77

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

compiler/rustc_mir_build/src/builder/coverageinfo.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,21 @@ impl CoverageInfoBuilder {
157157
// if there's nothing interesting in it.
158158
Box::new(CoverageInfoHi { num_block_markers, branch_spans })
159159
}
160+
161+
pub(crate) fn as_done(&self) -> Box<CoverageInfoHi> {
162+
let &Self { nots: _, markers: BlockMarkerGen { num_block_markers }, ref branch_info } =
163+
self;
164+
165+
let branch_spans = branch_info
166+
.as_ref()
167+
.map(|branch_info| branch_info.branch_spans.as_slice())
168+
.unwrap_or_default()
169+
.to_owned();
170+
171+
// For simplicity, always return an info struct (without Option), even
172+
// if there's nothing interesting in it.
173+
Box::new(CoverageInfoHi { num_block_markers, branch_spans })
174+
}
160175
}
161176

162177
impl<'tcx> Builder<'_, 'tcx> {

compiler/rustc_mir_build/src/builder/mod.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -790,6 +790,28 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
790790
builder
791791
}
792792

793+
#[allow(dead_code)]
794+
fn dump_for_debugging(&self) {
795+
let mut body = Body::new(
796+
MirSource::item(self.def_id.to_def_id()),
797+
self.cfg.basic_blocks.clone(),
798+
self.source_scopes.clone(),
799+
self.local_decls.clone(),
800+
self.canonical_user_type_annotations.clone(),
801+
self.arg_count.clone(),
802+
self.var_debug_info.clone(),
803+
self.fn_span.clone(),
804+
self.coroutine.clone(),
805+
None,
806+
);
807+
body.coverage_info_hi = self.coverage_info.as_ref().map(|b| b.as_done());
808+
809+
use rustc_middle::mir::pretty;
810+
let options = pretty::PrettyPrintMirOptions::from_cli(self.tcx);
811+
pretty::write_mir_fn(self.tcx, &body, &mut |_, _| Ok(()), &mut std::io::stdout(), options)
812+
.unwrap();
813+
}
814+
793815
fn finish(self) -> Body<'tcx> {
794816
let mut body = Body::new(
795817
MirSource::item(self.def_id.to_def_id()),

0 commit comments

Comments
 (0)