Skip to content

Commit 49c1b07

Browse files
committed
Decouple QueryContext from DepContext.
1 parent 6f04883 commit 49c1b07

File tree

7 files changed

+87
-53
lines changed

7 files changed

+87
-53
lines changed

compiler/rustc_middle/src/ty/query/plumbing.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ impl QueryContext for TyCtxt<'tcx> {
4848
&self,
4949
token: QueryJobId<Self::DepKind>,
5050
diagnostics: Option<&Lock<ThinVec<Diagnostic>>>,
51-
compute: impl FnOnce(Self) -> R,
51+
compute: impl FnOnce() -> R,
5252
) -> R {
5353
// The `TyCtxt` stored in TLS has the same global interner lifetime
5454
// as `self`, so we use `with_related_context` to relate the 'tcx lifetimes
@@ -65,7 +65,7 @@ impl QueryContext for TyCtxt<'tcx> {
6565

6666
// Use the `ImplicitCtxt` while we execute the query.
6767
tls::enter_context(&new_icx, |_| {
68-
rustc_data_structures::stack::ensure_sufficient_stack(|| compute(*self))
68+
rustc_data_structures::stack::ensure_sufficient_stack(compute)
6969
})
7070
})
7171
}

compiler/rustc_query_system/src/dep_graph/graph.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use super::debug::EdgeFilter;
2323
use super::prev::PreviousDepGraph;
2424
use super::query::DepGraphQuery;
2525
use super::serialized::SerializedDepNodeIndex;
26-
use super::{DepContext, DepKind, DepNode, WorkProductId};
26+
use super::{DepContext, DepKind, DepNode, HasDepContext, WorkProductId};
2727

