Skip to content

Commit 4783dcc

Browse files
committed
Remove track_errors.
`track_errors` returns `Err` if any errors were issued during the execution of the closure. It uses a global count to determine this and `span_delayed_debug` to provide the `ErrorGuaranteed`, which is a hacky way to do it. There are four calls to it. - Three of them are to simple checking functions where it's easy to just return `Result<(), ErrorGuaranteed>`, which this commit does. - The fourth is much harder to fix, but it's the only remaining call, so this commit inlines and removes `track_errors`.
1 parent 6e5bba0 commit 4783dcc

File tree

5 files changed

+39
-44
lines changed

5 files changed

+39
-44
lines changed

compiler/rustc_hir_analysis/src/collect/type_of/opaque.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use rustc_errors::StashKey;
1+
use rustc_errors::{ErrorGuaranteed, StashKey};
22
use rustc_hir::def::DefKind;
33
use rustc_hir::def_id::{LocalDefId, CRATE_DEF_ID};
44
use rustc_hir::intravisit::{self, Visitor};
@@ -9,16 +9,19 @@ use rustc_span::{sym, DUMMY_SP};
99

1010
use crate::errors::{TaitForwardCompat, TypeOf, UnconstrainedOpaqueType};
1111

12-
pub fn test_opaque_hidden_types(tcx: TyCtxt<'_>) {
12+
pub fn test_opaque_hidden_types(tcx: TyCtxt<'_>) -> Result<(), ErrorGuaranteed> {
13+
let mut result = Ok(());
1314
if tcx.has_attr(CRATE_DEF_ID, sym::rustc_hidden_type_of_opaques) {
1415
for id in tcx.hir().items() {
1516
if matches!(tcx.def_kind(id.owner_id), DefKind::OpaqueTy) {
1617
let type_of = tcx.type_of(id.owner_id).instantiate_identity();
1718

18-
tcx.dcx().emit_err(TypeOf { span: tcx.def_span(id.owner_id), type_of });
19+
result =
20+
Err(tcx.dcx().emit_err(TypeOf { span: tcx.def_span(id.owner_id), type_of }));
1921
}
2022
}
2123
}
24+
result
2225
}
2326

2427
/// Checks "defining uses" of opaque `impl Trait` types to ensure that they meet the restrictions

compiler/rustc_hir_analysis/src/lib.rs

+22-20
Original file line numberDiff line numberDiff line change
@@ -169,38 +169,40 @@ pub fn check_crate(tcx: TyCtxt<'_>) -> Result<(), ErrorGuaranteed> {
169169
// FIXME(matthewjasper) We shouldn't need to use `track_errors` anywhere in this function
170170
// or the compiler in general.
171171
if tcx.features().rustc_attrs {
172-
tcx.sess.track_errors(|| {
173-
tcx.sess.time("outlives_testing", || outlives::test::test_inferred_outlives(tcx));
174-
})?;
172+
tcx.sess.time("outlives_testing", || outlives::test::test_inferred_outlives(tcx))?;
175173
}
176174

177-
tcx.sess.track_errors(|| {
178-
tcx.sess.time("coherence_checking", || {
179-
// Check impls constrain their parameters
180-
tcx.hir().for_each_module(|module| tcx.ensure().check_mod_impl_wf(module));
181-
182-
for &trait_def_id in tcx.all_local_trait_impls(()).keys() {
183-
tcx.ensure().coherent_trait(trait_def_id);
184-
}
175+
// FIXME: it would be better to bubble up `ErrorGuaranteed` results for
176+
// evidence of errors, rather than using the global count, but that's not
177+
// easy because there are many checking functions that get called within
178+
// this block.
179+
let old_err_count = tcx.dcx().err_count();
180+
tcx.sess.time("coherence_checking", || {
181+
// Check impls constrain their parameters
182+
tcx.hir().for_each_module(|module| tcx.ensure().check_mod_impl_wf(module));
183+
184+
for &trait_def_id in tcx.all_local_trait_impls(()).keys() {
185+
tcx.ensure().coherent_trait(trait_def_id);
186+
}
185187

186-
// these queries are executed for side-effects (error reporting):
187-
tcx.ensure().crate_inherent_impls(());
188-
tcx.ensure().crate_inherent_impls_overlap_check(());
189-
});
190-
})?;
188+
// these queries are executed for side-effects (error reporting):
189+
tcx.ensure().crate_inherent_impls(());
190+
tcx.ensure().crate_inherent_impls_overlap_check(());
191+
});
192+
if tcx.dcx().err_count() != old_err_count {
193+
return Err(tcx.dcx().delayed_bug("`check_crate` counted an error but no error emitted"));
194+
}
191195

192196
if tcx.features().rustc_attrs {
193-
tcx.sess.track_errors(|| {
194-
tcx.sess.time("variance_testing", || variance::test::test_variance(tcx));
195-
})?;
197+
tcx.sess.time("variance_testing", || variance::test::test_variance(tcx))?;
196198
}
197199

198200
tcx.sess.time("wf_checking", || {
199201
tcx.hir().try_par_for_each_module(|module| tcx.ensure().check_mod_type_wf(module))
200202
})?;
201203

202204
if tcx.features().rustc_attrs {
203-
tcx.sess.track_errors(|| collect::test_opaque_hidden_types(tcx))?;
205+
collect::test_opaque_hidden_types(tcx)?;
204206
}
205207

206208
// Freeze definitions as we don't add new ones at this point. This improves performance by

compiler/rustc_hir_analysis/src/outlives/test.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use rustc_middle::ty::{self, TyCtxt};
22
use rustc_span::symbol::sym;
33

4-
pub fn test_inferred_outlives(tcx: TyCtxt<'_>) {
4+
pub fn test_inferred_outlives(tcx: TyCtxt<'_>) -> Result<(), rustc_errors::ErrorGuaranteed> {
5+
let mut result = Ok(());
56
for id in tcx.hir().items() {
67
// For unit testing: check for a special "rustc_outlives"
78
// attribute and report an error with various results if found.
@@ -22,7 +23,8 @@ pub fn test_inferred_outlives(tcx: TyCtxt<'_>) {
2223
for p in pred {
2324
err.note(p);
2425
}
25-
err.emit();
26+
result = Err(err.emit());
2627
}
2728
}
29+
result
2830
}

compiler/rustc_hir_analysis/src/variance/test.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,17 @@ use rustc_span::symbol::sym;
55

66
use crate::errors;
77

8-
pub fn test_variance(tcx: TyCtxt<'_>) {
8+
pub fn test_variance(tcx: TyCtxt<'_>) -> Result<(), rustc_errors::ErrorGuaranteed> {
9+
let mut result = Ok(());
910
if tcx.has_attr(CRATE_DEF_ID, sym::rustc_variance_of_opaques) {
1011
for id in tcx.hir().items() {
1112
if matches!(tcx.def_kind(id.owner_id), DefKind::OpaqueTy) {
1213
let variances_of = tcx.variances_of(id.owner_id);
1314

14-
tcx.dcx().emit_err(errors::VariancesOf {
15+
result = Err(tcx.dcx().emit_err(errors::VariancesOf {
1516
span: tcx.def_span(id.owner_id),
1617
variances_of: format!("{variances_of:?}"),
17-
});
18+
}));
1819
}
1920
}
2021
}
@@ -25,10 +26,11 @@ pub fn test_variance(tcx: TyCtxt<'_>) {
2526
if tcx.has_attr(id.owner_id, sym::rustc_variance) {
2627
let variances_of = tcx.variances_of(id.owner_id);
2728

28-
tcx.dcx().emit_err(errors::VariancesOf {
29+
result = Err(tcx.dcx().emit_err(errors::VariancesOf {
2930
span: tcx.def_span(id.owner_id),
3031
variances_of: format!("{variances_of:?}"),
31-
});
32+
}));
3233
}
3334
}
35+
result
3436
}

compiler/rustc_session/src/session.rs

-14
Original file line numberDiff line numberDiff line change
@@ -331,20 +331,6 @@ impl Session {
331331
}
332332
}
333333

334-
// FIXME(matthewjasper) Remove this method, it should never be needed.
335-
pub fn track_errors<F, T>(&self, f: F) -> Result<T, ErrorGuaranteed>
336-
where
337-
F: FnOnce() -> T,
338-
{
339-
let old_count = self.dcx().err_count();
340-
let result = f();
341-
if self.dcx().err_count() == old_count {
342-
Ok(result)
343-
} else {
344-
Err(self.dcx().delayed_bug("`self.err_count()` changed but an error was not emitted"))
345-
}
346-
}
347-
348334
/// Used for code paths of expensive computations that should only take place when
349335
/// warnings or errors are emitted. If no messages are emitted ("good path"), then
350336
/// it's likely a bug.

0 commit comments

Comments
 (0)