]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_query_impl/src/keys.rs
Allow to create definitions inside the query system.
[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_middle::infer::canonical::Canonical;
5 use rustc_middle::mir;
6 use rustc_middle::traits;
7 use rustc_middle::ty::fast_reject::SimplifiedType;
8 use rustc_middle::ty::subst::{GenericArg, SubstsRef};
9 use rustc_middle::ty::{self, Ty, TyCtxt};
10 use rustc_span::symbol::{Ident, Symbol};
11 use rustc_span::{Span, DUMMY_SP};
12
13 /// The `Key` trait controls what types can legally be used as the key
14 /// for a query.
15 pub trait Key {
16     /// Given an instance of this key, what crate is it referring to?
17     /// This is used to find the provider.
18     fn query_crate_is_local(&self) -> bool;
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?
22     fn default_span(&self, tcx: TyCtxt<'_>) -> Span;
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     }
29 }
30
31 impl Key for () {
32     #[inline(always)]
33     fn query_crate_is_local(&self) -> bool {
34         true
35     }
36
37     fn default_span(&self, _: TyCtxt<'_>) -> Span {
38         DUMMY_SP
39     }
40 }
41
42 impl Key for ty::RawLocalDefId {
43     #[inline(always)]
44     fn query_crate_is_local(&self) -> bool {
45         true
46     }
47
48     fn default_span(&self, _: TyCtxt<'_>) -> Span {
49         DUMMY_SP
50     }
51 }
52
53 impl<'tcx> Key for ty::InstanceDef<'tcx> {
54     #[inline(always)]
55     fn query_crate_is_local(&self) -> bool {
56         true
57     }
58
59     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
60         tcx.def_span(self.def_id())
61     }
62 }
63
64 impl<'tcx> Key for ty::Instance<'tcx> {
65     #[inline(always)]
66     fn query_crate_is_local(&self) -> bool {
67         true
68     }
69
70     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
71         tcx.def_span(self.def_id())
72     }
73 }
74
75 impl<'tcx> Key for mir::interpret::GlobalId<'tcx> {
76     #[inline(always)]
77     fn query_crate_is_local(&self) -> bool {
78         true
79     }
80
81     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
82         self.instance.default_span(tcx)
83     }
84 }
85
86 impl<'tcx> Key for (Ty<'tcx>, Option<ty::PolyExistentialTraitRef<'tcx>>) {
87     #[inline(always)]
88     fn query_crate_is_local(&self) -> bool {
89         true
90     }
91
92     fn default_span(&self, _: TyCtxt<'_>) -> Span {
93         DUMMY_SP
94     }
95 }
96
97 impl<'tcx> Key for mir::interpret::LitToConstInput<'tcx> {
98     #[inline(always)]
99     fn query_crate_is_local(&self) -> bool {
100         true
101     }
102
103     fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
104         DUMMY_SP
105     }
106 }
107
108 impl Key for CrateNum {
109     #[inline(always)]
110     fn query_crate_is_local(&self) -> bool {
111         *self == LOCAL_CRATE
112     }
113     fn default_span(&self, _: TyCtxt<'_>) -> Span {
114         DUMMY_SP
115     }
116 }
117
118 impl Key for LocalDefId {
119     #[inline(always)]
120     fn query_crate_is_local(&self) -> bool {
121         true
122     }
123     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
124         self.to_def_id().default_span(tcx)
125     }
126     fn key_as_def_id(&self) -> Option<DefId> {
127         Some(self.to_def_id())
128     }
129 }
130
131 impl Key for DefId {
132     #[inline(always)]
133     fn query_crate_is_local(&self) -> bool {
134         self.krate == LOCAL_CRATE
135     }
136     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
137         tcx.def_span(*self)
138     }
139     #[inline(always)]
140     fn key_as_def_id(&self) -> Option<DefId> {
141         Some(*self)
142     }
143 }
144
145 impl Key for ty::WithOptConstParam<LocalDefId> {
146     #[inline(always)]
147     fn query_crate_is_local(&self) -> bool {
148         true
149     }
150     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
151         self.did.default_span(tcx)
152     }
153 }
154
155 impl Key for SimplifiedType {
156     #[inline(always)]
157     fn query_crate_is_local(&self) -> bool {
158         true
159     }
160     fn default_span(&self, _: TyCtxt<'_>) -> Span {
161         DUMMY_SP
162     }
163 }
164
165 impl Key for (DefId, DefId) {
166     #[inline(always)]
167     fn query_crate_is_local(&self) -> bool {
168         self.0.krate == LOCAL_CRATE
169     }
170     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
171         self.1.default_span(tcx)
172     }
173 }
174
175 impl<'tcx> Key for (ty::Instance<'tcx>, LocalDefId) {
176     #[inline(always)]
177     fn query_crate_is_local(&self) -> bool {
178         true
179     }
180     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
181         self.0.default_span(tcx)
182     }
183 }
184
185 impl Key for (DefId, LocalDefId) {
186     #[inline(always)]
187     fn query_crate_is_local(&self) -> bool {
188         self.0.krate == LOCAL_CRATE
189     }
190     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
191         self.1.default_span(tcx)
192     }
193 }
194
195 impl Key for (LocalDefId, DefId) {
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 (Ty<'tcx>, Ty<'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 &'tcx ty::List<ty::Predicate<'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 ty::ParamEnv<'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, T: Key> Key for ty::ParamEnvAnd<'tcx, T> {
430     #[inline(always)]
431     fn query_crate_is_local(&self) -> bool {
432         self.value.query_crate_is_local()
433     }
434     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
435         self.value.default_span(tcx)
436     }
437 }
438
439 impl Key for Symbol {
440     #[inline(always)]
441     fn query_crate_is_local(&self) -> bool {
442         true
443     }
444     fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
445         DUMMY_SP
446     }
447 }
448
449 impl Key for Option<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 /// Canonical query goals correspond to abstract trait operations that
460 /// are not tied to any crate in particular.
461 impl<'tcx, T> Key for Canonical<'tcx, T> {
462     #[inline(always)]
463     fn query_crate_is_local(&self) -> bool {
464         true
465     }
466
467     fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
468         DUMMY_SP
469     }
470 }
471
472 impl Key for (Symbol, u32, u32) {
473     #[inline(always)]
474     fn query_crate_is_local(&self) -> bool {
475         true
476     }
477
478     fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
479         DUMMY_SP
480     }
481 }
482
483 impl<'tcx> Key for (DefId, Ty<'tcx>, SubstsRef<'tcx>, ty::ParamEnv<'tcx>) {
484     #[inline(always)]
485     fn query_crate_is_local(&self) -> bool {
486         true
487     }
488
489     fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
490         DUMMY_SP
491     }
492 }
493
494 impl<'tcx> Key for (ty::Predicate<'tcx>, traits::WellFormedLoc) {
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<'tcx> Key for (ty::PolyFnSig<'tcx>, &'tcx ty::List<Ty<'tcx>>) {
506     #[inline(always)]
507     fn query_crate_is_local(&self) -> bool {
508         true
509     }
510
511     fn default_span(&self, _: TyCtxt<'_>) -> Span {
512         DUMMY_SP
513     }
514 }
515
516 impl<'tcx> Key for (ty::Instance<'tcx>, &'tcx ty::List<Ty<'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         self.0.default_span(tcx)
524     }
525 }
526
527 impl<'tcx> Key for (Ty<'tcx>, ty::ValTree<'tcx>) {
528     #[inline(always)]
529     fn query_crate_is_local(&self) -> bool {
530         true
531     }
532
533     fn default_span(&self, _: TyCtxt<'_>) -> Span {
534         DUMMY_SP
535     }
536 }