]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_query_impl/src/keys.rs
Rollup merge of #102273 - woppopo:relax_const_bound, r=fee1-dead
[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::OwnerId;
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 OwnerId {
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 LocalDefId {
122     #[inline(always)]
123     fn query_crate_is_local(&self) -> bool {
124         true
125     }
126     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
127         self.to_def_id().default_span(tcx)
128     }
129     fn key_as_def_id(&self) -> Option<DefId> {
130         Some(self.to_def_id())
131     }
132 }
133
134 impl Key for DefId {
135     #[inline(always)]
136     fn query_crate_is_local(&self) -> bool {
137         self.krate == LOCAL_CRATE
138     }
139     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
140         tcx.def_span(*self)
141     }
142     #[inline(always)]
143     fn key_as_def_id(&self) -> Option<DefId> {
144         Some(*self)
145     }
146 }
147
148 impl Key for ty::WithOptConstParam<LocalDefId> {
149     #[inline(always)]
150     fn query_crate_is_local(&self) -> bool {
151         true
152     }
153     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
154         self.did.default_span(tcx)
155     }
156 }
157
158 impl Key for SimplifiedType {
159     #[inline(always)]
160     fn query_crate_is_local(&self) -> bool {
161         true
162     }
163     fn default_span(&self, _: TyCtxt<'_>) -> Span {
164         DUMMY_SP
165     }
166 }
167
168 impl Key for (DefId, DefId) {
169     #[inline(always)]
170     fn query_crate_is_local(&self) -> bool {
171         self.0.krate == LOCAL_CRATE
172     }
173     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
174         self.1.default_span(tcx)
175     }
176 }
177
178 impl<'tcx> Key for (ty::Instance<'tcx>, LocalDefId) {
179     #[inline(always)]
180     fn query_crate_is_local(&self) -> bool {
181         true
182     }
183     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
184         self.0.default_span(tcx)
185     }
186 }
187
188 impl Key for (DefId, LocalDefId) {
189     #[inline(always)]
190     fn query_crate_is_local(&self) -> bool {
191         self.0.krate == LOCAL_CRATE
192     }
193     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
194         self.1.default_span(tcx)
195     }
196 }
197
198 impl Key for (LocalDefId, DefId) {
199     #[inline(always)]
200     fn query_crate_is_local(&self) -> bool {
201         true
202     }
203     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
204         self.0.default_span(tcx)
205     }
206 }
207
208 impl Key for (LocalDefId, LocalDefId) {
209     #[inline(always)]
210     fn query_crate_is_local(&self) -> bool {
211         true
212     }
213     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
214         self.0.default_span(tcx)
215     }
216 }
217
218 impl Key for (DefId, Option<Ident>) {
219     #[inline(always)]
220     fn query_crate_is_local(&self) -> bool {
221         self.0.krate == LOCAL_CRATE
222     }
223     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
224         tcx.def_span(self.0)
225     }
226     #[inline(always)]
227     fn key_as_def_id(&self) -> Option<DefId> {
228         Some(self.0)
229     }
230 }
231
232 impl Key for (DefId, LocalDefId, Ident) {
233     #[inline(always)]
234     fn query_crate_is_local(&self) -> bool {
235         self.0.krate == LOCAL_CRATE
236     }
237     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
238         self.1.default_span(tcx)
239     }
240 }
241
242 impl Key for (CrateNum, DefId) {
243     #[inline(always)]
244     fn query_crate_is_local(&self) -> bool {
245         self.0 == LOCAL_CRATE
246     }
247     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
248         self.1.default_span(tcx)
249     }
250 }
251
252 impl Key for (CrateNum, SimplifiedType) {
253     #[inline(always)]
254     fn query_crate_is_local(&self) -> bool {
255         self.0 == LOCAL_CRATE
256     }
257     fn default_span(&self, _: TyCtxt<'_>) -> Span {
258         DUMMY_SP
259     }
260 }
261
262 impl Key for (DefId, SimplifiedType) {
263     #[inline(always)]
264     fn query_crate_is_local(&self) -> bool {
265         self.0.krate == LOCAL_CRATE
266     }
267     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
268         self.0.default_span(tcx)
269     }
270 }
271
272 impl<'tcx> Key for SubstsRef<'tcx> {
273     #[inline(always)]
274     fn query_crate_is_local(&self) -> bool {
275         true
276     }
277     fn default_span(&self, _: TyCtxt<'_>) -> Span {
278         DUMMY_SP
279     }
280 }
281
282 impl<'tcx> Key for (DefId, SubstsRef<'tcx>) {
283     #[inline(always)]
284     fn query_crate_is_local(&self) -> bool {
285         self.0.krate == LOCAL_CRATE
286     }
287     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
288         self.0.default_span(tcx)
289     }
290 }
291
292 impl<'tcx> Key for (ty::UnevaluatedConst<'tcx>, ty::UnevaluatedConst<'tcx>) {
293     #[inline(always)]
294     fn query_crate_is_local(&self) -> bool {
295         (self.0).def.did.krate == LOCAL_CRATE
296     }
297     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
298         (self.0).def.did.default_span(tcx)
299     }
300 }
301
302 impl<'tcx> Key for (LocalDefId, DefId, SubstsRef<'tcx>) {
303     #[inline(always)]
304     fn query_crate_is_local(&self) -> bool {
305         true
306     }
307     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
308         self.0.default_span(tcx)
309     }
310 }
311
312 impl<'tcx> Key for (ty::ParamEnv<'tcx>, ty::PolyTraitRef<'tcx>) {
313     #[inline(always)]
314     fn query_crate_is_local(&self) -> bool {
315         self.1.def_id().krate == LOCAL_CRATE
316     }
317     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
318         tcx.def_span(self.1.def_id())
319     }
320 }
321
322 impl<'tcx> Key for (ty::Const<'tcx>, mir::Field) {
323     #[inline(always)]
324     fn query_crate_is_local(&self) -> bool {
325         true
326     }
327     fn default_span(&self, _: TyCtxt<'_>) -> Span {
328         DUMMY_SP
329     }
330 }
331
332 impl<'tcx> Key for mir::interpret::ConstAlloc<'tcx> {
333     #[inline(always)]
334     fn query_crate_is_local(&self) -> bool {
335         true
336     }
337     fn default_span(&self, _: TyCtxt<'_>) -> Span {
338         DUMMY_SP
339     }
340 }
341
342 impl<'tcx> Key for ty::PolyTraitRef<'tcx> {
343     #[inline(always)]
344     fn query_crate_is_local(&self) -> bool {
345         self.def_id().krate == LOCAL_CRATE
346     }
347     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
348         tcx.def_span(self.def_id())
349     }
350 }
351
352 impl<'tcx> Key for ty::PolyExistentialTraitRef<'tcx> {
353     #[inline(always)]
354     fn query_crate_is_local(&self) -> bool {
355         self.def_id().krate == LOCAL_CRATE
356     }
357     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
358         tcx.def_span(self.def_id())
359     }
360 }
361
362 impl<'tcx> Key for (ty::PolyTraitRef<'tcx>, ty::PolyTraitRef<'tcx>) {
363     #[inline(always)]
364     fn query_crate_is_local(&self) -> bool {
365         self.0.def_id().krate == LOCAL_CRATE
366     }
367     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
368         tcx.def_span(self.0.def_id())
369     }
370 }
371
372 impl<'tcx> Key for GenericArg<'tcx> {
373     #[inline(always)]
374     fn query_crate_is_local(&self) -> bool {
375         true
376     }
377     fn default_span(&self, _: TyCtxt<'_>) -> Span {
378         DUMMY_SP
379     }
380 }
381
382 impl<'tcx> Key for mir::ConstantKind<'tcx> {
383     #[inline(always)]
384     fn query_crate_is_local(&self) -> bool {
385         true
386     }
387     fn default_span(&self, _: TyCtxt<'_>) -> Span {
388         DUMMY_SP
389     }
390 }
391
392 impl<'tcx> Key for ty::Const<'tcx> {
393     #[inline(always)]
394     fn query_crate_is_local(&self) -> bool {
395         true
396     }
397     fn default_span(&self, _: TyCtxt<'_>) -> Span {
398         DUMMY_SP
399     }
400 }
401
402 impl<'tcx> Key for Ty<'tcx> {
403     #[inline(always)]
404     fn query_crate_is_local(&self) -> bool {
405         true
406     }
407     fn default_span(&self, _: TyCtxt<'_>) -> Span {
408         DUMMY_SP
409     }
410 }
411
412 impl<'tcx> Key for TyAndLayout<'tcx> {
413     #[inline(always)]
414     fn query_crate_is_local(&self) -> bool {
415         true
416     }
417     fn default_span(&self, _: TyCtxt<'_>) -> Span {
418         DUMMY_SP
419     }
420 }
421
422 impl<'tcx> Key for (Ty<'tcx>, Ty<'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 &'tcx ty::List<ty::Predicate<'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 ty::ParamEnv<'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, T: Key> Key for ty::ParamEnvAnd<'tcx, T> {
453     #[inline(always)]
454     fn query_crate_is_local(&self) -> bool {
455         self.value.query_crate_is_local()
456     }
457     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
458         self.value.default_span(tcx)
459     }
460 }
461
462 impl Key for Symbol {
463     #[inline(always)]
464     fn query_crate_is_local(&self) -> bool {
465         true
466     }
467     fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
468         DUMMY_SP
469     }
470 }
471
472 impl Key for Option<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 /// Canonical query goals correspond to abstract trait operations that
483 /// are not tied to any crate in particular.
484 impl<'tcx, T> Key for Canonical<'tcx, T> {
485     #[inline(always)]
486     fn query_crate_is_local(&self) -> bool {
487         true
488     }
489
490     fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
491         DUMMY_SP
492     }
493 }
494
495 impl Key for (Symbol, u32, u32) {
496     #[inline(always)]
497     fn query_crate_is_local(&self) -> bool {
498         true
499     }
500
501     fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
502         DUMMY_SP
503     }
504 }
505
506 impl<'tcx> Key for (DefId, Ty<'tcx>, SubstsRef<'tcx>, ty::ParamEnv<'tcx>) {
507     #[inline(always)]
508     fn query_crate_is_local(&self) -> bool {
509         true
510     }
511
512     fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
513         DUMMY_SP
514     }
515 }
516
517 impl<'tcx> Key for (ty::Predicate<'tcx>, traits::WellFormedLoc) {
518     #[inline(always)]
519     fn query_crate_is_local(&self) -> bool {
520         true
521     }
522
523     fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
524         DUMMY_SP
525     }
526 }
527
528 impl<'tcx> Key for (ty::PolyFnSig<'tcx>, &'tcx ty::List<Ty<'tcx>>) {
529     #[inline(always)]
530     fn query_crate_is_local(&self) -> bool {
531         true
532     }
533
534     fn default_span(&self, _: TyCtxt<'_>) -> Span {
535         DUMMY_SP
536     }
537 }
538
539 impl<'tcx> Key for (ty::Instance<'tcx>, &'tcx ty::List<Ty<'tcx>>) {
540     #[inline(always)]
541     fn query_crate_is_local(&self) -> bool {
542         true
543     }
544
545     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
546         self.0.default_span(tcx)
547     }
548 }
549
550 impl<'tcx> Key for (Ty<'tcx>, ty::ValTree<'tcx>) {
551     #[inline(always)]
552     fn query_crate_is_local(&self) -> bool {
553         true
554     }
555
556     fn default_span(&self, _: TyCtxt<'_>) -> Span {
557         DUMMY_SP
558     }
559 }