Skip to content

Commit

Permalink
Avoid repetition in flush_delayed calls.
Browse files Browse the repository at this point in the history
There are two places that handle normal delayed bugs. This commit
factors out some repeated code.

Also, we can use `std::mem::take` instead of `std::mem::replace`.
  • Loading branch information
nnethercote committed Jan 11, 2024
1 parent 62d7ed4 commit 3330940
Showing 1 changed file with 20 additions and 15 deletions.
35 changes: 20 additions & 15 deletions compiler/rustc_errors/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,11 @@ fn default_track_diagnostic(diag: Diagnostic, f: &mut dyn FnMut(Diagnostic)) {
pub static TRACK_DIAGNOSTIC: AtomicRef<fn(Diagnostic, &mut dyn FnMut(Diagnostic))> =
AtomicRef::new(&(default_track_diagnostic as _));

enum DelayedBugKind {
Normal,
GoodPath,
}

#[derive(Copy, Clone, Default)]
pub struct DiagCtxtFlags {
/// If false, warning-level lints are suppressed.
Expand All @@ -541,8 +546,7 @@ impl Drop for DiagCtxtInner {
self.emit_stashed_diagnostics();

if !self.has_errors() {
let bugs = std::mem::replace(&mut self.span_delayed_bugs, Vec::new());
self.flush_delayed(bugs, "no errors encountered even though `span_delayed_bug` issued");
self.flush_delayed(DelayedBugKind::Normal)
}

// FIXME(eddyb) this explains what `good_path_delayed_bugs` are!
Expand All @@ -551,11 +555,7 @@ impl Drop for DiagCtxtInner {
// lints can be `#[allow]`'d, potentially leading to this triggering.
// Also, "good path" should be replaced with a better naming.
if !self.has_printed && !self.suppressed_expected_diag && !std::thread::panicking() {
let bugs = std::mem::replace(&mut self.good_path_delayed_bugs, Vec::new());
self.flush_delayed(
bugs,
"no warnings or errors encountered even though `good_path_delayed_bugs` issued",
);
self.flush_delayed(DelayedBugKind::GoodPath);
}

if self.check_unstable_expect_diagnostics {
Expand Down Expand Up @@ -1218,9 +1218,7 @@ impl DiagCtxt {
}

pub fn flush_delayed(&self) {
let mut inner = self.inner.borrow_mut();
let bugs = std::mem::replace(&mut inner.span_delayed_bugs, Vec::new());
inner.flush_delayed(bugs, "no errors encountered even though `span_delayed_bug` issued");
self.inner.borrow_mut().flush_delayed(DelayedBugKind::Normal);
}
}

Expand Down Expand Up @@ -1396,11 +1394,18 @@ impl DiagCtxtInner {
self.emit_diagnostic(Diagnostic::new(FailureNote, msg));
}

fn flush_delayed(
&mut self,
bugs: Vec<DelayedDiagnostic>,
explanation: impl Into<DiagnosticMessage> + Copy,
) {
fn flush_delayed(&mut self, kind: DelayedBugKind) {
let (bugs, explanation) = match kind {
DelayedBugKind::Normal => (
std::mem::take(&mut self.span_delayed_bugs),
"no errors encountered even though `span_delayed_bug` issued",
),
DelayedBugKind::GoodPath => (
std::mem::take(&mut self.good_path_delayed_bugs),
"no warnings or errors encountered even though `good_path_delayed_bugs` issued",
),
};

if bugs.is_empty() {
return;
}
Expand Down

0 comments on commit 3330940

Please sign in to comment.