Skip to content

Commit a68e0e4

Browse files
committed
Make QueryDispatcher::Qcx an associated type
1 parent de6d33c commit a68e0e4

File tree

4 files changed

+86
-92
lines changed

4 files changed

+86
-92
lines changed

compiler/rustc_query_impl/src/lib.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,11 @@ impl<'tcx, C: QueryCache, const ANON: bool, const DEPTH_LIMIT: bool, const FEEDA
6767

6868
// This is `impl QueryDispatcher for SemiDynamicQueryDispatcher`.
6969
impl<'tcx, C: QueryCache, const ANON: bool, const DEPTH_LIMIT: bool, const FEEDABLE: bool>
70-
QueryDispatcher<QueryCtxt<'tcx>>
71-
for SemiDynamicQueryDispatcher<'tcx, C, ANON, DEPTH_LIMIT, FEEDABLE>
70+
QueryDispatcher for SemiDynamicQueryDispatcher<'tcx, C, ANON, DEPTH_LIMIT, FEEDABLE>
7271
where
7372
for<'a> C::Key: HashStable<StableHashingContext<'a>>,
7473
{
74+
type Qcx = QueryCtxt<'tcx>;
7575
type Key = C::Key;
7676
type Value = C::Value;
7777
type Cache = C;
@@ -104,10 +104,7 @@ where
104104
}
105105

