Skip to content

Commit 6ee8ac8

Browse files
committed
Don't use HasDepContext in DepGraph::with_task
The need for a `HasDepContext` impl on tuples can be avoided by passing the query vtable as part of an argument tuple instead.
1 parent 5365d88 commit 6ee8ac8

4 files changed

Lines changed: 40 additions & 53 deletions

File tree

compiler/rustc_middle/src/dep_graph/graph.rs

Lines changed: 28 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -252,17 +252,17 @@ impl DepGraph {
252252
}
253253

254254
#[inline(always)]
255-
pub fn with_task<'tcx, Ctxt: HasDepContext<'tcx>, A: Debug, R>(
255+
pub fn with_task<'tcx, A: Debug, R>(
256256
&self,
257-
key: DepNode,
258-
cx: Ctxt,
259-
arg: A,
260-
task: fn(Ctxt, A) -> R,
257+
dep_node: DepNode,
258+
tcx: TyCtxt<'tcx>,
259+
task_arg: A,
260+
task_fn: fn(tcx: TyCtxt<'tcx>, task_arg: A) -> R,
261261
hash_result: Option<fn(&mut StableHashingContext<'_>, &R) -> Fingerprint>,
262262
) -> (R, DepNodeIndex) {
263263
match self.data() {
264-
Some(data) => data.with_task(key, cx, arg, task, hash_result),
265-
None => (task(cx, arg), self.next_virtual_depnode_index()),
264+
Some(data) => data.with_task(dep_node, tcx, task_arg, task_fn, hash_result),
265+
None => (task_fn(tcx, task_arg), self.next_virtual_depnode_index()),
266266
}
267267
}
268268

@@ -294,66 +294,50 @@ impl DepGraphData {
294294
/// prevent implicit 'leaks' of tracked state into the task (which
295295
/// could then be read without generating correct edges in the
296296
/// dep-graph -- see the [rustc dev guide] for more details on
297-
/// the dep-graph). To this end, the task function gets exactly two
298-
/// pieces of state: the context `cx` and an argument `arg`. Both
299-
/// of these bits of state must be of some type that implements
300-
/// `DepGraphSafe` and hence does not leak.
301-
///
302-
/// The choice of two arguments is not fundamental. One argument
303-
/// would work just as well, since multiple values can be
304-
/// collected using tuples. However, using two arguments works out
305-
/// to be quite convenient, since it is common to need a context
306-
/// (`cx`) and some argument (e.g., a `DefId` identifying what
307-
/// item to process).
297+
/// the dep-graph).
308298
///
309-
/// For cases where you need some other number of arguments:
310-
///
311-
/// - If you only need one argument, just use `()` for the `arg`
312-
/// parameter.
313-
/// - If you need 3+ arguments, use a tuple for the
314-
/// `arg` parameter.
299+
/// Therefore, the task function takes a `TyCtxt`, plus exactly one
300+
/// additional argument, `task_arg`. The additional argument type can be
301+
/// `()` if no argument is needed, or a tuple if multiple arguments are
302+
/// needed.
315303
///
316304
/// [rustc dev guide]: https://rustc-dev-guide.rust-lang.org/queries/incremental-compilation.html
317305
#[inline(always)]
318-
pub fn with_task<'tcx, Ctxt: HasDepContext<'tcx>, A: Debug, R>(
306+
pub fn with_task<'tcx, A: Debug, R>(
319307
&self,
320-
key: DepNode,
321-
cx: Ctxt,
322-
arg: A,
323-
task: fn(Ctxt, A) -> R,
308+
dep_node: DepNode,
309+
tcx: TyCtxt<'tcx>,
310+
task_arg: A,
311+
task_fn: fn(tcx: TyCtxt<'tcx>, task_arg: A) -> R,
324312
hash_result: Option<fn(&mut StableHashingContext<'_>, &R) -> Fingerprint>,
325313
) -> (R, DepNodeIndex) {
326314
// If the following assertion triggers, it can have two reasons:
327315
// 1. Something is wrong with DepNode creation, either here or
328316
// in `DepGraph::try_mark_green()`.
329317
// 2. Two distinct query keys get mapped to the same `DepNode`
330318
// (see for example #48923).
331-
self.assert_dep_node_not_yet_allocated_in_current_session(
332-
cx.dep_context().sess,
333-
&key,
334-
|| {
335-
format!(
336-
"forcing query with already existing `DepNode`\n\
337-
- query-key: {arg:?}\n\
338-
- dep-node: {key:?}"
339-
)
340-
},
341-
);
319+
self.assert_dep_node_not_yet_allocated_in_current_session(tcx.sess, &dep_node, || {
320+
format!(
321+
"forcing query with already existing `DepNode`\n\
322+
- query-key: {task_arg:?}\n\
323+
- dep-node: {dep_node:?}"
324+
)
325+
});
342326

343-
let with_deps = |task_deps| with_deps(task_deps, || task(cx, arg));
344-
let (result, edges) = if cx.dep_context().is_eval_always(key.kind) {
327+
let with_deps = |task_deps| with_deps(task_deps, || task_fn(tcx, task_arg));
328+
let (result, edges) = if tcx.is_eval_always(dep_node.kind) {
345329
(with_deps(TaskDepsRef::EvalAlways), EdgesVec::new())
346330
} else {
347331
let task_deps = Lock::new(TaskDeps::new(
348332
#[cfg(debug_assertions)]
349-
Some(key),
333+
Some(dep_node),
350334
0,
351335
));
352336
(with_deps(TaskDepsRef::Allow(&task_deps)), task_deps.into_inner().reads)
353337
};
354338

355339
let dep_node_index =
356-
self.hash_result_and_alloc_node(cx.dep_context(), key, edges, &result, hash_result);
340+
self.hash_result_and_alloc_node(tcx, dep_node, edges, &result, hash_result);
357341

358342
(result, dep_node_index)
359343
}

compiler/rustc_middle/src/dep_graph/mod.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,6 @@ impl<'tcx> HasDepContext<'tcx> for TyCtxt<'tcx> {
3434
}
3535
}
3636

37-
impl<'tcx, T: HasDepContext<'tcx>, Q: Copy> HasDepContext<'tcx> for (T, Q) {
38-
fn dep_context(&self) -> TyCtxt<'tcx> {
39-
self.0.dep_context()
40-
}
41-
}
42-
4337
/// Describes the contents of the fingerprint generated by a given query.
4438
///
4539
/// This is mainly for determining whether and how we can reconstruct a key

compiler/rustc_query_impl/src/execution.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -473,9 +473,9 @@ fn execute_job_incr<'tcx, C: QueryCache, const FLAGS: QueryFlags>(
473473
// Call the query provider.
474474
dep_graph_data.with_task(
475475
dep_node,
476-
(tcx, query),
477-
key,
478-
|(tcx, query), key| query.invoke_provider(tcx, key),
476+
tcx,
477+
(query, key),
478+
|tcx, (query, key)| query.invoke_provider(tcx, key),
479479
query.hash_result(),
480480
)
481481
});

compiler/rustc_query_impl/src/lib.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#![feature(try_blocks)]
1010
// tidy-alphabetical-end
1111

12+
use std::fmt;
1213
use std::marker::ConstParamTy;
1314

1415
use rustc_data_structures::sync::AtomicU64;
@@ -76,6 +77,14 @@ impl<'tcx, C: QueryCache, const FLAGS: QueryFlags> Clone
7677
}
7778
}
7879

80+
impl<'tcx, C: QueryCache, const FLAGS: QueryFlags> fmt::Debug
81+
for SemiDynamicQueryDispatcher<'tcx, C, FLAGS>
82+
{
83+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
84+
f.write_str(self.name())
85+
}
86+
}
87+
7988
impl<'tcx, C: QueryCache, const FLAGS: QueryFlags> SemiDynamicQueryDispatcher<'tcx, C, FLAGS> {
8089
#[inline(always)]
8190
fn name(self) -> &'static str {

0 commit comments

Comments
 (0)