Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion compiler/rustc_query_impl/src/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use rustc_data_structures::{outline, sharded, sync};
use rustc_errors::{Diag, FatalError, StashKey};
use rustc_middle::dep_graph::DepsType;
use rustc_middle::ty::TyCtxt;
use rustc_query_system::dep_graph::{DepGraphData, DepNodeKey, HasDepContext};
use rustc_query_system::dep_graph::{DepGraphData, DepNodeKey};
use rustc_query_system::query::{
ActiveKeyStatus, CycleError, CycleErrorHandling, QueryCache, QueryContext, QueryDispatcher,
QueryJob, QueryJobId, QueryJobInfo, QueryLatch, QueryMap, QueryMode, QueryStackDeferred,
Expand Down Expand Up @@ -482,6 +482,7 @@ where

dep_graph_data.with_task(
dep_node,
qcx.tcx,
(qcx, query),
key,
|(qcx, query), key| query.compute(qcx, key),
Expand Down
6 changes: 2 additions & 4 deletions compiler/rustc_query_impl/src/plumbing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use rustc_middle::ty::codec::TyEncoder;
use rustc_middle::ty::print::with_reduced_queries;
use rustc_middle::ty::tls::{self, ImplicitCtxt};
use rustc_middle::ty::{self, TyCtxt};
use rustc_query_system::dep_graph::{DepNodeKey, FingerprintStyle, HasDepContext};
use rustc_query_system::dep_graph::{DepNodeKey, FingerprintStyle};
use rustc_query_system::ich::StableHashingContext;
use rustc_query_system::query::{
QueryCache, QueryContext, QueryDispatcher, QueryJobId, QueryMap, QuerySideEffect,
Expand Down Expand Up @@ -119,17 +119,15 @@ impl<'tcx> QueryCtxt<'tcx> {
}
}

impl<'tcx> HasDepContext for QueryCtxt<'tcx> {
impl<'tcx> QueryContext<'tcx> for QueryCtxt<'tcx> {
type Deps = DepsType;
type DepContext = TyCtxt<'tcx>;

#[inline]
fn dep_context(&self) -> &Self::DepContext {
&self.tcx
}
}

impl<'tcx> QueryContext<'tcx> for QueryCtxt<'tcx> {
#[inline]
fn jobserver_proxy(&self) -> &Proxy {
&self.tcx.jobserver_proxy
Expand Down
36 changes: 16 additions & 20 deletions compiler/rustc_query_system/src/dep_graph/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use {super::debug::EdgeFilter, std::env};

use super::query::DepGraphQuery;
use super::serialized::{GraphEncoder, SerializedDepGraph, SerializedDepNodeIndex};
use super::{DepContext, DepKind, DepNode, Deps, HasDepContext, WorkProductId};
use super::{DepContext, DepKind, DepNode, Deps, WorkProductId};
use crate::dep_graph::edges::EdgesVec;
use crate::ich::StableHashingContext;
use crate::query::{QueryContext, QuerySideEffect};
Expand Down Expand Up @@ -260,7 +260,7 @@ impl<D: Deps> DepGraph<D> {
}

#[inline(always)]
pub fn with_task<Ctxt: HasDepContext<Deps = D>, A: Debug, R>(
pub fn with_task<Ctxt: DepContext<Deps = D>, A: Debug, R>(
&self,
key: DepNode,
cx: Ctxt,
Expand All @@ -269,7 +269,7 @@ impl<D: Deps> DepGraph<D> {
hash_result: Option<fn(&mut StableHashingContext<'_>, &R) -> Fingerprint>,
) -> (R, DepNodeIndex) {
match self.data() {
Some(data) => data.with_task(key, cx, arg, task, hash_result),
Some(data) => data.with_task(key, cx, cx, arg, task, hash_result),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remark: Passing in cx twice is unfortunate, but I guess there are two different callers of DepGraphData::with_task, and the other one uses a different (tupled) task context.

None => (task(cx, arg), self.next_virtual_depnode_index()),
}
}
Expand Down Expand Up @@ -323,33 +323,30 @@ impl<D: Deps> DepGraphData<D> {
///
/// [rustc dev guide]: https://rustc-dev-guide.rust-lang.org/queries/incremental-compilation.html
#[inline(always)]
pub fn with_task<Ctxt: HasDepContext<Deps = D>, A: Debug, R>(
pub fn with_task<Ctxt: DepContext<Deps = D>, A: Debug, C: Copy, R>(
&self,
key: DepNode,
cx: Ctxt,
task_context: C,
arg: A,
task: fn(Ctxt, A) -> R,
task: fn(C, A) -> R,
hash_result: Option<fn(&mut StableHashingContext<'_>, &R) -> Fingerprint>,
) -> (R, DepNodeIndex) {
// If the following assertion triggers, it can have two reasons:
// 1. Something is wrong with DepNode creation, either here or
// in `DepGraph::try_mark_green()`.
// 2. Two distinct query keys get mapped to the same `DepNode`
// (see for example #48923).
self.assert_dep_node_not_yet_allocated_in_current_session(
cx.dep_context().sess(),
&key,
|| {
format!(
"forcing query with already existing `DepNode`\n\
self.assert_dep_node_not_yet_allocated_in_current_session(cx.sess(), &key, || {
format!(
"forcing query with already existing `DepNode`\n\
- query-key: {arg:?}\n\
- dep-node: {key:?}"
)
},
);
)
});

let with_deps = |task_deps| D::with_deps(task_deps, || task(cx, arg));
let (result, edges) = if cx.dep_context().is_eval_always(key.kind) {
let with_deps = |task_deps| D::with_deps(task_deps, || task(task_context, arg));
let (result, edges) = if cx.is_eval_always(key.kind) {
(with_deps(TaskDepsRef::EvalAlways), EdgesVec::new())
} else {
let task_deps = Lock::new(TaskDeps::new(
Expand All @@ -360,8 +357,7 @@ impl<D: Deps> DepGraphData<D> {
(with_deps(TaskDepsRef::Allow(&task_deps)), task_deps.into_inner().reads)
};

let dcx = cx.dep_context();
let dep_node_index = self.hash_result_and_alloc_node(dcx, key, edges, &result, hash_result);
let dep_node_index = self.hash_result_and_alloc_node(cx, key, edges, &result, hash_result);

(result, dep_node_index)
}
Expand Down Expand Up @@ -448,7 +444,7 @@ impl<D: Deps> DepGraphData<D> {
/// Intern the new `DepNode` with the dependencies up-to-now.
fn hash_result_and_alloc_node<Ctxt: DepContext<Deps = D>, R>(
&self,
cx: &Ctxt,
cx: Ctxt,
node: DepNode,
edges: EdgesVec,
result: &R,
Expand Down Expand Up @@ -617,7 +613,7 @@ impl<D: Deps> DepGraph<D> {
}
});

data.hash_result_and_alloc_node(&cx, node, edges, result, hash_result)
data.hash_result_and_alloc_node(cx, node, edges, result, hash_result)
} else {
// Incremental compilation is turned off. We just execute the task
// without tracking. We still provide a dep-node index that uniquely
Expand Down
25 changes: 0 additions & 25 deletions compiler/rustc_query_system/src/dep_graph/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,31 +119,6 @@ pub trait Deps: DynSync {
const DEP_KIND_MAX: u16;
}

pub trait HasDepContext: Copy {
type Deps: self::Deps;
type DepContext: self::DepContext<Deps = Self::Deps>;

fn dep_context(&self) -> &Self::DepContext;
}

impl<T: DepContext> HasDepContext for T {
type Deps = T::Deps;
type DepContext = Self;

fn dep_context(&self) -> &Self::DepContext {
self
}
}

impl<T: HasDepContext, Q: Copy> HasDepContext for (T, Q) {
type Deps = T::Deps;
type DepContext = T::DepContext;

fn dep_context(&self) -> &Self::DepContext {
self.0.dep_context()
}
}

/// Describes the contents of the fingerprint generated by a given query.
///
/// This is mainly for determining whether and how we can reconstruct a key
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_query_system/src/query/dispatcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ use rustc_data_structures::fingerprint::Fingerprint;
use rustc_span::ErrorGuaranteed;

use super::QueryStackFrameExtra;
use crate::dep_graph::{DepKind, DepNode, DepNodeKey, HasDepContext, SerializedDepNodeIndex};
use crate::dep_graph::{DepKind, DepNode, DepNodeKey, SerializedDepNodeIndex};
use crate::ich::StableHashingContext;
use crate::query::caches::QueryCache;
use crate::query::{CycleError, CycleErrorHandling, DepNodeIndex, QueryContext, QueryState};

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

/// Unambiguous shorthand for `<This::Qcx as HasDepContext>::DepContext`.
/// Unambiguous shorthand for `<This::Qcx as QueryContext<'tcx>>::DepContext`.
#[expect(type_alias_bounds)]
type DepContextOf<'tcx, This: QueryDispatcher<'tcx>> =
<<This as QueryDispatcher<'tcx>>::Qcx as HasDepContext>::DepContext;
<<This as QueryDispatcher<'tcx>>::Qcx as QueryContext<'tcx>>::DepContext;

/// Trait that can be used as a vtable for a single query, providing operations
/// and metadata for that query.
Expand Down
9 changes: 7 additions & 2 deletions compiler/rustc_query_system/src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub use self::job::{
print_query_stack, report_cycle,
};
pub use self::plumbing::*;
use crate::dep_graph::{DepKind, DepNodeIndex, HasDepContext, SerializedDepNodeIndex};
use crate::dep_graph::{DepContext, DepKind, DepNodeIndex, Deps, SerializedDepNodeIndex};

mod caches;
mod dispatcher;
Expand Down Expand Up @@ -156,7 +156,12 @@ pub enum QuerySideEffect {
Diagnostic(DiagInner),
}

pub trait QueryContext<'tcx>: HasDepContext {
pub trait QueryContext<'tcx>: Copy {
type Deps: Deps;
type DepContext: DepContext<Deps = Self::Deps>;

fn dep_context(&self) -> &Self::DepContext;

/// Gets a jobserver reference which is used to release then acquire
/// a token while waiting on a query.
fn jobserver_proxy(&self) -> &Proxy;
Expand Down
Loading