Skip to content

Commit c0e49b5

Browse files
committed
Auto merge of rust-lang#122140 - oli-obk:track_errors13, r=<try>
Run a single huge par_body_owners instead of many small ones after each other. This improves parallel rustc parallelism by avoiding the bottleneck after each individual `par_body_owners` (because it needs to wait for queries to finish, so if there is one long running one, a lot of cores will be idle while waiting for the single query). based on rust-lang#121500
2 parents 8c9a75b + 164473b commit c0e49b5

File tree

99 files changed

+1473
-1508
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

99 files changed

+1473
-1508
lines changed

compiler/rustc_driver_impl/src/pretty.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,8 @@ pub fn print<'tcx>(sess: &Session, ppm: PpMode, ex: PrintExtra<'tcx>) {
336336
ThirTree => {
337337
let tcx = ex.tcx();
338338
let mut out = String::new();
339-
if rustc_hir_analysis::check_crate(tcx).is_err() {
339+
rustc_hir_analysis::check_crate(tcx);
340+
if tcx.dcx().has_errors().is_some() {
340341
FatalError.raise();
341342
}
342343
debug!("pretty printing THIR tree");
@@ -348,7 +349,8 @@ pub fn print<'tcx>(sess: &Session, ppm: PpMode, ex: PrintExtra<'tcx>) {
348349
ThirFlat => {
349350
let tcx = ex.tcx();
350351
let mut out = String::new();
351-
if rustc_hir_analysis::check_crate(tcx).is_err() {
352+
rustc_hir_analysis::check_crate(tcx);
353+
if tcx.dcx().has_errors().is_some() {
352354
FatalError.raise();
353355
}
354356
debug!("pretty printing THIR flat");

compiler/rustc_hir_analysis/src/check/wfcheck.rs

+20-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
use crate::autoderef::Autoderef;
2+
use crate::collect::CollectItemTypesVisitor;
23
use crate::constrained_generic_params::{identify_constrained_generic_params, Parameter};
34
use crate::errors;
45

6+
use hir::intravisit::Visitor;
57
use rustc_ast as ast;
68
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexSet};
79
use rustc_errors::{codes::*, pluralize, struct_span_code_err, Applicability, ErrorGuaranteed};
@@ -225,6 +227,7 @@ fn check_item<'tcx>(tcx: TyCtxt<'tcx>, item: &'tcx hir::Item<'tcx>) -> Result<()
225227
?item.owner_id,
226228
item.name = ? tcx.def_path_str(def_id)
227229
);
230+
CollectItemTypesVisitor { tcx }.visit_item(item);
228231

229232
let res = match item.kind {
230233
// Right now we check that every default trait implementation
@@ -336,9 +339,14 @@ fn check_item<'tcx>(tcx: TyCtxt<'tcx>, item: &'tcx hir::Item<'tcx>) -> Result<()
336339
res
337340
}
338341

339-
fn check_foreign_item(tcx: TyCtxt<'_>, item: &hir::ForeignItem<'_>) -> Result<(), ErrorGuaranteed> {
342+
fn check_foreign_item<'tcx>(
343+
tcx: TyCtxt<'tcx>,
344+
item: &'tcx hir::ForeignItem<'tcx>,
345+
) -> Result<(), ErrorGuaranteed> {
340346
let def_id = item.owner_id.def_id;
341347

348+
CollectItemTypesVisitor { tcx }.visit_foreign_item(item);
349+
342350
debug!(
343351
?item.owner_id,
344352
item.name = ? tcx.def_path_str(def_id)
@@ -355,12 +363,14 @@ fn check_foreign_item(tcx: TyCtxt<'_>, item: &hir::ForeignItem<'_>) -> Result<()
355363
}
356364
}
357365

358-
fn check_trait_item(
359-
tcx: TyCtxt<'_>,
360-
trait_item: &hir::TraitItem<'_>,
366+
fn check_trait_item<'tcx>(
367+
tcx: TyCtxt<'tcx>,
368+
trait_item: &'tcx hir::TraitItem<'tcx>,
361369
) -> Result<(), ErrorGuaranteed> {
362370
let def_id = trait_item.owner_id.def_id;
363371

372+
CollectItemTypesVisitor { tcx }.visit_trait_item(trait_item);
373+
364374
let (method_sig, span) = match trait_item.kind {
365375
hir::TraitItemKind::Fn(ref sig, _) => (Some(sig), trait_item.span),
366376
hir::TraitItemKind::Type(_bounds, Some(ty)) => (None, ty.span),
@@ -895,7 +905,12 @@ fn check_object_unsafe_self_trait_by_name(tcx: TyCtxt<'_>, item: &hir::TraitItem
895905
}
896906
}
897907

898-
fn check_impl_item(tcx: TyCtxt<'_>, impl_item: &hir::ImplItem<'_>) -> Result<(), ErrorGuaranteed> {
908+
fn check_impl_item<'tcx>(
909+
tcx: TyCtxt<'tcx>,
910+
impl_item: &'tcx hir::ImplItem<'tcx>,
911+
) -> Result<(), ErrorGuaranteed> {
912+
CollectItemTypesVisitor { tcx }.visit_impl_item(impl_item);
913+
899914
let (method_sig, span) = match impl_item.kind {
900915
hir::ImplItemKind::Fn(ref sig, _) => (Some(sig), impl_item.span),
901916
// Constrain binding and overflow error spans to `<Ty>` in `type foo = <Ty>`.

compiler/rustc_hir_analysis/src/collect.rs

+3-9
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use rustc_data_structures::unord::UnordMap;
2020
use rustc_errors::{Applicability, Diag, ErrorGuaranteed, StashKey};
2121
use rustc_hir as hir;
2222
use rustc_hir::def::DefKind;
23-
use rustc_hir::def_id::{DefId, LocalDefId, LocalModDefId};
23+
use rustc_hir::def_id::{DefId, LocalDefId};
2424
use rustc_hir::intravisit::{self, Visitor};
2525
use rustc_hir::{GenericParamKind, Node};
2626
use rustc_infer::infer::{InferCtxt, TyCtxtInferExt};
@@ -52,11 +52,6 @@ mod resolve_bound_vars;
5252
mod type_of;
5353

5454
///////////////////////////////////////////////////////////////////////////
55-
// Main entry point
56-
57-
fn collect_mod_item_types(tcx: TyCtxt<'_>, module_def_id: LocalModDefId) {
58-
tcx.hir().visit_item_likes_in_module(module_def_id, &mut CollectItemTypesVisitor { tcx });
59-
}
6055

6156
pub fn provide(providers: &mut Providers) {
6257
resolve_bound_vars::provide(providers);
@@ -82,7 +77,6 @@ pub fn provide(providers: &mut Providers) {
8277
impl_trait_header,
8378
coroutine_kind,
8479
coroutine_for_closure,
85-
collect_mod_item_types,
8680
is_type_alias_impl_trait,
8781
find_field,
8882
..*providers
@@ -155,8 +149,8 @@ impl<'v> Visitor<'v> for HirPlaceholderCollector {
155149
}
156150
}
157151

158-
struct CollectItemTypesVisitor<'tcx> {
159-
tcx: TyCtxt<'tcx>,
152+
pub struct CollectItemTypesVisitor<'tcx> {
153+
pub tcx: TyCtxt<'tcx>,
160154
}
161155

162156
/// If there are any placeholder types (`_`), emit an error explaining that this is not allowed

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

+3-5
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,20 @@ use rustc_hir::intravisit::{self, Visitor};
55
use rustc_hir::{self as hir, def, Expr, ImplItem, Item, Node, TraitItem};
66
use rustc_middle::hir::nested_filter;
77
use rustc_middle::ty::{self, Ty, TyCtxt, TypeVisitableExt};
8-
use rustc_span::{sym, ErrorGuaranteed, DUMMY_SP};
8+
use rustc_span::{sym, DUMMY_SP};
99

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

12-
pub fn test_opaque_hidden_types(tcx: TyCtxt<'_>) -> Result<(), ErrorGuaranteed> {
13-
let mut res = Ok(());
12+
pub fn test_opaque_hidden_types(tcx: TyCtxt<'_>) {
1413
if tcx.has_attr(CRATE_DEF_ID, sym::rustc_hidden_type_of_opaques) {
1514
for id in tcx.hir().items() {
1615
if matches!(tcx.def_kind(id.owner_id), DefKind::OpaqueTy) {
1716
let type_of = tcx.type_of(id.owner_id).instantiate_identity();
1817

19-
res = Err(tcx.dcx().emit_err(TypeOf { span: tcx.def_span(id.owner_id), type_of }));
18+
tcx.dcx().emit_err(TypeOf { span: tcx.def_span(id.owner_id), type_of });
2019
}
2120
}
2221
}
23-
res
2422
}
2523

2624
/// Checks "defining uses" of opaque `impl Trait` in associated types.

compiler/rustc_hir_analysis/src/lib.rs

+4-32
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ mod outlives;
9898
pub mod structured_errors;
9999
mod variance;
100100

101-
use rustc_errors::ErrorGuaranteed;
102101
use rustc_hir as hir;
103102
use rustc_middle::middle;
104103
use rustc_middle::query::Providers;
@@ -156,17 +155,13 @@ pub fn provide(providers: &mut Providers) {
156155
hir_wf_check::provide(providers);
157156
}
158157

159-
pub fn check_crate(tcx: TyCtxt<'_>) -> Result<(), ErrorGuaranteed> {
158+
pub fn check_crate(tcx: TyCtxt<'_>) {
160159
let _prof_timer = tcx.sess.timer("type_check_crate");
161160

162-
// this ensures that later parts of type checking can assume that items
163-
// have valid types and not error
164-
tcx.sess.time("type_collecting", || {
165-
tcx.hir().for_each_module(|module| tcx.ensure().collect_mod_item_types(module))
166-
});
167-
168161
if tcx.features().rustc_attrs {
169-
tcx.sess.time("outlives_testing", || outlives::test::test_inferred_outlives(tcx))?;
162+
tcx.sess.time("outlives_testing", || outlives::test::test_inferred_outlives(tcx));
163+
tcx.sess.time("variance_testing", || variance::test::test_variance(tcx));
164+
collect::test_opaque_hidden_types(tcx);
170165
}
171166

172167
tcx.sess.time("coherence_checking", || {
@@ -182,14 +177,6 @@ pub fn check_crate(tcx: TyCtxt<'_>) -> Result<(), ErrorGuaranteed> {
182177
let _ = tcx.ensure().crate_inherent_impls_overlap_check(());
183178
});
184179

185-
if tcx.features().rustc_attrs {
186-
tcx.sess.time("variance_testing", || variance::test::test_variance(tcx))?;
187-
}
188-
189-
if tcx.features().rustc_attrs {
190-
collect::test_opaque_hidden_types(tcx)?;
191-
}
192-
193180
// Make sure we evaluate all static and (non-associated) const items, even if unused.
194181
// If any of these fail to evaluate, we do not want this crate to pass compilation.
195182
tcx.hir().par_body_owners(|item_def_id| {
@@ -204,21 +191,6 @@ pub fn check_crate(tcx: TyCtxt<'_>) -> Result<(), ErrorGuaranteed> {
204191
// Freeze definitions as we don't add new ones at this point. This improves performance by
205192
// allowing lock-free access to them.
206193
tcx.untracked().definitions.freeze();
207-
208-
// FIXME: Remove this when we implement creating `DefId`s
209-
// for anon constants during their parents' typeck.
210-
// Typeck all body owners in parallel will produce queries
211-
// cycle errors because it may typeck on anon constants directly.
212-
tcx.hir().par_body_owners(|item_def_id| {
213-
let def_kind = tcx.def_kind(item_def_id);
214-
if !matches!(def_kind, DefKind::AnonConst) {
215-
tcx.ensure().typeck(item_def_id);
216-
}
217-
});
218-
219-
tcx.ensure().check_unused_traits(());
220-
221-
Ok(())
222194
}
223195

224196
/// A quasi-deprecated helper used in rustdoc and clippy to get

compiler/rustc_hir_analysis/src/outlives/test.rs

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

4-
pub fn test_inferred_outlives(tcx: TyCtxt<'_>) -> Result<(), ErrorGuaranteed> {
5-
let mut res = Ok(());
4+
pub fn test_inferred_outlives(tcx: TyCtxt<'_>) {
65
for id in tcx.hir().items() {
76
// For unit testing: check for a special "rustc_outlives"
87
// attribute and report an error with various results if found.
@@ -23,8 +22,7 @@ pub fn test_inferred_outlives(tcx: TyCtxt<'_>) -> Result<(), ErrorGuaranteed> {
2322
for p in pred {
2423
err.note(p);
2524
}
26-
res = Err(err.emit());
25+
err.emit();
2726
}
2827
}
29-
res
3028
}

compiler/rustc_hir_analysis/src/variance/test.rs

+5-8
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,19 @@ use rustc_hir::def::DefKind;
22
use rustc_hir::def_id::CRATE_DEF_ID;
33
use rustc_middle::ty::TyCtxt;
44
use rustc_span::symbol::sym;
5-
use rustc_span::ErrorGuaranteed;
65

76
use crate::errors;
87

9-
pub fn test_variance(tcx: TyCtxt<'_>) -> Result<(), ErrorGuaranteed> {
10-
let mut res = Ok(());
8+
pub fn test_variance(tcx: TyCtxt<'_>) {
119
if tcx.has_attr(CRATE_DEF_ID, sym::rustc_variance_of_opaques) {
1210
for id in tcx.hir().items() {
1311
if matches!(tcx.def_kind(id.owner_id), DefKind::OpaqueTy) {
1412
let variances_of = tcx.variances_of(id.owner_id);
1513

16-
res = Err(tcx.dcx().emit_err(errors::VariancesOf {
14+
tcx.dcx().emit_err(errors::VariancesOf {
1715
span: tcx.def_span(id.owner_id),
1816
variances_of: format!("{variances_of:?}"),
19-
}));
17+
});
2018
}
2119
}
2220
}
@@ -27,11 +25,10 @@ pub fn test_variance(tcx: TyCtxt<'_>) -> Result<(), ErrorGuaranteed> {
2725
if tcx.has_attr(id.owner_id, sym::rustc_variance) {
2826
let variances_of = tcx.variances_of(id.owner_id);
2927

30-
res = Err(tcx.dcx().emit_err(errors::VariancesOf {
28+
tcx.dcx().emit_err(errors::VariancesOf {
3129
span: tcx.def_span(id.owner_id),
3230
variances_of: format!("{variances_of:?}"),
33-
}));
31+
});
3432
}
3533
}
36-
res
3734
}

compiler/rustc_interface/src/passes.rs

+18-15
Original file line numberDiff line numberDiff line change
@@ -727,19 +727,22 @@ fn analysis(tcx: TyCtxt<'_>, (): ()) -> Result<()> {
727727
});
728728

729729
// passes are timed inside typeck
730-
rustc_hir_analysis::check_crate(tcx)?;
730+
rustc_hir_analysis::check_crate(tcx);
731731

732-
sess.time("MIR_borrow_checking", || {
732+
sess.time("typeck_and_mir_analyses", || {
733733
tcx.hir().par_body_owners(|def_id| {
734+
let def_kind = tcx.def_kind(def_id);
735+
// FIXME: Remove this when we implement creating `DefId`s
736+
// for anon constants during their parents' typeck.
737+
// Typeck all body owners in parallel will produce queries
738+
// cycle errors because it may typeck on anon constants directly.
739+
if !matches!(def_kind, rustc_hir::def::DefKind::AnonConst) {
740+
tcx.ensure().typeck(def_id);
741+
}
734742
// Run unsafety check because it's responsible for stealing and
735743
// deallocating THIR.
736744
tcx.ensure().check_unsafety(def_id);
737-
tcx.ensure().mir_borrowck(def_id)
738-
});
739-
});
740-
741-
sess.time("MIR_effect_checking", || {
742-
for def_id in tcx.hir().body_owners() {
745+
tcx.ensure().mir_borrowck(def_id);
743746
if !tcx.sess.opts.unstable_opts.thir_unsafeck {
744747
rustc_mir_transform::check_unsafety::check_unsafety(tcx, def_id);
745748
}
@@ -754,16 +757,16 @@ fn analysis(tcx: TyCtxt<'_>, (): ()) -> Result<()> {
754757
tcx.ensure().mir_drops_elaborated_and_const_checked(def_id);
755758
tcx.ensure().unused_generic_params(ty::InstanceDef::Item(def_id.to_def_id()));
756759
}
757-
}
758-
});
759760

760-
tcx.hir().par_body_owners(|def_id| {
761-
if tcx.is_coroutine(def_id.to_def_id()) {
762-
tcx.ensure().mir_coroutine_witnesses(def_id);
763-
tcx.ensure().check_coroutine_obligations(def_id);
764-
}
761+
if tcx.is_coroutine(def_id.to_def_id()) {
762+
tcx.ensure().mir_coroutine_witnesses(def_id);
763+
tcx.ensure().check_coroutine_obligations(def_id);
764+
}
765+
})
765766
});
766767

768+
tcx.ensure().check_unused_traits(());
769+
767770
sess.time("layout_testing", || layout_test::test_layout(tcx));
768771
sess.time("abi_testing", || abi_test::test_abi(tcx));
769772

compiler/rustc_middle/src/query/mod.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -763,6 +763,7 @@ rustc_queries! {
763763
desc { |tcx| "computing the variances of `{}`", tcx.def_path_str(def_id) }
764764
cache_on_disk_if { def_id.is_local() }
765765
separate_provide_extern
766+
cycle_delay_bug
766767
}
767768

768769
/// Maps from thee `DefId` of a type to its (inferred) outlives.
@@ -960,10 +961,6 @@ rustc_queries! {
960961
ensure_forwards_result_if_red
961962
}
962963

963-
query collect_mod_item_types(key: LocalModDefId) {
964-
desc { |tcx| "collecting item types in {}", describe_as_module(key, tcx) }
965-
}
966-
967964
/// Caches `CoerceUnsized` kinds for impls on custom types.
968965
query coerce_unsized_info(key: DefId) -> Result<ty::adjustment::CoerceUnsizedInfo, ErrorGuaranteed> {
969966
desc { |tcx| "computing CoerceUnsized info for `{}`", tcx.def_path_str(key) }

compiler/rustc_middle/src/values.rs

+21
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,27 @@ impl<'tcx> Value<TyCtxt<'tcx>> for ty::EarlyBinder<ty::Binder<'_, ty::FnSig<'_>>
131131
}
132132
}
133133

134+
impl<'tcx> Value<TyCtxt<'tcx>> for &[ty::Variance] {
135+
fn from_cycle_error(
136+
tcx: TyCtxt<'tcx>,
137+
cycle_error: &CycleError,
138+
_guar: ErrorGuaranteed,
139+
) -> Self {
140+
if let Some(frame) = cycle_error.cycle.get(0)
141+
&& frame.query.dep_kind == dep_kinds::variances_of
142+
&& let Some(def_id) = frame.query.def_id
143+
{
144+
let n = tcx.generics_of(def_id).params.len();
145+
vec![ty::Variance::Bivariant; n].leak()
146+
} else {
147+
span_bug!(
148+
cycle_error.usage.as_ref().unwrap().0,
149+
"only `variances_of` returns `&[ty::Variance]`"
150+
);
151+
}
152+
}
153+
}
154+
134155
// Take a cycle of `Q` and try `try_cycle` on every permutation, falling back to `otherwise`.
135156
fn search_for_cycle_permutation<Q, T>(
136157
cycle: &[Q],

0 commit comments

Comments
 (0)