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_span::symbol::{Ident, Symbol};
12 use rustc_span::{Span, DUMMY_SP};
14 /// The `Key` trait controls what types can legally be used as the key
17 /// Given an instance of this key, what crate is it referring to?
18 /// This is used to find the provider.
19 fn query_crate_is_local(&self) -> bool;
21 /// In the event that a cycle occurs, if no explicit span has been
22 /// given for a query with key `self`, what span should we use?
23 fn default_span(&self, tcx: TyCtxt<'_>) -> Span;
25 /// If the key is a [`DefId`] or `DefId`--equivalent, return that `DefId`.
26 /// Otherwise, return `None`.
27 fn key_as_def_id(&self) -> Option<DefId> {
31 fn ty_adt_id(&self) -> Option<DefId> {
38 fn query_crate_is_local(&self) -> bool {
42 fn default_span(&self, _: TyCtxt<'_>) -> Span {
47 impl<'tcx> Key for ty::InstanceDef<'tcx> {
49 fn query_crate_is_local(&self) -> bool {
53 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
54 tcx.def_span(self.def_id())
58 impl<'tcx> Key for ty::Instance<'tcx> {
60 fn query_crate_is_local(&self) -> bool {
64 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
65 tcx.def_span(self.def_id())
69 impl<'tcx> Key for mir::interpret::GlobalId<'tcx> {
71 fn query_crate_is_local(&self) -> bool {
75 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
76 self.instance.default_span(tcx)
80 impl<'tcx> Key for (Ty<'tcx>, Option<ty::PolyExistentialTraitRef<'tcx>>) {
82 fn query_crate_is_local(&self) -> bool {
86 fn default_span(&self, _: TyCtxt<'_>) -> Span {
91 impl<'tcx> Key for mir::interpret::LitToConstInput<'tcx> {
93 fn query_crate_is_local(&self) -> bool {
97 fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
102 impl Key for CrateNum {
104 fn query_crate_is_local(&self) -> bool {
107 fn default_span(&self, _: TyCtxt<'_>) -> Span {
112 impl Key for OwnerId {
114 fn query_crate_is_local(&self) -> bool {
117 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
118 self.to_def_id().default_span(tcx)
120 fn key_as_def_id(&self) -> Option<DefId> {
121 Some(self.to_def_id())
125 impl Key for LocalDefId {
127 fn query_crate_is_local(&self) -> bool {
130 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
131 self.to_def_id().default_span(tcx)
133 fn key_as_def_id(&self) -> Option<DefId> {
134 Some(self.to_def_id())
140 fn query_crate_is_local(&self) -> bool {
141 self.krate == LOCAL_CRATE
143 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
147 fn key_as_def_id(&self) -> Option<DefId> {
152 impl Key for ty::WithOptConstParam<LocalDefId> {
154 fn query_crate_is_local(&self) -> bool {
157 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
158 self.did.default_span(tcx)
162 impl Key for SimplifiedType {
164 fn query_crate_is_local(&self) -> bool {
167 fn default_span(&self, _: TyCtxt<'_>) -> Span {
172 impl Key for (DefId, DefId) {
174 fn query_crate_is_local(&self) -> bool {
175 self.0.krate == LOCAL_CRATE
177 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
178 self.1.default_span(tcx)
182 impl<'tcx> Key for (ty::Instance<'tcx>, LocalDefId) {
184 fn query_crate_is_local(&self) -> bool {
187 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
188 self.0.default_span(tcx)
192 impl Key for (DefId, LocalDefId) {
194 fn query_crate_is_local(&self) -> bool {
195 self.0.krate == LOCAL_CRATE
197 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
198 self.1.default_span(tcx)
202 impl Key for (LocalDefId, DefId) {
204 fn query_crate_is_local(&self) -> bool {
207 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
208 self.0.default_span(tcx)
212 impl Key for (LocalDefId, LocalDefId) {
214 fn query_crate_is_local(&self) -> bool {
217 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
218 self.0.default_span(tcx)
222 impl Key for (DefId, Option<Ident>) {
224 fn query_crate_is_local(&self) -> bool {
225 self.0.krate == LOCAL_CRATE
227 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
231 fn key_as_def_id(&self) -> Option<DefId> {
236 impl Key for (DefId, LocalDefId, Ident) {
238 fn query_crate_is_local(&self) -> bool {
239 self.0.krate == LOCAL_CRATE
241 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
242 self.1.default_span(tcx)
246 impl Key for (CrateNum, DefId) {
248 fn query_crate_is_local(&self) -> bool {
249 self.0 == LOCAL_CRATE
251 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
252 self.1.default_span(tcx)
256 impl Key for (CrateNum, SimplifiedType) {
258 fn query_crate_is_local(&self) -> bool {
259 self.0 == LOCAL_CRATE
261 fn default_span(&self, _: TyCtxt<'_>) -> Span {
266 impl Key for (DefId, SimplifiedType) {
268 fn query_crate_is_local(&self) -> bool {
269 self.0.krate == LOCAL_CRATE
271 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
272 self.0.default_span(tcx)
276 impl<'tcx> Key for SubstsRef<'tcx> {
278 fn query_crate_is_local(&self) -> bool {
281 fn default_span(&self, _: TyCtxt<'_>) -> Span {
286 impl<'tcx> Key for (DefId, SubstsRef<'tcx>) {
288 fn query_crate_is_local(&self) -> bool {
289 self.0.krate == LOCAL_CRATE
291 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
292 self.0.default_span(tcx)
296 impl<'tcx> Key for (ty::UnevaluatedConst<'tcx>, ty::UnevaluatedConst<'tcx>) {
298 fn query_crate_is_local(&self) -> bool {
299 (self.0).def.did.krate == LOCAL_CRATE
301 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
302 (self.0).def.did.default_span(tcx)
306 impl<'tcx> Key for (LocalDefId, DefId, SubstsRef<'tcx>) {
308 fn query_crate_is_local(&self) -> bool {
311 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
312 self.0.default_span(tcx)
316 impl<'tcx> Key for (ty::ParamEnv<'tcx>, ty::PolyTraitRef<'tcx>) {
318 fn query_crate_is_local(&self) -> bool {
319 self.1.def_id().krate == LOCAL_CRATE
321 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
322 tcx.def_span(self.1.def_id())
326 impl<'tcx> Key for (ty::Const<'tcx>, mir::Field) {
328 fn query_crate_is_local(&self) -> bool {
331 fn default_span(&self, _: TyCtxt<'_>) -> Span {
336 impl<'tcx> Key for mir::interpret::ConstAlloc<'tcx> {
338 fn query_crate_is_local(&self) -> bool {
341 fn default_span(&self, _: TyCtxt<'_>) -> Span {
346 impl<'tcx> Key for ty::PolyTraitRef<'tcx> {
348 fn query_crate_is_local(&self) -> bool {
349 self.def_id().krate == LOCAL_CRATE
351 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
352 tcx.def_span(self.def_id())
356 impl<'tcx> Key for ty::PolyExistentialTraitRef<'tcx> {
358 fn query_crate_is_local(&self) -> bool {
359 self.def_id().krate == LOCAL_CRATE
361 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
362 tcx.def_span(self.def_id())
366 impl<'tcx> Key for (ty::PolyTraitRef<'tcx>, ty::PolyTraitRef<'tcx>) {
368 fn query_crate_is_local(&self) -> bool {
369 self.0.def_id().krate == LOCAL_CRATE
371 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
372 tcx.def_span(self.0.def_id())
376 impl<'tcx> Key for GenericArg<'tcx> {
378 fn query_crate_is_local(&self) -> bool {
381 fn default_span(&self, _: TyCtxt<'_>) -> Span {
386 impl<'tcx> Key for mir::ConstantKind<'tcx> {
388 fn query_crate_is_local(&self) -> bool {
391 fn default_span(&self, _: TyCtxt<'_>) -> Span {
396 impl<'tcx> Key for ty::Const<'tcx> {
398 fn query_crate_is_local(&self) -> bool {
401 fn default_span(&self, _: TyCtxt<'_>) -> Span {
406 impl<'tcx> Key for Ty<'tcx> {
408 fn query_crate_is_local(&self) -> bool {
411 fn default_span(&self, _: TyCtxt<'_>) -> Span {
414 fn ty_adt_id(&self) -> Option<DefId> {
416 ty::Adt(adt, _) => Some(adt.did()),
422 impl<'tcx> Key for TyAndLayout<'tcx> {
424 fn query_crate_is_local(&self) -> bool {
427 fn default_span(&self, _: TyCtxt<'_>) -> Span {
432 impl<'tcx> Key for (Ty<'tcx>, Ty<'tcx>) {
434 fn query_crate_is_local(&self) -> bool {
437 fn default_span(&self, _: TyCtxt<'_>) -> Span {
442 impl<'tcx> Key for &'tcx ty::List<ty::Predicate<'tcx>> {
444 fn query_crate_is_local(&self) -> bool {
447 fn default_span(&self, _: TyCtxt<'_>) -> Span {
452 impl<'tcx> Key for ty::ParamEnv<'tcx> {
454 fn query_crate_is_local(&self) -> bool {
457 fn default_span(&self, _: TyCtxt<'_>) -> Span {
462 impl<'tcx, T: Key> Key for ty::ParamEnvAnd<'tcx, T> {
464 fn query_crate_is_local(&self) -> bool {
465 self.value.query_crate_is_local()
467 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
468 self.value.default_span(tcx)
472 impl Key for Symbol {
474 fn query_crate_is_local(&self) -> bool {
477 fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
482 impl Key for Option<Symbol> {
484 fn query_crate_is_local(&self) -> bool {
487 fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
492 /// Canonical query goals correspond to abstract trait operations that
493 /// are not tied to any crate in particular.
494 impl<'tcx, T> Key for Canonical<'tcx, T> {
496 fn query_crate_is_local(&self) -> bool {
500 fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
505 impl Key for (Symbol, u32, u32) {
507 fn query_crate_is_local(&self) -> bool {
511 fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
516 impl<'tcx> Key for (DefId, Ty<'tcx>, SubstsRef<'tcx>, ty::ParamEnv<'tcx>) {
518 fn query_crate_is_local(&self) -> bool {
522 fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
527 impl<'tcx> Key for (ty::Predicate<'tcx>, traits::WellFormedLoc) {
529 fn query_crate_is_local(&self) -> bool {
533 fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
538 impl<'tcx> Key for (ty::PolyFnSig<'tcx>, &'tcx ty::List<Ty<'tcx>>) {
540 fn query_crate_is_local(&self) -> bool {
544 fn default_span(&self, _: TyCtxt<'_>) -> Span {
549 impl<'tcx> Key for (ty::Instance<'tcx>, &'tcx ty::List<Ty<'tcx>>) {
551 fn query_crate_is_local(&self) -> bool {
555 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
556 self.0.default_span(tcx)
560 impl<'tcx> Key for (Ty<'tcx>, ty::ValTree<'tcx>) {
562 fn query_crate_is_local(&self) -> bool {
566 fn default_span(&self, _: TyCtxt<'_>) -> Span {
573 fn query_crate_is_local(&self) -> bool {
577 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
578 tcx.hir().span(*self)
582 fn key_as_def_id(&self) -> Option<DefId> {