Skip to content

Commit 3330940

Browse files
committed
Avoid repetition in flush_delayed calls.
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`.
1 parent 62d7ed4 commit 3330940

File tree

1 file changed

+20
-15
lines changed
  • compiler/rustc_errors/src

1 file changed

+20
-15
lines changed

compiler/rustc_errors/src/lib.rs

+20-15
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,11 @@ fn default_track_diagnostic(diag: Diagnostic, f: &mut dyn FnMut(Diagnostic)) {
519519
pub static TRACK_DIAGNOSTIC: AtomicRef<fn(Diagnostic, &mut dyn FnMut(Diagnostic))> =
520520
AtomicRef::new(&(default_track_diagnostic as _));
521521

522+
enum DelayedBugKind {
523+
Normal,
524+
GoodPath,
525+
}
526+
522527
#[derive(Copy, Clone, Default)]
523528
pub struct DiagCtxtFlags {
524529
/// If false, warning-level lints are suppressed.
@@ -541,8 +546,7 @@ impl Drop for DiagCtxtInner {
541546
self.emit_stashed_diagnostics();
542547

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

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

561561
if self.check_unstable_expect_diagnostics {
@@ -1218,9 +1218,7 @@ impl DiagCtxt {
12181218
}
12191219

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

@@ -1396,11 +1394,18 @@ impl DiagCtxtInner {
13961394
self.emit_diagnostic(Diagnostic::new(FailureNote, msg));
13971395
}
13981396

1399-
fn flush_delayed(
1400-
&mut self,
1401-
bugs: Vec<DelayedDiagnostic>,
1402-
explanation: impl Into<DiagnosticMessage> + Copy,
1403-
) {
1397+
fn flush_delayed(&mut self, kind: DelayedBugKind) {
1398+
let (bugs, explanation) = match kind {
1399+
DelayedBugKind::Normal => (
1400+
std::mem::take(&mut self.span_delayed_bugs),
1401+
"no errors encountered even though `span_delayed_bug` issued",
1402+
),
1403+
DelayedBugKind::GoodPath => (
1404+
std::mem::take(&mut self.good_path_delayed_bugs),
1405+
"no warnings or errors encountered even though `good_path_delayed_bugs` issued",
1406+
),
1407+
};
1408+
14041409
if bugs.is_empty() {
14051410
return;
14061411
}

0 commit comments

Comments
 (0)