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