Skip to content

Commit affa73e

Browse files
committed
Auto merge of rust-lang#119966 - cjgillot:tls, r=<try>
Move TLS to rustc_query_system Revival of rust-lang#86038 r? `@ghost`
2 parents 2730354 + 68e9415 commit affa73e

File tree

16 files changed

+390
-390
lines changed

16 files changed

+390
-390
lines changed

compiler/rustc_incremental/src/persist/save.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::errors;
33
use rustc_data_structures::fx::FxIndexMap;
44
use rustc_data_structures::sync::join;
55
use rustc_middle::dep_graph::{
6-
DepGraph, SerializedDepGraph, WorkProduct, WorkProductId, WorkProductMap,
6+
DepGraph, DepsType, SerializedDepGraph, WorkProduct, WorkProductId, WorkProductMap,
77
};
88
use rustc_middle::ty::TyCtxt;
99
use rustc_serialize::opaque::{FileEncodeResult, FileEncoder};
@@ -171,7 +171,7 @@ pub(crate) fn build_dep_graph(
171171
// First encode the commandline arguments hash
172172
sess.opts.dep_tracking_hash(false).encode(&mut encoder);
173173

174-
Some(DepGraph::new(
174+
Some(DepGraph::new::<DepsType>(
175175
&sess.prof,
176176
prev_graph,
177177
prev_work_products,

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

+2-25
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;
@@ -16,43 +16,20 @@ pub use rustc_query_system::dep_graph::{
1616
pub use dep_node::{dep_kinds, label_strs, DepKind, DepNode, DepNodeExt};
1717
pub(crate) use dep_node::{make_compile_codegen_unit, make_compile_mono_item};
1818

19-
pub type DepGraph = rustc_query_system::dep_graph::DepGraph<DepsType>;
19+
pub type DepGraph = rustc_query_system::dep_graph::DepGraph;
2020

2121
pub type DepKindStruct<'tcx> = rustc_query_system::dep_graph::DepKindStruct<TyCtxt<'tcx>>;
2222

2323
#[derive(Clone)]
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;
5130
}
5231

5332
impl<'tcx> DepContext for TyCtxt<'tcx> {
54-
type Deps = DepsType;
55-
5633
#[inline]
5734
fn with_stable_hashing_context<R>(self, f: impl FnOnce(StableHashingContext<'_>) -> R) -> R {
5835
TyCtxt::with_stable_hashing_context(self, f)

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

-155
This file was deleted.

0 commit comments

Comments
 (0)