Skip to content

Commit 1c3d9ab

Browse files
committed
Reduce generics use in the query system.
`QueryStackFrame<I>` is a generic type, and the `I` parameter leaks out to many other related types. However, it only has two instantiations in practice: - `QueryStackFrame<QueryStackFrameExtra>` - `QueryStackFrame<QueryStackDeferred<'tcx>>` And most of the places that currently use `QueryStackFrame<I>` are actually specific to one of the instantiations. This commit removes the unneeded genericity. The following types are only ever used with `QueryStackDeferred<'tcx>`: - QueryMap - QueryJobInfo - QueryJob - QueryWaiter - QueryLatchInfo - QueryLatch - QueryState - QueryResult and their `<I>` parameter is changed to `<'tcx>`. Also, the `QueryContext::QueryInfo` associated type is removed. `CycleError<I>` and `QueryInfo<I>` are still generic over type, because they are used with both instantiations. This required also adding a `<'tcx>` parameter to the traits `QueryDispatcher` and `QueryContext`, which is annoying but I can't see how to avoid it.
1 parent 637e2cb commit 1c3d9ab

9 files changed

Lines changed: 140 additions & 153 deletions

File tree

compiler/rustc_middle/src/query/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ use rustc_index::IndexVec;
8888
use rustc_lint_defs::LintId;
8989
use rustc_macros::rustc_queries;
9090
use rustc_query_system::ich::StableHashingContext;
91-
use rustc_query_system::query::{QueryMode, QueryStackDeferred, QueryState};
91+
use rustc_query_system::query::{QueryMode, QueryState};
9292
use rustc_session::Limits;
9393
use rustc_session::config::{EntryFnType, OptLevel, OutputFilenames, SymbolManglingVersion};
9494
use rustc_session::cstore::{

compiler/rustc_middle/src/query/plumbing.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ macro_rules! define_callbacks {
440440
#[derive(Default)]
441441
pub struct QueryStates<'tcx> {
442442
$(
443-
pub $name: QueryState<$($K)*, QueryStackDeferred<'tcx>>,
443+
pub $name: QueryState<'tcx, $($K)*>,
444444
)*
445445
}
446446

compiler/rustc_query_impl/src/lib.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use rustc_query_system::dep_graph::SerializedDepNodeIndex;
2121
use rustc_query_system::ich::StableHashingContext;
2222
use rustc_query_system::query::{
2323
CycleError, CycleErrorHandling, HashResult, QueryCache, QueryDispatcher, QueryMap, QueryMode,
24-
QueryStackDeferred, QueryState, get_query_incr, get_query_non_incr,
24+
QueryState, get_query_incr, get_query_non_incr,
2525
};
2626
use rustc_span::{ErrorGuaranteed, Span};
2727

@@ -66,7 +66,7 @@ impl<'tcx, C: QueryCache, const ANON: bool, const DEPTH_LIMIT: bool, const FEEDA
6666

6767
// This is `impl QueryDispatcher for SemiDynamicQueryDispatcher`.
6868
impl<'tcx, C: QueryCache, const ANON: bool, const DEPTH_LIMIT: bool, const FEEDABLE: bool>
69-
QueryDispatcher for SemiDynamicQueryDispatcher<'tcx, C, ANON, DEPTH_LIMIT, FEEDABLE>
69+
QueryDispatcher<'tcx> for SemiDynamicQueryDispatcher<'tcx, C, ANON, DEPTH_LIMIT, FEEDABLE>
7070
where
7171
for<'a> C::Key: HashStable<StableHashingContext<'a>>,
7272
{
@@ -86,10 +86,7 @@ where
8686
}
8787

8888
#[inline(always)]
89-
fn query_state<'a>(
90-
self,
91-
qcx: QueryCtxt<'tcx>,
92-
) -> &'a QueryState<Self::Key, QueryStackDeferred<'tcx>>
89+
fn query_state<'a>(self, qcx: QueryCtxt<'tcx>) -> &'a QueryState<'tcx, Self::Key>
9390
where
9491
QueryCtxt<'tcx>: 'a,
9592
{
@@ -98,7 +95,7 @@ where
9895
unsafe {
9996
&*(&qcx.tcx.query_system.states as *const QueryStates<'tcx>)
10097
.byte_add(self.vtable.query_state)
101-
.cast::<QueryState<Self::Key, QueryStackDeferred<'tcx>>>()
98+
.cast::<QueryState<'tcx, Self::Key>>()
10299
}
103100
}
104101

@@ -211,13 +208,15 @@ where
211208
/// on the type `rustc_query_impl::query_impl::$name::QueryType`.
212209
trait QueryDispatcherUnerased<'tcx> {
213210
type UnerasedValue;
214-
type Dispatcher: QueryDispatcher<Qcx = QueryCtxt<'tcx>>;
211+
type Dispatcher: QueryDispatcher<'tcx, Qcx = QueryCtxt<'tcx>>;
215212

216213
const NAME: &'static &'static str;
217214

218215
fn query_dispatcher(tcx: TyCtxt<'tcx>) -> Self::Dispatcher;
219216

220-
fn restore_val(value: <Self::Dispatcher as QueryDispatcher>::Value) -> Self::UnerasedValue;
217+
fn restore_val(
218+
value: <Self::Dispatcher as QueryDispatcher<'tcx>>::Value,
219+
) -> Self::UnerasedValue;
221220
}
222221

223222
pub fn query_system<'a>(

compiler/rustc_query_impl/src/plumbing.rs

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,7 @@ impl<'tcx> HasDepContext for QueryCtxt<'tcx> {
6060
}
6161
}
6262

