]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_query_impl/src/keys.rs
Rollup merge of #93613 - crlf0710:rename_to_async_iter, r=yaahc
[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 (DefId, DefId) {
145     #[inline(always)]
146     fn query_crate_is_local(&self) -> bool {
147         self.0.krate == LOCAL_CRATE
148     }
149     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
150         self.1.default_span(tcx)
151     }
152 }
153
154 impl<'tcx> Key for (ty::Instance<'tcx>, LocalDefId) {
155     #[inline(always)]
156     fn query_crate_is_local(&self) -> bool {
157         true
158     }
159     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
160         self.0.default_span(tcx)
161     }
162 }
163
164 impl Key for (DefId, LocalDefId) {
165     #[inline(always)]
166     fn query_crate_is_local(&self) -> bool {
167         self.0.krate == LOCAL_CRATE
168     }
169     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
170         self.1.default_span(tcx)
171     }
172 }
173
174 impl Key for (LocalDefId, DefId) {
175     #[inline(always)]
176     fn query_crate_is_local(&self) -> bool {
177         true
178     }
179     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
180         self.0.default_span(tcx)
181     }
182 }
183
184 impl Key for (DefId, Option<Ident>) {
185     #[inline(always)]
186     fn query_crate_is_local(&self) -> bool {
187         self.0.krate == LOCAL_CRATE
188     }
189     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
190         tcx.def_span(self.0)
191     }
192     #[inline(always)]
193     fn key_as_def_id(&self) -> Option<DefId> {
194         Some(self.0)
195     }
196 }
197
198 impl Key for (DefId, LocalDefId, Ident) {
199     #[inline(always)]
200     fn query_crate_is_local(&self) -> bool {
201         self.0.krate == LOCAL_CRATE
202     }
203     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
204         self.1.default_span(tcx)
205     }
206 }
207
208 impl Key for (CrateNum, DefId) {
209     #[inline(always)]
210     fn query_crate_is_local(&self) -> bool {
211         self.0 == LOCAL_CRATE
212     }
213     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
214         self.1.default_span(tcx)
215     }
216 }
217
218 impl Key for (DefId, SimplifiedType) {
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         self.0.default_span(tcx)
225     }
226 }
227
228 impl<'tcx> Key for SubstsRef<'tcx> {
229     #[inline(always)]
230     fn query_crate_is_local(&self) -> bool {
231         true
232     }
233     fn default_span(&self, _: TyCtxt<'_>) -> Span {
234         DUMMY_SP
235     }
236 }
237
238 impl<'tcx> Key for (DefId, SubstsRef<'tcx>) {
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 (ty::Unevaluated<'tcx, ()>, ty::Unevaluated<'tcx, ()>) {
249     #[inline(always)]
250     fn query_crate_is_local(&self) -> bool {
251         (self.0).def.did.krate == LOCAL_CRATE
252     }
253     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
254         (self.0).def.did.default_span(tcx)
255     }
256 }
257
258 impl<'tcx> Key for (LocalDefId, DefId, SubstsRef<'tcx>) {
259     #[inline(always)]
260     fn query_crate_is_local(&self) -> bool {
261         true
262     }
263     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
264         self.0.default_span(tcx)
265     }
266 }
267
268 impl<'tcx> Key for (ty::ParamEnv<'tcx>, ty::PolyTraitRef<'tcx>) {
269     #[inline(always)]
270     fn query_crate_is_local(&self) -> bool {
271         self.1.def_id().krate == LOCAL_CRATE
272     }
273     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
274         tcx.def_span(self.1.def_id())
275     }
276 }
277
278 impl<'tcx> Key for (ty::Const<'tcx>, mir::Field) {
279     #[inline(always)]
280     fn query_crate_is_local(&self) -> bool {
281         true
282     }
283     fn default_span(&self, _: TyCtxt<'_>) -> Span {
284         DUMMY_SP
285     }
286 }
287
288 impl<'tcx> Key for mir::interpret::ConstAlloc<'tcx> {
289     #[inline(always)]
290     fn query_crate_is_local(&self) -> bool {
291         true
292     }
293     fn default_span(&self, _: TyCtxt<'_>) -> Span {
294         DUMMY_SP
295     }
296 }
297
298 impl<'tcx> Key for ty::PolyTraitRef<'tcx> {
299     #[inline(always)]
300     fn query_crate_is_local(&self) -> bool {
301         self.def_id().krate == LOCAL_CRATE
302     }
303     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
304         tcx.def_span(self.def_id())
305     }
306 }
307
308 impl<'tcx> Key for ty::PolyExistentialTraitRef<'tcx> {
309     #[inline(always)]
310     fn query_crate_is_local(&self) -> bool {
311         self.def_id().krate == LOCAL_CRATE
312     }
313     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
314         tcx.def_span(self.def_id())
315     }
316 }
317
318 impl<'tcx> Key for (ty::PolyTraitRef<'tcx>, ty::PolyTraitRef<'tcx>) {
319     #[inline(always)]
320     fn query_crate_is_local(&self) -> bool {
321         self.0.def_id().krate == LOCAL_CRATE
322     }
323     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
324         tcx.def_span(self.0.def_id())
325     }
326 }
327
328 impl<'tcx> Key for GenericArg<'tcx> {
329     #[inline(always)]
330     fn query_crate_is_local(&self) -> bool {
331         true
332     }
333     fn default_span(&self, _: TyCtxt<'_>) -> Span {
334         DUMMY_SP
335     }
336 }
337
338 impl<'tcx> Key for mir::ConstantKind<'tcx> {
339     #[inline(always)]
340     fn query_crate_is_local(&self) -> bool {
341         true
342     }
343     fn default_span(&self, _: TyCtxt<'_>) -> Span {
344         DUMMY_SP
345     }
346 }
347
348 impl<'tcx> Key for ty::Const<'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 Ty<'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<'tcx>, Ty<'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 &'tcx ty::List<ty::Predicate<'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::ParamEnv<'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, T: Key> Key for ty::ParamEnvAnd<'tcx, T> {
399     #[inline(always)]
400     fn query_crate_is_local(&self) -> bool {
401         self.value.query_crate_is_local()
402     }
403     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
404         self.value.default_span(tcx)
405     }
406 }
407
408 impl Key for Symbol {
409     #[inline(always)]
410     fn query_crate_is_local(&self) -> bool {
411         true
412     }
413     fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
414         DUMMY_SP
415     }
416 }
417
418 /// Canonical query goals correspond to abstract trait operations that
419 /// are not tied to any crate in particular.
420 impl<'tcx, T> Key for Canonical<'tcx, T> {
421     #[inline(always)]
422     fn query_crate_is_local(&self) -> bool {
423         true
424     }
425
426     fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
427         DUMMY_SP
428     }
429 }
430
431 impl Key for (Symbol, u32, u32) {
432     #[inline(always)]
433     fn query_crate_is_local(&self) -> bool {
434         true
435     }
436
437     fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
438         DUMMY_SP
439     }
440 }
441
442 impl<'tcx> Key for (DefId, Ty<'tcx>, SubstsRef<'tcx>, ty::ParamEnv<'tcx>) {
443     #[inline(always)]
444     fn query_crate_is_local(&self) -> bool {
445         true
446     }
447
448     fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
449         DUMMY_SP
450     }
451 }
452
453 impl<'tcx> Key for (ty::Predicate<'tcx>, traits::WellFormedLoc) {
454     #[inline(always)]
455     fn query_crate_is_local(&self) -> bool {
456         true
457     }
458
459     fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
460         DUMMY_SP
461     }
462 }
463
464 impl<'tcx> Key for (ty::PolyFnSig<'tcx>, &'tcx ty::List<Ty<'tcx>>) {
465     #[inline(always)]
466     fn query_crate_is_local(&self) -> bool {
467         true
468     }
469
470     fn default_span(&self, _: TyCtxt<'_>) -> Span {
471         DUMMY_SP
472     }
473 }
474
475 impl<'tcx> Key for (ty::Instance<'tcx>, &'tcx ty::List<Ty<'tcx>>) {
476     #[inline(always)]
477     fn query_crate_is_local(&self) -> bool {
478         true
479     }
480
481     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
482         self.0.default_span(tcx)
483     }
484 }