Skip to content

Commit bac96bd

Browse files
committed
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.
1 parent 67f14e8 commit bac96bd

File tree

1 file changed

+21
-21
lines changed
  • compiler/rustc_errors/src

1 file changed

+21
-21
lines changed

compiler/rustc_errors/src/lib.rs

+21-21
Original file line numberDiff line numberDiff line change
@@ -1514,14 +1514,26 @@ impl DiagCtxtInner {
15141514
let bugs: Vec<_> =
15151515
std::mem::take(&mut self.delayed_bugs).into_iter().map(|(b, _)| b).collect();
15161516

1517-
// If backtraces are enabled, also print the query stack
15181517
let backtrace = std::env::var_os("RUST_BACKTRACE").map_or(true, |x| &x != "0");
1519-
for (i, bug) in bugs.into_iter().enumerate() {
1520-
if let Some(file) = self.ice_file.as_ref()
1521-
&& let Ok(mut out) = std::fs::File::options().create(true).append(true).open(file)
1522-
{
1523-
let _ = write!(
1524-
&mut out,
1518+
let decorate = backtrace || self.ice_file.is_none();
1519+
let mut out = self
1520+
.ice_file
1521+
.as_ref()
1522+
.and_then(|file| std::fs::File::options().create(true).append(true).open(file).ok());
1523+
1524+
// Put the overall explanation before the `DelayedBug`s, to frame them
1525+
// better (e.g. separate warnings from them). Also, use notes, which
1526+
// don't count as errors, to avoid possibly triggering
1527+
// `-Ztreat-err-as-bug`, which we don't want.
1528+
let note1 = "no errors encountered even though delayed bugs were created";
1529+
let note2 = "those delayed bugs will now be shown as internal compiler errors";
1530+
self.emit_diagnostic(Diagnostic::new(Note, note1));
1531+
self.emit_diagnostic(Diagnostic::new(Note, note2));
1532+
1533+
for bug in bugs {
1534+
if let Some(out) = &mut out {
1535+
_ = write!(
1536+
out,
15251537
"delayed bug: {}\n{}\n",
15261538
bug.inner
15271539
.messages
@@ -1532,21 +1544,9 @@ impl DiagCtxtInner {
15321544
);
15331545
}
15341546

1535-
if i == 0 {
1536-
// Put the overall explanation before the `DelayedBug`s, to
1537-
// frame them better (e.g. separate warnings from them). Also,
1538-
// make it a note so it doesn't count as an error, because that
1539-
// could trigger `-Ztreat-err-as-bug`, which we don't want.
1540-
let note1 = "no errors encountered even though delayed bugs were created";
1541-
let note2 = "those delayed bugs will now be shown as internal compiler errors";
1542-
self.emit_diagnostic(Diagnostic::new(Note, note1));
1543-
self.emit_diagnostic(Diagnostic::new(Note, note2));
1544-
}
1545-
1546-
let mut bug =
1547-
if backtrace || self.ice_file.is_none() { bug.decorate(self) } else { bug.inner };
1547+
let mut bug = if decorate { bug.decorate(self) } else { bug.inner };
15481548

1549-
// "Undelay" the delayed bugs (into plain `Bug`s).
1549+
// "Undelay" the delayed bugs into plain bugs.
15501550
if bug.level != DelayedBug {
15511551
// NOTE(eddyb) not panicking here because we're already producing
15521552
// an ICE, and the more information the merrier.

0 commit comments

Comments
 (0)