Rollup merge of #132450 - bjorn3:better_mir_errors, r=jieyouxu

Show actual MIR when MIR building forgot to terminate block

This makes it significantly easier to debug bugs of this kind.
This commit is contained in:
Guillaume Gomez 2024-11-02 03:08:54 +08:00 committed by GitHub
commit 7bfdc3d898
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 44 additions and 31 deletions

View File

@ -762,32 +762,34 @@ fn write_basic_block<'tcx, F>(
// Terminator at the bottom.
extra_data(PassWhere::BeforeLocation(current_location), w)?;
let indented_terminator = format!("{0}{0}{1:?};", INDENT, data.terminator().kind);
if options.include_extra_comments {
writeln!(
w,
"{:A$} // {}{}",
indented_terminator,
if tcx.sess.verbose_internals() {
format!("{current_location:?}: ")
} else {
String::new()
},
comment(tcx, data.terminator().source_info),
A = ALIGN,
)?;
} else {
writeln!(w, "{indented_terminator}")?;
}
if data.terminator.is_some() {
let indented_terminator = format!("{0}{0}{1:?};", INDENT, data.terminator().kind);
if options.include_extra_comments {
writeln!(
w,
"{:A$} // {}{}",
indented_terminator,
if tcx.sess.verbose_internals() {
format!("{current_location:?}: ")
} else {
String::new()
},
comment(tcx, data.terminator().source_info),
A = ALIGN,
)?;
} else {
writeln!(w, "{indented_terminator}")?;
}
write_extra(
tcx,
w,
|visitor| {
visitor.visit_terminator(data.terminator(), current_location);
},
options,
)?;
write_extra(
tcx,
w,
|visitor| {
visitor.visit_terminator(data.terminator(), current_location);
},
options,
)?;
}
extra_data(PassWhere::AfterLocation(current_location), w)?;
extra_data(PassWhere::AfterTerminator(block), w)?;

View File

@ -791,12 +791,6 @@ fn new(
}
fn finish(self) -> Body<'tcx> {
for (index, block) in self.cfg.basic_blocks.iter().enumerate() {
if block.terminator.is_none() {
span_bug!(self.fn_span, "no terminator on block {:?}", index);
}
}
let mut body = Body::new(
MirSource::item(self.def_id.to_def_id()),
self.cfg.basic_blocks,
@ -810,6 +804,23 @@ fn finish(self) -> Body<'tcx> {
None,
);
body.coverage_info_hi = self.coverage_info.map(|b| b.into_done());
for (index, block) in body.basic_blocks.iter().enumerate() {
if block.terminator.is_none() {
use rustc_middle::mir::pretty;
let options = pretty::PrettyPrintMirOptions::from_cli(self.tcx);
pretty::write_mir_fn(
self.tcx,
&body,
&mut |_, _| Ok(()),
&mut std::io::stdout(),
options,
)
.unwrap();
span_bug!(self.fn_span, "no terminator on block {:?}", index);
}
}
body
}