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