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