]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_middle/src/query/keys.rs
Rollup merge of #105930 - JakobDegen:nal-unsound, r=oli-obk
[rust.git] / compiler / rustc_middle / src / query / keys.rs
1 //! Defines the set of legal keys that can be used in queries.
2
3 use crate::infer::canonical::Canonical;
4 use crate::mir;
5 use crate::traits;
6 use crate::ty::fast_reject::SimplifiedType;
7 use crate::ty::subst::{GenericArg, SubstsRef};
8 use crate::ty::{self, layout::TyAndLayout, Ty, TyCtxt};
9 use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, LOCAL_CRATE};
10 use rustc_hir::hir_id::{HirId, OwnerId};
11 use rustc_query_system::query::{DefaultCacheSelector, VecCacheSelector};
12 use rustc_span::symbol::{Ident, Symbol};
13 use rustc_span::{Span, DUMMY_SP};
14
15 /// The `Key` trait controls what types can legally be used as the key
16 /// for a query.
17 pub trait Key: Sized {
18     // N.B. Most of the keys down below have `type CacheSelector = DefaultCacheSelector<Self>;`,
19     //      it would be reasonable to use associated type defaults, to remove the duplication...
20     //
21     //      ...But r-a doesn't support them yet and using a default here causes r-a to not infer
22     //      return types of queries which is very annoying. Thus, until r-a support associated
23     //      type defaults, plese restrain from using them here <3
24     //
25     //      r-a issue: <https://github.com/rust-lang/rust-analyzer/issues/13693>
26     type CacheSelector;
27
28     /// Given an instance of this key, what crate is it referring to?
29     /// This is used to find the provider.
30     fn query_crate_is_local(&self) -> bool;
31
32     /// In the event that a cycle occurs, if no explicit span has been
33     /// given for a query with key `self`, what span should we use?
34     fn default_span(&self, tcx: TyCtxt<'_>) -> Span;
35
36     /// If the key is a [`DefId`] or `DefId`--equivalent, return that `DefId`.
37     /// Otherwise, return `None`.
38     fn key_as_def_id(&self) -> Option<DefId> {
39         None
40     }
41
42     fn ty_adt_id(&self) -> Option<DefId> {
43         None
44     }
45 }
46
47 impl Key for () {
48     type CacheSelector = DefaultCacheSelector<Self>;
49
50     #[inline(always)]
51     fn query_crate_is_local(&self) -> bool {
52         true
53     }
54
55     fn default_span(&self, _: TyCtxt<'_>) -> Span {
56         DUMMY_SP
57     }
58 }
59
60 impl<'tcx> Key for ty::InstanceDef<'tcx> {
61     type CacheSelector = DefaultCacheSelector<Self>;
62
63     #[inline(always)]
64     fn query_crate_is_local(&self) -> bool {
65         true
66     }
67
68     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
69         tcx.def_span(self.def_id())
70     }
71 }
72
73 impl<'tcx> Key for ty::Instance<'tcx> {
74     type CacheSelector = DefaultCacheSelector<Self>;
75
76     #[inline(always)]
77     fn query_crate_is_local(&self) -> bool {
78         true
79     }
80
81     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
82         tcx.def_span(self.def_id())
83     }
84 }
85
86 impl<'tcx> Key for mir::interpret::GlobalId<'tcx> {
87     type CacheSelector = DefaultCacheSelector<Self>;
88
89     #[inline(always)]
90     fn query_crate_is_local(&self) -> bool {
91         true
92     }
93
94     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
95         self.instance.default_span(tcx)
96     }
97 }
98
99 impl<'tcx> Key for (Ty<'tcx>, Option<ty::PolyExistentialTraitRef<'tcx>>) {
100     type CacheSelector = DefaultCacheSelector<Self>;
101
102     #[inline(always)]
103     fn query_crate_is_local(&self) -> bool {
104         true
105     }
106
107     fn default_span(&self, _: TyCtxt<'_>) -> Span {
108         DUMMY_SP
109     }
110 }
111
112 impl<'tcx> Key for mir::interpret::LitToConstInput<'tcx> {
113     type CacheSelector = DefaultCacheSelector<Self>;
114
115     #[inline(always)]
116     fn query_crate_is_local(&self) -> bool {
117         true
118     }
119
120     fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
121         DUMMY_SP
122     }
123 }
124
125 impl Key for CrateNum {
126     type CacheSelector = VecCacheSelector<Self>;
127
128     #[inline(always)]
129     fn query_crate_is_local(&self) -> bool {
130         *self == LOCAL_CRATE
131     }
132     fn default_span(&self, _: TyCtxt<'_>) -> Span {
133         DUMMY_SP
134     }
135 }
136
137 impl Key for OwnerId {
138     type CacheSelector = VecCacheSelector<Self>;
139
140     #[inline(always)]
141     fn query_crate_is_local(&self) -> bool {
142         true
143     }
144     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
145         self.to_def_id().default_span(tcx)
146     }
147     fn key_as_def_id(&self) -> Option<DefId> {
148         Some(self.to_def_id())
149     }
150 }
151
152 impl Key for LocalDefId {
153     type CacheSelector = VecCacheSelector<Self>;
154
155     #[inline(always)]
156     fn query_crate_is_local(&self) -> bool {
157         true
158     }
159     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
160         self.to_def_id().default_span(tcx)
161     }
162     fn key_as_def_id(&self) -> Option<DefId> {
163         Some(self.to_def_id())
164     }
165 }
166
167 impl Key for DefId {
168     type CacheSelector = DefaultCacheSelector<Self>;
169
170     #[inline(always)]
171     fn query_crate_is_local(&self) -> bool {
172         self.krate == LOCAL_CRATE
173     }
174     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
175         tcx.def_span(*self)
176     }
177     #[inline(always)]
178     fn key_as_def_id(&self) -> Option<DefId> {
179         Some(*self)
180     }
181 }
182
183 impl Key for ty::WithOptConstParam<LocalDefId> {
184     type CacheSelector = DefaultCacheSelector<Self>;
185
186     #[inline(always)]
187     fn query_crate_is_local(&self) -> bool {
188         true
189     }
190     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
191         self.did.default_span(tcx)
192     }
193 }
194
195 impl Key for SimplifiedType {
196     type CacheSelector = DefaultCacheSelector<Self>;
197
198     #[inline(always)]
199     fn query_crate_is_local(&self) -> bool {
200         true
201     }
202     fn default_span(&self, _: TyCtxt<'_>) -> Span {
203         DUMMY_SP
204     }
205 }
206
207 impl Key for (DefId, DefId) {
208     type CacheSelector = DefaultCacheSelector<Self>;
209
210     #[inline(always)]
211     fn query_crate_is_local(&self) -> bool {
212         self.0.krate == LOCAL_CRATE
213     }
214     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
215         self.1.default_span(tcx)
216     }
217 }
218
219 impl<'tcx> Key for (ty::Instance<'tcx>, LocalDefId) {
220     type CacheSelector = DefaultCacheSelector<Self>;
221
222     #[inline(always)]
223     fn query_crate_is_local(&self) -> bool {
224         true
225     }
226     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
227         self.0.default_span(tcx)
228     }
229 }
230
231 impl Key for (DefId, LocalDefId) {
232     type CacheSelector = DefaultCacheSelector<Self>;
233
234     #[inline(always)]
235     fn query_crate_is_local(&self) -> bool {
236         self.0.krate == LOCAL_CRATE
237     }
238     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
239         self.1.default_span(tcx)
240     }
241 }
242
243 impl Key for (LocalDefId, DefId) {
244     type CacheSelector = DefaultCacheSelector<Self>;
245
246     #[inline(always)]
247     fn query_crate_is_local(&self) -> bool {
248         true
249     }
250     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
251         self.0.default_span(tcx)
252     }
253 }
254
255 impl Key for (LocalDefId, LocalDefId) {
256     type CacheSelector = DefaultCacheSelector<Self>;
257
258     #[inline(always)]
259     fn query_crate_is_local(&self) -> bool {
260         true
261     }
262     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
263         self.0.default_span(tcx)
264     }
265 }
266
267 impl Key for (DefId, Option<Ident>) {
268     type CacheSelector = DefaultCacheSelector<Self>;
269
270     #[inline(always)]
271     fn query_crate_is_local(&self) -> bool {
272         self.0.krate == LOCAL_CRATE
273     }
274     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
275         tcx.def_span(self.0)
276     }
277     #[inline(always)]
278     fn key_as_def_id(&self) -> Option<DefId> {
279         Some(self.0)
280     }
281 }
282
283 impl Key for (DefId, LocalDefId, Ident) {
284     type CacheSelector = DefaultCacheSelector<Self>;
285
286     #[inline(always)]
287     fn query_crate_is_local(&self) -> bool {
288         self.0.krate == LOCAL_CRATE
289     }
290     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
291         self.1.default_span(tcx)
292     }
293 }
294
295 impl Key for (CrateNum, DefId) {
296     type CacheSelector = DefaultCacheSelector<Self>;
297
298     #[inline(always)]
299     fn query_crate_is_local(&self) -> bool {
300         self.0 == LOCAL_CRATE
301     }
302     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
303         self.1.default_span(tcx)
304     }
305 }
306
307 impl Key for (CrateNum, SimplifiedType) {
308     type CacheSelector = DefaultCacheSelector<Self>;
309
310     #[inline(always)]
311     fn query_crate_is_local(&self) -> bool {
312         self.0 == LOCAL_CRATE
313     }
314     fn default_span(&self, _: TyCtxt<'_>) -> Span {
315         DUMMY_SP
316     }
317 }
318
319 impl Key for (DefId, SimplifiedType) {
320     type CacheSelector = DefaultCacheSelector<Self>;
321
322     #[inline(always)]
323     fn query_crate_is_local(&self) -> bool {
324         self.0.krate == LOCAL_CRATE
325     }
326     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
327         self.0.default_span(tcx)
328     }
329 }
330
331 impl<'tcx> Key for SubstsRef<'tcx> {
332     type CacheSelector = DefaultCacheSelector<Self>;
333
334     #[inline(always)]
335     fn query_crate_is_local(&self) -> bool {
336         true
337     }
338     fn default_span(&self, _: TyCtxt<'_>) -> Span {
339         DUMMY_SP
340     }
341 }
342
343 impl<'tcx> Key for (DefId, SubstsRef<'tcx>) {
344     type CacheSelector = DefaultCacheSelector<Self>;
345
346     #[inline(always)]
347     fn query_crate_is_local(&self) -> bool {
348         self.0.krate == LOCAL_CRATE
349     }
350     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
351         self.0.default_span(tcx)
352     }
353 }
354
355 impl<'tcx> Key for (ty::UnevaluatedConst<'tcx>, ty::UnevaluatedConst<'tcx>) {
356     type CacheSelector = DefaultCacheSelector<Self>;
357
358     #[inline(always)]
359     fn query_crate_is_local(&self) -> bool {
360         (self.0).def.did.krate == LOCAL_CRATE
361     }
362     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
363         (self.0).def.did.default_span(tcx)
364     }
365 }
366
367 impl<'tcx> Key for (LocalDefId, DefId, SubstsRef<'tcx>) {
368     type CacheSelector = DefaultCacheSelector<Self>;
369
370     #[inline(always)]
371     fn query_crate_is_local(&self) -> bool {
372         true
373     }
374     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
375         self.0.default_span(tcx)
376     }
377 }
378
379 impl<'tcx> Key for (ty::ParamEnv<'tcx>, ty::PolyTraitRef<'tcx>) {
380     type CacheSelector = DefaultCacheSelector<Self>;
381
382     #[inline(always)]
383     fn query_crate_is_local(&self) -> bool {
384         self.1.def_id().krate == LOCAL_CRATE
385     }
386     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
387         tcx.def_span(self.1.def_id())
388     }
389 }
390
391 impl<'tcx> Key for (ty::Const<'tcx>, mir::Field) {
392     type CacheSelector = DefaultCacheSelector<Self>;
393
394     #[inline(always)]
395     fn query_crate_is_local(&self) -> bool {
396         true
397     }
398     fn default_span(&self, _: TyCtxt<'_>) -> Span {
399         DUMMY_SP
400     }
401 }
402
403 impl<'tcx> Key for mir::interpret::ConstAlloc<'tcx> {
404     type CacheSelector = DefaultCacheSelector<Self>;
405
406     #[inline(always)]
407     fn query_crate_is_local(&self) -> bool {
408         true
409     }
410     fn default_span(&self, _: TyCtxt<'_>) -> Span {
411         DUMMY_SP
412     }
413 }
414
415 impl<'tcx> Key for ty::PolyTraitRef<'tcx> {
416     type CacheSelector = DefaultCacheSelector<Self>;
417
418     #[inline(always)]
419     fn query_crate_is_local(&self) -> bool {
420         self.def_id().krate == LOCAL_CRATE
421     }
422     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
423         tcx.def_span(self.def_id())
424     }
425 }
426
427 impl<'tcx> Key for ty::PolyExistentialTraitRef<'tcx> {
428     type CacheSelector = DefaultCacheSelector<Self>;
429
430     #[inline(always)]
431     fn query_crate_is_local(&self) -> bool {
432         self.def_id().krate == LOCAL_CRATE
433     }
434     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
435         tcx.def_span(self.def_id())
436     }
437 }
438
439 impl<'tcx> Key for (ty::PolyTraitRef<'tcx>, ty::PolyTraitRef<'tcx>) {
440     type CacheSelector = DefaultCacheSelector<Self>;
441
442     #[inline(always)]
443     fn query_crate_is_local(&self) -> bool {
444         self.0.def_id().krate == LOCAL_CRATE
445     }
446     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
447         tcx.def_span(self.0.def_id())
448     }
449 }
450
451 impl<'tcx> Key for GenericArg<'tcx> {
452     type CacheSelector = DefaultCacheSelector<Self>;
453
454     #[inline(always)]
455     fn query_crate_is_local(&self) -> bool {
456         true
457     }
458     fn default_span(&self, _: TyCtxt<'_>) -> Span {
459         DUMMY_SP
460     }
461 }
462
463 impl<'tcx> Key for mir::ConstantKind<'tcx> {
464     type CacheSelector = DefaultCacheSelector<Self>;
465
466     #[inline(always)]
467     fn query_crate_is_local(&self) -> bool {
468         true
469     }
470     fn default_span(&self, _: TyCtxt<'_>) -> Span {
471         DUMMY_SP
472     }
473 }
474
475 impl<'tcx> Key for ty::Const<'tcx> {
476     type CacheSelector = DefaultCacheSelector<Self>;
477
478     #[inline(always)]
479     fn query_crate_is_local(&self) -> bool {
480         true
481     }
482     fn default_span(&self, _: TyCtxt<'_>) -> Span {
483         DUMMY_SP
484     }
485 }
486
487 impl<'tcx> Key for Ty<'tcx> {
488     type CacheSelector = DefaultCacheSelector<Self>;
489
490     #[inline(always)]
491     fn query_crate_is_local(&self) -> bool {
492         true
493     }
494     fn default_span(&self, _: TyCtxt<'_>) -> Span {
495         DUMMY_SP
496     }
497     fn ty_adt_id(&self) -> Option<DefId> {
498         match self.kind() {
499             ty::Adt(adt, _) => Some(adt.did()),
500             _ => None,
501         }
502     }
503 }
504
505 impl<'tcx> Key for TyAndLayout<'tcx> {
506     type CacheSelector = DefaultCacheSelector<Self>;
507
508     #[inline(always)]
509     fn query_crate_is_local(&self) -> bool {
510         true
511     }
512     fn default_span(&self, _: TyCtxt<'_>) -> Span {
513         DUMMY_SP
514     }
515 }
516
517 impl<'tcx> Key for (Ty<'tcx>, Ty<'tcx>) {
518     type CacheSelector = DefaultCacheSelector<Self>;
519
520     #[inline(always)]
521     fn query_crate_is_local(&self) -> bool {
522         true
523     }
524     fn default_span(&self, _: TyCtxt<'_>) -> Span {
525         DUMMY_SP
526     }
527 }
528
529 impl<'tcx> Key for &'tcx ty::List<ty::Predicate<'tcx>> {
530     type CacheSelector = DefaultCacheSelector<Self>;
531
532     #[inline(always)]
533     fn query_crate_is_local(&self) -> bool {
534         true
535     }
536     fn default_span(&self, _: TyCtxt<'_>) -> Span {
537         DUMMY_SP
538     }
539 }
540
541 impl<'tcx> Key for ty::ParamEnv<'tcx> {
542     type CacheSelector = DefaultCacheSelector<Self>;
543
544     #[inline(always)]
545     fn query_crate_is_local(&self) -> bool {
546         true
547     }
548     fn default_span(&self, _: TyCtxt<'_>) -> Span {
549         DUMMY_SP
550     }
551 }
552
553 impl<'tcx, T: Key> Key for ty::ParamEnvAnd<'tcx, T> {
554     type CacheSelector = DefaultCacheSelector<Self>;
555
556     #[inline(always)]
557     fn query_crate_is_local(&self) -> bool {
558         self.value.query_crate_is_local()
559     }
560     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
561         self.value.default_span(tcx)
562     }
563 }
564
565 impl Key for Symbol {
566     type CacheSelector = DefaultCacheSelector<Self>;
567
568     #[inline(always)]
569     fn query_crate_is_local(&self) -> bool {
570         true
571     }
572     fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
573         DUMMY_SP
574     }
575 }
576
577 impl Key for Option<Symbol> {
578     type CacheSelector = DefaultCacheSelector<Self>;
579
580     #[inline(always)]
581     fn query_crate_is_local(&self) -> bool {
582         true
583     }
584     fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
585         DUMMY_SP
586     }
587 }
588
589 /// Canonical query goals correspond to abstract trait operations that
590 /// are not tied to any crate in particular.
591 impl<'tcx, T> Key for Canonical<'tcx, T> {
592     type CacheSelector = DefaultCacheSelector<Self>;
593
594     #[inline(always)]
595     fn query_crate_is_local(&self) -> bool {
596         true
597     }
598
599     fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
600         DUMMY_SP
601     }
602 }
603
604 impl Key for (Symbol, u32, u32) {
605     type CacheSelector = DefaultCacheSelector<Self>;
606
607     #[inline(always)]
608     fn query_crate_is_local(&self) -> bool {
609         true
610     }
611
612     fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
613         DUMMY_SP
614     }
615 }
616
617 impl<'tcx> Key for (DefId, Ty<'tcx>, SubstsRef<'tcx>, ty::ParamEnv<'tcx>) {
618     type CacheSelector = DefaultCacheSelector<Self>;
619
620     #[inline(always)]
621     fn query_crate_is_local(&self) -> bool {
622         true
623     }
624
625     fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
626         DUMMY_SP
627     }
628 }
629
630 impl<'tcx> Key for (ty::Predicate<'tcx>, traits::WellFormedLoc) {
631     type CacheSelector = DefaultCacheSelector<Self>;
632
633     #[inline(always)]
634     fn query_crate_is_local(&self) -> bool {
635         true
636     }
637
638     fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
639         DUMMY_SP
640     }
641 }
642
643 impl<'tcx> Key for (ty::PolyFnSig<'tcx>, &'tcx ty::List<Ty<'tcx>>) {
644     type CacheSelector = DefaultCacheSelector<Self>;
645
646     #[inline(always)]
647     fn query_crate_is_local(&self) -> bool {
648         true
649     }
650
651     fn default_span(&self, _: TyCtxt<'_>) -> Span {
652         DUMMY_SP
653     }
654 }
655
656 impl<'tcx> Key for (ty::Instance<'tcx>, &'tcx ty::List<Ty<'tcx>>) {
657     type CacheSelector = DefaultCacheSelector<Self>;
658
659     #[inline(always)]
660     fn query_crate_is_local(&self) -> bool {
661         true
662     }
663
664     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
665         self.0.default_span(tcx)
666     }
667 }
668
669 impl<'tcx> Key for (Ty<'tcx>, ty::ValTree<'tcx>) {
670     type CacheSelector = DefaultCacheSelector<Self>;
671
672     #[inline(always)]
673     fn query_crate_is_local(&self) -> bool {
674         true
675     }
676
677     fn default_span(&self, _: TyCtxt<'_>) -> Span {
678         DUMMY_SP
679     }
680 }
681
682 impl Key for HirId {
683     type CacheSelector = DefaultCacheSelector<Self>;
684
685     #[inline(always)]
686     fn query_crate_is_local(&self) -> bool {
687         true
688     }
689
690     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
691         tcx.hir().span(*self)
692     }
693
694     #[inline(always)]
695     fn key_as_def_id(&self) -> Option<DefId> {
696         None
697     }
698 }