1 //! Defines the set of legal keys that can be used in queries.
3 use crate::infer::canonical::Canonical;
6 use crate::ty::fast_reject::SimplifiedType;
7 use crate::ty::subst::{GenericArg, SubstsRef};
8 use crate::ty::{self, layout::TyAndLayout, Ty, TyCtxt};
9 use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, LOCAL_CRATE};
10 use rustc_hir::hir_id::{HirId, OwnerId};
11 use rustc_query_system::query::{DefaultCacheSelector, VecCacheSelector};
12 use rustc_span::symbol::{Ident, Symbol};
13 use rustc_span::{Span, DUMMY_SP};
15 /// The `Key` trait controls what types can legally be used as the key
17 pub trait Key: Sized {
18 // N.B. Most of the keys down below have `type CacheSelector = DefaultCacheSelector<Self>;`,
19 // it would be reasonable to use associated type defaults, to remove the duplication...
21 // ...But r-a doesn't support them yet and using a default here causes r-a to not infer
22 // return types of queries which is very annoying. Thus, until r-a support associated
23 // type defaults, plese restrain from using them here <3
25 // r-a issue: <https://github.com/rust-lang/rust-analyzer/issues/13693>
28 /// Given an instance of this key, what crate is it referring to?
29 /// This is used to find the provider.
30 fn query_crate_is_local(&self) -> bool;
32 /// In the event that a cycle occurs, if no explicit span has been
33 /// given for a query with key `self`, what span should we use?
34 fn default_span(&self, tcx: TyCtxt<'_>) -> Span;
36 /// If the key is a [`DefId`] or `DefId`--equivalent, return that `DefId`.
37 /// Otherwise, return `None`.
38 fn key_as_def_id(&self) -> Option<DefId> {
42 fn ty_adt_id(&self) -> Option<DefId> {
48 type CacheSelector = DefaultCacheSelector<Self>;
51 fn query_crate_is_local(&self) -> bool {
55 fn default_span(&self, _: TyCtxt<'_>) -> Span {
60 impl<'tcx> Key for ty::InstanceDef<'tcx> {
61 type CacheSelector = DefaultCacheSelector<Self>;
64 fn query_crate_is_local(&self) -> bool {
68 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
69 tcx.def_span(self.def_id())
73 impl<'tcx> Key for ty::Instance<'tcx> {
74 type CacheSelector = DefaultCacheSelector<Self>;
77 fn query_crate_is_local(&self) -> bool {
81 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
82 tcx.def_span(self.def_id())
86 impl<'tcx> Key for mir::interpret::GlobalId<'tcx> {
87 type CacheSelector = DefaultCacheSelector<Self>;
90 fn query_crate_is_local(&self) -> bool {
94 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
95 self.instance.default_span(tcx)
99 impl<'tcx> Key for (Ty<'tcx>, Option<ty::PolyExistentialTraitRef<'tcx>>) {
100 type CacheSelector = DefaultCacheSelector<Self>;
103 fn query_crate_is_local(&self) -> bool {
107 fn default_span(&self, _: TyCtxt<'_>) -> Span {
112 impl<'tcx> Key for mir::interpret::LitToConstInput<'tcx> {
113 type CacheSelector = DefaultCacheSelector<Self>;
116 fn query_crate_is_local(&self) -> bool {
120 fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
125 impl Key for CrateNum {
126 type CacheSelector = VecCacheSelector<Self>;
129 fn query_crate_is_local(&self) -> bool {
132 fn default_span(&self, _: TyCtxt<'_>) -> Span {
137 impl Key for OwnerId {
138 type CacheSelector = VecCacheSelector<Self>;
141 fn query_crate_is_local(&self) -> bool {
144 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
145 self.to_def_id().default_span(tcx)
147 fn key_as_def_id(&self) -> Option<DefId> {
148 Some(self.to_def_id())
152 impl Key for LocalDefId {
153 type CacheSelector = VecCacheSelector<Self>;
156 fn query_crate_is_local(&self) -> bool {
159 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
160 self.to_def_id().default_span(tcx)
162 fn key_as_def_id(&self) -> Option<DefId> {
163 Some(self.to_def_id())
168 type CacheSelector = DefaultCacheSelector<Self>;
171 fn query_crate_is_local(&self) -> bool {
172 self.krate == LOCAL_CRATE
174 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
178 fn key_as_def_id(&self) -> Option<DefId> {
183 impl Key for ty::WithOptConstParam<LocalDefId> {
184 type CacheSelector = DefaultCacheSelector<Self>;
187 fn query_crate_is_local(&self) -> bool {
190 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
191 self.did.default_span(tcx)
195 impl Key for SimplifiedType {
196 type CacheSelector = DefaultCacheSelector<Self>;
199 fn query_crate_is_local(&self) -> bool {
202 fn default_span(&self, _: TyCtxt<'_>) -> Span {
207 impl Key for (DefId, DefId) {
208 type CacheSelector = DefaultCacheSelector<Self>;
211 fn query_crate_is_local(&self) -> bool {
212 self.0.krate == LOCAL_CRATE
214 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
215 self.1.default_span(tcx)
219 impl<'tcx> Key for (ty::Instance<'tcx>, LocalDefId) {
220 type CacheSelector = DefaultCacheSelector<Self>;
223 fn query_crate_is_local(&self) -> bool {
226 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
227 self.0.default_span(tcx)
231 impl Key for (DefId, LocalDefId) {
232 type CacheSelector = DefaultCacheSelector<Self>;
235 fn query_crate_is_local(&self) -> bool {
236 self.0.krate == LOCAL_CRATE
238 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
239 self.1.default_span(tcx)
243 impl Key for (LocalDefId, DefId) {
244 type CacheSelector = DefaultCacheSelector<Self>;
247 fn query_crate_is_local(&self) -> bool {
250 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
251 self.0.default_span(tcx)
255 impl Key for (LocalDefId, LocalDefId) {
256 type CacheSelector = DefaultCacheSelector<Self>;
259 fn query_crate_is_local(&self) -> bool {
262 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
263 self.0.default_span(tcx)
267 impl Key for (DefId, Option<Ident>) {
268 type CacheSelector = DefaultCacheSelector<Self>;
271 fn query_crate_is_local(&self) -> bool {
272 self.0.krate == LOCAL_CRATE
274 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
278 fn key_as_def_id(&self) -> Option<DefId> {
283 impl Key for (DefId, LocalDefId, Ident) {
284 type CacheSelector = DefaultCacheSelector<Self>;
287 fn query_crate_is_local(&self) -> bool {
288 self.0.krate == LOCAL_CRATE
290 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
291 self.1.default_span(tcx)
295 impl Key for (CrateNum, DefId) {
296 type CacheSelector = DefaultCacheSelector<Self>;
299 fn query_crate_is_local(&self) -> bool {
300 self.0 == LOCAL_CRATE
302 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
303 self.1.default_span(tcx)
307 impl Key for (CrateNum, SimplifiedType) {
308 type CacheSelector = DefaultCacheSelector<Self>;
311 fn query_crate_is_local(&self) -> bool {
312 self.0 == LOCAL_CRATE
314 fn default_span(&self, _: TyCtxt<'_>) -> Span {
319 impl Key for (DefId, SimplifiedType) {
320 type CacheSelector = DefaultCacheSelector<Self>;
323 fn query_crate_is_local(&self) -> bool {
324 self.0.krate == LOCAL_CRATE
326 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
327 self.0.default_span(tcx)
331 impl<'tcx> Key for SubstsRef<'tcx> {
332 type CacheSelector = DefaultCacheSelector<Self>;
335 fn query_crate_is_local(&self) -> bool {
338 fn default_span(&self, _: TyCtxt<'_>) -> Span {
343 impl<'tcx> Key for (DefId, SubstsRef<'tcx>) {
344 type CacheSelector = DefaultCacheSelector<Self>;
347 fn query_crate_is_local(&self) -> bool {
348 self.0.krate == LOCAL_CRATE
350 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
351 self.0.default_span(tcx)
355 impl<'tcx> Key for (ty::UnevaluatedConst<'tcx>, ty::UnevaluatedConst<'tcx>) {
356 type CacheSelector = DefaultCacheSelector<Self>;
359 fn query_crate_is_local(&self) -> bool {
360 (self.0).def.did.krate == LOCAL_CRATE
362 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
363 (self.0).def.did.default_span(tcx)
367 impl<'tcx> Key for (LocalDefId, DefId, SubstsRef<'tcx>) {
368 type CacheSelector = DefaultCacheSelector<Self>;
371 fn query_crate_is_local(&self) -> bool {
374 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
375 self.0.default_span(tcx)
379 impl<'tcx> Key for (ty::ParamEnv<'tcx>, ty::PolyTraitRef<'tcx>) {
380 type CacheSelector = DefaultCacheSelector<Self>;
383 fn query_crate_is_local(&self) -> bool {
384 self.1.def_id().krate == LOCAL_CRATE
386 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
387 tcx.def_span(self.1.def_id())
391 impl<'tcx> Key for (ty::Const<'tcx>, mir::Field) {
392 type CacheSelector = DefaultCacheSelector<Self>;
395 fn query_crate_is_local(&self) -> bool {
398 fn default_span(&self, _: TyCtxt<'_>) -> Span {
403 impl<'tcx> Key for mir::interpret::ConstAlloc<'tcx> {
404 type CacheSelector = DefaultCacheSelector<Self>;
407 fn query_crate_is_local(&self) -> bool {
410 fn default_span(&self, _: TyCtxt<'_>) -> Span {
415 impl<'tcx> Key for ty::PolyTraitRef<'tcx> {
416 type CacheSelector = DefaultCacheSelector<Self>;
419 fn query_crate_is_local(&self) -> bool {
420 self.def_id().krate == LOCAL_CRATE
422 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
423 tcx.def_span(self.def_id())
427 impl<'tcx> Key for ty::PolyExistentialTraitRef<'tcx> {
428 type CacheSelector = DefaultCacheSelector<Self>;
431 fn query_crate_is_local(&self) -> bool {
432 self.def_id().krate == LOCAL_CRATE
434 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
435 tcx.def_span(self.def_id())
439 impl<'tcx> Key for (ty::PolyTraitRef<'tcx>, ty::PolyTraitRef<'tcx>) {
440 type CacheSelector = DefaultCacheSelector<Self>;
443 fn query_crate_is_local(&self) -> bool {
444 self.0.def_id().krate == LOCAL_CRATE
446 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
447 tcx.def_span(self.0.def_id())
451 impl<'tcx> Key for GenericArg<'tcx> {
452 type CacheSelector = DefaultCacheSelector<Self>;
455 fn query_crate_is_local(&self) -> bool {
458 fn default_span(&self, _: TyCtxt<'_>) -> Span {
463 impl<'tcx> Key for mir::ConstantKind<'tcx> {
464 type CacheSelector = DefaultCacheSelector<Self>;
467 fn query_crate_is_local(&self) -> bool {
470 fn default_span(&self, _: TyCtxt<'_>) -> Span {
475 impl<'tcx> Key for ty::Const<'tcx> {
476 type CacheSelector = DefaultCacheSelector<Self>;
479 fn query_crate_is_local(&self) -> bool {
482 fn default_span(&self, _: TyCtxt<'_>) -> Span {
487 impl<'tcx> Key for Ty<'tcx> {
488 type CacheSelector = DefaultCacheSelector<Self>;
491 fn query_crate_is_local(&self) -> bool {
494 fn default_span(&self, _: TyCtxt<'_>) -> Span {
497 fn ty_adt_id(&self) -> Option<DefId> {
499 ty::Adt(adt, _) => Some(adt.did()),
505 impl<'tcx> Key for TyAndLayout<'tcx> {
506 type CacheSelector = DefaultCacheSelector<Self>;
509 fn query_crate_is_local(&self) -> bool {
512 fn default_span(&self, _: TyCtxt<'_>) -> Span {
517 impl<'tcx> Key for (Ty<'tcx>, Ty<'tcx>) {
518 type CacheSelector = DefaultCacheSelector<Self>;
521 fn query_crate_is_local(&self) -> bool {
524 fn default_span(&self, _: TyCtxt<'_>) -> Span {
529 impl<'tcx> Key for &'tcx ty::List<ty::Predicate<'tcx>> {
530 type CacheSelector = DefaultCacheSelector<Self>;
533 fn query_crate_is_local(&self) -> bool {
536 fn default_span(&self, _: TyCtxt<'_>) -> Span {
541 impl<'tcx> Key for ty::ParamEnv<'tcx> {
542 type CacheSelector = DefaultCacheSelector<Self>;
545 fn query_crate_is_local(&self) -> bool {
548 fn default_span(&self, _: TyCtxt<'_>) -> Span {
553 impl<'tcx, T: Key> Key for ty::ParamEnvAnd<'tcx, T> {
554 type CacheSelector = DefaultCacheSelector<Self>;
557 fn query_crate_is_local(&self) -> bool {
558 self.value.query_crate_is_local()
560 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
561 self.value.default_span(tcx)
565 impl Key for Symbol {
566 type CacheSelector = DefaultCacheSelector<Self>;
569 fn query_crate_is_local(&self) -> bool {
572 fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
577 impl Key for Option<Symbol> {
578 type CacheSelector = DefaultCacheSelector<Self>;
581 fn query_crate_is_local(&self) -> bool {
584 fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
589 /// Canonical query goals correspond to abstract trait operations that
590 /// are not tied to any crate in particular.
591 impl<'tcx, T> Key for Canonical<'tcx, T> {
592 type CacheSelector = DefaultCacheSelector<Self>;
595 fn query_crate_is_local(&self) -> bool {
599 fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
604 impl Key for (Symbol, u32, u32) {
605 type CacheSelector = DefaultCacheSelector<Self>;
608 fn query_crate_is_local(&self) -> bool {
612 fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
617 impl<'tcx> Key for (DefId, Ty<'tcx>, SubstsRef<'tcx>, ty::ParamEnv<'tcx>) {
618 type CacheSelector = DefaultCacheSelector<Self>;
621 fn query_crate_is_local(&self) -> bool {
625 fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
630 impl<'tcx> Key for (ty::Predicate<'tcx>, traits::WellFormedLoc) {
631 type CacheSelector = DefaultCacheSelector<Self>;
634 fn query_crate_is_local(&self) -> bool {
638 fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
643 impl<'tcx> Key for (ty::PolyFnSig<'tcx>, &'tcx ty::List<Ty<'tcx>>) {
644 type CacheSelector = DefaultCacheSelector<Self>;
647 fn query_crate_is_local(&self) -> bool {
651 fn default_span(&self, _: TyCtxt<'_>) -> Span {
656 impl<'tcx> Key for (ty::Instance<'tcx>, &'tcx ty::List<Ty<'tcx>>) {
657 type CacheSelector = DefaultCacheSelector<Self>;
660 fn query_crate_is_local(&self) -> bool {
664 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
665 self.0.default_span(tcx)
669 impl<'tcx> Key for (Ty<'tcx>, ty::ValTree<'tcx>) {
670 type CacheSelector = DefaultCacheSelector<Self>;
673 fn query_crate_is_local(&self) -> bool {
677 fn default_span(&self, _: TyCtxt<'_>) -> Span {
683 type CacheSelector = DefaultCacheSelector<Self>;
686 fn query_crate_is_local(&self) -> bool {
690 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
691 tcx.hir().span(*self)
695 fn key_as_def_id(&self) -> Option<DefId> {