]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_query_impl/src/keys.rs
Auto merge of #98866 - nagisa:nagisa/align-offset-wroom, r=Mark-Simulacrum
[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<'tcx> Key for ty::InstanceDef<'tcx> {
43     #[inline(always)]
44     fn query_crate_is_local(&self) -> bool {
45         true
46     }
47
48     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
49         tcx.def_span(self.def_id())
50     }
51 }
52
53 impl<'tcx> Key for ty::Instance<'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 mir::interpret::GlobalId<'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         self.instance.default_span(tcx)
72     }
73 }
74
75 impl<'tcx> Key for (Ty<'tcx>, Option<ty::PolyExistentialTraitRef<'tcx>>) {
76     #[inline(always)]
77     fn query_crate_is_local(&self) -> bool {
78         true
79     }
80
81     fn default_span(&self, _: TyCtxt<'_>) -> Span {
82         DUMMY_SP
83     }
84 }
85
86 impl<'tcx> Key for mir::interpret::LitToConstInput<'tcx> {
87     #[inline(always)]
88     fn query_crate_is_local(&self) -> bool {
89         true
90     }
91
92     fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
93         DUMMY_SP
94     }
95 }
96
97 impl Key for CrateNum {
98     #[inline(always)]
99     fn query_crate_is_local(&self) -> bool {
100         *self == LOCAL_CRATE
101     }
102     fn default_span(&self, _: TyCtxt<'_>) -> Span {
103         DUMMY_SP
104     }
105 }
106
107 impl Key for LocalDefId {
108     #[inline(always)]
109     fn query_crate_is_local(&self) -> bool {
110         true
111     }
112     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
113         self.to_def_id().default_span(tcx)
114     }
115     fn key_as_def_id(&self) -> Option<DefId> {
116         Some(self.to_def_id())
117     }
118 }
119
120 impl Key for DefId {
121     #[inline(always)]
122     fn query_crate_is_local(&self) -> bool {
123         self.krate == LOCAL_CRATE
124     }
125     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
126         tcx.def_span(*self)
127     }
128     #[inline(always)]
129     fn key_as_def_id(&self) -> Option<DefId> {
130         Some(*self)
131     }
132 }
133
134 impl Key for ty::WithOptConstParam<LocalDefId> {
135     #[inline(always)]
136     fn query_crate_is_local(&self) -> bool {
137         true
138     }
139     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
140         self.did.default_span(tcx)
141     }
142 }
143
144 impl Key for SimplifiedType {
145     #[inline(always)]
146     fn query_crate_is_local(&self) -> bool {
147         true
148     }
149     fn default_span(&self, _: TyCtxt<'_>) -> Span {
150         DUMMY_SP
151     }
152 }
153
154 impl Key for (DefId, DefId) {
155     #[inline(always)]
156     fn query_crate_is_local(&self) -> bool {
157         self.0.krate == LOCAL_CRATE
158     }
159     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
160         self.1.default_span(tcx)
161     }
162 }
163
164 impl<'tcx> Key for (ty::Instance<'tcx>, LocalDefId) {
165     #[inline(always)]
166     fn query_crate_is_local(&self) -> bool {
167         true
168     }
169     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
170         self.0.default_span(tcx)
171     }
172 }
173
174 impl Key for (DefId, LocalDefId) {
175     #[inline(always)]
176     fn query_crate_is_local(&self) -> bool {
177         self.0.krate == LOCAL_CRATE
178     }
179     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
180         self.1.default_span(tcx)
181     }
182 }
183
184 impl Key for (LocalDefId, DefId) {
185     #[inline(always)]
186     fn query_crate_is_local(&self) -> bool {
187         true
188     }
189     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
190         self.0.default_span(tcx)
191     }
192 }
193
194 impl Key for (DefId, Option<Ident>) {
195     #[inline(always)]
196     fn query_crate_is_local(&self) -> bool {
197         self.0.krate == LOCAL_CRATE
198     }
199     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
200         tcx.def_span(self.0)
201     }
202     #[inline(always)]
203     fn key_as_def_id(&self) -> Option<DefId> {
204         Some(self.0)
205     }
206 }
207
208 impl Key for (DefId, LocalDefId, Ident) {
209     #[inline(always)]
210     fn query_crate_is_local(&self) -> bool {
211         self.0.krate == LOCAL_CRATE
212     }
213     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
214         self.1.default_span(tcx)
215     }
216 }
217
218 impl Key for (CrateNum, DefId) {
219     #[inline(always)]
220     fn query_crate_is_local(&self) -> bool {
221         self.0 == LOCAL_CRATE
222     }
223     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
224         self.1.default_span(tcx)
225     }
226 }
227
228 impl Key for (CrateNum, SimplifiedType) {
229     #[inline(always)]
230     fn query_crate_is_local(&self) -> bool {
231         self.0 == LOCAL_CRATE
232     }
233     fn default_span(&self, _: TyCtxt<'_>) -> Span {
234         DUMMY_SP
235     }
236 }
237
238 impl Key for (DefId, SimplifiedType) {
239     #[inline(always)]
240     fn query_crate_is_local(&self) -> bool {
241         self.0.krate == LOCAL_CRATE
242     }
243     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
244         self.0.default_span(tcx)
245     }
246 }
247
248 impl<'tcx> Key for SubstsRef<'tcx> {
249     #[inline(always)]
250     fn query_crate_is_local(&self) -> bool {
251         true
252     }
253     fn default_span(&self, _: TyCtxt<'_>) -> Span {
254         DUMMY_SP
255     }
256 }
257
258 impl<'tcx> Key for (DefId, SubstsRef<'tcx>) {
259     #[inline(always)]
260     fn query_crate_is_local(&self) -> bool {
261         self.0.krate == LOCAL_CRATE
262     }
263     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
264         self.0.default_span(tcx)
265     }
266 }
267
268 impl<'tcx> Key for (ty::Unevaluated<'tcx, ()>, ty::Unevaluated<'tcx, ()>) {
269     #[inline(always)]
270     fn query_crate_is_local(&self) -> bool {
271         (self.0).def.did.krate == LOCAL_CRATE
272     }
273     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
274         (self.0).def.did.default_span(tcx)
275     }
276 }
277
278 impl<'tcx> Key for (LocalDefId, DefId, SubstsRef<'tcx>) {
279     #[inline(always)]
280     fn query_crate_is_local(&self) -> bool {
281         true
282     }
283     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
284         self.0.default_span(tcx)
285     }
286 }
287
288 impl<'tcx> Key for (ty::ParamEnv<'tcx>, ty::PolyTraitRef<'tcx>) {
289     #[inline(always)]
290     fn query_crate_is_local(&self) -> bool {
291         self.1.def_id().krate == LOCAL_CRATE
292     }
293     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
294         tcx.def_span(self.1.def_id())
295     }
296 }
297
298 impl<'tcx> Key for (ty::Const<'tcx>, mir::Field) {
299     #[inline(always)]
300     fn query_crate_is_local(&self) -> bool {
301         true
302     }
303     fn default_span(&self, _: TyCtxt<'_>) -> Span {
304         DUMMY_SP
305     }
306 }
307
308 impl<'tcx> Key for mir::interpret::ConstAlloc<'tcx> {
309     #[inline(always)]
310     fn query_crate_is_local(&self) -> bool {
311         true
312     }
313     fn default_span(&self, _: TyCtxt<'_>) -> Span {
314         DUMMY_SP
315     }
316 }
317
318 impl<'tcx> Key for ty::PolyTraitRef<'tcx> {
319     #[inline(always)]
320     fn query_crate_is_local(&self) -> bool {
321         self.def_id().krate == LOCAL_CRATE
322     }
323     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
324         tcx.def_span(self.def_id())
325     }
326 }
327
328 impl<'tcx> Key for ty::PolyExistentialTraitRef<'tcx> {
329     #[inline(always)]
330     fn query_crate_is_local(&self) -> bool {
331         self.def_id().krate == LOCAL_CRATE
332     }
333     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
334         tcx.def_span(self.def_id())
335     }
336 }
337
338 impl<'tcx> Key for (ty::PolyTraitRef<'tcx>, ty::PolyTraitRef<'tcx>) {
339     #[inline(always)]
340     fn query_crate_is_local(&self) -> bool {
341         self.0.def_id().krate == LOCAL_CRATE
342     }
343     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
344         tcx.def_span(self.0.def_id())
345     }
346 }
347
348 impl<'tcx> Key for GenericArg<'tcx> {
349     #[inline(always)]
350     fn query_crate_is_local(&self) -> bool {
351         true
352     }
353     fn default_span(&self, _: TyCtxt<'_>) -> Span {
354         DUMMY_SP
355     }
356 }
357
358 impl<'tcx> Key for mir::ConstantKind<'tcx> {
359     #[inline(always)]
360     fn query_crate_is_local(&self) -> bool {
361         true
362     }
363     fn default_span(&self, _: TyCtxt<'_>) -> Span {
364         DUMMY_SP
365     }
366 }
367
368 impl<'tcx> Key for ty::Const<'tcx> {
369     #[inline(always)]
370     fn query_crate_is_local(&self) -> bool {
371         true
372     }
373     fn default_span(&self, _: TyCtxt<'_>) -> Span {
374         DUMMY_SP
375     }
376 }
377
378 impl<'tcx> Key for Ty<'tcx> {
379     #[inline(always)]
380     fn query_crate_is_local(&self) -> bool {
381         true
382     }
383     fn default_span(&self, _: TyCtxt<'_>) -> Span {
384         DUMMY_SP
385     }
386 }
387
388 impl<'tcx> Key for (Ty<'tcx>, Ty<'tcx>) {
389     #[inline(always)]
390     fn query_crate_is_local(&self) -> bool {
391         true
392     }
393     fn default_span(&self, _: TyCtxt<'_>) -> Span {
394         DUMMY_SP
395     }
396 }
397
398 impl<'tcx> Key for &'tcx ty::List<ty::Predicate<'tcx>> {
399     #[inline(always)]
400     fn query_crate_is_local(&self) -> bool {
401         true
402     }
403     fn default_span(&self, _: TyCtxt<'_>) -> Span {
404         DUMMY_SP
405     }
406 }
407
408 impl<'tcx> Key for ty::ParamEnv<'tcx> {
409     #[inline(always)]
410     fn query_crate_is_local(&self) -> bool {
411         true
412     }
413     fn default_span(&self, _: TyCtxt<'_>) -> Span {
414         DUMMY_SP
415     }
416 }
417
418 impl<'tcx, T: Key> Key for ty::ParamEnvAnd<'tcx, T> {
419     #[inline(always)]
420     fn query_crate_is_local(&self) -> bool {
421         self.value.query_crate_is_local()
422     }
423     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
424         self.value.default_span(tcx)
425     }
426 }
427
428 impl Key for Symbol {
429     #[inline(always)]
430     fn query_crate_is_local(&self) -> bool {
431         true
432     }
433     fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
434         DUMMY_SP
435     }
436 }
437
438 impl Key for Option<Symbol> {
439     #[inline(always)]
440     fn query_crate_is_local(&self) -> bool {
441         true
442     }
443     fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
444         DUMMY_SP
445     }
446 }
447
448 /// Canonical query goals correspond to abstract trait operations that
449 /// are not tied to any crate in particular.
450 impl<'tcx, T> Key for Canonical<'tcx, T> {
451     #[inline(always)]
452     fn query_crate_is_local(&self) -> bool {
453         true
454     }
455
456     fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
457         DUMMY_SP
458     }
459 }
460
461 impl Key for (Symbol, u32, u32) {
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<'tcx> Key for (DefId, Ty<'tcx>, SubstsRef<'tcx>, ty::ParamEnv<'tcx>) {
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 (ty::Predicate<'tcx>, traits::WellFormedLoc) {
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::PolyFnSig<'tcx>, &'tcx ty::List<Ty<'tcx>>) {
495     #[inline(always)]
496     fn query_crate_is_local(&self) -> bool {
497         true
498     }
499
500     fn default_span(&self, _: TyCtxt<'_>) -> Span {
501         DUMMY_SP
502     }
503 }
504
505 impl<'tcx> Key for (ty::Instance<'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, tcx: TyCtxt<'_>) -> Span {
512         self.0.default_span(tcx)
513     }
514 }
515
516 impl<'tcx> Key for (Ty<'tcx>, ty::ValTree<'tcx>) {
517     #[inline(always)]
518     fn query_crate_is_local(&self) -> bool {
519         true
520     }
521
522     fn default_span(&self, _: TyCtxt<'_>) -> Span {
523         DUMMY_SP
524     }
525 }