Skip to content

Commit 9852ace

Browse files
authored
Rollup merge of #143394 - workingjubilee:reorganize-hir-analysis-provide-fn, r=compiler-errors
compiler: Document and reduce `fn provide`s in hir crates I found it hard to follow all these tiny micro-indirections. Much like you shouldn't pass around `&u32` if you can help it, you probably shouldn't use an indirection if the indirection overhead itself is literally bigger than the amount of data you are organizing. Generally a new `fn provide` amounts to around 3 LOC: - the signature with opening brace - the `rustc_middle::query::Providers` import - an end brace I am not even counting the cost in time and thought to go find the other `provide`, read it, understand, "Ah, yes, these functions", and then go to those. Thus I say we should collapse indirections of `provide` for modules that only export 1~2 queries. For higher-count indirections, I left them as-is, as I don't understand the crate well enough to judge their worth. Then I dropped a pointer to the actual module of interest for all these instances of the same function. I think documenting them is important because the comment that it relates to the query system makes it obvious that they have **nothing** to do with the rest of the module's logic and I can carry on ignoring them. Actively doing so is another cognitive cost, but much more minimal. There is also a small correctness issue in that all of these functions are technically mutating state. It's not a huge deal, but it's still easier to check all these mutations do not overlap if we have less instances of `fn provide` to check.
2 parents 01fe1c0 + 3b7f9f9 commit 9852ace

File tree

13 files changed

+36
-52
lines changed

13 files changed

+36
-52
lines changed

compiler/rustc_hir_analysis/src/check/mod.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,15 +100,17 @@ use self::compare_impl_item::collect_return_position_impl_trait_in_trait_tys;
100100
use self::region::region_scope_tree;
101101
use crate::{errors, require_c_abi_if_c_variadic};
102102

103-
pub fn provide(providers: &mut Providers) {
104-
wfcheck::provide(providers);
103+
/// Adds query implementations to the [Providers] vtable, see [`rustc_middle::query`]
104+
pub(super) fn provide(providers: &mut Providers) {
105105
*providers = Providers {
106106
adt_destructor,
107107
adt_async_destructor,
108108
region_scope_tree,
109109
collect_return_position_impl_trait_in_trait_tys,
110110
compare_impl_item: compare_impl_item::compare_impl_item,
111111
check_coroutine_obligations: check::check_coroutine_obligations,
112+
check_type_wf: wfcheck::check_type_wf,
113+
check_well_formed: wfcheck::check_well_formed,
112114
..*providers
113115
};
114116
}

compiler/rustc_hir_analysis/src/check/wfcheck.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ use rustc_infer::infer::{self, InferCtxt, SubregionOrigin, TyCtxtInferExt};
1515
use rustc_lint_defs::builtin::SUPERTRAIT_ITEM_SHADOWING_DEFINITION;
1616
use rustc_macros::LintDiagnostic;
1717
use rustc_middle::mir::interpret::ErrorHandled;
18-
use rustc_middle::query::Providers;
1918
use rustc_middle::traits::solve::NoSolution;
2019
use rustc_middle::ty::trait_def::TraitSpecializationKind;
2120
use rustc_middle::ty::{
@@ -189,7 +188,10 @@ where
189188
}
190189
}
191190

