Refactor DiagCtxtInner::flush_delayed.

This commit:
- Moves the ICE file create/open outside the loop. (Redoing it on every
  loop iteration works, but is really weird.)
- Moves the explanatory note emission above the loop, which removes the
  need for the `enumerate` call.
- Introduces a `decorate` local.
This commit is contained in:
Nicholas Nethercote 2024-02-28 09:43:23 +11:00
parent 3fbdec4937
commit 199be469ec

View File

@ -1498,14 +1498,26 @@ fn flush_delayed(&mut self) {
let bugs: Vec<_> = let bugs: Vec<_> =
std::mem::take(&mut self.delayed_bugs).into_iter().map(|(b, _)| b).collect(); std::mem::take(&mut self.delayed_bugs).into_iter().map(|(b, _)| b).collect();
// If backtraces are enabled, also print the query stack
let backtrace = std::env::var_os("RUST_BACKTRACE").map_or(true, |x| &x != "0"); let backtrace = std::env::var_os("RUST_BACKTRACE").map_or(true, |x| &x != "0");
for (i, bug) in bugs.into_iter().enumerate() { let decorate = backtrace || self.ice_file.is_none();
if let Some(file) = self.ice_file.as_ref() let mut out = self
&& let Ok(mut out) = std::fs::File::options().create(true).append(true).open(file) .ice_file
{ .as_ref()
let _ = write!( .and_then(|file| std::fs::File::options().create(true).append(true).open(file).ok());
&mut out,
// Put the overall explanation before the `DelayedBug`s, to frame them
// better (e.g. separate warnings from them). Also, use notes, which
// don't count as errors, to avoid possibly triggering
// `-Ztreat-err-as-bug`, which we don't want.
let note1 = "no errors encountered even though delayed bugs were created";
let note2 = "those delayed bugs will now be shown as internal compiler errors";
self.emit_diagnostic(DiagInner::new(Note, note1));
self.emit_diagnostic(DiagInner::new(Note, note2));
for bug in bugs {
if let Some(out) = &mut out {
_ = write!(
out,
"delayed bug: {}\n{}\n", "delayed bug: {}\n{}\n",
bug.inner bug.inner
.messages .messages
@ -1516,21 +1528,9 @@ fn flush_delayed(&mut self) {
); );
} }
if i == 0 { let mut bug = if decorate { bug.decorate(self) } else { bug.inner };
// Put the overall explanation before the `DelayedBug`s, to
// frame them better (e.g. separate warnings from them). Also,
// make it a note so it doesn't count as an error, because that
// could trigger `-Ztreat-err-as-bug`, which we don't want.
let note1 = "no errors encountered even though delayed bugs were created";
let note2 = "those delayed bugs will now be shown as internal compiler errors";
self.emit_diagnostic(DiagInner::new(Note, note1));
self.emit_diagnostic(DiagInner::new(Note, note2));
}
let mut bug = // "Undelay" the delayed bugs into plain bugs.
if backtrace || self.ice_file.is_none() { bug.decorate(self) } else { bug.inner };
// "Undelay" the delayed bugs (into plain `Bug`s).
if bug.level != DelayedBug { if bug.level != DelayedBug {
// NOTE(eddyb) not panicking here because we're already producing // NOTE(eddyb) not panicking here because we're already producing
// an ICE, and the more information the merrier. // an ICE, and the more information the merrier.