Skip to content

Commit 46f4983

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 9919c3d commit 46f4983

File tree

10 files changed

+41
-35
lines changed

10 files changed

+41
-35
lines changed

compiler/rustc_errors/src/lib.rs

+26-20
Original file line numberDiff line numberDiff line change
@@ -754,13 +754,20 @@ impl DiagCtxt {
754754
self.inner.borrow_mut().emit_stashed_diagnostics()
755755
}
756756

757-
/// This excludes lint errors, delayed bugs, and stashed errors.
757+
/// This excludes lint errors, delayed bugs and stashed errors.
758758
#[inline]
759-
pub fn err_count(&self) -> usize {
759+
pub fn err_count_excluding_lint_errs(&self) -> usize {
760760
self.inner.borrow().err_guars.len()
761761
}
762762

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

772-
/// This excludes lint errors, delayed bugs, and stashed errors.
773-
pub fn has_errors(&self) -> Option<ErrorGuaranteed> {
774-
self.inner.borrow().has_errors()
779+
/// This excludes lint errors, delayed bugs, and stashed errors. Unless
780+
/// absolutely necessary, prefer `has_errors` to this method.
781+
pub fn has_errors_excluding_lint_errors(&self) -> Option<ErrorGuaranteed> {
782+
self.inner.borrow().has_errors_excluding_lint_errors()
775783
}
776784

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

783790
/// This excludes stashed errors. Unless absolutely necessary, prefer
784-
/// `has_errors` or `has_errors_or_lint_errors` to this method.
785-
pub fn has_errors_or_lint_errors_or_delayed_bugs(&self) -> Option<ErrorGuaranteed> {
786-
self.inner.borrow().has_errors_or_lint_errors_or_delayed_bugs()
791+
/// `has_errors` to this method.
792+
pub fn has_errors_or_delayed_bugs(&self) -> Option<ErrorGuaranteed> {
793+
self.inner.borrow().has_errors_or_delayed_bugs()
787794
}
788795

789796
pub fn print_error_count(&self, registry: &Registry) {
@@ -1328,7 +1335,7 @@ impl DiagCtxtInner {
13281335
DelayedBug => {
13291336
// If we have already emitted at least one error, we don't need
13301337
// to record the delayed bug, because it'll never be used.
1331-
return if let Some(guar) = self.has_errors_or_lint_errors() {
1338+
return if let Some(guar) = self.has_errors() {
13321339
Some(guar)
13331340
} else {
13341341
let backtrace = std::backtrace::Backtrace::capture();
@@ -1444,17 +1451,16 @@ impl DiagCtxtInner {
14441451
.is_some_and(|c| self.err_guars.len() + self.lint_err_guars.len() + 1 >= c.get())
14451452
}
14461453

1447-
fn has_errors(&self) -> Option<ErrorGuaranteed> {
1454+
fn has_errors_excluding_lint_errors(&self) -> Option<ErrorGuaranteed> {
14481455
self.err_guars.get(0).copied()
14491456
}
14501457

1451-
fn has_errors_or_lint_errors(&self) -> Option<ErrorGuaranteed> {
1452-
self.has_errors().or_else(|| self.lint_err_guars.get(0).copied())
1458+
fn has_errors(&self) -> Option<ErrorGuaranteed> {
1459+
self.has_errors_excluding_lint_errors().or_else(|| self.lint_err_guars.get(0).copied())
14531460
}
14541461

1455-
fn has_errors_or_lint_errors_or_delayed_bugs(&self) -> Option<ErrorGuaranteed> {
1456-
self.has_errors_or_lint_errors()
1457-
.or_else(|| self.delayed_bugs.get(0).map(|(_, guar)| guar).copied())
1462+
fn has_errors_or_delayed_bugs(&self) -> Option<ErrorGuaranteed> {
1463+
self.has_errors().or_else(|| self.delayed_bugs.get(0).map(|(_, guar)| guar).copied())
14581464
}
14591465

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

compiler/rustc_incremental/src/persist/fs.rs

+1-1
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

+2-2
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

+6-3
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

+1-1
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

+1-1
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

+1-1
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

+1-2
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

+1-2
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

+1-2
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)