192-
fn check_well_formed(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Result<(), ErrorGuaranteed> {
191+
pub(super) fn check_well_formed(
192+
tcx: TyCtxt<'_>,
193+
def_id: LocalDefId,
194+
) -> Result<(), ErrorGuaranteed> {
193195
let mut res = crate::check::check::check_item_type(tcx, def_id);
194196

195197
for param in &tcx.generics_of(def_id).own_params {
@@ -2249,7 +2251,7 @@ impl<'tcx> WfCheckingCtxt<'_, 'tcx> {
22492251
}
22502252
}
22512253

2252-
fn check_type_wf(tcx: TyCtxt<'_>, (): ()) -> Result<(), ErrorGuaranteed> {
2254+
pub(super) fn check_type_wf(tcx: TyCtxt<'_>, (): ()) -> Result<(), ErrorGuaranteed> {
22532255
let items = tcx.hir_crate_items(());
22542256
let res = items
22552257
.par_items(|item| tcx.ensure_ok().check_well_formed(item.owner_id.def_id))
@@ -2397,7 +2399,3 @@ struct RedundantLifetimeArgsLint<'tcx> {
23972399
// The lifetime we can replace the victim with.
23982400
candidate: ty::Region<'tcx>,
23992401
}
2400-
2401-
pub fn provide(providers: &mut Providers) {
2402-
*providers = Providers { check_type_wf, check_well_formed, ..*providers };
2403-
}

compiler/rustc_hir_analysis/src/check_unused.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
11
use rustc_data_structures::unord::{ExtendUnord, UnordSet};
22
use rustc_hir::def::DefKind;
33
use rustc_hir::def_id::LocalDefId;
4-
use rustc_middle::query::Providers;
54
use rustc_middle::ty::TyCtxt;
65
use rustc_session::lint;
76
use tracing::debug;
87

9-
pub(crate) fn provide(providers: &mut Providers) {
10-
*providers = Providers { check_unused_traits, ..*providers };
11-
}
12-
13-
fn check_unused_traits(tcx: TyCtxt<'_>, (): ()) {
8+
pub(super) fn check_unused_traits(tcx: TyCtxt<'_>, (): ()) {
149
let mut used_trait_imports = UnordSet::<LocalDefId>::default();
1510

1611
// FIXME: Use `tcx.hir_par_body_owners()` when we implement creating `DefId`s

compiler/rustc_hir_analysis/src/coherence/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ fn enforce_empty_impls_for_marker_traits(
130130
.emit())
131131
}
132132

133+
/// Adds query implementations to the [Providers] vtable, see [`rustc_middle::query`].
133134
pub(crate) fn provide(providers: &mut Providers) {
134135
use self::builtin::coerce_unsized_info;
135136
use self::inherent_impls::{

compiler/rustc_hir_analysis/src/collect.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ mod type_of;
6161

6262
///////////////////////////////////////////////////////////////////////////
6363

64+
/// Adds query implementations to the [Providers] vtable, see [`rustc_middle::query`]
6465
pub(crate) fn provide(providers: &mut Providers) {
6566
resolve_bound_vars::provide(providers);
6667
*providers = Providers {

compiler/rustc_hir_analysis/src/collect/resolve_bound_vars.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ enum BinderScopeType {
222222

223223
type ScopeRef<'a> = &'a Scope<'a>;
224224

225+
/// Adds query implementations to the [Providers] vtable, see [`rustc_middle::query`]
225226
pub(crate) fn provide(providers: &mut Providers) {
226227
*providers = Providers {
227228
resolve_bound_vars,

compiler/rustc_hir_analysis/src/hir_wf_check.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,16 @@ use rustc_hir::{self as hir, AmbigArg, ForeignItem, ForeignItemKind};
44
use rustc_infer::infer::TyCtxtInferExt;
55
use rustc_infer::traits::{ObligationCause, ObligationCauseCode, WellFormedLoc};
66
use rustc_middle::bug;
7-
use rustc_middle::query::Providers;
87
use rustc_middle::ty::{self, TyCtxt, TypeVisitableExt, TypingMode, fold_regions};
98
use rustc_span::def_id::LocalDefId;
109
use rustc_trait_selection::traits::{self, ObligationCtxt};
1110
use tracing::debug;
1211

1312
use crate::collect::ItemCtxt;
1413

15-
pub(crate) fn provide(providers: &mut Providers) {
16-
*providers = Providers { diagnostic_hir_wf_check, ..*providers };
17-
}
18-
1914
// Ideally, this would be in `rustc_trait_selection`, but we
2015
// need access to `ItemCtxt`
21-
fn diagnostic_hir_wf_check<'tcx>(
16+
pub(super) fn diagnostic_hir_wf_check<'tcx>(
2217
tcx: TyCtxt<'tcx>,
2318
(predicate, loc): (ty::Predicate<'tcx>, WellFormedLoc),
2419
) -> Option<ObligationCause<'tcx>> {

compiler/rustc_hir_analysis/src/lib.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,18 +150,21 @@ fn require_c_abi_if_c_variadic(
150150
.emit();
151151
}
152152

153+
/// Adds query implementations to the [Providers] vtable, see [`rustc_middle::query`]
153154
pub fn provide(providers: &mut Providers) {
154155
collect::provide(providers);
155156
coherence::provide(providers);
156157
check::provide(providers);
157-
check_unused::provide(providers);
158-
variance::provide(providers);
159-
outlives::provide(providers);
160-
hir_wf_check::provide(providers);
161158
*providers = Providers {
159+
check_unused_traits: check_unused::check_unused_traits,
160+
diagnostic_hir_wf_check: hir_wf_check::diagnostic_hir_wf_check,
161+
inferred_outlives_crate: outlives::inferred_outlives_crate,
162+
inferred_outlives_of: outlives::inferred_outlives_of,
162163
inherit_sig_for_delegation_item: delegation::inherit_sig_for_delegation_item,
163164
enforce_impl_non_lifetime_params_are_constrained:
164165
impl_wf_check::enforce_impl_non_lifetime_params_are_constrained,
166+
crate_variances: variance::crate_variances,
167+
variances_of: variance::variances_of,
165168
..*providers
166169
};
167170
}

compiler/rustc_hir_analysis/src/outlives/mod.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use rustc_hir::def::DefKind;
22
use rustc_hir::def_id::LocalDefId;
3-
use rustc_middle::query::Providers;
43
use rustc_middle::ty::{self, CratePredicatesMap, GenericArgKind, TyCtxt, Upcast};
54
use rustc_span::Span;
65

@@ -9,11 +8,10 @@ mod explicit;
98
mod implicit_infer;
109
mod utils;
1110

12-
pub(crate) fn provide(providers: &mut Providers) {
13-
*providers = Providers { inferred_outlives_of, inferred_outlives_crate, ..*providers };
14-
}
15-
16-
fn inferred_outlives_of(tcx: TyCtxt<'_>, item_def_id: LocalDefId) -> &[(ty::Clause<'_>, Span)] {
11+
pub(super) fn inferred_outlives_of(
12+
tcx: TyCtxt<'_>,
13+
item_def_id: LocalDefId,
14+
) -> &[(ty::Clause<'_>, Span)] {
1715
match tcx.def_kind(item_def_id) {
1816
DefKind::Struct | DefKind::Enum | DefKind::Union => {
1917
let crate_map = tcx.inferred_outlives_crate(());
@@ -48,7 +46,7 @@ fn inferred_outlives_of(tcx: TyCtxt<'_>, item_def_id: LocalDefId) -> &[(ty::Clau
4846
}
4947
}
5048

51-
fn inferred_outlives_crate(tcx: TyCtxt<'_>, (): ()) -> CratePredicatesMap<'_> {
49+
pub(super) fn inferred_outlives_crate(tcx: TyCtxt<'_>, (): ()) -> CratePredicatesMap<'_> {
5250
// Compute a map from each ADT (struct/enum/union) and lazy type alias to
5351
// the **explicit** outlives predicates (`T: 'a`, `'a: 'b`) that the user wrote.
5452
// Typically there won't be many of these, except in older code where

compiler/rustc_hir_analysis/src/variance/mod.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use rustc_arena::DroplessArena;
88
use rustc_hir as hir;
99
use rustc_hir::def::DefKind;
1010
use rustc_hir::def_id::{DefId, LocalDefId};
11-
use rustc_middle::query::Providers;
1211
use rustc_middle::span_bug;
1312
use rustc_middle::ty::{
1413
self, CrateVariancesMap, GenericArgsRef, Ty, TyCtxt, TypeSuperVisitable, TypeVisitable,
@@ -27,18 +26,14 @@ mod solve;
2726

2827
pub(crate) mod dump;
2928

30-
pub(crate) fn provide(providers: &mut Providers) {
31-
*providers = Providers { variances_of, crate_variances, ..*providers };
32-
}
33-
34-
fn crate_variances(tcx: TyCtxt<'_>, (): ()) -> CrateVariancesMap<'_> {
29+
pub(super) fn crate_variances(tcx: TyCtxt<'_>, (): ()) -> CrateVariancesMap<'_> {
3530
let arena = DroplessArena::default();
3631
let terms_cx = terms::determine_parameters_to_be_inferred(tcx, &arena);
3732
let constraints_cx = constraints::add_constraints_from_crate(terms_cx);
3833
solve::solve_constraints(constraints_cx)
3934
}
4035

41-
fn variances_of(tcx: TyCtxt<'_>, item_def_id: LocalDefId) -> &[ty::Variance] {
36+
pub(super) fn variances_of(tcx: TyCtxt<'_>, item_def_id: LocalDefId) -> &[ty::Variance] {
4237
// Skip items with no generics - there's nothing to infer in them.
4338
if tcx.generics_of(item_def_id).is_empty() {
4439
return &[];

compiler/rustc_hir_typeck/src/lib.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,12 @@ fn fatally_break_rust(tcx: TyCtxt<'_>, span: Span) -> ! {
536536
diag.emit()
537537
}
538538

539+
/// Adds query implementations to the [Providers] vtable, see [`rustc_middle::query`]
539540
pub fn provide(providers: &mut Providers) {
540-
method::provide(providers);
541-
*providers = Providers { typeck, used_trait_imports, ..*providers };
541+
*providers = Providers {
542+
method_autoderef_steps: method::probe::method_autoderef_steps,
543+
typeck,
544+
used_trait_imports,
545+
..*providers
546+
};
542547
}

compiler/rustc_hir_typeck/src/method/mod.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ use rustc_hir::def::{CtorOf, DefKind, Namespace};
1313
use rustc_hir::def_id::DefId;
1414
use rustc_infer::infer::{BoundRegionConversionTime, InferOk};
1515
use rustc_infer::traits::PredicateObligations;
16-
use rustc_middle::query::Providers;
1716
use rustc_middle::traits::ObligationCause;
1817
use rustc_middle::ty::{
1918
self, GenericArgs, GenericArgsRef, GenericParamDefKind, Ty, TypeVisitableExt,
@@ -28,10 +27,6 @@ pub(crate) use self::MethodError::*;
2827
use self::probe::{IsSuggestion, ProbeScope};
2928
use crate::FnCtxt;
3029

31-
pub(crate) fn provide(providers: &mut Providers) {
32-
probe::provide(providers);
33-
}
34-
3530
#[derive(Clone, Copy, Debug)]
3631
pub(crate) struct MethodCallee<'tcx> {
3732
/// Impl method ID, for inherent methods, or trait method ID, otherwise.

compiler/rustc_hir_typeck/src/method/probe.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ use rustc_infer::infer::canonical::{Canonical, OriginalQueryValues, QueryRespons
1515
use rustc_infer::infer::{BoundRegionConversionTime, DefineOpaqueTypes, InferOk, TyCtxtInferExt};
1616
use rustc_infer::traits::ObligationCauseCode;
1717
use rustc_middle::middle::stability;
18-
use rustc_middle::query::Providers;
1918
use rustc_middle::ty::elaborate::supertrait_def_ids;
2019
use rustc_middle::ty::fast_reject::{DeepRejectCtxt, TreatParams, simplify_type};
2120
use rustc_middle::ty::{
@@ -554,11 +553,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
554553
}
555554
}
556555

557-
pub(crate) fn provide(providers: &mut Providers) {
558-
providers.method_autoderef_steps = method_autoderef_steps;
559-
}
560-
561-
fn method_autoderef_steps<'tcx>(
556+
pub(crate) fn method_autoderef_steps<'tcx>(
562557
tcx: TyCtxt<'tcx>,
563558
goal: CanonicalTyGoal<'tcx>,
564559
) -> MethodAutoderefStepsResult<'tcx> {

0 commit comments

Comments
 (0)