1 //! Defines the set of legal keys that can be used in queries.
3 use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, LOCAL_CRATE};
4 use rustc_middle::infer::canonical::Canonical;
6 use rustc_middle::traits;
7 use rustc_middle::ty::fast_reject::SimplifiedType;
8 use rustc_middle::ty::subst::{GenericArg, SubstsRef};
9 use rustc_middle::ty::{self, layout::TyAndLayout, Ty, TyCtxt};
10 use rustc_span::symbol::{Ident, Symbol};
11 use rustc_span::{Span, DUMMY_SP};
13 /// The `Key` trait controls what types can legally be used as the key
16 /// Given an instance of this key, what crate is it referring to?
17 /// This is used to find the provider.
18 fn query_crate_is_local(&self) -> bool;
20 /// In the event that a cycle occurs, if no explicit span has been
21 /// given for a query with key `self`, what span should we use?
22 fn default_span(&self, tcx: TyCtxt<'_>) -> Span;
24 /// If the key is a [`DefId`] or `DefId`--equivalent, return that `DefId`.
25 /// Otherwise, return `None`.
26 fn key_as_def_id(&self) -> Option<DefId> {
33 fn query_crate_is_local(&self) -> bool {
37 fn default_span(&self, _: TyCtxt<'_>) -> Span {
42 impl<'tcx> Key for ty::InstanceDef<'tcx> {
44 fn query_crate_is_local(&self) -> bool {
48 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
49 tcx.def_span(self.def_id())
53 impl<'tcx> Key for ty::Instance<'tcx> {
55 fn query_crate_is_local(&self) -> bool {
59 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
60 tcx.def_span(self.def_id())
64 impl<'tcx> Key for mir::interpret::GlobalId<'tcx> {
66 fn query_crate_is_local(&self) -> bool {
70 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
71 self.instance.default_span(tcx)
75 impl<'tcx> Key for (Ty<'tcx>, Option<ty::PolyExistentialTraitRef<'tcx>>) {
77 fn query_crate_is_local(&self) -> bool {
81 fn default_span(&self, _: TyCtxt<'_>) -> Span {
86 impl<'tcx> Key for mir::interpret::LitToConstInput<'tcx> {
88 fn query_crate_is_local(&self) -> bool {
92 fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
97 impl Key for CrateNum {
99 fn query_crate_is_local(&self) -> bool {
102 fn default_span(&self, _: TyCtxt<'_>) -> Span {
107 impl Key for LocalDefId {
109 fn query_crate_is_local(&self) -> bool {
112 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
113 self.to_def_id().default_span(tcx)
115 fn key_as_def_id(&self) -> Option<DefId> {
116 Some(self.to_def_id())
122 fn query_crate_is_local(&self) -> bool {
123 self.krate == LOCAL_CRATE
125 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
129 fn key_as_def_id(&self) -> Option<DefId> {
134 impl Key for ty::WithOptConstParam<LocalDefId> {
136 fn query_crate_is_local(&self) -> bool {
139 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
140 self.did.default_span(tcx)
144 impl Key for SimplifiedType {
146 fn query_crate_is_local(&self) -> bool {
149 fn default_span(&self, _: TyCtxt<'_>) -> Span {
154 impl Key for (DefId, DefId) {
156 fn query_crate_is_local(&self) -> bool {
157 self.0.krate == LOCAL_CRATE
159 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
160 self.1.default_span(tcx)
164 impl<'tcx> Key for (ty::Instance<'tcx>, LocalDefId) {
166 fn query_crate_is_local(&self) -> bool {
169 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
170 self.0.default_span(tcx)
174 impl Key for (DefId, LocalDefId) {
176 fn query_crate_is_local(&self) -> bool {
177 self.0.krate == LOCAL_CRATE
179 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
180 self.1.default_span(tcx)
184 impl Key for (LocalDefId, DefId) {
186 fn query_crate_is_local(&self) -> bool {
189 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
190 self.0.default_span(tcx)
194 impl Key for (LocalDefId, LocalDefId) {
196 fn query_crate_is_local(&self) -> bool {
199 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
200 self.0.default_span(tcx)
204 impl Key for (DefId, Option<Ident>) {
206 fn query_crate_is_local(&self) -> bool {
207 self.0.krate == LOCAL_CRATE
209 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
213 fn key_as_def_id(&self) -> Option<DefId> {
218 impl Key for (DefId, LocalDefId, Ident) {
220 fn query_crate_is_local(&self) -> bool {
221 self.0.krate == LOCAL_CRATE
223 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
224 self.1.default_span(tcx)
228 impl Key for (CrateNum, DefId) {
230 fn query_crate_is_local(&self) -> bool {
231 self.0 == LOCAL_CRATE
233 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
234 self.1.default_span(tcx)
238 impl Key for (CrateNum, SimplifiedType) {
240 fn query_crate_is_local(&self) -> bool {
241 self.0 == LOCAL_CRATE
243 fn default_span(&self, _: TyCtxt<'_>) -> Span {
248 impl Key for (DefId, SimplifiedType) {
250 fn query_crate_is_local(&self) -> bool {
251 self.0.krate == LOCAL_CRATE
253 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
254 self.0.default_span(tcx)
258 impl<'tcx> Key for SubstsRef<'tcx> {
260 fn query_crate_is_local(&self) -> bool {
263 fn default_span(&self, _: TyCtxt<'_>) -> Span {
268 impl<'tcx> Key for (DefId, SubstsRef<'tcx>) {
270 fn query_crate_is_local(&self) -> bool {
271 self.0.krate == LOCAL_CRATE
273 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
274 self.0.default_span(tcx)
278 impl<'tcx> Key for (ty::UnevaluatedConst<'tcx>, ty::UnevaluatedConst<'tcx>) {
280 fn query_crate_is_local(&self) -> bool {
281 (self.0).def.did.krate == LOCAL_CRATE
283 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
284 (self.0).def.did.default_span(tcx)
288 impl<'tcx> Key for (LocalDefId, DefId, SubstsRef<'tcx>) {
290 fn query_crate_is_local(&self) -> bool {
293 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
294 self.0.default_span(tcx)
298 impl<'tcx> Key for (ty::ParamEnv<'tcx>, ty::PolyTraitRef<'tcx>) {
300 fn query_crate_is_local(&self) -> bool {
301 self.1.def_id().krate == LOCAL_CRATE
303 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
304 tcx.def_span(self.1.def_id())
308 impl<'tcx> Key for (ty::Const<'tcx>, mir::Field) {
310 fn query_crate_is_local(&self) -> bool {
313 fn default_span(&self, _: TyCtxt<'_>) -> Span {
318 impl<'tcx> Key for mir::interpret::ConstAlloc<'tcx> {
320 fn query_crate_is_local(&self) -> bool {
323 fn default_span(&self, _: TyCtxt<'_>) -> Span {
328 impl<'tcx> Key for ty::PolyTraitRef<'tcx> {
330 fn query_crate_is_local(&self) -> bool {
331 self.def_id().krate == LOCAL_CRATE
333 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
334 tcx.def_span(self.def_id())
338 impl<'tcx> Key for ty::PolyExistentialTraitRef<'tcx> {
340 fn query_crate_is_local(&self) -> bool {
341 self.def_id().krate == LOCAL_CRATE
343 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
344 tcx.def_span(self.def_id())
348 impl<'tcx> Key for (ty::PolyTraitRef<'tcx>, ty::PolyTraitRef<'tcx>) {
350 fn query_crate_is_local(&self) -> bool {
351 self.0.def_id().krate == LOCAL_CRATE
353 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
354 tcx.def_span(self.0.def_id())
358 impl<'tcx> Key for GenericArg<'tcx> {
360 fn query_crate_is_local(&self) -> bool {
363 fn default_span(&self, _: TyCtxt<'_>) -> Span {
368 impl<'tcx> Key for mir::ConstantKind<'tcx> {
370 fn query_crate_is_local(&self) -> bool {
373 fn default_span(&self, _: TyCtxt<'_>) -> Span {
378 impl<'tcx> Key for ty::Const<'tcx> {
380 fn query_crate_is_local(&self) -> bool {
383 fn default_span(&self, _: TyCtxt<'_>) -> Span {
388 impl<'tcx> Key for Ty<'tcx> {
390 fn query_crate_is_local(&self) -> bool {
393 fn default_span(&self, _: TyCtxt<'_>) -> Span {
398 impl<'tcx> Key for TyAndLayout<'tcx> {
400 fn query_crate_is_local(&self) -> bool {
403 fn default_span(&self, _: TyCtxt<'_>) -> Span {
408 impl<'tcx> Key for (Ty<'tcx>, Ty<'tcx>) {
410 fn query_crate_is_local(&self) -> bool {
413 fn default_span(&self, _: TyCtxt<'_>) -> Span {
418 impl<'tcx> Key for &'tcx ty::List<ty::Predicate<'tcx>> {
420 fn query_crate_is_local(&self) -> bool {
423 fn default_span(&self, _: TyCtxt<'_>) -> Span {
428 impl<'tcx> Key for ty::ParamEnv<'tcx> {
430 fn query_crate_is_local(&self) -> bool {
433 fn default_span(&self, _: TyCtxt<'_>) -> Span {
438 impl<'tcx, T: Key> Key for ty::ParamEnvAnd<'tcx, T> {
440 fn query_crate_is_local(&self) -> bool {
441 self.value.query_crate_is_local()
443 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
444 self.value.default_span(tcx)
448 impl Key for Symbol {
450 fn query_crate_is_local(&self) -> bool {
453 fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
458 impl Key for Option<Symbol> {
460 fn query_crate_is_local(&self) -> bool {
463 fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
468 /// Canonical query goals correspond to abstract trait operations that
469 /// are not tied to any crate in particular.
470 impl<'tcx, T> Key for Canonical<'tcx, T> {
472 fn query_crate_is_local(&self) -> bool {
476 fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
481 impl Key for (Symbol, u32, u32) {
483 fn query_crate_is_local(&self) -> bool {
487 fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
492 impl<'tcx> Key for (DefId, Ty<'tcx>, SubstsRef<'tcx>, ty::ParamEnv<'tcx>) {
494 fn query_crate_is_local(&self) -> bool {
498 fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
503 impl<'tcx> Key for (ty::Predicate<'tcx>, traits::WellFormedLoc) {
505 fn query_crate_is_local(&self) -> bool {
509 fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
514 impl<'tcx> Key for (ty::PolyFnSig<'tcx>, &'tcx ty::List<Ty<'tcx>>) {
516 fn query_crate_is_local(&self) -> bool {
520 fn default_span(&self, _: TyCtxt<'_>) -> Span {
525 impl<'tcx> Key for (ty::Instance<'tcx>, &'tcx ty::List<Ty<'tcx>>) {
527 fn query_crate_is_local(&self) -> bool {
531 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
532 self.0.default_span(tcx)
536 impl<'tcx> Key for (Ty<'tcx>, ty::ValTree<'tcx>) {
538 fn query_crate_is_local(&self) -> bool {
542 fn default_span(&self, _: TyCtxt<'_>) -> Span {