From 760338526f8a7d7238682fca6b776b28412fd528 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Mon, 7 Oct 2024 10:25:58 +0200 Subject: [PATCH] Show actual MIR when MIR building forgot to terminate block This makes it significantly easier to debug bugs of this kind. --- compiler/rustc_middle/src/mir/pretty.rs | 46 ++++++++++++----------- compiler/rustc_mir_build/src/build/mod.rs | 23 +++++++++--- 2 files changed, 41 insertions(+), 28 deletions(-) diff --git a/compiler/rustc_middle/src/mir/pretty.rs b/compiler/rustc_middle/src/mir/pretty.rs index e690bf74b6b4b..80ae5a7146dad 100644 --- a/compiler/rustc_middle/src/mir/pretty.rs +++ b/compiler/rustc_middle/src/mir/pretty.rs @@ -762,33 +762,35 @@ where // 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!( + 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, - "{:A$} // {}{}", - indented_terminator, - if tcx.sess.verbose_internals() { - format!("{current_location:?}: ") - } else { - String::new() + |visitor| { + visitor.visit_terminator(data.terminator(), current_location); }, - comment(tcx, data.terminator().source_info), - A = ALIGN, + options, )?; - } else { - writeln!(w, "{indented_terminator}")?; } - 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)?; diff --git a/compiler/rustc_mir_build/src/build/mod.rs b/compiler/rustc_mir_build/src/build/mod.rs index 1f853f6e1c302..46be2aee63742 100644 --- a/compiler/rustc_mir_build/src/build/mod.rs +++ b/compiler/rustc_mir_build/src/build/mod.rs @@ -791,12 +791,6 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { } 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 @@ impl<'a, 'tcx> Builder<'a, '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 }