1use std::{mem, ptr};
23use rustc_data_structures::sync;
45use super::{GlobalCtxt, TyCtxt};
6use crate::dep_graph::TaskDepsRef;
7use crate::query::QueryJobId;
89/// This is the implicit state of rustc. It contains the current
10/// `TyCtxt` and query. It is updated when creating a local interner or
11/// executing a new query. Whenever there's a `TyCtxt` value available
12/// you should also have access to an `ImplicitCtxt` through the functions
13/// in this module.
14#[derive(#[automatically_derived]
impl<'a, 'tcx> ::core::clone::Clone for ImplicitCtxt<'a, 'tcx> {
#[inline]
fn clone(&self) -> ImplicitCtxt<'a, 'tcx> {
ImplicitCtxt {
tcx: ::core::clone::Clone::clone(&self.tcx),
query: ::core::clone::Clone::clone(&self.query),
query_depth: ::core::clone::Clone::clone(&self.query_depth),
task_deps: ::core::clone::Clone::clone(&self.task_deps),
}
}
}Clone)]
15pub struct ImplicitCtxt<'a, 'tcx> {
16/// The current `TyCtxt`.
17pub tcx: TyCtxt<'tcx>,
1819/// The current query job, if any.
20pub query: Option<QueryJobId>,
2122/// Used to prevent queries from calling too deeply.
23pub query_depth: usize,
2425/// The current dep graph task. This is used to add dependencies to queries
26 /// when executing them.
27pub task_deps: TaskDepsRef<'a>,
28}
2930impl<'a, 'tcx> ImplicitCtxt<'a, 'tcx> {
31pub fn new(gcx: &'tcx GlobalCtxt<'tcx>) -> Self {
32let tcx = TyCtxt { gcx };
33ImplicitCtxt { tcx, query: None, query_depth: 0, task_deps: TaskDepsRef::Ignore }
34 }
35}
3637// Import the thread-local variable from Rayon, which is preserved for Rayon jobs.
38use rustc_thread_pool::tlv::TLV;
3940#[inline]
41fn erase(context: &ImplicitCtxt<'_, '_>) -> *const () {
42contextas *const _ as *const ()
43}
4445#[inline]
46unsafe fn downcast<'a, 'tcx>(context: *const ()) -> &'a ImplicitCtxt<'a, 'tcx> {
47unsafe { &*(contextas *const ImplicitCtxt<'a, 'tcx>) }
48}
4950/// Sets `context` as the new current `ImplicitCtxt` for the duration of the function `f`.
51#[inline]
52pub fn enter_context<'a, 'tcx, F, R>(context: &ImplicitCtxt<'a, 'tcx>, f: F) -> R
53where
54F: FnOnce() -> R,
55{
56TLV.with(|tlv| {
57let old = tlv.replace(erase(context));
58let _reset = rustc_data_structures::defer(move || tlv.set(old));
59f()
60 })
61}
6263/// Allows access to the current `ImplicitCtxt` in a closure if one is available.
64#[inline]
65#[track_caller]
66pub fn with_context_opt<F, R>(f: F) -> R
67where
68F: for<'a, 'tcx> FnOnce(Option<&ImplicitCtxt<'a, 'tcx>>) -> R,
69{
70let context = TLV.get();
71if context.is_null() {
72f(None)
73 } else {
74// We could get an `ImplicitCtxt` pointer from another thread.
75 // Ensure that `ImplicitCtxt` is `DynSync`.
76sync::assert_dyn_sync::<ImplicitCtxt<'_, '_>>();
7778unsafe { f(Some(downcast(context))) }
79 }
80}
8182/// Allows access to the current `ImplicitCtxt`.
83/// Panics if there is no `ImplicitCtxt` available.
84#[inline]
85pub fn with_context<F, R>(f: F) -> R
86where
87F: for<'a, 'tcx> FnOnce(&ImplicitCtxt<'a, 'tcx>) -> R,
88{
89with_context_opt(|opt_context| f(opt_context.expect("no ImplicitCtxt stored in tls")))
90}
9192/// Allows access to the current `ImplicitCtxt` whose tcx field is the same as the tcx argument
93/// passed in. This means the closure is given an `ImplicitCtxt` with the same `'tcx` lifetime
94/// as the `TyCtxt` passed in.
95/// This will panic if you pass it a `TyCtxt` which is different from the current
96/// `ImplicitCtxt`'s `tcx` field.
97#[inline]
98pub fn with_related_context<'tcx, F, R>(tcx: TyCtxt<'tcx>, f: F) -> R
99where
100F: FnOnce(&ImplicitCtxt<'_, 'tcx>) -> R,
101{
102with_context(|context| {
103// The two gcx have different invariant lifetimes, so we need to erase them for the comparison.
104if !ptr::eq(context.tcx.gcx as *const _ as *const (),
tcx.gcx as *const _ as *const ()) {
::core::panicking::panic("assertion failed: ptr::eq(context.tcx.gcx as *const _ as *const (),\n tcx.gcx as *const _ as *const ())")
};assert!(ptr::eq(
105 context.tcx.gcx as *const _ as *const (),
106 tcx.gcx as *const _ as *const ()
107 ));
108109let context: &ImplicitCtxt<'_, '_> = unsafe { mem::transmute(context) };
110111f(context)
112 })
113}
114115/// Allows access to the `TyCtxt` in the current `ImplicitCtxt`.
116/// Panics if there is no `ImplicitCtxt` available.
117#[inline]
118pub fn with<F, R>(f: F) -> R
119where
120F: for<'tcx> FnOnce(TyCtxt<'tcx>) -> R,
121{
122with_context(|context| f(context.tcx))
123}
124125/// Allows access to the `TyCtxt` in the current `ImplicitCtxt`.
126/// The closure is passed None if there is no `ImplicitCtxt` available.
127#[inline]
128#[track_caller]
129pub fn with_opt<F, R>(f: F) -> R
130where
131F: for<'tcx> FnOnce(Option<TyCtxt<'tcx>>) -> R,
132{
133with_context_opt(
134#[track_caller]
135|opt_context| f(opt_context.map(|context| context.tcx)),
136 )
137}