Skip to content

Commit 6fa6f55

Browse files
committed
Adjust the has_errors* methods.
Currently `has_errors` excludes lint errors. This commit changes it to include lint errors. The motivation for this is that for most places it doesn't matter whether lint errors are included or not. But there are multiple places where they must be includes, and only one place where they must not be included. So it makes sense for `has_errors` to do the thing that fits the most situations, and the new `has_errors_excluding_lint_errors` method in the one exceptional place. The same change is made for `err_count`. Annoyingly, this requires the introduction of `err_count_excluding_lint_errs` for one place, to preserve existing error printing behaviour. But I still think the change is worthwhile overall.
1 parent ebb78c2 commit 6fa6f55

File tree

10 files changed

+41
-35
lines changed

10 files changed

+41
-35
lines changed

compiler/rustc_errors/src/lib.rs

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -759,13 +759,20 @@ impl DiagCtxt {
759759
self.inner.borrow_mut().emit_stashed_diagnostics()
760760
}
761761

762-
/// This excludes lint errors, delayed bugs, and stashed errors.
762+
/// This excludes lint errors, delayed bugs and stashed errors.
763763
#[inline]
764-
pub fn err_count(&self) -> usize {
764+
pub fn err_count_excluding_lint_errs(&self) -> usize {
765765
self.inner.borrow().err_guars.len()
766766
}
767767

768-
/// This excludes normal errors, lint errors and delayed bugs. Unless
768+
/// This excludes delayed bugs and stashed errors.
769+
#[inline]
770+
pub fn err_count(&self) -> usize {
771+
let inner = self.inner.borrow();
772+
inner.err_guars.len() + inner.lint_err_guars.len()
773+
}
774+
775+
/// This excludes normal errors, lint errors, and delayed bugs. Unless
769776
/// absolutely necessary, avoid using this. It's dubious because stashed
770777
/// errors can later be cancelled, so the presence of a stashed error at
771778
/// some point of time doesn't guarantee anything -- there are no
@@ -774,21 +781,21 @@ impl DiagCtxt {
774781
self.inner.borrow().stashed_err_count
775782
}
776783

777-
/// This excludes lint errors, delayed bugs, and stashed errors.
778-
pub fn has_errors(&self) -> Option<ErrorGuaranteed> {
779-
self.inner.borrow().has_errors()
784+
/// This excludes lint errors, delayed bugs, and stashed errors. Unless
785+
/// absolutely necessary, prefer `has_errors` to this method.
786+
pub fn has_errors_excluding_lint_errors(&self) -> Option<ErrorGuaranteed> {
787+
self.inner.borrow().has_errors_excluding_lint_errors()
780788
}
781789

782-
/// This excludes delayed bugs and stashed errors. Unless absolutely
783-
/// necessary, prefer `has_errors` to this method.
784-
pub fn has_errors_or_lint_errors(&self) -> Option<ErrorGuaranteed> {
785-
self.inner.borrow().has_errors_or_lint_errors()
790+
/// This excludes delayed bugs and stashed errors.
791+
pub fn has_errors(&self) -> Option<ErrorGuaranteed> {
792+
self.inner.borrow().has_errors()
786793
}
787794

788795
/// This excludes stashed errors. Unless absolutely necessary, prefer
789-
/// `has_errors` or `has_errors_or_lint_errors` to this method.
790-
pub fn has_errors_or_lint_errors_or_delayed_bugs(&self) -> Option<ErrorGuaranteed> {
791-
self.inner.borrow().has_errors_or_lint_errors_or_delayed_bugs()
796+
/// `has_errors` to this method.
797+
pub fn has_errors_or_delayed_bugs(&self) -> Option<ErrorGuaranteed> {
798+
self.inner.borrow().has_errors_or_delayed_bugs()
792799
}
793800

794801
pub fn print_error_count(&self, registry: &Registry) {
@@ -1333,7 +1340,7 @@ impl DiagCtxtInner {
13331340
DelayedBug => {
13341341
// If we have already emitted at least one error, we don't need
13351342
// to record the delayed bug, because it'll never be used.
1336-
return if let Some(guar) = self.has_errors_or_lint_errors() {
1343+
return if let Some(guar) = self.has_errors() {
13371344
Some(guar)
13381345
} else {
13391346
let backtrace = std::backtrace::Backtrace::capture();
@@ -1450,17 +1457,16 @@ impl DiagCtxtInner {
14501457
.is_some_and(|c| self.err_guars.len() + self.lint_err_guars.len() + 1 >= c.get())
14511458
}
14521459

1453-
fn has_errors(&self) -> Option<ErrorGuaranteed> {
1460+
fn has_errors_excluding_lint_errors(&self) -> Option<ErrorGuaranteed> {
14541461
self.err_guars.get(0).copied()
14551462
}
14561463

1457-
fn has_errors_or_lint_errors(&self) -> Option<ErrorGuaranteed> {
1458-
self.has_errors().or_else(|| self.lint_err_guars.get(0).copied())
1464+
fn has_errors(&self) -> Option<ErrorGuaranteed> {
1465+
self.has_errors_excluding_lint_errors().or_else(|| self.lint_err_guars.get(0).copied())
14591466
}
14601467

1461-
fn has_errors_or_lint_errors_or_delayed_bugs(&self) -> Option<ErrorGuaranteed> {
1462-
self.has_errors_or_lint_errors()
1463-
.or_else(|| self.delayed_bugs.get(0).map(|(_, guar)| guar).copied())
1468+
fn has_errors_or_delayed_bugs(&self) -> Option<ErrorGuaranteed> {
1469+
self.has_errors().or_else(|| self.delayed_bugs.get(0).map(|(_, guar)| guar).copied())
14641470
}
14651471

14661472
/// Translate `message` eagerly with `args` to `SubdiagnosticMessage::Eager`.

compiler/rustc_incremental/src/persist/fs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ pub fn finalize_session_directory(sess: &Session, svh: Option<Svh>) {
312312

313313
let incr_comp_session_dir: PathBuf = sess.incr_comp_session_dir().clone();
314314

315-
if sess.dcx().has_errors_or_lint_errors_or_delayed_bugs().is_some() {
315+
if sess.dcx().has_errors_or_delayed_bugs().is_some() {
316316
// If there have been any errors during compilation, we don't want to
317317
// publish this session directory. Rather, we'll just delete it.
318318

compiler/rustc_incremental/src/persist/save.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ pub fn save_dep_graph(tcx: TyCtxt<'_>) {
3232
return;
3333
}
3434
// This is going to be deleted in finalize_session_directory, so let's not create it.
35-
if sess.dcx().has_errors_or_lint_errors_or_delayed_bugs().is_some() {
35+
if sess.dcx().has_errors_or_delayed_bugs().is_some() {
3636
return;
3737
}
3838

@@ -87,7 +87,7 @@ pub fn save_work_product_index(
8787
return;
8888
}
8989
// This is going to be deleted in finalize_session_directory, so let's not create it
90-
if sess.dcx().has_errors_or_lint_errors().is_some() {
90+
if sess.dcx().has_errors().is_some() {
9191
return;
9292
}
9393

compiler/rustc_infer/src/infer/mod.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -713,7 +713,7 @@ impl<'tcx> InferCtxtBuilder<'tcx> {
713713
reported_trait_errors: Default::default(),
714714
reported_signature_mismatch: Default::default(),
715715
tainted_by_errors: Cell::new(None),
716-
err_count_on_creation: tcx.dcx().err_count(),
716+
err_count_on_creation: tcx.dcx().err_count_excluding_lint_errs(),
717717
stashed_err_count_on_creation: tcx.dcx().stashed_err_count(),
718718
universe: Cell::new(ty::UniverseIndex::ROOT),
719719
intercrate,
@@ -1268,8 +1268,11 @@ impl<'tcx> InferCtxt<'tcx> {
12681268
pub fn tainted_by_errors(&self) -> Option<ErrorGuaranteed> {
12691269
if let Some(guar) = self.tainted_by_errors.get() {
12701270
Some(guar)
1271-
} else if self.dcx().err_count() > self.err_count_on_creation {
1272-
// Errors reported since this infcx was made.
1271+
} else if self.dcx().err_count_excluding_lint_errs() > self.err_count_on_creation {
1272+
// Errors reported since this infcx was made. Lint errors are
1273+
// excluded to avoid some being swallowed in the presence of
1274+
// non-lint errors. (It's arguable whether or not this exclusion is
1275+
// important.)
12731276
let guar = self.dcx().has_errors().unwrap();
12741277
self.set_tainted_by_errors(guar);
12751278
Some(guar)

compiler/rustc_interface/src/passes.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -772,7 +772,7 @@ fn analysis(tcx: TyCtxt<'_>, (): ()) -> Result<()> {
772772
// lot of annoying errors in the ui tests (basically,
773773
// lint warnings and so on -- kindck used to do this abort, but
774774
// kindck is gone now). -nmatsakis
775-
if let Some(reported) = sess.dcx().has_errors() {
775+
if let Some(reported) = sess.dcx().has_errors_excluding_lint_errors() {
776776
return Err(reported);
777777
} else if sess.dcx().stashed_err_count() > 0 {
778778
// Without this case we sometimes get delayed bug ICEs and I don't

compiler/rustc_metadata/src/creader.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -926,7 +926,7 @@ impl<'a, 'tcx> CrateLoader<'a, 'tcx> {
926926
what: &str,
927927
needs_dep: &dyn Fn(&CrateMetadata) -> bool,
928928
) {
929-
// don't perform this validation if the session has errors, as one of
929+
// Don't perform this validation if the session has errors, as one of
930930
// those errors may indicate a circular dependency which could cause
931931
// this to stack overflow.
932932
if self.dcx().has_errors().is_some() {

compiler/rustc_query_system/src/dep_graph/graph.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -817,7 +817,7 @@ impl<D: Deps> DepGraphData<D> {
817817
None => {}
818818
}
819819

820-
if let None = qcx.dep_context().sess().dcx().has_errors_or_lint_errors_or_delayed_bugs() {
820+
if let None = qcx.dep_context().sess().dcx().has_errors_or_delayed_bugs() {
821821
panic!("try_mark_previous_green() - Forcing the DepNode should have set its color")
822822
}
823823

compiler/rustc_session/src/session.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,8 +313,7 @@ impl Session {
313313
}
314314

315315
pub fn compile_status(&self) -> Result<(), ErrorGuaranteed> {
316-
// We must include lint errors here.
317-
if let Some(reported) = self.dcx().has_errors_or_lint_errors() {
316+
if let Some(reported) = self.dcx().has_errors() {
318317
self.dcx().emit_stashed_diagnostics();
319318
Err(reported)
320319
} else {

src/librustdoc/core.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -452,8 +452,7 @@ pub(crate) fn run_global_ctxt(
452452

453453
tcx.sess.time("check_lint_expectations", || tcx.check_expectations(Some(sym::rustdoc)));
454454

455-
// We must include lint errors here.
456-
if tcx.dcx().has_errors_or_lint_errors().is_some() {
455+
if tcx.dcx().has_errors().is_some() {
457456
rustc_errors::FatalError.raise();
458457
}
459458

src/librustdoc/doctest.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,7 @@ pub(crate) fn run(options: RustdocOptions) -> Result<(), ErrorGuaranteed> {
153153

154154
collector
155155
});
156-
// We must include lint errors here.
157-
if compiler.sess.dcx().has_errors_or_lint_errors().is_some() {
156+
if compiler.sess.dcx().has_errors().is_some() {
158157
FatalError.raise();
159158
}
160159

0 commit comments

Comments
 (0)