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