Skip to content

Commit 76f9cdb

Browse files
committed
Fix DiagCtxtInner::reset_err_count.
Several fields were not being reset. Using destructuring makes it much harder to miss a field.
1 parent 144989c commit 76f9cdb

File tree

1 file changed

+45
-14
lines changed
  • compiler/rustc_errors/src

1 file changed

+45
-14
lines changed

compiler/rustc_errors/src/lib.rs

+45-14
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ use std::fmt;
7878
use std::hash::Hash;
7979
use std::io::Write;
8080
use std::num::NonZeroUsize;
81+
use std::ops::DerefMut;
8182
use std::panic;
8283
use std::path::{Path, PathBuf};
8384

@@ -662,21 +663,51 @@ impl DiagCtxt {
662663
/// tools that want to reuse a `Parser` cleaning the previously emitted diagnostics as well as
663664
/// the overall count of emitted error diagnostics.
664665
pub fn reset_err_count(&self) {
666+
// Use destructuring so that if a field gets added to `DiagCtxtInner`, it's impossible to
667+
// fail to update this method as well.
665668
let mut inner = self.inner.borrow_mut();
666-
inner.stashed_err_count = 0;
667-
inner.deduplicated_err_count = 0;
668-
inner.deduplicated_warn_count = 0;
669-
inner.has_printed = false;
670-
671-
// actually free the underlying memory (which `clear` would not do)
672-
inner.err_guars = Default::default();
673-
inner.lint_err_guars = Default::default();
674-
inner.delayed_bugs = Default::default();
675-
inner.good_path_delayed_bugs = Default::default();
676-
inner.taught_diagnostics = Default::default();
677-
inner.emitted_diagnostic_codes = Default::default();
678-
inner.emitted_diagnostics = Default::default();
679-
inner.stashed_diagnostics = Default::default();
669+
let DiagCtxtInner {
670+
flags: _,
671+
err_guars,
672+
lint_err_guars,
673+
delayed_bugs,
674+
good_path_delayed_bugs,
675+
stashed_err_count,
676+
deduplicated_err_count,
677+
deduplicated_warn_count,
678+
has_printed,
679+
emitter: _,
680+
suppressed_expected_diag,
681+
taught_diagnostics,
682+
emitted_diagnostic_codes,
683+
emitted_diagnostics,
684+
stashed_diagnostics,
685+
future_breakage_diagnostics,
686+
check_unstable_expect_diagnostics,
687+
unstable_expect_diagnostics,
688+
fulfilled_expectations,
689+
ice_file: _,
690+
} = inner.deref_mut();
691+
692+
// For the `Vec`s and `HashMap`s, we overwrite with an empty container to free the
693+
// underlying memory (which `clear` would not do).
694+
*err_guars = Default::default();
695+
*lint_err_guars = Default::default();
696+
*delayed_bugs = Default::default();
697+
*good_path_delayed_bugs = Default::default();
698+
*stashed_err_count = 0;
699+
*deduplicated_err_count = 0;
700+
*deduplicated_warn_count = 0;
701+
*has_printed = false;
702+
*suppressed_expected_diag = false;
703+
*taught_diagnostics = Default::default();
704+
*emitted_diagnostic_codes = Default::default();
705+
*emitted_diagnostics = Default::default();
706+
*stashed_diagnostics = Default::default();
707+
*future_breakage_diagnostics = Default::default();
708+
*check_unstable_expect_diagnostics = false;
709+
*unstable_expect_diagnostics = Default::default();
710+
*fulfilled_expectations = Default::default();
680711
}
681712

682713
/// Stash a given diagnostic with the given `Span` and [`StashKey`] as the key.

0 commit comments

Comments
 (0)