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_hir::hir_id::OwnerId;
5 use rustc_middle::infer::canonical::Canonical;
7 use rustc_middle::traits;
8 use rustc_middle::ty::fast_reject::SimplifiedType;
9 use rustc_middle::ty::subst::{GenericArg, SubstsRef};
10 use rustc_middle::ty::{self, layout::TyAndLayout, Ty, TyCtxt};
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> {
34 fn query_crate_is_local(&self) -> bool {
38 fn default_span(&self, _: TyCtxt<'_>) -> Span {
43 impl<'tcx> Key for ty::InstanceDef<'tcx> {
45 fn query_crate_is_local(&self) -> bool {
49 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
50 tcx.def_span(self.def_id())
54 impl<'tcx> Key for ty::Instance<'tcx> {
56 fn query_crate_is_local(&self) -> bool {
60 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
61 tcx.def_span(self.def_id())
65 impl<'tcx> Key for mir::interpret::GlobalId<'tcx> {
67 fn query_crate_is_local(&self) -> bool {
71 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
72 self.instance.default_span(tcx)
76 impl<'tcx> Key for (Ty<'tcx>, Option<ty::PolyExistentialTraitRef<'tcx>>) {
78 fn query_crate_is_local(&self) -> bool {
82 fn default_span(&self, _: TyCtxt<'_>) -> Span {
87 impl<'tcx> Key for mir::interpret::LitToConstInput<'tcx> {
89 fn query_crate_is_local(&self) -> bool {
93 fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
98 impl Key for CrateNum {
100 fn query_crate_is_local(&self) -> bool {
103 fn default_span(&self, _: TyCtxt<'_>) -> Span {
108 impl Key for OwnerId {
110 fn query_crate_is_local(&self) -> bool {
113 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
114 self.to_def_id().default_span(tcx)
116 fn key_as_def_id(&self) -> Option<DefId> {
117 Some(self.to_def_id())
121 impl Key for LocalDefId {
123 fn query_crate_is_local(&self) -> bool {
126 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
127 self.to_def_id().default_span(tcx)
129 fn key_as_def_id(&self) -> Option<DefId> {
130 Some(self.to_def_id())
136 fn query_crate_is_local(&self) -> bool {
137 self.krate == LOCAL_CRATE
139 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
143 fn key_as_def_id(&self) -> Option<DefId> {
148 impl Key for ty::WithOptConstParam<LocalDefId> {
150 fn query_crate_is_local(&self) -> bool {
153 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
154 self.did.default_span(tcx)
158 impl Key for SimplifiedType {
160 fn query_crate_is_local(&self) -> bool {
163 fn default_span(&self, _: TyCtxt<'_>) -> Span {
168 impl Key for (DefId, DefId) {
170 fn query_crate_is_local(&self) -> bool {
171 self.0.krate == LOCAL_CRATE
173 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
174 self.1.default_span(tcx)
178 impl<'tcx> Key for (ty::Instance<'tcx>, LocalDefId) {
180 fn query_crate_is_local(&self) -> bool {
183 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
184 self.0.default_span(tcx)
188 impl Key for (DefId, LocalDefId) {
190 fn query_crate_is_local(&self) -> bool {
191 self.0.krate == LOCAL_CRATE
193 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
194 self.1.default_span(tcx)
198 impl Key for (LocalDefId, DefId) {
200 fn query_crate_is_local(&self) -> bool {
203 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
204 self.0.default_span(tcx)
208 impl Key for (LocalDefId, LocalDefId) {
210 fn query_crate_is_local(&self) -> bool {
213 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
214 self.0.default_span(tcx)
218 impl Key for (DefId, Option<Ident>) {
220 fn query_crate_is_local(&self) -> bool {
221 self.0.krate == LOCAL_CRATE
223 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
227 fn key_as_def_id(&self) -> Option<DefId> {
232 impl Key for (DefId, LocalDefId, Ident) {
234 fn query_crate_is_local(&self) -> bool {
235 self.0.krate == LOCAL_CRATE
237 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
238 self.1.default_span(tcx)
242 impl Key for (CrateNum, DefId) {
244 fn query_crate_is_local(&self) -> bool {
245 self.0 == LOCAL_CRATE
247 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
248 self.1.default_span(tcx)
252 impl Key for (CrateNum, SimplifiedType) {
254 fn query_crate_is_local(&self) -> bool {
255 self.0 == LOCAL_CRATE
257 fn default_span(&self, _: TyCtxt<'_>) -> Span {
262 impl Key for (DefId, SimplifiedType) {
264 fn query_crate_is_local(&self) -> bool {
265 self.0.krate == LOCAL_CRATE
267 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
268 self.0.default_span(tcx)
272 impl<'tcx> Key for SubstsRef<'tcx> {
274 fn query_crate_is_local(&self) -> bool {
277 fn default_span(&self, _: TyCtxt<'_>) -> Span {
282 impl<'tcx> Key for (DefId, SubstsRef<'tcx>) {
284 fn query_crate_is_local(&self) -> bool {
285 self.0.krate == LOCAL_CRATE
287 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
288 self.0.default_span(tcx)
292 impl<'tcx> Key for (ty::UnevaluatedConst<'tcx>, ty::UnevaluatedConst<'tcx>) {
294 fn query_crate_is_local(&self) -> bool {
295 (self.0).def.did.krate == LOCAL_CRATE
297 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
298 (self.0).def.did.default_span(tcx)
302 impl<'tcx> Key for (LocalDefId, DefId, SubstsRef<'tcx>) {
304 fn query_crate_is_local(&self) -> bool {
307 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
308 self.0.default_span(tcx)
312 impl<'tcx> Key for (ty::ParamEnv<'tcx>, ty::PolyTraitRef<'tcx>) {
314 fn query_crate_is_local(&self) -> bool {
315 self.1.def_id().krate == LOCAL_CRATE
317 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
318 tcx.def_span(self.1.def_id())
322 impl<'tcx> Key for (ty::Const<'tcx>, mir::Field) {
324 fn query_crate_is_local(&self) -> bool {
327 fn default_span(&self, _: TyCtxt<'_>) -> Span {
332 impl<'tcx> Key for mir::interpret::ConstAlloc<'tcx> {
334 fn query_crate_is_local(&self) -> bool {
337 fn default_span(&self, _: TyCtxt<'_>) -> Span {
342 impl<'tcx> Key for ty::PolyTraitRef<'tcx> {
344 fn query_crate_is_local(&self) -> bool {
345 self.def_id().krate == LOCAL_CRATE
347 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
348 tcx.def_span(self.def_id())
352 impl<'tcx> Key for ty::PolyExistentialTraitRef<'tcx> {
354 fn query_crate_is_local(&self) -> bool {
355 self.def_id().krate == LOCAL_CRATE
357 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
358 tcx.def_span(self.def_id())
362 impl<'tcx> Key for (ty::PolyTraitRef<'tcx>, ty::PolyTraitRef<'tcx>) {
364 fn query_crate_is_local(&self) -> bool {
365 self.0.def_id().krate == LOCAL_CRATE
367 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
368 tcx.def_span(self.0.def_id())
372 impl<'tcx> Key for GenericArg<'tcx> {
374 fn query_crate_is_local(&self) -> bool {
377 fn default_span(&self, _: TyCtxt<'_>) -> Span {
382 impl<'tcx> Key for mir::ConstantKind<'tcx> {
384 fn query_crate_is_local(&self) -> bool {
387 fn default_span(&self, _: TyCtxt<'_>) -> Span {
392 impl<'tcx> Key for ty::Const<'tcx> {
394 fn query_crate_is_local(&self) -> bool {
397 fn default_span(&self, _: TyCtxt<'_>) -> Span {
402 impl<'tcx> Key for Ty<'tcx> {
404 fn query_crate_is_local(&self) -> bool {
407 fn default_span(&self, _: TyCtxt<'_>) -> Span {
412 impl<'tcx> Key for TyAndLayout<'tcx> {
414 fn query_crate_is_local(&self) -> bool {
417 fn default_span(&self, _: TyCtxt<'_>) -> Span {
422 impl<'tcx> Key for (Ty<'tcx>, Ty<'tcx>) {
424 fn query_crate_is_local(&self) -> bool {
427 fn default_span(&self, _: TyCtxt<'_>) -> Span {
432 impl<'tcx> Key for &'tcx ty::List<ty::Predicate<'tcx>> {
434 fn query_crate_is_local(&self) -> bool {
437 fn default_span(&self, _: TyCtxt<'_>) -> Span {
442 impl<'tcx> Key for ty::ParamEnv<'tcx> {
444 fn query_crate_is_local(&self) -> bool {
447 fn default_span(&self, _: TyCtxt<'_>) -> Span {
452 impl<'tcx, T: Key> Key for ty::ParamEnvAnd<'tcx, T> {
454 fn query_crate_is_local(&self) -> bool {
455 self.value.query_crate_is_local()
457 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
458 self.value.default_span(tcx)
462 impl Key for Symbol {
464 fn query_crate_is_local(&self) -> bool {
467 fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
472 impl Key for Option<Symbol> {
474 fn query_crate_is_local(&self) -> bool {
477 fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
482 /// Canonical query goals correspond to abstract trait operations that
483 /// are not tied to any crate in particular.
484 impl<'tcx, T> Key for Canonical<'tcx, T> {
486 fn query_crate_is_local(&self) -> bool {
490 fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
495 impl Key for (Symbol, u32, u32) {
497 fn query_crate_is_local(&self) -> bool {
501 fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
506 impl<'tcx> Key for (DefId, Ty<'tcx>, SubstsRef<'tcx>, ty::ParamEnv<'tcx>) {
508 fn query_crate_is_local(&self) -> bool {
512 fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
517 impl<'tcx> Key for (ty::Predicate<'tcx>, traits::WellFormedLoc) {
519 fn query_crate_is_local(&self) -> bool {
523 fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
528 impl<'tcx> Key for (ty::PolyFnSig<'tcx>, &'tcx ty::List<Ty<'tcx>>) {
530 fn query_crate_is_local(&self) -> bool {
534 fn default_span(&self, _: TyCtxt<'_>) -> Span {
539 impl<'tcx> Key for (ty::Instance<'tcx>, &'tcx ty::List<Ty<'tcx>>) {
541 fn query_crate_is_local(&self) -> bool {
545 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
546 self.0.default_span(tcx)
550 impl<'tcx> Key for (Ty<'tcx>, ty::ValTree<'tcx>) {
552 fn query_crate_is_local(&self) -> bool {
556 fn default_span(&self, _: TyCtxt<'_>) -> Span {