2828
#[derive(Clone)]
2929
pub struct DepGraph<K: DepKind> {
@@ -235,7 +235,7 @@ impl<K: DepKind> DepGraph<K> {
235235
/// `arg` parameter.
236236
///
237237
/// [rustc dev guide]: https://rustc-dev-guide.rust-lang.org/incremental-compilation.html
238-
pub fn with_task<Ctxt: DepContext<DepKind = K>, A, R>(
238+
pub fn with_task<Ctxt: HasDepContext<DepKind = K>, A, R>(
239239
&self,
240240
key: DepNode<K>,
241241
cx: Ctxt,
@@ -261,7 +261,7 @@ impl<K: DepKind> DepGraph<K> {
261261
)
262262
}
263263

264-
fn with_task_impl<Ctxt: DepContext<DepKind = K>, A, R>(
264+
fn with_task_impl<Ctxt: HasDepContext<DepKind = K>, A, R>(
265265
&self,
266266
key: DepNode<K>,
267267
cx: Ctxt,
@@ -271,14 +271,15 @@ impl<K: DepKind> DepGraph<K> {
271271
hash_result: impl FnOnce(&mut Ctxt::StableHashingContext, &R) -> Option<Fingerprint>,
272272
) -> (R, DepNodeIndex) {
273273
if let Some(ref data) = self.data {
274+
let dcx = cx.dep_context();
274275
let task_deps = create_task(key).map(Lock::new);
275276
let result = K::with_deps(task_deps.as_ref(), || task(cx, arg));
276277
let edges = task_deps.map_or_else(|| smallvec![], |lock| lock.into_inner().reads);
277278

278-
let mut hcx = cx.create_stable_hashing_context();
279+
let mut hcx = dcx.create_stable_hashing_context();
279280
let current_fingerprint = hash_result(&mut hcx, &result);
280281

281-
let print_status = cfg!(debug_assertions) && cx.debug_dep_tasks();
282+
let print_status = cfg!(debug_assertions) && dcx.debug_dep_tasks();
282283

283284
// Intern the new `DepNode`.
284285
let dep_node_index = if let Some(prev_index) = data.previous.node_to_index_opt(&key) {
@@ -408,7 +409,7 @@ impl<K: DepKind> DepGraph<K> {
408409

409410
/// Executes something within an "eval-always" task which is a task
410411
/// that runs whenever anything changes.
411-
pub fn with_eval_always_task<Ctxt: DepContext<DepKind = K>, A, R>(
412+
pub fn with_eval_always_task<Ctxt: HasDepContext<DepKind = K>, A, R>(
412413
&self,
413414
key: DepNode<K>,
414415
cx: Ctxt,

compiler/rustc_query_system/src/dep_graph/mod.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,27 @@ pub trait DepContext: Copy {
6363
fn profiler(&self) -> &SelfProfilerRef;
6464
}
6565

66+
pub trait HasDepContext: Copy {
67+
type DepKind: self::DepKind;
68+
type StableHashingContext;
69+
type DepContext: self::DepContext<
70+
DepKind = Self::DepKind,
71+
StableHashingContext = Self::StableHashingContext,
72+
>;
73+
74+
fn dep_context(&self) -> &Self::DepContext;
75+
}
76+
77+
impl<T: DepContext> HasDepContext for T {
78+
type DepKind = T::DepKind;
79+
type StableHashingContext = T::StableHashingContext;
80+
type DepContext = Self;
81+
82+
fn dep_context(&self) -> &Self::DepContext {
83+
self
84+
}
85+
}
86+
6687
/// Describe the different families of dependency nodes.
6788
pub trait DepKind: Copy + fmt::Debug + Eq + Hash {
6889
const NULL: Self;

compiler/rustc_query_system/src/query/config.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ pub(crate) struct QueryVtable<CTX: QueryContext, K, V> {
3333
}
3434

3535
impl<CTX: QueryContext, K, V> QueryVtable<CTX, K, V> {
36-
pub(crate) fn to_dep_node(&self, tcx: CTX, key: &K) -> DepNode<CTX::DepKind>
36+
pub(crate) fn to_dep_node(&self, tcx: CTX::DepContext, key: &K) -> DepNode<CTX::DepKind>
3737
where
38-
K: crate::dep_graph::DepNodeParams<CTX>,
38+
K: crate::dep_graph::DepNodeParams<CTX::DepContext>,
3939
{
4040
DepNode::construct(tcx, self.dep_kind, key)
4141
}

compiler/rustc_query_system/src/query/job.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ use std::num::NonZeroU32;
1010

1111
#[cfg(parallel_compiler)]
1212
use {
13-
super::QueryContext,
13+
crate::dep_graph::DepContext,
14+
crate::query::QueryContext,
1415
parking_lot::{Condvar, Mutex},
1516
rustc_data_structures::fx::FxHashSet,
1617
rustc_data_structures::stable_hasher::{HashStable, StableHasher},
@@ -432,7 +433,7 @@ where
432433
{
433434
// Deterministically pick an entry point
434435
// FIXME: Sort this instead
435-
let mut hcx = tcx.create_stable_hashing_context();
436+
let mut hcx = tcx.dep_context().create_stable_hashing_context();
436437
queries
437438
.iter()
438439
.min_by_key(|v| {

compiler/rustc_query_system/src/query/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub use self::caches::{
1414
mod config;
1515
pub use self::config::{QueryAccessors, QueryConfig, QueryDescription};
1616

17-
use crate::dep_graph::DepContext;
17+
use crate::dep_graph::HasDepContext;
1818
use crate::query::job::QueryMap;
1919

2020
use rustc_data_structures::stable_hasher::HashStable;
@@ -23,7 +23,7 @@ use rustc_data_structures::thin_vec::ThinVec;
2323
use rustc_errors::Diagnostic;
2424
use rustc_span::def_id::DefId;
2525

26-
pub trait QueryContext: DepContext {
26+
pub trait QueryContext: HasDepContext {
2727
type Query: Clone + HashStable<Self::StableHashingContext>;
2828

2929
fn incremental_verify_ich(&self) -> bool;
@@ -44,6 +44,6 @@ pub trait QueryContext: DepContext {
4444
&self,
4545
token: QueryJobId<Self::DepKind>,
4646
diagnostics: Option<&Lock<ThinVec<Diagnostic>>>,
47-
compute: impl FnOnce(Self) -> R,
47+
compute: impl FnOnce() -> R,
4848
) -> R;
4949
}

0 commit comments

Comments
 (0)