Skip to content

Commit bef2e85

Browse files
authored
Rollup merge of #119986 - nnethercote:fix-error-counting, r=compiler-errors,oli-obk
Fix error counting There is some messiness in how errors get counted. Here are some cleanups. r? `@compiler-errors`
2 parents 6687e8e + 1f9fa23 commit bef2e85

File tree

14 files changed

+42
-39
lines changed

14 files changed

+42
-39
lines changed

compiler/rustc_builtin_macros/src/format.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,7 @@ fn make_format_args(
529529

530530
// Only check for unused named argument names if there are no other errors to avoid causing
531531
// too much noise in output errors, such as when a named argument is entirely unused.
532-
if invalid_refs.is_empty() && ecx.dcx().err_count() == 0 {
532+
if invalid_refs.is_empty() && ecx.dcx().has_errors().is_none() {
533533
for &(index, span, used_as) in &numeric_refences_to_named_arg {
534534
let (position_sp_to_replace, position_sp_for_msg) = match used_as {
535535
Placeholder(pspan) => (span, pspan),

compiler/rustc_errors/src/lib.rs

+18-22
Original file line numberDiff line numberDiff line change
@@ -421,16 +421,16 @@ pub struct DiagCtxt {
421421
struct DiagCtxtInner {
422422
flags: DiagCtxtFlags,
423423

424-
/// The number of lint errors that have been emitted.
424+
/// The number of lint errors that have been emitted, including duplicates.
425425
lint_err_count: usize,
426-
/// The number of errors that have been emitted, including duplicates.
427-
///
428-
/// This is not necessarily the count that's reported to the user once
429-
/// compilation ends.
426+
/// The number of non-lint errors that have been emitted, including duplicates.
430427
err_count: usize,
428+
429+
/// The error count shown to the user at the end.
431430
deduplicated_err_count: usize,
432-
/// The warning count, used for a recap upon finishing
431+
/// The warning count shown to the user at the end.
433432
deduplicated_warn_count: usize,
433+
434434
/// Has this diagnostic context printed any diagnostics? (I.e. has
435435
/// `self.emitter.emit_diagnostic()` been called?
436436
has_printed: bool,
@@ -927,42 +927,38 @@ impl DiagCtxt {
927927
self.struct_bug(msg).emit()
928928
}
929929

930+
/// This excludes lint errors and delayed bugs.
930931
#[inline]
931932
pub fn err_count(&self) -> usize {
932933
self.inner.borrow().err_count
933934
}
934935

936+
/// This excludes lint errors and delayed bugs.
935937
pub fn has_errors(&self) -> Option<ErrorGuaranteed> {
936938
self.inner.borrow().has_errors().then(|| {
937939
#[allow(deprecated)]
938940
ErrorGuaranteed::unchecked_claim_error_was_emitted()
939941
})
940942
}
941943

944+
/// This excludes delayed bugs. Unless absolutely necessary, prefer
945+
/// `has_errors` to this method.
942946
pub fn has_errors_or_lint_errors(&self) -> Option<ErrorGuaranteed> {
943947
let inner = self.inner.borrow();
944-
let has_errors_or_lint_errors = inner.has_errors() || inner.lint_err_count > 0;
945-
has_errors_or_lint_errors.then(|| {
946-
#[allow(deprecated)]
947-
ErrorGuaranteed::unchecked_claim_error_was_emitted()
948-
})
949-
}
950-
951-
pub fn has_errors_or_span_delayed_bugs(&self) -> Option<ErrorGuaranteed> {
952-
let inner = self.inner.borrow();
953-
let has_errors_or_span_delayed_bugs =
954-
inner.has_errors() || !inner.span_delayed_bugs.is_empty();
955-
has_errors_or_span_delayed_bugs.then(|| {
948+
let result = inner.has_errors() || inner.lint_err_count > 0;
949+
result.then(|| {
956950
#[allow(deprecated)]
957951
ErrorGuaranteed::unchecked_claim_error_was_emitted()
958952
})
959953
}
960954

961-
pub fn is_compilation_going_to_fail(&self) -> Option<ErrorGuaranteed> {
955+
/// Unless absolutely necessary, prefer `has_errors` or
956+
/// `has_errors_or_lint_errors` to this method.
957+
pub fn has_errors_or_lint_errors_or_delayed_bugs(&self) -> Option<ErrorGuaranteed> {
962958
let inner = self.inner.borrow();
963-
let will_fail =
959+
let result =
964960
inner.has_errors() || inner.lint_err_count > 0 || !inner.span_delayed_bugs.is_empty();
965-
will_fail.then(|| {
961+
result.then(|| {
966962
#[allow(deprecated)]
967963
ErrorGuaranteed::unchecked_claim_error_was_emitted()
968964
})
@@ -1162,7 +1158,7 @@ impl DiagCtxt {
11621158
let mut inner = self.inner.borrow_mut();
11631159

11641160
if loud && lint_level.is_error() {
1165-
inner.err_count += 1;
1161+
inner.lint_err_count += 1;
11661162
inner.panic_if_treat_err_as_bug();
11671163
}
11681164

compiler/rustc_hir_analysis/src/check/wfcheck.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ where
116116
let errors = wfcx.select_all_or_error();
117117
if !errors.is_empty() {
118118
let err = infcx.err_ctxt().report_fulfillment_errors(errors);
119-
if tcx.dcx().err_count() > 0 {
119+
if tcx.dcx().has_errors().is_some() {
120120
return Err(err);
121121
} else {
122122
// HACK(oli-obk): tests/ui/specialization/min_specialization/specialize_on_type_error.rs

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 let Some(_) = sess.dcx().has_errors_or_span_delayed_bugs() {
315+
if sess.dcx().has_errors_or_lint_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

+3-3
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ pub fn save_dep_graph(tcx: TyCtxt<'_>) {
3131
if sess.opts.incremental.is_none() {
3232
return;
3333
}
34-
// This is going to be deleted in finalize_session_directory, so let's not create it
35-
if let Some(_) = sess.dcx().has_errors_or_span_delayed_bugs() {
34+
// 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() {
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 let Some(_) = sess.dcx().has_errors_or_span_delayed_bugs() {
90+
if sess.dcx().has_errors_or_lint_errors().is_some() {
9191
return;
9292
}
9393

compiler/rustc_infer/src/infer/error_reporting/mod.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,9 @@ fn escape_literal(s: &str) -> String {
117117
/// field is only populated during an in-progress typeck.
118118
/// Get an instance by calling `InferCtxt::err_ctxt` or `FnCtxt::err_ctxt`.
119119
///
120-
/// You must only create this if you intend to actually emit an error.
121-
/// This provides a lot of utility methods which should not be used
122-
/// during the happy path.
120+
/// You must only create this if you intend to actually emit an error (or
121+
/// perhaps a warning, though preferably not.) It provides a lot of utility
122+
/// methods which should not be used during the happy path.
123123
pub struct TypeErrCtxt<'a, 'tcx> {
124124
pub infcx: &'a InferCtxt<'tcx>,
125125
pub typeck_results: Option<std::cell::Ref<'a, ty::TypeckResults<'tcx>>>,
@@ -133,9 +133,10 @@ pub struct TypeErrCtxt<'a, 'tcx> {
133133

134134
impl Drop for TypeErrCtxt<'_, '_> {
135135
fn drop(&mut self) {
136-
if let Some(_) = self.dcx().has_errors_or_span_delayed_bugs() {
137-
// ok, emitted an error.
136+
if self.dcx().has_errors().is_some() {
137+
// Ok, emitted an error.
138138
} else {
139+
// Didn't emit an error; maybe it was created but not yet emitted.
139140
self.infcx
140141
.tcx
141142
.sess

compiler/rustc_middle/src/ty/visit.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,13 @@ pub trait TypeVisitableExt<'tcx>: TypeVisitable<TyCtxt<'tcx>> {
5555
}
5656
fn error_reported(&self) -> Result<(), ErrorGuaranteed> {
5757
if self.references_error() {
58-
if let Some(reported) = ty::tls::with(|tcx| tcx.dcx().is_compilation_going_to_fail()) {
58+
// We must include lint errors and span delayed bugs here.
59+
if let Some(reported) =
60+
ty::tls::with(|tcx| tcx.dcx().has_errors_or_lint_errors_or_delayed_bugs())
61+
{
5962
Err(reported)
6063
} else {
61-
bug!("expect tcx.sess.is_compilation_going_to_fail return `Some`");
64+
bug!("expected some kind of error in `error_reported`");
6265
}
6366
} else {
6467
Ok(())

compiler/rustc_query_system/src/dep_graph/graph.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -818,7 +818,7 @@ impl<D: Deps> DepGraphData<D> {
818818
None => {}
819819
}
820820

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

compiler/rustc_session/src/session.rs

+1
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,7 @@ impl Session {
323323
}
324324

325325
pub fn compile_status(&self) -> Result<(), ErrorGuaranteed> {
326+
// We must include lint errors here.
326327
if let Some(reported) = self.dcx().has_errors_or_lint_errors() {
327328
let _ = self.dcx().emit_stashed_diagnostics();
328329
Err(reported)

src/librustdoc/core.rs

+1
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,7 @@ pub(crate) fn run_global_ctxt(
449449

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

452+
// We must include lint errors here.
452453
if tcx.dcx().has_errors_or_lint_errors().is_some() {
453454
rustc_errors::FatalError.raise();
454455
}

src/librustdoc/doctest.rs

+1
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ pub(crate) fn run(options: RustdocOptions) -> Result<(), ErrorGuaranteed> {
150150

151151
collector
152152
});
153+
// We must include lint errors here.
153154
if compiler.sess.dcx().has_errors_or_lint_errors().is_some() {
154155
FatalError.raise();
155156
}

src/librustdoc/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -796,7 +796,7 @@ fn main_args(
796796

797797
compiler.enter(|queries| {
798798
let mut gcx = abort_on_err(queries.global_ctxt(), sess);
799-
if sess.dcx().has_errors_or_lint_errors().is_some() {
799+
if sess.dcx().has_errors().is_some() {
800800
sess.dcx().fatal("Compilation failed, aborting rustdoc");
801801
}
802802

src/librustdoc/passes/collect_trait_impls.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub(crate) fn collect_trait_impls(mut krate: Crate, cx: &mut DocContext<'_>) ->
2222
let tcx = cx.tcx;
2323
// We need to check if there are errors before running this pass because it would crash when
2424
// we try to get auto and blanket implementations.
25-
if tcx.dcx().has_errors_or_lint_errors().is_some() {
25+
if tcx.dcx().has_errors().is_some() {
2626
return krate;
2727
}
2828

src/librustdoc/scrape_examples.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ pub(crate) fn run(
311311

312312
// The visitor might have found a type error, which we need to
313313
// promote to a fatal error
314-
if tcx.dcx().has_errors_or_lint_errors().is_some() {
314+
if tcx.dcx().has_errors().is_some() {
315315
return Err(String::from("Compilation failed, aborting rustdoc"));
316316
}
317317

0 commit comments

Comments
 (0)