63-
impl<'tcx> QueryContext for QueryCtxt<'tcx> {
64-
type QueryInfo = QueryStackDeferred<'tcx>;
65-
63+
impl<'tcx> QueryContext<'tcx> for QueryCtxt<'tcx> {
6664
#[inline]
6765
fn jobserver_proxy(&self) -> &Proxy {
6866
&self.tcx.jobserver_proxy
@@ -93,10 +91,7 @@ impl<'tcx> QueryContext for QueryCtxt<'tcx> {
9391
/// Prefer passing `false` to `require_complete` to avoid potential deadlocks,
9492
/// especially when called from within a deadlock handler, unless a
9593
/// complete map is needed and no deadlock is possible at this call site.
96-
fn collect_active_jobs(
97-
self,
98-
require_complete: bool,
99-
) -> Result<QueryMap<QueryStackDeferred<'tcx>>, QueryMap<QueryStackDeferred<'tcx>>> {
94+
fn collect_active_jobs(self, require_complete: bool) -> Result<QueryMap<'tcx>, QueryMap<'tcx>> {
10095
let mut jobs = QueryMap::default();
10196
let mut complete = true;
10297

@@ -415,7 +410,7 @@ pub(crate) fn encode_query_results<'a, 'tcx, Q>(
415410
}
416411

417412
pub(crate) fn query_key_hash_verify<'tcx>(
418-
query: impl QueryDispatcher<Qcx = QueryCtxt<'tcx>>,
413+
query: impl QueryDispatcher<'tcx, Qcx = QueryCtxt<'tcx>>,
419414
qcx: QueryCtxt<'tcx>,
420415
) {
421416
let _timer = qcx.tcx.prof.generic_activity_with_arg("query_key_hash_verify_for", query.name());
@@ -443,7 +438,7 @@ pub(crate) fn query_key_hash_verify<'tcx>(
443438

444439
fn try_load_from_on_disk_cache<'tcx, Q>(query: Q, tcx: TyCtxt<'tcx>, dep_node: DepNode)
445440
where
446-
Q: QueryDispatcher<Qcx = QueryCtxt<'tcx>>,
441+
Q: QueryDispatcher<'tcx, Qcx = QueryCtxt<'tcx>>,
447442
{
448443
debug_assert!(tcx.dep_graph.is_green(&dep_node));
449444

@@ -489,7 +484,7 @@ where
489484

490485
fn force_from_dep_node<'tcx, Q>(query: Q, tcx: TyCtxt<'tcx>, dep_node: DepNode) -> bool
491486
where
492-
Q: QueryDispatcher<Qcx = QueryCtxt<'tcx>>,
487+
Q: QueryDispatcher<'tcx, Qcx = QueryCtxt<'tcx>>,
493488
{
494489
// We must avoid ever having to call `force_from_dep_node()` for a
495490
// `DepNode::codegen_unit`:
@@ -732,14 +727,14 @@ macro_rules! define_queries {
732727
}
733728

734729
#[inline(always)]
735-
fn restore_val(value: <Self::Dispatcher as QueryDispatcher>::Value) -> Self::UnerasedValue {
730+
fn restore_val(value: <Self::Dispatcher as QueryDispatcher<'tcx>>::Value) -> Self::UnerasedValue {
736731
erase::restore_val::<queries::$name::Value<'tcx>>(value)
737732
}
738733
}
739734

740735
pub(crate) fn collect_active_jobs<'tcx>(
741736
tcx: TyCtxt<'tcx>,
742-
qmap: &mut QueryMap<QueryStackDeferred<'tcx>>,
737+
qmap: &mut QueryMap<'tcx>,
743738
require_complete: bool,
744739
) -> Option<()> {
745740
let make_query = |tcx, key| {
@@ -823,7 +818,7 @@ macro_rules! define_queries {
823818
// These arrays are used for iteration and can't be indexed by `DepKind`.
824819

825820
const COLLECT_ACTIVE_JOBS: &[
826-
for<'tcx> fn(TyCtxt<'tcx>, &mut QueryMap<QueryStackDeferred<'tcx>>, bool) -> Option<()>
821+
for<'tcx> fn(TyCtxt<'tcx>, &mut QueryMap<'tcx>, bool) -> Option<()>
827822
] =
828823
&[$(query_impl::$name::collect_active_jobs),*];
829824

compiler/rustc_query_system/src/dep_graph/graph.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,11 @@ impl<D: Deps> DepGraph<D> {
519519
/// This encodes a diagnostic by creating a node with an unique index and associating
520520
/// `diagnostic` with it, for use in the next session.
521521
#[inline]
522-
pub fn record_diagnostic<Qcx: QueryContext>(&self, qcx: Qcx, diagnostic: &DiagInner) {
522+
pub fn record_diagnostic<'tcx, Qcx: QueryContext<'tcx>>(
523+
&self,
524+
qcx: Qcx,
525+
diagnostic: &DiagInner,
526+
) {
523527
if let Some(ref data) = self.data {
524528
D::read_deps(|task_deps| match task_deps {
525529
TaskDepsRef::EvalAlways | TaskDepsRef::Ignore => return,
@@ -532,7 +536,7 @@ impl<D: Deps> DepGraph<D> {
532536
/// This forces a diagnostic node green by running its side effect. `prev_index` would
533537
/// refer to a node created used `encode_diagnostic` in the previous session.
534538
#[inline]
535-
pub fn force_diagnostic_node<Qcx: QueryContext>(
539+
pub fn force_diagnostic_node<'tcx, Qcx: QueryContext<'tcx>>(
536540
&self,
537541
qcx: Qcx,
538542
prev_index: SerializedDepNodeIndex,
@@ -669,7 +673,7 @@ impl<D: Deps> DepGraphData<D> {
669673
/// This encodes a diagnostic by creating a node with an unique index and associating
670674
/// `diagnostic` with it, for use in the next session.
671675
#[inline]
672-
fn encode_diagnostic<Qcx: QueryContext>(
676+
fn encode_diagnostic<'tcx, Qcx: QueryContext<'tcx>>(
673677
&self,
674678
qcx: Qcx,
675679
diagnostic: &DiagInner,
@@ -693,7 +697,7 @@ impl<D: Deps> DepGraphData<D> {
693697
/// This forces a diagnostic node green by running its side effect. `prev_index` would
694698
/// refer to a node created used `encode_diagnostic` in the previous session.
695699
#[inline]
696-
fn force_diagnostic_node<Qcx: QueryContext>(
700+
fn force_diagnostic_node<'tcx, Qcx: QueryContext<'tcx>>(
697701
&self,
698702
qcx: Qcx,
699703
prev_index: SerializedDepNodeIndex,
@@ -843,7 +847,7 @@ impl<D: Deps> DepGraph<D> {
843847
DepNodeColor::Unknown
844848
}
845849

846-
pub fn try_mark_green<Qcx: QueryContext<Deps = D>>(
850+
pub fn try_mark_green<'tcx, Qcx: QueryContext<'tcx, Deps = D>>(
847851
&self,
848852
qcx: Qcx,
849853
dep_node: &DepNode,
@@ -858,7 +862,7 @@ impl<D: Deps> DepGraphData<D> {
858862
/// A node will have an index, when it's already been marked green, or when we can mark it
859863
/// green. This function will mark the current task as a reader of the specified node, when
860864
/// a node index can be found for that node.
861-
pub(crate) fn try_mark_green<Qcx: QueryContext<Deps = D>>(
865+
pub(crate) fn try_mark_green<'tcx, Qcx: QueryContext<'tcx, Deps = D>>(
862866
&self,
863867
qcx: Qcx,
864868
dep_node: &DepNode,
@@ -883,7 +887,7 @@ impl<D: Deps> DepGraphData<D> {
883887
}
884888

885889
#[instrument(skip(self, qcx, parent_dep_node_index, frame), level = "debug")]
886-
fn try_mark_parent_green<Qcx: QueryContext<Deps = D>>(
890+
fn try_mark_parent_green<'tcx, Qcx: QueryContext<'tcx, Deps = D>>(
887891
&self,
888892
qcx: Qcx,
889893
parent_dep_node_index: SerializedDepNodeIndex,
@@ -973,7 +977,7 @@ impl<D: Deps> DepGraphData<D> {
973977

974978
/// Try to mark a dep-node which existed in the previous compilation session as green.
975979
#[instrument(skip(self, qcx, prev_dep_node_index, frame), level = "debug")]
976-
fn try_mark_previous_green<Qcx: QueryContext<Deps = D>>(
980+
fn try_mark_previous_green<'tcx, Qcx: QueryContext<'tcx, Deps = D>>(
977981
&self,
978982
qcx: Qcx,
979983
prev_dep_node_index: SerializedDepNodeIndex,

compiler/rustc_query_system/src/query/dispatcher.rs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ pub type HashResult<V> = Option<fn(&mut StableHashingContext<'_>, &V) -> Fingerp
1414

1515
/// Unambiguous shorthand for `<This::Qcx as HasDepContext>::DepContext`.
1616
#[expect(type_alias_bounds)]
17-
type DepContextOf<This: QueryDispatcher> =
18-
<<This as QueryDispatcher>::Qcx as HasDepContext>::DepContext;
17+
type DepContextOf<'tcx, This: QueryDispatcher<'tcx>> =
18+
<<This as QueryDispatcher<'tcx>>::Qcx as HasDepContext>::DepContext;
1919

2020
/// Trait that can be used as a vtable for a single query, providing operations
2121
/// and metadata for that query.
@@ -25,34 +25,31 @@ type DepContextOf<This: QueryDispatcher> =
2525
/// Those types are not visible from this `rustc_query_system` crate.
2626
///
2727
/// "Dispatcher" should be understood as a near-synonym of "vtable".
28-
pub trait QueryDispatcher: Copy {
28+
pub trait QueryDispatcher<'tcx>: Copy {
2929
fn name(self) -> &'static str;
3030

3131
/// Query context used by this dispatcher, i.e. `rustc_query_impl::QueryCtxt`.
32-
type Qcx: QueryContext;
32+
type Qcx: QueryContext<'tcx>;
3333

3434
// `Key` and `Value` are `Copy` instead of `Clone` to ensure copying them stays cheap,
3535
// but it isn't necessary.
36-
type Key: DepNodeParams<DepContextOf<Self>> + Eq + Hash + Copy + Debug;
36+
type Key: DepNodeParams<DepContextOf<'tcx, Self>> + Eq + Hash + Copy + Debug;
3737
type Value: Copy;
3838

3939
type Cache: QueryCache<Key = Self::Key, Value = Self::Value>;
4040

4141
fn format_value(self) -> fn(&Self::Value) -> String;
4242

4343
// Don't use this method to access query results, instead use the methods on TyCtxt
44-
fn query_state<'a>(
45-
self,
46-
tcx: Self::Qcx,
47-
) -> &'a QueryState<Self::Key, <Self::Qcx as QueryContext>::QueryInfo>;
44+
fn query_state<'a>(self, tcx: Self::Qcx) -> &'a QueryState<'tcx, Self::Key>;
4845

4946
// Don't use this method to access query results, instead use the methods on TyCtxt
5047
fn query_cache<'a>(self, tcx: Self::Qcx) -> &'a Self::Cache;
5148

52-
fn cache_on_disk(self, tcx: DepContextOf<Self>, key: &Self::Key) -> bool;
49+
fn cache_on_disk(self, tcx: DepContextOf<'tcx, Self>, key: &Self::Key) -> bool;
5350

5451
// Don't use this method to compute query results, instead use the methods on TyCtxt
55-
fn execute_query(self, tcx: DepContextOf<Self>, k: Self::Key) -> Self::Value;
52+
fn execute_query(self, tcx: DepContextOf<'tcx, Self>, k: Self::Key) -> Self::Value;
5653

5754
fn compute(self, tcx: Self::Qcx, key: Self::Key) -> Self::Value;
5855

@@ -74,7 +71,7 @@ pub trait QueryDispatcher: Copy {
7471
/// Synthesize an error value to let compilation continue after a cycle.
7572
fn value_from_cycle_error(
7673
self,
77-
tcx: DepContextOf<Self>,
74+
tcx: DepContextOf<'tcx, Self>,
7875
cycle_error: &CycleError<QueryStackFrameExtra>,
7976
guar: ErrorGuaranteed,
8077
) -> Self::Value;
@@ -89,7 +86,7 @@ pub trait QueryDispatcher: Copy {
8986
fn hash_result(self) -> HashResult<Self::Value>;
9087

9188
// Just here for convenience and checking that the key matches the kind, don't override this.
92-
fn construct_dep_node(self, tcx: DepContextOf<Self>, key: &Self::Key) -> DepNode {
89+
fn construct_dep_node(self, tcx: DepContextOf<'tcx, Self>, key: &Self::Key) -> DepNode {
9390
DepNode::construct(tcx, self.dep_kind(), key)
9491
}
9592
}

0 commit comments

Comments
 (0)