Skip to content

Commit 190c668

Browse files
committed
Move query TLS to rustc_query_system.
1 parent c4e8f18 commit 190c668

File tree

14 files changed

+264
-263
lines changed

14 files changed

+264
-263
lines changed

compiler/rustc_interface/src/callbacks.rs

+2-23
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99
//! The functions in this file should fall back to the default set in their
1010
//! origin crate when the `TyCtxt` is not present in TLS.
1111
12-
use rustc_errors::{Diagnostic, TRACK_DIAGNOSTIC};
13-
use rustc_middle::dep_graph::{DepNodeExt, TaskDepsRef};
12+
use rustc_middle::dep_graph::DepNodeExt;
1413
use rustc_middle::ty::tls;
1514
use rustc_query_system::dep_graph::dep_node::default_dep_kind_debug;
1615
use rustc_query_system::dep_graph::{DepContext, DepKind, DepNode};
@@ -26,26 +25,6 @@ fn track_span_parent(def_id: rustc_span::def_id::LocalDefId) {
2625
})
2726
}
2827

29-
/// This is a callback from `rustc_errors` as it cannot access the implicit state
30-
/// in `rustc_middle` otherwise. It is used when diagnostic messages are
31-
/// emitted and stores them in the current query, if there is one.
32-
fn track_diagnostic(diagnostic: Diagnostic, f: &mut dyn FnMut(Diagnostic)) {
33-
tls::with_context_opt(|icx| {
34-
if let Some(icx) = icx {
35-
if let Some(diagnostics) = icx.diagnostics {
36-
diagnostics.lock().extend(Some(diagnostic.clone()));
37-
}
38-
39-
// Diagnostics are tracked, we can ignore the dependency.
40-
let icx = tls::ImplicitCtxt { task_deps: TaskDepsRef::Ignore, ..icx.clone() };
41-
return tls::enter_context(&icx, move || (*f)(diagnostic));
42-
}
43-
44-
// In any other case, invoke diagnostics anyway.
45-
(*f)(diagnostic);
46-
})
47-
}
48-
4928
/// This is a callback from `rustc_hir` as it cannot access the implicit state
5029
/// in `rustc_middle` otherwise.
5130
fn def_id_debug(def_id: rustc_hir::def_id::DefId, f: &mut fmt::Formatter<'_>) -> fmt::Result {
@@ -103,5 +82,5 @@ pub fn setup_callbacks() {
10382
.swap(&(dep_kind_debug as fn(_, &mut fmt::Formatter<'_>) -> _));
10483
rustc_query_system::dep_graph::dep_node::DEP_NODE_DEBUG
10584
.swap(&(dep_node_debug as fn(_, &mut fmt::Formatter<'_>) -> _));
106-
TRACK_DIAGNOSTIC.swap(&(track_diagnostic as _));
85+
rustc_errors::TRACK_DIAGNOSTIC.swap(&(rustc_query_system::tls::track_diagnostic as _));
10786
}

compiler/rustc_interface/src/interface.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -450,11 +450,10 @@ pub fn try_print_query_stack(
450450
// Be careful relying on global state here: this code is called from
451451
// a panic hook, which means that the global `DiagCtxt` may be in a weird
452452
// state if it was responsible for triggering the panic.
453-
let i = ty::tls::with_context_opt(|icx| {
454-
if let Some(icx) = icx {
453+
let i = ty::tls::with_opt(|tcx| {
454+
if let Some(tcx) = tcx {
455455
ty::print::with_no_queries!(print_query_stack(
456-
QueryCtxt::new(icx.tcx),
457-
icx.query,
456+
QueryCtxt::new(tcx),
458457
dcx,
459458
num_frames,
460459
file,

compiler/rustc_middle/src/dep_graph/mod.rs

+1-22
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::ty::{self, TyCtxt};
1+
use crate::ty::TyCtxt;
22
use rustc_data_structures::profiling::SelfProfilerRef;
33
use rustc_query_system::ich::StableHashingContext;
44
use rustc_session::Session;
@@ -24,27 +24,6 @@ pub type DepKindStruct<'tcx> = rustc_query_system::dep_graph::DepKindStruct<TyCt
2424
pub struct DepsType;
2525

2626
impl Deps for DepsType {
27-
fn with_deps<OP, R>(task_deps: TaskDepsRef<'_>, op: OP) -> R
28-
where
29-
OP: FnOnce() -> R,
30-
{
31-
ty::tls::with_context(|icx| {
32-
let icx = ty::tls::ImplicitCtxt { task_deps, ..icx.clone() };
33-
34-
ty::tls::enter_context(&icx, op)
35-
})
36-
}
37-
38-
fn read_deps<OP>(op: OP)
39-
where
40-
OP: for<'a> FnOnce(TaskDepsRef<'a>),
41-
{
42-
ty::tls::with_context_opt(|icx| {
43-
let Some(icx) = icx else { return };
44-
op(icx.task_deps)
45-
})
46-
}
47-
4827
const DEP_KIND_NULL: DepKind = dep_kinds::Null;
4928
const DEP_KIND_RED: DepKind = dep_kinds::Red;
5029
const DEP_KIND_MAX: u16 = dep_node::DEP_KIND_VARIANTS - 1;

compiler/rustc_middle/src/query/plumbing.rs

-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ use rustc_hir::def_id::{DefId, LocalDefId};
1616
use rustc_hir::hir_id::OwnerId;
1717
use rustc_query_system::dep_graph::DepNodeIndex;
1818
use rustc_query_system::dep_graph::SerializedDepNodeIndex;
19-
pub(crate) use rustc_query_system::query::QueryJobId;
2019
use rustc_query_system::query::*;
2120
use rustc_query_system::HandleCycleError;
2221
use rustc_span::{ErrorGuaranteed, Span, DUMMY_SP};

compiler/rustc_middle/src/ty/context.rs

+27-4
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
33
#![allow(rustc::usage_of_ty_tykind)]
44

5-
pub mod tls;
6-
75
use crate::arena::Arena;
86
use crate::dep_graph::{DepGraph, DepKindStruct};
97
use crate::infer::canonical::{CanonicalParamEnvCache, CanonicalVarInfo, CanonicalVarInfos};
@@ -662,15 +660,40 @@ impl<'tcx> GlobalCtxt<'tcx> {
662660
where
663661
F: FnOnce(TyCtxt<'tcx>) -> R,
664662
{
665-
let icx = tls::ImplicitCtxt::new(self);
666-
tls::enter_context(&icx, || f(icx.tcx))
663+
rustc_query_system::tls::create_and_enter_context(self, || f(TyCtxt { gcx: self }))
667664
}
668665

669666
pub fn finish(&self) -> FileEncodeResult {
670667
self.dep_graph.finish_encoding(&self.sess.prof)
671668
}
672669
}
673670

671+
pub mod tls {
672+
use super::TyCtxt;
673+
674+
/// Allows access to the `TyCtxt` in the current `ImplicitCtxt`.
675+
/// Panics if there is no `ImplicitCtxt` available.
676+
#[inline]
677+
pub fn with<F, R>(f: F) -> R
678+
where
679+
F: for<'tcx> FnOnce(TyCtxt<'tcx>) -> R,
680+
{
681+
rustc_query_system::tls::with(|gcx| f(TyCtxt { gcx: unsafe { &*gcx.cast() } }))
682+
}
683+
684+
/// Allows access to the `TyCtxt` in the current `ImplicitCtxt`.
685+
/// The closure is passed None if there is no `ImplicitCtxt` available.
686+
#[inline]
687+
pub fn with_opt<F, R>(f: F) -> R
688+
where
689+
F: for<'tcx> FnOnce(Option<TyCtxt<'tcx>>) -> R,
690+
{
691+
rustc_query_system::tls::with_opt(|opt_context| {
692+
f(opt_context.map(|gcx| TyCtxt { gcx: unsafe { &*gcx.cast() } }))
693+
})
694+
}
695+
}
696+
674697
impl<'tcx> TyCtxt<'tcx> {
675698
/// Expects a body and returns its codegen attributes.
676699
///

compiler/rustc_middle/src/ty/context/tls.rs

-130
This file was deleted.

compiler/rustc_query_impl/src/plumbing.rs

+2-40
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ use crate::rustc_middle::dep_graph::DepContext;
66
use crate::rustc_middle::ty::TyEncoder;
77
use crate::QueryConfigRestored;
88
use rustc_data_structures::stable_hasher::{Hash64, HashStable, StableHasher};
9-
use rustc_data_structures::sync::Lock;
10-
use rustc_errors::Diagnostic;
119

1210
use rustc_index::Idx;
1311
use rustc_middle::dep_graph::dep_kinds;
@@ -17,7 +15,6 @@ use rustc_middle::dep_graph::{
1715
use rustc_middle::query::on_disk_cache::AbsoluteBytePos;
1816
use rustc_middle::query::on_disk_cache::{CacheDecoder, CacheEncoder, EncodedDepNodeIndex};
1917
use rustc_middle::query::Key;
20-
use rustc_middle::ty::tls::{self, ImplicitCtxt};
2118
use rustc_middle::ty::{self, print::with_no_queries, TyCtxt};
2219
use rustc_query_system::dep_graph::{DepNodeParams, HasDepContext};
2320
use rustc_query_system::ich::StableHashingContext;
@@ -31,7 +28,6 @@ use rustc_serialize::Encodable;
3128
use rustc_session::Limit;
3229
use rustc_span::def_id::LOCAL_CRATE;
3330
use std::num::NonZeroU64;
34-
use thin_vec::ThinVec;
3531

3632
#[derive(Copy, Clone)]
3733
pub struct QueryCtxt<'tcx> {
@@ -75,11 +71,6 @@ impl QueryContext for QueryCtxt<'_> {
7571
)
7672
}
7773

78-
#[inline]
79-
fn current_query_job(self) -> Option<QueryJobId> {
80-
tls::with_context(|icx| icx.query)
81-
}
82-
8374
fn collect_active_jobs(self) -> QueryMap {
8475
let mut jobs = QueryMap::default();
8576

@@ -119,37 +110,8 @@ impl QueryContext for QueryCtxt<'_> {
119110
}
120111
}
121112

122-
/// Executes a job by changing the `ImplicitCtxt` to point to the
123-
/// new query job while it executes. It returns the diagnostics
124-
/// captured during execution and the actual result.
125-
#[inline(always)]
126-
fn start_query<R>(
127-
self,
128-
token: QueryJobId,
129-
depth_limit: bool,
130-
diagnostics: Option<&Lock<ThinVec<Diagnostic>>>,
131-
compute: impl FnOnce() -> R,
132-
) -> R {
133-
// The `TyCtxt` stored in TLS has the same global interner lifetime
134-
// as `self`, so we use `with_context` to relate the 'tcx lifetimes
135-
// when accessing the `ImplicitCtxt`.
136-
tls::with_context(move |current_icx| {
137-
if depth_limit && !self.recursion_limit().value_within_limit(current_icx.query_depth) {
138-
self.depth_limit_error(token);
139-
}
140-
141-
// Update the `ImplicitCtxt` to point to our new query job.
142-
let new_icx = ImplicitCtxt {
143-
tcx: self.tcx,
144-
query: Some(token),
145-
diagnostics,
146-
query_depth: current_icx.query_depth + depth_limit as usize,
147-
task_deps: current_icx.task_deps,
148-
};
149-
150-
// Use the `ImplicitCtxt` while we execute the query.
151-
tls::enter_context(&new_icx, compute)
152-
})
113+
fn recursion_limit(self) -> Limit {
114+
self.tcx.recursion_limit()
153115
}
154116

155117
fn depth_limit_error(self, job: QueryJobId) {

0 commit comments

Comments
 (0)