1use rustc_hir::def_id::{CrateNum, DefId, LOCAL_CRATE, LocalDefId, LocalModDefId, ModDefId};
4use rustc_hir::hir_id::{HirId, OwnerId};
5use rustc_query_system::dep_graph::DepNodeIndex;
6use rustc_query_system::query::{DefIdCache, DefaultCache, SingleCache, VecCache};
7use rustc_span::{DUMMY_SP, Ident, Span, Symbol};
8
9use crate::infer::canonical::CanonicalQueryInput;
10use crate::mir::mono::CollectionMode;
11use crate::ty::fast_reject::SimplifiedType;
12use crate::ty::layout::{TyAndLayout, ValidityRequirement};
13use crate::ty::{self, GenericArg, GenericArgsRef, Ty, TyCtxt};
14use crate::{mir, traits};
15
16#[derive(Copy, Clone, Debug)]
18pub struct LocalCrate;
19
20pub trait Key: Sized {
23 type Cache<V>;
38
39 fn default_span(&self, tcx: TyCtxt<'_>) -> Span;
42
43 fn key_as_def_id(&self) -> Option<DefId> {
46 None
47 }
48
49 fn def_id_for_ty_in_cycle(&self) -> Option<DefId> {
51 None
52 }
53}
54
55pub trait AsLocalKey: Key {
56 type LocalKey;
57
58 fn as_local_key(&self) -> Option<Self::LocalKey>;
61}
62
63impl Key for () {
64 type Cache<V> = SingleCache<V>;
65
66 fn default_span(&self, _: TyCtxt<'_>) -> Span {
67 DUMMY_SP
68 }
69}
70
71impl<'tcx> Key for ty::InstanceKind<'tcx> {
72 type Cache<V> = DefaultCache<Self, V>;
73
74 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
75 tcx.def_span(self.def_id())
76 }
77}
78
79impl<'tcx> AsLocalKey for ty::InstanceKind<'tcx> {
80 type LocalKey = Self;
81
82 #[inline(always)]
83 fn as_local_key(&self) -> Option<Self::LocalKey> {
84 self.def_id().is_local().then(|| *self)
85 }
86}
87
88impl<'tcx> Key for ty::Instance<'tcx> {
89 type Cache<V> = DefaultCache<Self, V>;
90
91 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
92 tcx.def_span(self.def_id())
93 }
94}
95
96impl<'tcx> Key for mir::interpret::GlobalId<'tcx> {
97 type Cache<V> = DefaultCache<Self, V>;
98
99 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
100 self.instance.default_span(tcx)
101 }
102}
103
104impl<'tcx> Key for (Ty<'tcx>, Option<ty::ExistentialTraitRef<'tcx>>) {
105 type Cache<V> = DefaultCache<Self, V>;
106
107 fn default_span(&self, _: TyCtxt<'_>) -> Span {
108 DUMMY_SP
109 }
110}
111
112impl<'tcx> Key for mir::interpret::LitToConstInput<'tcx> {
113 type Cache<V> = DefaultCache<Self, V>;
114
115 fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
116 DUMMY_SP
117 }
118}
119
120impl Key for CrateNum {
121 type Cache<V> = VecCache<Self, V, DepNodeIndex>;
122
123 fn default_span(&self, _: TyCtxt<'_>) -> Span {
124 DUMMY_SP
125 }
126}
127
128impl AsLocalKey for CrateNum {
129 type LocalKey = LocalCrate;
130
131 #[inline(always)]
132 fn as_local_key(&self) -> Option<Self::LocalKey> {
133 (*self == LOCAL_CRATE).then_some(LocalCrate)
134 }
135}
136
137impl Key for OwnerId {
138 type Cache<V> = VecCache<Self, V, DepNodeIndex>;
139
140 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
141 self.to_def_id().default_span(tcx)
142 }
143
144 fn key_as_def_id(&self) -> Option<DefId> {
145 Some(self.to_def_id())
146 }
147}
148
149impl Key for LocalDefId {
150 type Cache<V> = VecCache<Self, V, DepNodeIndex>;
151
152 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
153 self.to_def_id().default_span(tcx)
154 }
155
156 fn key_as_def_id(&self) -> Option<DefId> {
157 Some(self.to_def_id())
158 }
159}
160
161impl Key for DefId {
162 type Cache<V> = DefIdCache<V>;
163
164 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
165 tcx.def_span(*self)
166 }
167
168 #[inline(always)]
169 fn key_as_def_id(&self) -> Option<DefId> {
170 Some(*self)
171 }
172}
173
174impl AsLocalKey for DefId {
175 type LocalKey = LocalDefId;
176
177 #[inline(always)]
178 fn as_local_key(&self) -> Option<Self::LocalKey> {
179 self.as_local()
180 }
181}
182
183impl Key for LocalModDefId {
184 type Cache<V> = DefaultCache<Self, V>;
185
186 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
187 tcx.def_span(*self)
188 }
189
190 #[inline(always)]
191 fn key_as_def_id(&self) -> Option<DefId> {
192 Some(self.to_def_id())
193 }
194}
195
196impl Key for ModDefId {
197 type Cache<V> = DefaultCache<Self, V>;
198
199 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
200 tcx.def_span(*self)
201 }
202
203 #[inline(always)]
204 fn key_as_def_id(&self) -> Option<DefId> {
205 Some(self.to_def_id())
206 }
207}
208
209impl AsLocalKey for ModDefId {
210 type LocalKey = LocalModDefId;
211
212 #[inline(always)]
213 fn as_local_key(&self) -> Option<Self::LocalKey> {
214 self.as_local()
215 }
216}
217
218impl Key for SimplifiedType {
219 type Cache<V> = DefaultCache<Self, V>;
220
221 fn default_span(&self, _: TyCtxt<'_>) -> Span {
222 DUMMY_SP
223 }
224}
225
226impl Key for (DefId, DefId) {
227 type Cache<V> = DefaultCache<Self, V>;
228
229 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
230 self.1.default_span(tcx)
231 }
232}
233
234impl<'tcx> Key for (ty::Instance<'tcx>, LocalDefId) {
235 type Cache<V> = DefaultCache<Self, V>;
236
237 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
238 self.0.default_span(tcx)
239 }
240}
241
242impl Key for (DefId, LocalDefId) {
243 type Cache<V> = DefaultCache<Self, V>;
244
245 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
246 self.1.default_span(tcx)
247 }
248}
249
250impl Key for (LocalDefId, DefId) {
251 type Cache<V> = DefaultCache<Self, V>;
252
253 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
254 self.0.default_span(tcx)
255 }
256}
257
258impl Key for (LocalDefId, LocalDefId) {
259 type Cache<V> = DefaultCache<Self, V>;
260
261 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
262 self.0.default_span(tcx)
263 }
264}
265
266impl Key for (DefId, Ident) {
267 type Cache<V> = DefaultCache<Self, V>;
268
269 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
270 tcx.def_span(self.0)
271 }
272
273 #[inline(always)]
274 fn key_as_def_id(&self) -> Option<DefId> {
275 Some(self.0)
276 }
277}
278
279impl Key for (LocalDefId, LocalDefId, Ident) {
280 type Cache<V> = DefaultCache<Self, V>;
281
282 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
283 self.1.default_span(tcx)
284 }
285}
286
287impl Key for (CrateNum, DefId) {
288 type Cache<V> = DefaultCache<Self, V>;
289
290 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
291 self.1.default_span(tcx)
292 }
293}
294
295impl AsLocalKey for (CrateNum, DefId) {
296 type LocalKey = DefId;
297
298 #[inline(always)]
299 fn as_local_key(&self) -> Option<Self::LocalKey> {
300 (self.0 == LOCAL_CRATE).then(|| self.1)
301 }
302}
303
304impl Key for (CrateNum, SimplifiedType) {
305 type Cache<V> = DefaultCache<Self, V>;
306
307 fn default_span(&self, _: TyCtxt<'_>) -> Span {
308 DUMMY_SP
309 }
310}
311
312impl AsLocalKey for (CrateNum, SimplifiedType) {
313 type LocalKey = SimplifiedType;
314
315 #[inline(always)]
316 fn as_local_key(&self) -> Option<Self::LocalKey> {
317 (self.0 == LOCAL_CRATE).then(|| self.1)
318 }
319}
320
321impl Key for (DefId, SimplifiedType) {
322 type Cache<V> = DefaultCache<Self, V>;
323
324 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
325 self.0.default_span(tcx)
326 }
327}
328
329impl<'tcx> Key for GenericArgsRef<'tcx> {
330 type Cache<V> = DefaultCache<Self, V>;
331
332 fn default_span(&self, _: TyCtxt<'_>) -> Span {
333 DUMMY_SP
334 }
335}
336
337impl<'tcx> Key for (DefId, GenericArgsRef<'tcx>) {
338 type Cache<V> = DefaultCache<Self, V>;
339
340 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
341 self.0.default_span(tcx)
342 }
343}
344
345impl<'tcx> Key for (ty::UnevaluatedConst<'tcx>, ty::UnevaluatedConst<'tcx>) {
346 type Cache<V> = DefaultCache<Self, V>;
347
348 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
349 (self.0).def.default_span(tcx)
350 }
351}
352
353impl<'tcx> Key for (LocalDefId, DefId, GenericArgsRef<'tcx>) {
354 type Cache<V> = DefaultCache<Self, V>;
355
356 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
357 self.0.default_span(tcx)
358 }
359}
360
361impl<'tcx> Key for (ty::ParamEnv<'tcx>, ty::TraitRef<'tcx>) {
362 type Cache<V> = DefaultCache<Self, V>;
363
364 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
365 tcx.def_span(self.1.def_id)
366 }
367}
368
369impl<'tcx> Key for ty::ParamEnvAnd<'tcx, Ty<'tcx>> {
370 type Cache<V> = DefaultCache<Self, V>;
371
372 fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
373 DUMMY_SP
374 }
375}
376
377impl<'tcx> Key for ty::TraitRef<'tcx> {
378 type Cache<V> = DefaultCache<Self, V>;
379
380 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
381 tcx.def_span(self.def_id)
382 }
383}
384
385impl<'tcx> Key for ty::PolyTraitRef<'tcx> {
386 type Cache<V> = DefaultCache<Self, V>;
387
388 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
389 tcx.def_span(self.def_id())
390 }
391}
392
393impl<'tcx> Key for ty::PolyExistentialTraitRef<'tcx> {
394 type Cache<V> = DefaultCache<Self, V>;
395
396 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
397 tcx.def_span(self.def_id())
398 }
399}
400
401impl<'tcx> Key for (ty::PolyTraitRef<'tcx>, ty::PolyTraitRef<'tcx>) {
402 type Cache<V> = DefaultCache<Self, V>;
403
404 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
405 tcx.def_span(self.0.def_id())
406 }
407}
408
409impl<'tcx> Key for GenericArg<'tcx> {
410 type Cache<V> = DefaultCache<Self, V>;
411
412 fn default_span(&self, _: TyCtxt<'_>) -> Span {
413 DUMMY_SP
414 }
415}
416
417impl<'tcx> Key for ty::Const<'tcx> {
418 type Cache<V> = DefaultCache<Self, V>;
419
420 fn default_span(&self, _: TyCtxt<'_>) -> Span {
421 DUMMY_SP
422 }
423}
424
425impl<'tcx> Key for Ty<'tcx> {
426 type Cache<V> = DefaultCache<Self, V>;
427
428 fn default_span(&self, _: TyCtxt<'_>) -> Span {
429 DUMMY_SP
430 }
431
432 fn def_id_for_ty_in_cycle(&self) -> Option<DefId> {
433 match *self.kind() {
434 ty::Adt(adt, _) => Some(adt.did()),
435 ty::Coroutine(def_id, ..) => Some(def_id),
436 _ => None,
437 }
438 }
439}
440
441impl<'tcx> Key for TyAndLayout<'tcx> {
442 type Cache<V> = DefaultCache<Self, V>;
443
444 fn default_span(&self, _: TyCtxt<'_>) -> Span {
445 DUMMY_SP
446 }
447}
448
449impl<'tcx> Key for (Ty<'tcx>, Ty<'tcx>) {
450 type Cache<V> = DefaultCache<Self, V>;
451
452 fn default_span(&self, _: TyCtxt<'_>) -> Span {
453 DUMMY_SP
454 }
455}
456
457impl<'tcx> Key for ty::Clauses<'tcx> {
458 type Cache<V> = DefaultCache<Self, V>;
459
460 fn default_span(&self, _: TyCtxt<'_>) -> Span {
461 DUMMY_SP
462 }
463}
464
465impl<'tcx> Key for ty::ParamEnv<'tcx> {
466 type Cache<V> = DefaultCache<Self, V>;
467
468 fn default_span(&self, _: TyCtxt<'_>) -> Span {
469 DUMMY_SP
470 }
471}
472
473impl<'tcx, T: Key> Key for ty::PseudoCanonicalInput<'tcx, T> {
474 type Cache<V> = DefaultCache<Self, V>;
475
476 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
477 self.value.default_span(tcx)
478 }
479
480 fn def_id_for_ty_in_cycle(&self) -> Option<DefId> {
481 self.value.def_id_for_ty_in_cycle()
482 }
483}
484
485impl Key for Symbol {
486 type Cache<V> = DefaultCache<Self, V>;
487
488 fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
489 DUMMY_SP
490 }
491}
492
493impl Key for Option<Symbol> {
494 type Cache<V> = DefaultCache<Self, V>;
495
496 fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
497 DUMMY_SP
498 }
499}
500
501impl<'tcx, T: Clone> Key for CanonicalQueryInput<'tcx, T> {
504 type Cache<V> = DefaultCache<Self, V>;
505
506 fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
507 DUMMY_SP
508 }
509}
510
511impl<'tcx, T: Clone> Key for (CanonicalQueryInput<'tcx, T>, bool) {
512 type Cache<V> = DefaultCache<Self, V>;
513
514 fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
515 DUMMY_SP
516 }
517}
518
519impl Key for (Symbol, u32, u32) {
520 type Cache<V> = DefaultCache<Self, V>;
521
522 fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
523 DUMMY_SP
524 }
525}
526
527impl<'tcx> Key for (DefId, Ty<'tcx>, GenericArgsRef<'tcx>, ty::ParamEnv<'tcx>) {
528 type Cache<V> = DefaultCache<Self, V>;
529
530 fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
531 DUMMY_SP
532 }
533}
534
535impl<'tcx> Key for (Ty<'tcx>, rustc_abi::VariantIdx) {
536 type Cache<V> = DefaultCache<Self, V>;
537
538 fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
539 DUMMY_SP
540 }
541}
542
543impl<'tcx> Key for (ty::Predicate<'tcx>, traits::WellFormedLoc) {
544 type Cache<V> = DefaultCache<Self, V>;
545
546 fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
547 DUMMY_SP
548 }
549}
550
551impl<'tcx> Key for (ty::PolyFnSig<'tcx>, &'tcx ty::List<Ty<'tcx>>) {
552 type Cache<V> = DefaultCache<Self, V>;
553
554 fn default_span(&self, _: TyCtxt<'_>) -> Span {
555 DUMMY_SP
556 }
557}
558
559impl<'tcx> Key for (ty::Instance<'tcx>, &'tcx ty::List<Ty<'tcx>>) {
560 type Cache<V> = DefaultCache<Self, V>;
561
562 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
563 self.0.default_span(tcx)
564 }
565}
566
567impl<'tcx> Key for ty::Value<'tcx> {
568 type Cache<V> = DefaultCache<Self, V>;
569
570 fn default_span(&self, _: TyCtxt<'_>) -> Span {
571 DUMMY_SP
572 }
573}
574
575impl Key for HirId {
576 type Cache<V> = DefaultCache<Self, V>;
577
578 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
579 tcx.hir().span(*self)
580 }
581
582 #[inline(always)]
583 fn key_as_def_id(&self) -> Option<DefId> {
584 None
585 }
586}
587
588impl Key for (LocalDefId, HirId) {
589 type Cache<V> = DefaultCache<Self, V>;
590
591 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
592 tcx.hir().span(self.1)
593 }
594
595 #[inline(always)]
596 fn key_as_def_id(&self) -> Option<DefId> {
597 Some(self.0.into())
598 }
599}
600
601impl<'tcx> Key for (ValidityRequirement, ty::PseudoCanonicalInput<'tcx, Ty<'tcx>>) {
602 type Cache<V> = DefaultCache<Self, V>;
603
604 fn default_span(&self, _: TyCtxt<'_>) -> Span {
607 DUMMY_SP
608 }
609
610 fn def_id_for_ty_in_cycle(&self) -> Option<DefId> {
611 match self.1.value.kind() {
612 ty::Adt(adt, _) => Some(adt.did()),
613 _ => None,
614 }
615 }
616}
617
618impl<'tcx> Key for (ty::Instance<'tcx>, CollectionMode) {
619 type Cache<V> = DefaultCache<Self, V>;
620
621 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
622 self.0.default_span(tcx)
623 }
624}