]> git.proxmox.com Git - rustc.git/blob - compiler/rustc_query_impl/src/keys.rs
New upstream version 1.52.0~beta.3+dfsg1
[rustc.git] / compiler / rustc_query_impl / src / keys.rs
1 //! Defines the set of legal keys that can be used in queries.
2
3 use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, LOCAL_CRATE};
4 use rustc_middle::infer::canonical::Canonical;
5 use rustc_middle::mir;
6 use rustc_middle::ty::fast_reject::SimplifiedType;
7 use rustc_middle::ty::subst::{GenericArg, SubstsRef};
8 use rustc_middle::ty::{self, Ty, TyCtxt};
9 use rustc_span::symbol::{Ident, Symbol};
10 use rustc_span::{Span, DUMMY_SP};
11
12 /// The `Key` trait controls what types can legally be used as the key
13 /// for a query.
14 pub trait Key {
15 /// Given an instance of this key, what crate is it referring to?
16 /// This is used to find the provider.
17 fn query_crate(&self) -> CrateNum;
18
19 /// In the event that a cycle occurs, if no explicit span has been
20 /// given for a query with key `self`, what span should we use?
21 fn default_span(&self, tcx: TyCtxt<'_>) -> Span;
22 }
23
24 impl<'tcx> Key for ty::InstanceDef<'tcx> {
25 fn query_crate(&self) -> CrateNum {
26 LOCAL_CRATE
27 }
28
29 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
30 tcx.def_span(self.def_id())
31 }
32 }
33
34 impl<'tcx> Key for ty::Instance<'tcx> {
35 fn query_crate(&self) -> CrateNum {
36 LOCAL_CRATE
37 }
38
39 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
40 tcx.def_span(self.def_id())
41 }
42 }
43
44 impl<'tcx> Key for mir::interpret::GlobalId<'tcx> {
45 fn query_crate(&self) -> CrateNum {
46 self.instance.query_crate()
47 }
48
49 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
50 self.instance.default_span(tcx)
51 }
52 }
53
54 impl<'tcx> Key for mir::interpret::LitToConstInput<'tcx> {
55 fn query_crate(&self) -> CrateNum {
56 LOCAL_CRATE
57 }
58
59 fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
60 DUMMY_SP
61 }
62 }
63
64 impl Key for CrateNum {
65 fn query_crate(&self) -> CrateNum {
66 *self
67 }
68 fn default_span(&self, _: TyCtxt<'_>) -> Span {
69 DUMMY_SP
70 }
71 }
72
73 impl Key for LocalDefId {
74 fn query_crate(&self) -> CrateNum {
75 self.to_def_id().query_crate()
76 }
77 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
78 self.to_def_id().default_span(tcx)
79 }
80 }
81
82 impl Key for DefId {
83 fn query_crate(&self) -> CrateNum {
84 self.krate
85 }
86 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
87 tcx.def_span(*self)
88 }
89 }
90
91 impl Key for ty::WithOptConstParam<LocalDefId> {
92 fn query_crate(&self) -> CrateNum {
93 self.did.query_crate()
94 }
95 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
96 self.did.default_span(tcx)
97 }
98 }
99
100 impl Key for (DefId, DefId) {
101 fn query_crate(&self) -> CrateNum {
102 self.0.krate
103 }
104 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
105 self.1.default_span(tcx)
106 }
107 }
108
109 impl Key for (ty::Instance<'tcx>, LocalDefId) {
110 fn query_crate(&self) -> CrateNum {
111 self.0.query_crate()
112 }
113 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
114 self.0.default_span(tcx)
115 }
116 }
117
118 impl Key for (DefId, LocalDefId) {
119 fn query_crate(&self) -> CrateNum {
120 self.0.krate
121 }
122 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
123 self.1.default_span(tcx)
124 }
125 }
126
127 impl Key for (LocalDefId, DefId) {
128 fn query_crate(&self) -> CrateNum {
129 LOCAL_CRATE
130 }
131 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
132 self.0.default_span(tcx)
133 }
134 }
135
136 impl Key for (DefId, Option<Ident>) {
137 fn query_crate(&self) -> CrateNum {
138 self.0.krate
139 }
140 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
141 tcx.def_span(self.0)
142 }
143 }
144
145 impl Key for (DefId, LocalDefId, Ident) {
146 fn query_crate(&self) -> CrateNum {
147 self.0.krate
148 }
149 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
150 self.1.default_span(tcx)
151 }
152 }
153
154 impl Key for (CrateNum, DefId) {
155 fn query_crate(&self) -> CrateNum {
156 self.0
157 }
158 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
159 self.1.default_span(tcx)
160 }
161 }
162
163 impl Key for (DefId, SimplifiedType) {
164 fn query_crate(&self) -> CrateNum {
165 self.0.krate
166 }
167 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
168 self.0.default_span(tcx)
169 }
170 }
171
172 impl<'tcx> Key for SubstsRef<'tcx> {
173 fn query_crate(&self) -> CrateNum {
174 LOCAL_CRATE
175 }
176 fn default_span(&self, _: TyCtxt<'_>) -> Span {
177 DUMMY_SP
178 }
179 }
180
181 impl<'tcx> Key for (DefId, SubstsRef<'tcx>) {
182 fn query_crate(&self) -> CrateNum {
183 self.0.krate
184 }
185 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
186 self.0.default_span(tcx)
187 }
188 }
189
190 impl<'tcx> Key
191 for (
192 (ty::WithOptConstParam<DefId>, SubstsRef<'tcx>),
193 (ty::WithOptConstParam<DefId>, SubstsRef<'tcx>),
194 )
195 {
196 fn query_crate(&self) -> CrateNum {
197 (self.0).0.did.krate
198 }
199 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
200 (self.0).0.did.default_span(tcx)
201 }
202 }
203
204 impl<'tcx> Key for (LocalDefId, DefId, SubstsRef<'tcx>) {
205 fn query_crate(&self) -> CrateNum {
206 LOCAL_CRATE
207 }
208 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
209 self.0.default_span(tcx)
210 }
211 }
212
213 impl<'tcx> Key for (ty::ParamEnv<'tcx>, ty::PolyTraitRef<'tcx>) {
214 fn query_crate(&self) -> CrateNum {
215 self.1.def_id().krate
216 }
217 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
218 tcx.def_span(self.1.def_id())
219 }
220 }
221
222 impl<'tcx> Key for (&'tcx ty::Const<'tcx>, mir::Field) {
223 fn query_crate(&self) -> CrateNum {
224 LOCAL_CRATE
225 }
226 fn default_span(&self, _: TyCtxt<'_>) -> Span {
227 DUMMY_SP
228 }
229 }
230
231 impl<'tcx> Key for mir::interpret::ConstAlloc<'tcx> {
232 fn query_crate(&self) -> CrateNum {
233 LOCAL_CRATE
234 }
235 fn default_span(&self, _: TyCtxt<'_>) -> Span {
236 DUMMY_SP
237 }
238 }
239
240 impl<'tcx> Key for ty::PolyTraitRef<'tcx> {
241 fn query_crate(&self) -> CrateNum {
242 self.def_id().krate
243 }
244 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
245 tcx.def_span(self.def_id())
246 }
247 }
248
249 impl<'tcx> Key for GenericArg<'tcx> {
250 fn query_crate(&self) -> CrateNum {
251 LOCAL_CRATE
252 }
253 fn default_span(&self, _: TyCtxt<'_>) -> Span {
254 DUMMY_SP
255 }
256 }
257
258 impl<'tcx> Key for &'tcx ty::Const<'tcx> {
259 fn query_crate(&self) -> CrateNum {
260 LOCAL_CRATE
261 }
262 fn default_span(&self, _: TyCtxt<'_>) -> Span {
263 DUMMY_SP
264 }
265 }
266
267 impl<'tcx> Key for Ty<'tcx> {
268 fn query_crate(&self) -> CrateNum {
269 LOCAL_CRATE
270 }
271 fn default_span(&self, _: TyCtxt<'_>) -> Span {
272 DUMMY_SP
273 }
274 }
275
276 impl<'tcx> Key for &'tcx ty::List<ty::Predicate<'tcx>> {
277 fn query_crate(&self) -> CrateNum {
278 LOCAL_CRATE
279 }
280 fn default_span(&self, _: TyCtxt<'_>) -> Span {
281 DUMMY_SP
282 }
283 }
284
285 impl<'tcx> Key for ty::ParamEnv<'tcx> {
286 fn query_crate(&self) -> CrateNum {
287 LOCAL_CRATE
288 }
289 fn default_span(&self, _: TyCtxt<'_>) -> Span {
290 DUMMY_SP
291 }
292 }
293
294 impl<'tcx, T: Key> Key for ty::ParamEnvAnd<'tcx, T> {
295 fn query_crate(&self) -> CrateNum {
296 self.value.query_crate()
297 }
298 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
299 self.value.default_span(tcx)
300 }
301 }
302
303 impl Key for Symbol {
304 fn query_crate(&self) -> CrateNum {
305 LOCAL_CRATE
306 }
307 fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
308 DUMMY_SP
309 }
310 }
311
312 /// Canonical query goals correspond to abstract trait operations that
313 /// are not tied to any crate in particular.
314 impl<'tcx, T> Key for Canonical<'tcx, T> {
315 fn query_crate(&self) -> CrateNum {
316 LOCAL_CRATE
317 }
318
319 fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
320 DUMMY_SP
321 }
322 }
323
324 impl Key for (Symbol, u32, u32) {
325 fn query_crate(&self) -> CrateNum {
326 LOCAL_CRATE
327 }
328
329 fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
330 DUMMY_SP
331 }
332 }
333
334 impl<'tcx> Key for (DefId, Ty<'tcx>, SubstsRef<'tcx>, ty::ParamEnv<'tcx>) {
335 fn query_crate(&self) -> CrateNum {
336 LOCAL_CRATE
337 }
338
339 fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
340 DUMMY_SP
341 }
342 }