]> git.proxmox.com Git - rustc.git/blame - compiler/rustc_query_impl/src/keys.rs
New upstream version 1.64.0+dfsg1
[rustc.git] / compiler / rustc_query_impl / src / keys.rs
CommitLineData
ea8adc8c
XL
1//! Defines the set of legal keys that can be used in queries.
2
ba9703b0 3use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, LOCAL_CRATE};
6a06907d
XL
4use rustc_middle::infer::canonical::Canonical;
5use rustc_middle::mir;
136023e0 6use rustc_middle::traits;
6a06907d
XL
7use rustc_middle::ty::fast_reject::SimplifiedType;
8use rustc_middle::ty::subst::{GenericArg, SubstsRef};
064997fb 9use rustc_middle::ty::{self, layout::TyAndLayout, Ty, TyCtxt};
6a06907d 10use rustc_span::symbol::{Ident, Symbol};
dfeec247 11use rustc_span::{Span, DUMMY_SP};
ea8adc8c
XL
12
13/// The `Key` trait controls what types can legally be used as the key
14/// for a query.
74b04a01 15pub trait Key {
ea8adc8c
XL
16 /// Given an instance of this key, what crate is it referring to?
17 /// This is used to find the provider.
136023e0 18 fn query_crate_is_local(&self) -> bool;
ea8adc8c
XL
19
20 /// In the event that a cycle occurs, if no explicit span has been
21 /// given for a query with key `self`, what span should we use?
dc9dc135 22 fn default_span(&self, tcx: TyCtxt<'_>) -> Span;
94222f64
XL
23
24 /// If the key is a [`DefId`] or `DefId`--equivalent, return that `DefId`.
25 /// Otherwise, return `None`.
26 fn key_as_def_id(&self) -> Option<DefId> {
27 None
28 }
ea8adc8c
XL
29}
30
17df50a5 31impl Key for () {
136023e0
XL
32 #[inline(always)]
33 fn query_crate_is_local(&self) -> bool {
34 true
17df50a5
XL
35 }
36
37 fn default_span(&self, _: TyCtxt<'_>) -> Span {
38 DUMMY_SP
39 }
40}
41
ea8adc8c 42impl<'tcx> Key for ty::InstanceDef<'tcx> {
136023e0
XL
43 #[inline(always)]
44 fn query_crate_is_local(&self) -> bool {
45 true
ea8adc8c
XL
46 }
47
dc9dc135 48 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
ea8adc8c
XL
49 tcx.def_span(self.def_id())
50 }
51}
52
53impl<'tcx> Key for ty::Instance<'tcx> {
136023e0
XL
54 #[inline(always)]
55 fn query_crate_is_local(&self) -> bool {
56 true
ea8adc8c
XL
57 }
58
dc9dc135 59 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
ea8adc8c
XL
60 tcx.def_span(self.def_id())
61 }
62}
63
0531ce1d 64impl<'tcx> Key for mir::interpret::GlobalId<'tcx> {
136023e0
XL
65 #[inline(always)]
66 fn query_crate_is_local(&self) -> bool {
67 true
0531ce1d
XL
68 }
69
dc9dc135 70 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
0531ce1d
XL
71 self.instance.default_span(tcx)
72 }
73}
74
dc3f5686
XL
75impl<'tcx> Key for (Ty<'tcx>, Option<ty::PolyExistentialTraitRef<'tcx>>) {
76 #[inline(always)]
77 fn query_crate_is_local(&self) -> bool {
78 true
79 }
80
81 fn default_span(&self, _: TyCtxt<'_>) -> Span {
82 DUMMY_SP
83 }
84}
85
dfeec247 86impl<'tcx> Key for mir::interpret::LitToConstInput<'tcx> {
136023e0
XL
87 #[inline(always)]
88 fn query_crate_is_local(&self) -> bool {
89 true
dfeec247
XL
90 }
91
92 fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
93 DUMMY_SP
94 }
95}
96
ea8adc8c 97impl Key for CrateNum {
136023e0
XL
98 #[inline(always)]
99 fn query_crate_is_local(&self) -> bool {
100 *self == LOCAL_CRATE
ea8adc8c 101 }
dc9dc135 102 fn default_span(&self, _: TyCtxt<'_>) -> Span {
ea8adc8c
XL
103 DUMMY_SP
104 }
105}
106
ba9703b0 107impl Key for LocalDefId {
136023e0
XL
108 #[inline(always)]
109 fn query_crate_is_local(&self) -> bool {
110 true
ea8adc8c 111 }
ba9703b0
XL
112 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
113 self.to_def_id().default_span(tcx)
ea8adc8c 114 }
94222f64
XL
115 fn key_as_def_id(&self) -> Option<DefId> {
116 Some(self.to_def_id())
117 }
ea8adc8c
XL
118}
119
120impl Key for DefId {
136023e0
XL
121 #[inline(always)]
122 fn query_crate_is_local(&self) -> bool {
123 self.krate == LOCAL_CRATE
ea8adc8c 124 }
dc9dc135 125 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
ea8adc8c
XL
126 tcx.def_span(*self)
127 }
94222f64
XL
128 #[inline(always)]
129 fn key_as_def_id(&self) -> Option<DefId> {
130 Some(*self)
131 }
ea8adc8c
XL
132}
133
3dfed10e 134impl Key for ty::WithOptConstParam<LocalDefId> {
136023e0
XL
135 #[inline(always)]
136 fn query_crate_is_local(&self) -> bool {
137 true
3dfed10e
XL
138 }
139 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
140 self.did.default_span(tcx)
141 }
142}
143
5e7ed085
FG
144impl Key for SimplifiedType {
145 #[inline(always)]
146 fn query_crate_is_local(&self) -> bool {
147 true
148 }
149 fn default_span(&self, _: TyCtxt<'_>) -> Span {
150 DUMMY_SP
151 }
152}
153
ea8adc8c 154impl Key for (DefId, DefId) {
136023e0
XL
155 #[inline(always)]
156 fn query_crate_is_local(&self) -> bool {
157 self.0.krate == LOCAL_CRATE
ea8adc8c 158 }
dc9dc135 159 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
ea8adc8c
XL
160 self.1.default_span(tcx)
161 }
162}
163
a2a8927a 164impl<'tcx> Key for (ty::Instance<'tcx>, LocalDefId) {
136023e0
XL
165 #[inline(always)]
166 fn query_crate_is_local(&self) -> bool {
167 true
5869c6ff
XL
168 }
169 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
170 self.0.default_span(tcx)
171 }
172}
173
f9f354fc 174impl Key for (DefId, LocalDefId) {
136023e0
XL
175 #[inline(always)]
176 fn query_crate_is_local(&self) -> bool {
177 self.0.krate == LOCAL_CRATE
f9f354fc
XL
178 }
179 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
180 self.1.default_span(tcx)
181 }
182}
183
3dfed10e 184impl Key for (LocalDefId, DefId) {
136023e0
XL
185 #[inline(always)]
186 fn query_crate_is_local(&self) -> bool {
187 true
3dfed10e
XL
188 }
189 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
190 self.0.default_span(tcx)
191 }
192}
193
064997fb
FG
194impl Key for (LocalDefId, LocalDefId) {
195 #[inline(always)]
196 fn query_crate_is_local(&self) -> bool {
197 true
198 }
199 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
200 self.0.default_span(tcx)
201 }
202}
203
6a06907d 204impl Key for (DefId, Option<Ident>) {
136023e0
XL
205 #[inline(always)]
206 fn query_crate_is_local(&self) -> bool {
207 self.0.krate == LOCAL_CRATE
6a06907d
XL
208 }
209 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
210 tcx.def_span(self.0)
211 }
94222f64
XL
212 #[inline(always)]
213 fn key_as_def_id(&self) -> Option<DefId> {
214 Some(self.0)
215 }
6a06907d 216}
74b04a01 217
6a06907d 218impl Key for (DefId, LocalDefId, Ident) {
136023e0
XL
219 #[inline(always)]
220 fn query_crate_is_local(&self) -> bool {
221 self.0.krate == LOCAL_CRATE
6a06907d
XL
222 }
223 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
224 self.1.default_span(tcx)
225 }
226}
227
228impl Key for (CrateNum, DefId) {
136023e0
XL
229 #[inline(always)]
230 fn query_crate_is_local(&self) -> bool {
231 self.0 == LOCAL_CRATE
ea8adc8c 232 }
dc9dc135 233 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
ea8adc8c
XL
234 self.1.default_span(tcx)
235 }
236}
237
5e7ed085
FG
238impl Key for (CrateNum, SimplifiedType) {
239 #[inline(always)]
240 fn query_crate_is_local(&self) -> bool {
241 self.0 == LOCAL_CRATE
242 }
243 fn default_span(&self, _: TyCtxt<'_>) -> Span {
244 DUMMY_SP
245 }
246}
247
ea8adc8c 248impl Key for (DefId, SimplifiedType) {
136023e0
XL
249 #[inline(always)]
250 fn query_crate_is_local(&self) -> bool {
251 self.0.krate == LOCAL_CRATE
ea8adc8c 252 }
dc9dc135 253 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
ea8adc8c
XL
254 self.0.default_span(tcx)
255 }
256}
257
dfeec247 258impl<'tcx> Key for SubstsRef<'tcx> {
136023e0
XL
259 #[inline(always)]
260 fn query_crate_is_local(&self) -> bool {
261 true
dfeec247
XL
262 }
263 fn default_span(&self, _: TyCtxt<'_>) -> Span {
264 DUMMY_SP
265 }
266}
267
532ac7d7 268impl<'tcx> Key for (DefId, SubstsRef<'tcx>) {
136023e0
XL
269 #[inline(always)]
270 fn query_crate_is_local(&self) -> bool {
271 self.0.krate == LOCAL_CRATE
ea8adc8c 272 }
dc9dc135 273 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
ea8adc8c
XL
274 self.0.default_span(tcx)
275 }
276}
277
94222f64 278impl<'tcx> Key for (ty::Unevaluated<'tcx, ()>, ty::Unevaluated<'tcx, ()>) {
136023e0
XL
279 #[inline(always)]
280 fn query_crate_is_local(&self) -> bool {
94222f64 281 (self.0).def.did.krate == LOCAL_CRATE
1b1a35ee
XL
282 }
283 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
94222f64 284 (self.0).def.did.default_span(tcx)
1b1a35ee
XL
285 }
286}
287
3dfed10e 288impl<'tcx> Key for (LocalDefId, DefId, SubstsRef<'tcx>) {
136023e0
XL
289 #[inline(always)]
290 fn query_crate_is_local(&self) -> bool {
291 true
3dfed10e
XL
292 }
293 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
294 self.0.default_span(tcx)
295 }
296}
297
abe05a73 298impl<'tcx> Key for (ty::ParamEnv<'tcx>, ty::PolyTraitRef<'tcx>) {
136023e0
XL
299 #[inline(always)]
300 fn query_crate_is_local(&self) -> bool {
301 self.1.def_id().krate == LOCAL_CRATE
ea8adc8c 302 }
dc9dc135 303 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
abe05a73 304 tcx.def_span(self.1.def_id())
ea8adc8c
XL
305 }
306}
307
5099ac24 308impl<'tcx> Key for (ty::Const<'tcx>, mir::Field) {
136023e0
XL
309 #[inline(always)]
310 fn query_crate_is_local(&self) -> bool {
311 true
6a06907d
XL
312 }
313 fn default_span(&self, _: TyCtxt<'_>) -> Span {
314 DUMMY_SP
315 }
316}
74b04a01 317
6a06907d 318impl<'tcx> Key for mir::interpret::ConstAlloc<'tcx> {
136023e0
XL
319 #[inline(always)]
320 fn query_crate_is_local(&self) -> bool {
321 true
dc9dc135
XL
322 }
323 fn default_span(&self, _: TyCtxt<'_>) -> Span {
324 DUMMY_SP
325 }
326}
327
328impl<'tcx> Key for ty::PolyTraitRef<'tcx> {
136023e0
XL
329 #[inline(always)]
330 fn query_crate_is_local(&self) -> bool {
331 self.def_id().krate == LOCAL_CRATE
ea8adc8c 332 }
dc9dc135 333 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
abe05a73 334 tcx.def_span(self.def_id())
ea8adc8c
XL
335 }
336}
337
c295e0f8
XL
338impl<'tcx> Key for ty::PolyExistentialTraitRef<'tcx> {
339 #[inline(always)]
340 fn query_crate_is_local(&self) -> bool {
341 self.def_id().krate == LOCAL_CRATE
342 }
343 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
344 tcx.def_span(self.def_id())
345 }
346}
347
94222f64
XL
348impl<'tcx> Key for (ty::PolyTraitRef<'tcx>, ty::PolyTraitRef<'tcx>) {
349 #[inline(always)]
350 fn query_crate_is_local(&self) -> bool {
351 self.0.def_id().krate == LOCAL_CRATE
352 }
353 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
354 tcx.def_span(self.0.def_id())
355 }
356}
357
ba9703b0 358impl<'tcx> Key for GenericArg<'tcx> {
136023e0
XL
359 #[inline(always)]
360 fn query_crate_is_local(&self) -> bool {
361 true
ba9703b0
XL
362 }
363 fn default_span(&self, _: TyCtxt<'_>) -> Span {
364 DUMMY_SP
365 }
366}
367
cdc7bbd5 368impl<'tcx> Key for mir::ConstantKind<'tcx> {
136023e0
XL
369 #[inline(always)]
370 fn query_crate_is_local(&self) -> bool {
371 true
cdc7bbd5
XL
372 }
373 fn default_span(&self, _: TyCtxt<'_>) -> Span {
374 DUMMY_SP
375 }
376}
377
5099ac24 378impl<'tcx> Key for ty::Const<'tcx> {
136023e0
XL
379 #[inline(always)]
380 fn query_crate_is_local(&self) -> bool {
381 true
94b46f34 382 }
dc9dc135 383 fn default_span(&self, _: TyCtxt<'_>) -> Span {
94b46f34
XL
384 DUMMY_SP
385 }
386}
387
ea8adc8c 388impl<'tcx> Key for Ty<'tcx> {
136023e0 389 #[inline(always)]
94222f64
XL
390 fn query_crate_is_local(&self) -> bool {
391 true
392 }
064997fb
FG
393 fn default_span(&self, _: TyCtxt<'_>) -> Span {
394 DUMMY_SP
395 }
396}
397
398impl<'tcx> Key for TyAndLayout<'tcx> {
399 #[inline(always)]
400 fn query_crate_is_local(&self) -> bool {
401 true
402 }
94222f64
XL
403 fn default_span(&self, _: TyCtxt<'_>) -> Span {
404 DUMMY_SP
405 }
406}
407
408impl<'tcx> Key for (Ty<'tcx>, Ty<'tcx>) {
409 #[inline(always)]
136023e0
XL
410 fn query_crate_is_local(&self) -> bool {
411 true
ea8adc8c 412 }
dc9dc135 413 fn default_span(&self, _: TyCtxt<'_>) -> Span {
ea8adc8c
XL
414 DUMMY_SP
415 }
416}
417
3dfed10e 418impl<'tcx> Key for &'tcx ty::List<ty::Predicate<'tcx>> {
136023e0
XL
419 #[inline(always)]
420 fn query_crate_is_local(&self) -> bool {
421 true
3dfed10e
XL
422 }
423 fn default_span(&self, _: TyCtxt<'_>) -> Span {
424 DUMMY_SP
425 }
426}
427
83c7162d 428impl<'tcx> Key for ty::ParamEnv<'tcx> {
136023e0
XL
429 #[inline(always)]
430 fn query_crate_is_local(&self) -> bool {
431 true
83c7162d 432 }
dc9dc135 433 fn default_span(&self, _: TyCtxt<'_>) -> Span {
83c7162d
XL
434 DUMMY_SP
435 }
436}
437
ea8adc8c 438impl<'tcx, T: Key> Key for ty::ParamEnvAnd<'tcx, T> {
136023e0
XL
439 #[inline(always)]
440 fn query_crate_is_local(&self) -> bool {
441 self.value.query_crate_is_local()
ea8adc8c 442 }
dc9dc135 443 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
ea8adc8c
XL
444 self.value.default_span(tcx)
445 }
446}
447
e74abb32 448impl Key for Symbol {
136023e0
XL
449 #[inline(always)]
450 fn query_crate_is_local(&self) -> bool {
451 true
ea8adc8c 452 }
dc9dc135 453 fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
ea8adc8c
XL
454 DUMMY_SP
455 }
456}
0531ce1d 457
04454e1e
FG
458impl Key for Option<Symbol> {
459 #[inline(always)]
460 fn query_crate_is_local(&self) -> bool {
461 true
462 }
463 fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
464 DUMMY_SP
465 }
466}
467
8faf50e0
XL
468/// Canonical query goals correspond to abstract trait operations that
469/// are not tied to any crate in particular.
e74abb32 470impl<'tcx, T> Key for Canonical<'tcx, T> {
136023e0
XL
471 #[inline(always)]
472 fn query_crate_is_local(&self) -> bool {
473 true
e74abb32
XL
474 }
475
476 fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
477 DUMMY_SP
478 }
479}
480
481impl Key for (Symbol, u32, u32) {
136023e0
XL
482 #[inline(always)]
483 fn query_crate_is_local(&self) -> bool {
484 true
83c7162d
XL
485 }
486
dc9dc135 487 fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
83c7162d
XL
488 DUMMY_SP
489 }
490}
ba9703b0 491
f9f354fc 492impl<'tcx> Key for (DefId, Ty<'tcx>, SubstsRef<'tcx>, ty::ParamEnv<'tcx>) {
136023e0
XL
493 #[inline(always)]
494 fn query_crate_is_local(&self) -> bool {
495 true
496 }
497
498 fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
499 DUMMY_SP
500 }
501}
502
503impl<'tcx> Key for (ty::Predicate<'tcx>, traits::WellFormedLoc) {
504 #[inline(always)]
505 fn query_crate_is_local(&self) -> bool {
506 true
ba9703b0 507 }
f9f354fc
XL
508
509 fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
510 DUMMY_SP
ba9703b0
XL
511 }
512}
c295e0f8
XL
513
514impl<'tcx> Key for (ty::PolyFnSig<'tcx>, &'tcx ty::List<Ty<'tcx>>) {
515 #[inline(always)]
516 fn query_crate_is_local(&self) -> bool {
517 true
518 }
519
520 fn default_span(&self, _: TyCtxt<'_>) -> Span {
521 DUMMY_SP
522 }
523}
524
525impl<'tcx> Key for (ty::Instance<'tcx>, &'tcx ty::List<Ty<'tcx>>) {
526 #[inline(always)]
527 fn query_crate_is_local(&self) -> bool {
528 true
529 }
530
531 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
532 self.0.default_span(tcx)
533 }
534}
04454e1e
FG
535
536impl<'tcx> Key for (Ty<'tcx>, ty::ValTree<'tcx>) {
537 #[inline(always)]
538 fn query_crate_is_local(&self) -> bool {
539 true
540 }
541
542 fn default_span(&self, _: TyCtxt<'_>) -> Span {
543 DUMMY_SP
544 }
545}