]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_middle/src/query/keys.rs
Auto merge of #103880 - b-naber:field-ty-mir, r=lcnr
[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     type CacheSelector = DefaultCacheSelector<Self>;
19
20     /// Given an instance of this key, what crate is it referring to?
21     /// This is used to find the provider.
22     fn query_crate_is_local(&self) -> bool;
23
24     /// In the event that a cycle occurs, if no explicit span has been
25     /// given for a query with key `self`, what span should we use?
26     fn default_span(&self, tcx: TyCtxt<'_>) -> Span;
27
28     /// If the key is a [`DefId`] or `DefId`--equivalent, return that `DefId`.
29     /// Otherwise, return `None`.
30     fn key_as_def_id(&self) -> Option<DefId> {
31         None
32     }
33
34     fn ty_adt_id(&self) -> Option<DefId> {
35         None
36     }
37 }
38
39 impl Key for () {
40     #[inline(always)]
41     fn query_crate_is_local(&self) -> bool {
42         true
43     }
44
45     fn default_span(&self, _: TyCtxt<'_>) -> Span {
46         DUMMY_SP
47     }
48 }
49
50 impl<'tcx> Key for ty::InstanceDef<'tcx> {
51     #[inline(always)]
52     fn query_crate_is_local(&self) -> bool {
53         true
54     }
55
56     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
57         tcx.def_span(self.def_id())
58     }
59 }
60
61 impl<'tcx> Key for ty::Instance<'tcx> {
62     #[inline(always)]
63     fn query_crate_is_local(&self) -> bool {
64         true
65     }
66
67     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
68         tcx.def_span(self.def_id())
69     }
70 }
71
72 impl<'tcx> Key for mir::interpret::GlobalId<'tcx> {
73     #[inline(always)]
74     fn query_crate_is_local(&self) -> bool {
75         true
76     }
77
78     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
79         self.instance.default_span(tcx)
80     }
81 }
82
83 impl<'tcx> Key for (Ty<'tcx>, Option<ty::PolyExistentialTraitRef<'tcx>>) {
84     #[inline(always)]
85     fn query_crate_is_local(&self) -> bool {
86         true
87     }
88
89     fn default_span(&self, _: TyCtxt<'_>) -> Span {
90         DUMMY_SP
91     }
92 }
93
94 impl<'tcx> Key for mir::interpret::LitToConstInput<'tcx> {
95     #[inline(always)]
96     fn query_crate_is_local(&self) -> bool {
97         true
98     }
99
100     fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
101         DUMMY_SP
102     }
103 }
104
105 impl Key for CrateNum {
106     type CacheSelector = VecCacheSelector<Self>;
107
108     #[inline(always)]
109     fn query_crate_is_local(&self) -> bool {
110         *self == LOCAL_CRATE
111     }
112     fn default_span(&self, _: TyCtxt<'_>) -> Span {
113         DUMMY_SP
114     }
115 }
116
117 impl Key for OwnerId {
118     type CacheSelector = VecCacheSelector<Self>;
119
120     #[inline(always)]
121     fn query_crate_is_local(&self) -> bool {
122         true
123     }
124     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
125         self.to_def_id().default_span(tcx)
126     }
127     fn key_as_def_id(&self) -> Option<DefId> {
128         Some(self.to_def_id())
129     }
130 }
131
132 impl Key for LocalDefId {
133     type CacheSelector = VecCacheSelector<Self>;
134
135     #[inline(always)]
136     fn query_crate_is_local(&self) -> bool {
137         true
138     }
139     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
140         self.to_def_id().default_span(tcx)
141     }
142     fn key_as_def_id(&self) -> Option<DefId> {
143         Some(self.to_def_id())
144     }
145 }
146
147 impl Key for DefId {
148     #[inline(always)]
149     fn query_crate_is_local(&self) -> bool {
150         self.krate == LOCAL_CRATE
151     }
152     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
153         tcx.def_span(*self)
154     }
155     #[inline(always)]
156     fn key_as_def_id(&self) -> Option<DefId> {
157         Some(*self)
158     }
159 }
160
161 impl Key for ty::WithOptConstParam<LocalDefId> {
162     #[inline(always)]
163     fn query_crate_is_local(&self) -> bool {
164         true
165     }
166     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
167         self.did.default_span(tcx)
168     }
169 }
170
171 impl Key for SimplifiedType {
172     #[inline(always)]
173     fn query_crate_is_local(&self) -> bool {
174         true
175     }
176     fn default_span(&self, _: TyCtxt<'_>) -> Span {
177         DUMMY_SP
178     }
179 }
180
181 impl Key for (DefId, DefId) {
182     #[inline(always)]
183     fn query_crate_is_local(&self) -> bool {
184         self.0.krate == LOCAL_CRATE
185     }
186     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
187         self.1.default_span(tcx)
188     }
189 }
190
191 impl<'tcx> Key for (ty::Instance<'tcx>, LocalDefId) {
192     #[inline(always)]
193     fn query_crate_is_local(&self) -> bool {
194         true
195     }
196     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
197         self.0.default_span(tcx)
198     }
199 }
200
201 impl Key for (DefId, LocalDefId) {
202     #[inline(always)]
203     fn query_crate_is_local(&self) -> bool {
204         self.0.krate == LOCAL_CRATE
205     }
206     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
207         self.1.default_span(tcx)
208     }
209 }
210
211 impl Key for (LocalDefId, DefId) {
212     #[inline(always)]
213     fn query_crate_is_local(&self) -> bool {
214         true
215     }
216     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
217         self.0.default_span(tcx)
218     }
219 }
220
221 impl Key for (LocalDefId, LocalDefId) {
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, Option<Ident>) {
232     #[inline(always)]
233     fn query_crate_is_local(&self) -> bool {
234         self.0.krate == LOCAL_CRATE
235     }
236     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
237         tcx.def_span(self.0)
238     }
239     #[inline(always)]
240     fn key_as_def_id(&self) -> Option<DefId> {
241         Some(self.0)
242     }
243 }
244
245 impl Key for (DefId, LocalDefId, Ident) {
246     #[inline(always)]
247     fn query_crate_is_local(&self) -> bool {
248         self.0.krate == LOCAL_CRATE
249     }
250     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
251         self.1.default_span(tcx)
252     }
253 }
254
255 impl Key for (CrateNum, DefId) {
256     #[inline(always)]
257     fn query_crate_is_local(&self) -> bool {
258         self.0 == LOCAL_CRATE
259     }
260     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
261         self.1.default_span(tcx)
262     }
263 }
264
265 impl Key for (CrateNum, SimplifiedType) {
266     #[inline(always)]
267     fn query_crate_is_local(&self) -> bool {
268         self.0 == LOCAL_CRATE
269     }
270     fn default_span(&self, _: TyCtxt<'_>) -> Span {
271         DUMMY_SP
272     }
273 }
274
275 impl Key for (DefId, SimplifiedType) {
276     #[inline(always)]
277     fn query_crate_is_local(&self) -> bool {
278         self.0.krate == LOCAL_CRATE
279     }
280     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
281         self.0.default_span(tcx)
282     }
283 }
284
285 impl<'tcx> Key for SubstsRef<'tcx> {
286     #[inline(always)]
287     fn query_crate_is_local(&self) -> bool {
288         true
289     }
290     fn default_span(&self, _: TyCtxt<'_>) -> Span {
291         DUMMY_SP
292     }
293 }
294
295 impl<'tcx> Key for (DefId, SubstsRef<'tcx>) {
296     #[inline(always)]
297     fn query_crate_is_local(&self) -> bool {
298         self.0.krate == LOCAL_CRATE
299     }
300     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
301         self.0.default_span(tcx)
302     }
303 }
304
305 impl<'tcx> Key for (ty::UnevaluatedConst<'tcx>, ty::UnevaluatedConst<'tcx>) {
306     #[inline(always)]
307     fn query_crate_is_local(&self) -> bool {
308         (self.0).def.did.krate == LOCAL_CRATE
309     }
310     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
311         (self.0).def.did.default_span(tcx)
312     }
313 }
314
315 impl<'tcx> Key for (LocalDefId, DefId, SubstsRef<'tcx>) {
316     #[inline(always)]
317     fn query_crate_is_local(&self) -> bool {
318         true
319     }
320     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
321         self.0.default_span(tcx)
322     }
323 }
324
325 impl<'tcx> Key for (ty::ParamEnv<'tcx>, ty::PolyTraitRef<'tcx>) {
326     #[inline(always)]
327     fn query_crate_is_local(&self) -> bool {
328         self.1.def_id().krate == LOCAL_CRATE
329     }
330     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
331         tcx.def_span(self.1.def_id())
332     }
333 }
334
335 impl<'tcx> Key for (ty::Const<'tcx>, mir::Field) {
336     #[inline(always)]
337     fn query_crate_is_local(&self) -> bool {
338         true
339     }
340     fn default_span(&self, _: TyCtxt<'_>) -> Span {
341         DUMMY_SP
342     }
343 }
344
345 impl<'tcx> Key for mir::interpret::ConstAlloc<'tcx> {
346     #[inline(always)]
347     fn query_crate_is_local(&self) -> bool {
348         true
349     }
350     fn default_span(&self, _: TyCtxt<'_>) -> Span {
351         DUMMY_SP
352     }
353 }
354
355 impl<'tcx> Key for ty::PolyTraitRef<'tcx> {
356     #[inline(always)]
357     fn query_crate_is_local(&self) -> bool {
358         self.def_id().krate == LOCAL_CRATE
359     }
360     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
361         tcx.def_span(self.def_id())
362     }
363 }
364
365 impl<'tcx> Key for ty::PolyExistentialTraitRef<'tcx> {
366     #[inline(always)]
367     fn query_crate_is_local(&self) -> bool {
368         self.def_id().krate == LOCAL_CRATE
369     }
370     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
371         tcx.def_span(self.def_id())
372     }
373 }
374
375 impl<'tcx> Key for (ty::PolyTraitRef<'tcx>, ty::PolyTraitRef<'tcx>) {
376     #[inline(always)]
377     fn query_crate_is_local(&self) -> bool {
378         self.0.def_id().krate == LOCAL_CRATE
379     }
380     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
381         tcx.def_span(self.0.def_id())
382     }
383 }
384
385 impl<'tcx> Key for GenericArg<'tcx> {
386     #[inline(always)]
387     fn query_crate_is_local(&self) -> bool {
388         true
389     }
390     fn default_span(&self, _: TyCtxt<'_>) -> Span {
391         DUMMY_SP
392     }
393 }
394
395 impl<'tcx> Key for mir::ConstantKind<'tcx> {
396     #[inline(always)]
397     fn query_crate_is_local(&self) -> bool {
398         true
399     }
400     fn default_span(&self, _: TyCtxt<'_>) -> Span {
401         DUMMY_SP
402     }
403 }
404
405 impl<'tcx> Key for ty::Const<'tcx> {
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<'tcx> {
416     #[inline(always)]
417     fn query_crate_is_local(&self) -> bool {
418         true
419     }
420     fn default_span(&self, _: TyCtxt<'_>) -> Span {
421         DUMMY_SP
422     }
423     fn ty_adt_id(&self) -> Option<DefId> {
424         match self.kind() {
425             ty::Adt(adt, _) => Some(adt.did()),
426             _ => None,
427         }
428     }
429 }
430
431 impl<'tcx> Key for TyAndLayout<'tcx> {
432     #[inline(always)]
433     fn query_crate_is_local(&self) -> bool {
434         true
435     }
436     fn default_span(&self, _: TyCtxt<'_>) -> Span {
437         DUMMY_SP
438     }
439 }
440
441 impl<'tcx> Key for (Ty<'tcx>, Ty<'tcx>) {
442     #[inline(always)]
443     fn query_crate_is_local(&self) -> bool {
444         true
445     }
446     fn default_span(&self, _: TyCtxt<'_>) -> Span {
447         DUMMY_SP
448     }
449 }
450
451 impl<'tcx> Key for &'tcx ty::List<ty::Predicate<'tcx>> {
452     #[inline(always)]
453     fn query_crate_is_local(&self) -> bool {
454         true
455     }
456     fn default_span(&self, _: TyCtxt<'_>) -> Span {
457         DUMMY_SP
458     }
459 }
460
461 impl<'tcx> Key for ty::ParamEnv<'tcx> {
462     #[inline(always)]
463     fn query_crate_is_local(&self) -> bool {
464         true
465     }
466     fn default_span(&self, _: TyCtxt<'_>) -> Span {
467         DUMMY_SP
468     }
469 }
470
471 impl<'tcx, T: Key> Key for ty::ParamEnvAnd<'tcx, T> {
472     #[inline(always)]
473     fn query_crate_is_local(&self) -> bool {
474         self.value.query_crate_is_local()
475     }
476     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
477         self.value.default_span(tcx)
478     }
479 }
480
481 impl Key for Symbol {
482     #[inline(always)]
483     fn query_crate_is_local(&self) -> bool {
484         true
485     }
486     fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
487         DUMMY_SP
488     }
489 }
490
491 impl Key for Option<Symbol> {
492     #[inline(always)]
493     fn query_crate_is_local(&self) -> bool {
494         true
495     }
496     fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
497         DUMMY_SP
498     }
499 }
500
501 /// Canonical query goals correspond to abstract trait operations that
502 /// are not tied to any crate in particular.
503 impl<'tcx, T> Key for Canonical<'tcx, T> {
504     #[inline(always)]
505     fn query_crate_is_local(&self) -> bool {
506         true
507     }
508
509     fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
510         DUMMY_SP
511     }
512 }
513
514 impl Key for (Symbol, u32, u32) {
515     #[inline(always)]
516     fn query_crate_is_local(&self) -> bool {
517         true
518     }
519
520     fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
521         DUMMY_SP
522     }
523 }
524
525 impl<'tcx> Key for (DefId, Ty<'tcx>, SubstsRef<'tcx>, ty::ParamEnv<'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         DUMMY_SP
533     }
534 }
535
536 impl<'tcx> Key for (ty::Predicate<'tcx>, traits::WellFormedLoc) {
537     #[inline(always)]
538     fn query_crate_is_local(&self) -> bool {
539         true
540     }
541
542     fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
543         DUMMY_SP
544     }
545 }
546
547 impl<'tcx> Key for (ty::PolyFnSig<'tcx>, &'tcx ty::List<Ty<'tcx>>) {
548     #[inline(always)]
549     fn query_crate_is_local(&self) -> bool {
550         true
551     }
552
553     fn default_span(&self, _: TyCtxt<'_>) -> Span {
554         DUMMY_SP
555     }
556 }
557
558 impl<'tcx> Key for (ty::Instance<'tcx>, &'tcx ty::List<Ty<'tcx>>) {
559     #[inline(always)]
560     fn query_crate_is_local(&self) -> bool {
561         true
562     }
563
564     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
565         self.0.default_span(tcx)
566     }
567 }
568
569 impl<'tcx> Key for (Ty<'tcx>, ty::ValTree<'tcx>) {
570     #[inline(always)]
571     fn query_crate_is_local(&self) -> bool {
572         true
573     }
574
575     fn default_span(&self, _: TyCtxt<'_>) -> Span {
576         DUMMY_SP
577     }
578 }
579
580 impl Key for HirId {
581     #[inline(always)]
582     fn query_crate_is_local(&self) -> bool {
583         true
584     }
585
586     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
587         tcx.hir().span(*self)
588     }
589
590     #[inline(always)]
591     fn key_as_def_id(&self) -> Option<DefId> {
592         None
593     }
594 }