Skip to content

Commit 47a677d

Browse files
authored
Rollup merge of #152704 - Zalathar:query-ctxt, r=nnethercote
Remove `QueryCtxt` and trait `HasDepContext` - Follow-up to #152636. - Potentially waiting on #152703 to reduce conflicts. --- With the `QueryContext` trait removed, wrapper struct `QueryCtxt` no longer serves a purpose and can be replaced with `TyCtxt` everywhere. After that, the only obstacle to removing trait `HasDepContext` is `DepGraph::with_task`, which uses the trait to allow passing both a `TyCtxt` and a query vtable through the context argument. But we can achieve the same result by passing the vtable through the other argument instead, in a tuple alongside the query key. r? nnethercote
2 parents c108ad5 + 25d5cd2 commit 47a677d

8 files changed

Lines changed: 248 additions & 288 deletions

File tree

compiler/rustc_interface/src/interface.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use rustc_parse::lexer::StripTokens;
1616
use rustc_parse::new_parser_from_source_str;
1717
use rustc_parse::parser::Recovery;
1818
use rustc_parse::parser::attr::AllowLeadingUnsafe;
19-
use rustc_query_impl::{QueryCtxt, print_query_stack};
19+
use rustc_query_impl::print_query_stack;
2020
use rustc_session::config::{self, Cfg, CheckCfg, ExpectedValues, Input, OutFileName};
2121
use rustc_session::parse::ParseSess;
2222
use rustc_session::{CompilerIO, EarlyDiagCtxt, Session, lint};
@@ -556,7 +556,7 @@ pub fn try_print_query_stack(
556556
let all_frames = ty::tls::with_context_opt(|icx| {
557557
if let Some(icx) = icx {
558558
ty::print::with_no_queries!(print_query_stack(
559-
QueryCtxt::new(icx.tcx),
559+
icx.tcx,
560560
icx.query,
561561
dcx,
562562
limit_frames,

compiler/rustc_interface/src/util.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use rustc_data_structures::sync;
1818
use rustc_metadata::{DylibError, EncodedMetadata, load_symbol_from_dylib};
1919
use rustc_middle::dep_graph::{WorkProduct, WorkProductId};
2020
use rustc_middle::ty::{CurrentGcx, TyCtxt};
21+
use rustc_query_impl::collect_active_jobs_from_all_queries;
2122
use rustc_session::config::{
2223
Cfg, CrateType, OutFileName, OutputFilenames, OutputTypes, Sysroot, host_tuple,
2324
};
@@ -184,7 +185,7 @@ pub(crate) fn run_in_thread_pool_with_globals<
184185
use rustc_data_structures::defer;
185186
use rustc_data_structures::sync::FromDyn;
186187
use rustc_middle::ty::tls;
187-
use rustc_query_impl::{QueryCtxt, break_query_cycles};
188+
use rustc_query_impl::break_query_cycles;
188189

189190
let thread_stack_size = init_stack_size(thread_builder_diag);
190191

@@ -253,7 +254,7 @@ internal compiler error: query cycle handler thread panicked, aborting process";
253254
|| {
254255
// Ensure there were no errors collecting all active jobs.
255256
// We need the complete map to ensure we find a cycle to break.
256-
QueryCtxt::new(tcx).collect_active_jobs_from_all_queries(false).expect(
257+
collect_active_jobs_from_all_queries(tcx, false).expect(
257258
"failed to collect active queries in deadlock handler",
258259
)
259260
},

compiler/rustc_middle/src/dep_graph/graph.rs

Lines changed: 31 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use {super::debug::EdgeFilter, std::env};
2323

2424
use super::query::DepGraphQuery;
2525
use super::serialized::{GraphEncoder, SerializedDepGraph, SerializedDepNodeIndex};
26-
use super::{DepKind, DepNode, HasDepContext, WorkProductId, read_deps, with_deps};
26+
use super::{DepKind, DepNode, WorkProductId, read_deps, with_deps};
2727
use crate::dep_graph::edges::EdgesVec;
2828
use crate::ich::StableHashingContext;
2929
use crate::ty::TyCtxt;
@@ -268,17 +268,17 @@ impl DepGraph {
268268
}
269269

270270
#[inline(always)]
271-
pub fn with_task<'tcx, Ctxt: HasDepContext<'tcx>, A: Debug, R>(
271+
pub fn with_task<'tcx, A: Debug, R>(
272272
&self,
273-
key: DepNode,
274-
cx: Ctxt,
275-
arg: A,
276-
task: fn(Ctxt, A) -> R,
273+
dep_node: DepNode,
274+
tcx: TyCtxt<'tcx>,
275+
task_arg: A,
276+
task_fn: fn(tcx: TyCtxt<'tcx>, task_arg: A) -> R,
277277
hash_result: Option<fn(&mut StableHashingContext<'_>, &R) -> Fingerprint>,
278278
) -> (R, DepNodeIndex) {
279279
match self.data() {
280-
Some(data) => data.with_task(key, cx, arg, task, hash_result),
281-
None => (task(cx, arg), self.next_virtual_depnode_index()),
280+
Some(data) => data.with_task(dep_node, tcx, task_arg, task_fn, hash_result),
281+
None => (task_fn(tcx, task_arg), self.next_virtual_depnode_index()),
282282
}
283283
}
284284

@@ -310,66 +310,50 @@ impl DepGraphData {
310310
/// prevent implicit 'leaks' of tracked state into the task (which
311311
/// could then be read without generating correct edges in the
312312
/// dep-graph -- see the [rustc dev guide] for more details on
313-
/// the dep-graph). To this end, the task function gets exactly two
314-
/// pieces of state: the context `cx` and an argument `arg`. Both
315-
/// of these bits of state must be of some type that implements
316-
/// `DepGraphSafe` and hence does not leak.
317-
///
318-
/// The choice of two arguments is not fundamental. One argument
319-
/// would work just as well, since multiple values can be
320-
/// collected using tuples. However, using two arguments works out
321-
/// to be quite convenient, since it is common to need a context
322-
/// (`cx`) and some argument (e.g., a `DefId` identifying what
323-
/// item to process).
313+
/// the dep-graph).
324314
///
325-
/// For cases where you need some other number of arguments:
326-
///
327-
/// - If you only need one argument, just use `()` for the `arg`
328-
/// parameter.
329-
/// - If you need 3+ arguments, use a tuple for the
330-
/// `arg` parameter.
315+
/// Therefore, the task function takes a `TyCtxt`, plus exactly one
316+
/// additional argument, `task_arg`. The additional argument type can be
317+
/// `()` if no argument is needed, or a tuple if multiple arguments are
318+
/// needed.
331319
///
332320
/// [rustc dev guide]: https://rustc-dev-guide.rust-lang.org/queries/incremental-compilation.html
333321
#[inline(always)]
334-
pub fn with_task<'tcx, Ctxt: HasDepContext<'tcx>, A: Debug, R>(
322+
pub fn with_task<'tcx, A: Debug, R>(
335323
&self,
336-
key: DepNode,
337-
cx: Ctxt,
338-
arg: A,
339-
task: fn(Ctxt, A) -> R,
324+
dep_node: DepNode,
325+
tcx: TyCtxt<'tcx>,
326+
task_arg: A,
327+
task_fn: fn(tcx: TyCtxt<'tcx>, task_arg: A) -> R,
340328
hash_result: Option<fn(&mut StableHashingContext<'_>, &R) -> Fingerprint>,
341329
) -> (R, DepNodeIndex) {
342330
// If the following assertion triggers, it can have two reasons:
343331
// 1. Something is wrong with DepNode creation, either here or
344332
// in `DepGraph::try_mark_green()`.
345333
// 2. Two distinct query keys get mapped to the same `DepNode`
346334
// (see for example #48923).
347-
self.assert_dep_node_not_yet_allocated_in_current_session(
348-
cx.dep_context().sess,
349-
&key,
350-
|| {
351-
format!(
352-
"forcing query with already existing `DepNode`\n\
353-
- query-key: {arg:?}\n\
354-
- dep-node: {key:?}"
355-
)
356-
},
357-
);
335+
self.assert_dep_node_not_yet_allocated_in_current_session(tcx.sess, &dep_node, || {
336+
format!(
337+
"forcing query with already existing `DepNode`\n\
338+
- query-key: {task_arg:?}\n\
339+
- dep-node: {dep_node:?}"
340+
)
341+
});
358342

359-
let with_deps = |task_deps| with_deps(task_deps, || task(cx, arg));
360-
let (result, edges) = if cx.dep_context().is_eval_always(key.kind) {
343+
let with_deps = |task_deps| with_deps(task_deps, || task_fn(tcx, task_arg));
344+
let (result, edges) = if tcx.is_eval_always(dep_node.kind) {
361345
(with_deps(TaskDepsRef::EvalAlways), EdgesVec::new())
362346
} else {
363347
let task_deps = Lock::new(TaskDeps::new(
364348
#[cfg(debug_assertions)]
365-
Some(key),
349+
Some(dep_node),
366350
0,
367351
));
368352
(with_deps(TaskDepsRef::Allow(&task_deps)), task_deps.into_inner().reads)
369353
};
370354

371355
let dep_node_index =
372-
self.hash_result_and_alloc_node(cx.dep_context(), key, edges, &result, hash_result);
356+
self.hash_result_and_alloc_node(tcx, dep_node, edges, &result, hash_result);
373357

374358
(result, dep_node_index)
375359
}
@@ -954,7 +938,7 @@ impl DepGraphData {
954938

955939
// We failed to mark it green, so we try to force the query.
956940
debug!("trying to force dependency {dep_dep_node:?}");
957-
if !tcx.dep_context().try_force_from_dep_node(*dep_dep_node, parent_dep_node_index, frame) {
941+
if !tcx.try_force_from_dep_node(*dep_dep_node, parent_dep_node_index, frame) {
958942
// The DepNode could not be forced.
959943
debug!("dependency {dep_dep_node:?} could not be forced");
960944
return None;
@@ -1001,10 +985,7 @@ impl DepGraphData {
1001985
let frame = MarkFrame { index: prev_dep_node_index, parent: frame };
1002986

1003987
// We never try to mark eval_always nodes as green
1004-
debug_assert!(
1005-
!tcx.dep_context()
1006-
.is_eval_always(self.previous.index_to_node(prev_dep_node_index).kind)
1007-
);
988+
debug_assert!(!tcx.is_eval_always(self.previous.index_to_node(prev_dep_node_index).kind));
1008989

1009990
let prev_deps = self.previous.edge_targets_from(prev_dep_node_index);
1010991

compiler/rustc_middle/src/dep_graph/mod.rs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,6 @@ mod graph;
2525
mod query;
2626
mod serialized;
2727

28-
pub trait HasDepContext<'tcx>: Copy {
29-
fn dep_context(&self) -> TyCtxt<'tcx>;
30-
}
31-
32-
impl<'tcx> HasDepContext<'tcx> for TyCtxt<'tcx> {
33-
fn dep_context(&self) -> TyCtxt<'tcx> {
34-
*self
35-
}
36-
}
37-
38-
impl<'tcx, T: HasDepContext<'tcx>, Q: Copy> HasDepContext<'tcx> for (T, Q) {
39-
fn dep_context(&self) -> TyCtxt<'tcx> {
40-
self.0.dep_context()
41-
}
42-
}
43-
4428
/// Describes the contents of the fingerprint generated by a given query.
4529
///
4630
/// This is mainly for determining whether and how we can reconstruct a key

0 commit comments

Comments
 (0)