Skip to content

Commit 7603385

Browse files
committed
Show actual MIR when MIR building forgot to terminate block
This makes it significantly easier to debug bugs of this kind.
1 parent 9fa9ef3 commit 7603385

File tree

2 files changed

+41
-28
lines changed

2 files changed

+41
-28
lines changed

compiler/rustc_middle/src/mir/pretty.rs

+24-22
Original file line numberDiff line numberDiff line change
@@ -762,33 +762,35 @@ where
762762

763763
// Terminator at the bottom.
764764
extra_data(PassWhere::BeforeLocation(current_location), w)?;
765-
let indented_terminator = format!("{0}{0}{1:?};", INDENT, data.terminator().kind);
766-
if options.include_extra_comments {
767-
writeln!(
765+
if data.terminator.is_some() {
766+
let indented_terminator = format!("{0}{0}{1:?};", INDENT, data.terminator().kind);
767+
if options.include_extra_comments {
768+
writeln!(
769+
w,
770+
"{:A$} // {}{}",
771+
indented_terminator,
772+
if tcx.sess.verbose_internals() {
773+
format!("{current_location:?}: ")
774+
} else {
775+
String::new()
776+
},
777+
comment(tcx, data.terminator().source_info),
778+
A = ALIGN,
779+
)?;
780+
} else {
781+
writeln!(w, "{indented_terminator}")?;
782+
}
783+
784+
write_extra(
785+
tcx,
768786
w,
769-
"{:A$} // {}{}",
770-
indented_terminator,
771-
if tcx.sess.verbose_internals() {
772-
format!("{current_location:?}: ")
773-
} else {
774-
String::new()
787+
|visitor| {
788+
visitor.visit_terminator(data.terminator(), current_location);
775789
},
776-
comment(tcx, data.terminator().source_info),
777-
A = ALIGN,
790+
options,
778791
)?;
779-
} else {
780-
writeln!(w, "{indented_terminator}")?;
781792
}
782793

783-
write_extra(
784-
tcx,
785-
w,
786-
|visitor| {
787-
visitor.visit_terminator(data.terminator(), current_location);
788-
},
789-
options,
790-
)?;
791-
792794
extra_data(PassWhere::AfterLocation(current_location), w)?;
793795
extra_data(PassWhere::AfterTerminator(block), w)?;
794796

compiler/rustc_mir_build/src/build/mod.rs

+17-6
Original file line numberDiff line numberDiff line change
@@ -791,12 +791,6 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
791791
}
792792

793793
fn finish(self) -> Body<'tcx> {
794-
for (index, block) in self.cfg.basic_blocks.iter().enumerate() {
795-
if block.terminator.is_none() {
796-
span_bug!(self.fn_span, "no terminator on block {:?}", index);
797-
}
798-
}
799-
800794
let mut body = Body::new(
801795
MirSource::item(self.def_id.to_def_id()),
802796
self.cfg.basic_blocks,
@@ -810,6 +804,23 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
810804
None,
811805
);
812806
body.coverage_info_hi = self.coverage_info.map(|b| b.into_done());
807+
808+
for (index, block) in body.basic_blocks.iter().enumerate() {
809+
if block.terminator.is_none() {
810+
use rustc_middle::mir::pretty;
811+
let options = pretty::PrettyPrintMirOptions::from_cli(self.tcx);
812+
pretty::write_mir_fn(
813+
self.tcx,
814+
&body,
815+
&mut |_, _| Ok(()),
816+
&mut std::io::stdout(),
817+
options,
818+
)
819+
.unwrap();
820+
span_bug!(self.fn_span, "no terminator on block {:?}", index);
821+
}
822+
}
823+
813824
body
814825
}
815826

0 commit comments

Comments
 (0)