106106
#[inline(always)]
107-
fn query_cache<'a>(self, qcx: QueryCtxt<'tcx>) -> &'a Self::Cache
108-
where
109-
'tcx: 'a,
110-
{
107+
fn query_cache<'a>(self, qcx: QueryCtxt<'tcx>) -> &'a Self::Cache {
111108
// Safety:
112109
// This is just manually doing the subfield referencing through pointer math.
113110
unsafe {
@@ -215,15 +212,13 @@ where
215212
/// on the type `rustc_query_impl::query_impl::$name::QueryType`.
216213
trait QueryDispatcherUnerased<'tcx> {
217214
type UnerasedValue;
218-
type Dispatcher: QueryDispatcher<QueryCtxt<'tcx>>;
215+
type Dispatcher: QueryDispatcher<Qcx = QueryCtxt<'tcx>>;
219216

220217
const NAME: &'static &'static str;
221218

222219
fn query_dispatcher(tcx: TyCtxt<'tcx>) -> Self::Dispatcher;
223220

224-
fn restore_val(
225-
value: <Self::Dispatcher as QueryDispatcher<QueryCtxt<'tcx>>>::Value,
226-
) -> Self::UnerasedValue;
221+
fn restore_val(value: <Self::Dispatcher as QueryDispatcher>::Value) -> Self::UnerasedValue;
227222
}
228223

229224
pub fn query_system<'a>(

compiler/rustc_query_impl/src/plumbing.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ pub(crate) fn encode_query_results<'a, 'tcx, Q>(
414414
}
415415

416416
pub(crate) fn query_key_hash_verify<'tcx>(
417-
query: impl QueryDispatcher<QueryCtxt<'tcx>>,
417+
query: impl QueryDispatcher<Qcx = QueryCtxt<'tcx>>,
418418
qcx: QueryCtxt<'tcx>,
419419
) {
420420
let _timer = qcx.tcx.prof.generic_activity_with_arg("query_key_hash_verify_for", query.name());
@@ -442,7 +442,7 @@ pub(crate) fn query_key_hash_verify<'tcx>(
442442

443443
fn try_load_from_on_disk_cache<'tcx, Q>(query: Q, tcx: TyCtxt<'tcx>, dep_node: DepNode)
444444
where
445-
Q: QueryDispatcher<QueryCtxt<'tcx>>,
445+
Q: QueryDispatcher<Qcx = QueryCtxt<'tcx>>,
446446
{
447447
debug_assert!(tcx.dep_graph.is_green(&dep_node));
448448

@@ -488,7 +488,7 @@ where
488488

489489
fn force_from_dep_node<'tcx, Q>(query: Q, tcx: TyCtxt<'tcx>, dep_node: DepNode) -> bool
490490
where
491-
Q: QueryDispatcher<QueryCtxt<'tcx>>,
491+
Q: QueryDispatcher<Qcx = QueryCtxt<'tcx>>,
492492
{
493493
// We must avoid ever having to call `force_from_dep_node()` for a
494494
// `DepNode::codegen_unit`:
@@ -523,8 +523,7 @@ pub(crate) fn make_dep_kind_vtable_for_query<'tcx, Q>(
523523
where
524524
Q: QueryDispatcherUnerased<'tcx>,
525525
{
526-
let fingerprint_style =
527-
<Q::Dispatcher as QueryDispatcher<QueryCtxt<'tcx>>>::Key::fingerprint_style();
526+
let fingerprint_style = <Q::Dispatcher as QueryDispatcher>::Key::fingerprint_style();
528527

529528
if is_anon || !fingerprint_style.reconstructible() {
530529
return DepKindVTable {
@@ -731,7 +730,7 @@ macro_rules! define_queries {
731730
}
732731

733732
#[inline(always)]
734-
fn restore_val(value: <Self::Dispatcher as QueryDispatcher<QueryCtxt<'tcx>>>::Value) -> Self::UnerasedValue {
733+
fn restore_val(value: <Self::Dispatcher as QueryDispatcher>::Value) -> Self::UnerasedValue {
735734
restore::<queries::$name::Value<'tcx>>(value)
736735
}
737736
}

compiler/rustc_query_system/src/query/dispatcher.rs

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,18 @@ use rustc_data_structures::fingerprint::Fingerprint;
55
use rustc_span::ErrorGuaranteed;
66

77
use super::QueryStackFrameExtra;
8-
use crate::dep_graph::{DepKind, DepNode, DepNodeParams, SerializedDepNodeIndex};
8+
use crate::dep_graph::{DepKind, DepNode, DepNodeParams, HasDepContext, SerializedDepNodeIndex};
99
use crate::ich::StableHashingContext;
1010
use crate::query::caches::QueryCache;
1111
use crate::query::{CycleError, CycleErrorHandling, DepNodeIndex, QueryContext, QueryState};
1212

1313
pub type HashResult<V> = Option<fn(&mut StableHashingContext<'_>, &V) -> Fingerprint>;
1414

15+
/// Unambiguous shorthand for `<This::Qcx as HasDepContext>::DepContext`.
16+
#[expect(type_alias_bounds)]
17+
type DepContext<This: QueryDispatcher> =
18+
<<This as QueryDispatcher>::Qcx as HasDepContext>::DepContext;
19+
1520
/// Trait that can be used as a vtable for a single query, providing operations
1621
/// and metadata for that query.
1722
///
@@ -20,49 +25,56 @@ pub type HashResult<V> = Option<fn(&mut StableHashingContext<'_>, &V) -> Fingerp
2025
/// Those types are not visible from this `rustc_query_system` crate.
2126
///
2227
/// "Dispatcher" should be understood as a near-synonym of "vtable".
23-
pub trait QueryDispatcher<Qcx: QueryContext>: Copy {
28+
pub trait QueryDispatcher: Copy {
2429
fn name(self) -> &'static str;
2530

31+
/// Query context used by this dispatcher, i.e. `rustc_query_impl::QueryCtxt`.
32+
type Qcx: QueryContext;
33+
2634
// `Key` and `Value` are `Copy` instead of `Clone` to ensure copying them stays cheap,
2735
// but it isn't necessary.
28-
type Key: DepNodeParams<Qcx::DepContext> + Eq + Hash + Copy + Debug;
36+
type Key: DepNodeParams<DepContext<Self>> + Eq + Hash + Copy + Debug;
2937
type Value: Copy;
3038

3139
type Cache: QueryCache<Key = Self::Key, Value = Self::Value>;
3240

3341
fn format_value(self) -> fn(&Self::Value) -> String;
3442

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

4049
// Don't use this method to access query results, instead use the methods on TyCtxt
41-
fn query_cache<'a>(self, tcx: Qcx) -> &'a Self::Cache
42-
where
43-
Qcx: 'a;
50+
fn query_cache<'a>(self, tcx: Self::Qcx) -> &'a Self::Cache;
4451

45-
fn cache_on_disk(self, tcx: Qcx::DepContext, key: &Self::Key) -> bool;
52+
fn cache_on_disk(self, tcx: DepContext<Self>, key: &Self::Key) -> bool;
4653

4754
// Don't use this method to compute query results, instead use the methods on TyCtxt
48-
fn execute_query(self, tcx: Qcx::DepContext, k: Self::Key) -> Self::Value;
55+
fn execute_query(self, tcx: DepContext<Self>, k: Self::Key) -> Self::Value;
4956

50-
fn compute(self, tcx: Qcx, key: Self::Key) -> Self::Value;
57+
fn compute(self, tcx: Self::Qcx, key: Self::Key) -> Self::Value;
5158

5259
fn try_load_from_disk(
5360
self,
54-
tcx: Qcx,
61+
tcx: Self::Qcx,
5562
key: &Self::Key,
5663
prev_index: SerializedDepNodeIndex,
5764
index: DepNodeIndex,
5865
) -> Option<Self::Value>;
5966

60-
fn loadable_from_disk(self, qcx: Qcx, key: &Self::Key, idx: SerializedDepNodeIndex) -> bool;
67+
fn loadable_from_disk(
68+
self,
69+
qcx: Self::Qcx,
70+
key: &Self::Key,
71+
idx: SerializedDepNodeIndex,
72+
) -> bool;
6173

6274
/// Synthesize an error value to let compilation continue after a cycle.
6375
fn value_from_cycle_error(
6476
self,
65-
tcx: Qcx::DepContext,
77+
tcx: DepContext<Self>,
6678
cycle_error: &CycleError<QueryStackFrameExtra>,
6779
guar: ErrorGuaranteed,
6880
) -> Self::Value;
@@ -77,7 +89,7 @@ pub trait QueryDispatcher<Qcx: QueryContext>: Copy {
7789
fn hash_result(self) -> HashResult<Self::Value>;
7890

7991
// Just here for convenience and checking that the key matches the kind, don't override this.
80-
fn construct_dep_node(self, tcx: Qcx::DepContext, key: &Self::Key) -> DepNode {
92+
fn construct_dep_node(self, tcx: DepContext<Self>, key: &Self::Key) -> DepNode {
8193
DepNode::construct(tcx, self.dep_kind(), key)
8294
}
8395
}

0 commit comments

Comments
 (0)