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 type CacheSelector = DefaultCacheSelector<Self>;
20 /// Given an instance of this key, what crate is it referring to?
21 /// This is used to find the provider.
22 fn query_crate_is_local(&self) -> bool;
24 /// In the event that a cycle occurs, if no explicit span has been
25 /// given for a query with key `self`, what span should we use?
26 fn default_span(&self, tcx: TyCtxt<'_>) -> Span;
28 /// If the key is a [`DefId`] or `DefId`--equivalent, return that `DefId`.
29 /// Otherwise, return `None`.
30 fn key_as_def_id(&self) -> Option<DefId> {
34 fn ty_adt_id(&self) -> Option<DefId> {
41 fn query_crate_is_local(&self) -> bool {
45 fn default_span(&self, _: TyCtxt<'_>) -> Span {
50 impl<'tcx> Key for ty::InstanceDef<'tcx> {
52 fn query_crate_is_local(&self) -> bool {
56 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
57 tcx.def_span(self.def_id())
61 impl<'tcx> Key for ty::Instance<'tcx> {
63 fn query_crate_is_local(&self) -> bool {
67 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
68 tcx.def_span(self.def_id())
72 impl<'tcx> Key for mir::interpret::GlobalId<'tcx> {
74 fn query_crate_is_local(&self) -> bool {
78 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
79 self.instance.default_span(tcx)
83 impl<'tcx> Key for (Ty<'tcx>, Option<ty::PolyExistentialTraitRef<'tcx>>) {
85 fn query_crate_is_local(&self) -> bool {
89 fn default_span(&self, _: TyCtxt<'_>) -> Span {
94 impl<'tcx> Key for mir::interpret::LitToConstInput<'tcx> {
96 fn query_crate_is_local(&self) -> bool {
100 fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
105 impl Key for CrateNum {
106 type CacheSelector = VecCacheSelector<Self>;
109 fn query_crate_is_local(&self) -> bool {
112 fn default_span(&self, _: TyCtxt<'_>) -> Span {
117 impl Key for OwnerId {
118 type CacheSelector = VecCacheSelector<Self>;
121 fn query_crate_is_local(&self) -> bool {
124 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
125 self.to_def_id().default_span(tcx)
127 fn key_as_def_id(&self) -> Option<DefId> {
128 Some(self.to_def_id())
132 impl Key for LocalDefId {
133 type CacheSelector = VecCacheSelector<Self>;
136 fn query_crate_is_local(&self) -> bool {
139 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
140 self.to_def_id().default_span(tcx)
142 fn key_as_def_id(&self) -> Option<DefId> {
143 Some(self.to_def_id())
149 fn query_crate_is_local(&self) -> bool {
150 self.krate == LOCAL_CRATE
152 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
156 fn key_as_def_id(&self) -> Option<DefId> {
161 impl Key for ty::WithOptConstParam<LocalDefId> {
163 fn query_crate_is_local(&self) -> bool {
166 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
167 self.did.default_span(tcx)
171 impl Key for SimplifiedType {
173 fn query_crate_is_local(&self) -> bool {
176 fn default_span(&self, _: TyCtxt<'_>) -> Span {
181 impl Key for (DefId, DefId) {
183 fn query_crate_is_local(&self) -> bool {
184 self.0.krate == LOCAL_CRATE
186 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
187 self.1.default_span(tcx)
191 impl<'tcx> Key for (ty::Instance<'tcx>, LocalDefId) {
193 fn query_crate_is_local(&self) -> bool {
196 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
197 self.0.default_span(tcx)
201 impl Key for (DefId, LocalDefId) {
203 fn query_crate_is_local(&self) -> bool {
204 self.0.krate == LOCAL_CRATE
206 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
207 self.1.default_span(tcx)
211 impl Key for (LocalDefId, DefId) {
213 fn query_crate_is_local(&self) -> bool {
216 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
217 self.0.default_span(tcx)
221 impl Key for (LocalDefId, LocalDefId) {
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, Option<Ident>) {
233 fn query_crate_is_local(&self) -> bool {
234 self.0.krate == LOCAL_CRATE
236 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
240 fn key_as_def_id(&self) -> Option<DefId> {
245 impl Key for (DefId, LocalDefId, Ident) {
247 fn query_crate_is_local(&self) -> bool {
248 self.0.krate == LOCAL_CRATE
250 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
251 self.1.default_span(tcx)
255 impl Key for (CrateNum, DefId) {
257 fn query_crate_is_local(&self) -> bool {
258 self.0 == LOCAL_CRATE
260 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
261 self.1.default_span(tcx)
265 impl Key for (CrateNum, SimplifiedType) {
267 fn query_crate_is_local(&self) -> bool {
268 self.0 == LOCAL_CRATE
270 fn default_span(&self, _: TyCtxt<'_>) -> Span {
275 impl Key for (DefId, SimplifiedType) {
277 fn query_crate_is_local(&self) -> bool {
278 self.0.krate == LOCAL_CRATE
280 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
281 self.0.default_span(tcx)
285 impl<'tcx> Key for SubstsRef<'tcx> {
287 fn query_crate_is_local(&self) -> bool {
290 fn default_span(&self, _: TyCtxt<'_>) -> Span {
295 impl<'tcx> Key for (DefId, SubstsRef<'tcx>) {
297 fn query_crate_is_local(&self) -> bool {
298 self.0.krate == LOCAL_CRATE
300 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
301 self.0.default_span(tcx)
305 impl<'tcx> Key for (ty::UnevaluatedConst<'tcx>, ty::UnevaluatedConst<'tcx>) {
307 fn query_crate_is_local(&self) -> bool {
308 (self.0).def.did.krate == LOCAL_CRATE
310 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
311 (self.0).def.did.default_span(tcx)
315 impl<'tcx> Key for (LocalDefId, DefId, SubstsRef<'tcx>) {
317 fn query_crate_is_local(&self) -> bool {
320 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
321 self.0.default_span(tcx)
325 impl<'tcx> Key for (ty::ParamEnv<'tcx>, ty::PolyTraitRef<'tcx>) {
327 fn query_crate_is_local(&self) -> bool {
328 self.1.def_id().krate == LOCAL_CRATE
330 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
331 tcx.def_span(self.1.def_id())
335 impl<'tcx> Key for (ty::Const<'tcx>, mir::Field) {
337 fn query_crate_is_local(&self) -> bool {
340 fn default_span(&self, _: TyCtxt<'_>) -> Span {
345 impl<'tcx> Key for mir::interpret::ConstAlloc<'tcx> {
347 fn query_crate_is_local(&self) -> bool {
350 fn default_span(&self, _: TyCtxt<'_>) -> Span {
355 impl<'tcx> Key for ty::PolyTraitRef<'tcx> {
357 fn query_crate_is_local(&self) -> bool {
358 self.def_id().krate == LOCAL_CRATE
360 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
361 tcx.def_span(self.def_id())
365 impl<'tcx> Key for ty::PolyExistentialTraitRef<'tcx> {
367 fn query_crate_is_local(&self) -> bool {
368 self.def_id().krate == LOCAL_CRATE
370 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
371 tcx.def_span(self.def_id())
375 impl<'tcx> Key for (ty::PolyTraitRef<'tcx>, ty::PolyTraitRef<'tcx>) {
377 fn query_crate_is_local(&self) -> bool {
378 self.0.def_id().krate == LOCAL_CRATE
380 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
381 tcx.def_span(self.0.def_id())
385 impl<'tcx> Key for GenericArg<'tcx> {
387 fn query_crate_is_local(&self) -> bool {
390 fn default_span(&self, _: TyCtxt<'_>) -> Span {
395 impl<'tcx> Key for mir::ConstantKind<'tcx> {
397 fn query_crate_is_local(&self) -> bool {
400 fn default_span(&self, _: TyCtxt<'_>) -> Span {
405 impl<'tcx> Key for ty::Const<'tcx> {
407 fn query_crate_is_local(&self) -> bool {
410 fn default_span(&self, _: TyCtxt<'_>) -> Span {
415 impl<'tcx> Key for Ty<'tcx> {
417 fn query_crate_is_local(&self) -> bool {
420 fn default_span(&self, _: TyCtxt<'_>) -> Span {
423 fn ty_adt_id(&self) -> Option<DefId> {
425 ty::Adt(adt, _) => Some(adt.did()),
431 impl<'tcx> Key for TyAndLayout<'tcx> {
433 fn query_crate_is_local(&self) -> bool {
436 fn default_span(&self, _: TyCtxt<'_>) -> Span {
441 impl<'tcx> Key for (Ty<'tcx>, Ty<'tcx>) {
443 fn query_crate_is_local(&self) -> bool {
446 fn default_span(&self, _: TyCtxt<'_>) -> Span {
451 impl<'tcx> Key for &'tcx ty::List<ty::Predicate<'tcx>> {
453 fn query_crate_is_local(&self) -> bool {
456 fn default_span(&self, _: TyCtxt<'_>) -> Span {
461 impl<'tcx> Key for ty::ParamEnv<'tcx> {
463 fn query_crate_is_local(&self) -> bool {
466 fn default_span(&self, _: TyCtxt<'_>) -> Span {
471 impl<'tcx, T: Key> Key for ty::ParamEnvAnd<'tcx, T> {
473 fn query_crate_is_local(&self) -> bool {
474 self.value.query_crate_is_local()
476 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
477 self.value.default_span(tcx)
481 impl Key for Symbol {
483 fn query_crate_is_local(&self) -> bool {
486 fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
491 impl Key for Option<Symbol> {
493 fn query_crate_is_local(&self) -> bool {
496 fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
501 /// Canonical query goals correspond to abstract trait operations that
502 /// are not tied to any crate in particular.
503 impl<'tcx, T> Key for Canonical<'tcx, T> {
505 fn query_crate_is_local(&self) -> bool {
509 fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
514 impl Key for (Symbol, u32, u32) {
516 fn query_crate_is_local(&self) -> bool {
520 fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
525 impl<'tcx> Key for (DefId, Ty<'tcx>, SubstsRef<'tcx>, ty::ParamEnv<'tcx>) {
527 fn query_crate_is_local(&self) -> bool {
531 fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
536 impl<'tcx> Key for (ty::Predicate<'tcx>, traits::WellFormedLoc) {
538 fn query_crate_is_local(&self) -> bool {
542 fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
547 impl<'tcx> Key for (ty::PolyFnSig<'tcx>, &'tcx ty::List<Ty<'tcx>>) {
549 fn query_crate_is_local(&self) -> bool {
553 fn default_span(&self, _: TyCtxt<'_>) -> Span {
558 impl<'tcx> Key for (ty::Instance<'tcx>, &'tcx ty::List<Ty<'tcx>>) {
560 fn query_crate_is_local(&self) -> bool {
564 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
565 self.0.default_span(tcx)
569 impl<'tcx> Key for (Ty<'tcx>, ty::ValTree<'tcx>) {
571 fn query_crate_is_local(&self) -> bool {
575 fn default_span(&self, _: TyCtxt<'_>) -> Span {
582 fn query_crate_is_local(&self) -> bool {
586 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
587 tcx.hir().span(*self)
591 fn key_as_def_id(&self) -> Option<DefId> {