1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
use super::{GlobalCtxt, TyCtxt};
use crate::dep_graph::TaskDepsRef;
use crate::ty::query;
use rustc_data_structures::sync::{self, Lock};
use rustc_errors::Diagnostic;
use std::mem;
use std::ptr;
use thin_vec::ThinVec;
#[derive(Clone)]
pub struct ImplicitCtxt<'a, 'tcx> {
pub tcx: TyCtxt<'tcx>,
pub query: Option<query::QueryJobId>,
pub diagnostics: Option<&'a Lock<ThinVec<Diagnostic>>>,
pub query_depth: usize,
pub task_deps: TaskDepsRef<'a>,
}
impl<'a, 'tcx> ImplicitCtxt<'a, 'tcx> {
pub fn new(gcx: &'tcx GlobalCtxt<'tcx>) -> Self {
let tcx = TyCtxt { gcx };
ImplicitCtxt {
tcx,
query: None,
diagnostics: None,
query_depth: 0,
task_deps: TaskDepsRef::Ignore,
}
}
}
#[cfg(parallel_compiler)]
mod tlv {
use rustc_rayon_core as rayon_core;
use std::ptr;
#[inline]
pub(super) fn get_tlv() -> *const () {
ptr::from_exposed_addr(rayon_core::tlv::get())
}
#[inline]
pub(super) fn with_tlv<F: FnOnce() -> R, R>(value: *const (), f: F) -> R {
rayon_core::tlv::with(value.expose_addr(), f)
}
}
#[cfg(not(parallel_compiler))]
mod tlv {
use std::cell::Cell;
use std::ptr;
thread_local! {
static TLV: Cell<*const ()> = const { Cell::new(ptr::null()) };
}
#[inline]
pub(super) fn get_tlv() -> *const () {
TLV.with(|tlv| tlv.get())
}
#[inline]
pub(super) fn with_tlv<F: FnOnce() -> R, R>(value: *const (), f: F) -> R {
let old = TLV.replace(value);
let _reset = rustc_data_structures::OnDrop(move || TLV.set(old));
f()
}
}
#[inline]
fn erase(context: &ImplicitCtxt<'_, '_>) -> *const () {
context as *const _ as *const ()
}
#[inline]
unsafe fn downcast<'a, 'tcx>(context: *const ()) -> &'a ImplicitCtxt<'a, 'tcx> {
&*(context as *const ImplicitCtxt<'a, 'tcx>)
}
#[inline]
pub fn enter_context<'a, 'tcx, F, R>(context: &ImplicitCtxt<'a, 'tcx>, f: F) -> R
where
F: FnOnce() -> R,
{
tlv::with_tlv(erase(context), f)
}
#[inline]
pub fn with_context_opt<F, R>(f: F) -> R
where
F: for<'a, 'tcx> FnOnce(Option<&ImplicitCtxt<'a, 'tcx>>) -> R,
{
let context = tlv::get_tlv();
if context.is_null() {
f(None)
} else {
sync::assert_sync::<ImplicitCtxt<'_, '_>>();
unsafe { f(Some(downcast(context))) }
}
}
#[inline]
pub fn with_context<F, R>(f: F) -> R
where
F: for<'a, 'tcx> FnOnce(&ImplicitCtxt<'a, 'tcx>) -> R,
{
with_context_opt(|opt_context| f(opt_context.expect("no ImplicitCtxt stored in tls")))
}
#[inline]
pub fn with_related_context<'tcx, F, R>(tcx: TyCtxt<'tcx>, f: F) -> R
where
F: FnOnce(&ImplicitCtxt<'_, 'tcx>) -> R,
{
with_context(|context| {
assert!(ptr::eq(
context.tcx.gcx as *const _ as *const (),
tcx.gcx as *const _ as *const ()
));
let context: &ImplicitCtxt<'_, '_> = unsafe { mem::transmute(context) };
f(context)
})
}
#[inline]
pub fn with<F, R>(f: F) -> R
where
F: for<'tcx> FnOnce(TyCtxt<'tcx>) -> R,
{
with_context(|context| f(context.tcx))
}
#[inline]
pub fn with_opt<F, R>(f: F) -> R
where
F: for<'tcx> FnOnce(Option<TyCtxt<'tcx>>) -> R,
{
with_context_opt(|opt_context| f(opt_context.map(|context| context.tcx)))
}