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, 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 Key for ty::RawLocalDefId {
44 fn query_crate_is_local(&self) -> bool {
48 fn default_span(&self, _: TyCtxt<'_>) -> Span {
53 impl<'tcx> Key for ty::InstanceDef<'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 ty::Instance<'tcx> {
66 fn query_crate_is_local(&self) -> bool {
70 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
71 tcx.def_span(self.def_id())
75 impl<'tcx> Key for mir::interpret::GlobalId<'tcx> {
77 fn query_crate_is_local(&self) -> bool {
81 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
82 self.instance.default_span(tcx)
86 impl<'tcx> Key for (Ty<'tcx>, Option<ty::PolyExistentialTraitRef<'tcx>>) {
88 fn query_crate_is_local(&self) -> bool {
92 fn default_span(&self, _: TyCtxt<'_>) -> Span {
97 impl<'tcx> Key for mir::interpret::LitToConstInput<'tcx> {
99 fn query_crate_is_local(&self) -> bool {
103 fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
108 impl Key for CrateNum {
110 fn query_crate_is_local(&self) -> bool {
113 fn default_span(&self, _: TyCtxt<'_>) -> Span {
118 impl Key for LocalDefId {
120 fn query_crate_is_local(&self) -> bool {
123 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
124 self.to_def_id().default_span(tcx)
126 fn key_as_def_id(&self) -> Option<DefId> {
127 Some(self.to_def_id())
133 fn query_crate_is_local(&self) -> bool {
134 self.krate == LOCAL_CRATE
136 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
140 fn key_as_def_id(&self) -> Option<DefId> {
145 impl Key for ty::WithOptConstParam<LocalDefId> {
147 fn query_crate_is_local(&self) -> bool {
150 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
151 self.did.default_span(tcx)
155 impl Key for SimplifiedType {
157 fn query_crate_is_local(&self) -> bool {
160 fn default_span(&self, _: TyCtxt<'_>) -> Span {
165 impl Key for (DefId, DefId) {
167 fn query_crate_is_local(&self) -> bool {
168 self.0.krate == LOCAL_CRATE
170 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
171 self.1.default_span(tcx)
175 impl<'tcx> Key for (ty::Instance<'tcx>, LocalDefId) {
177 fn query_crate_is_local(&self) -> bool {
180 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
181 self.0.default_span(tcx)
185 impl Key for (DefId, LocalDefId) {
187 fn query_crate_is_local(&self) -> bool {
188 self.0.krate == LOCAL_CRATE
190 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
191 self.1.default_span(tcx)
195 impl Key for (LocalDefId, DefId) {
197 fn query_crate_is_local(&self) -> bool {
200 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
201 self.0.default_span(tcx)
205 impl Key for (DefId, Option<Ident>) {
207 fn query_crate_is_local(&self) -> bool {
208 self.0.krate == LOCAL_CRATE
210 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
214 fn key_as_def_id(&self) -> Option<DefId> {
219 impl Key for (DefId, LocalDefId, Ident) {
221 fn query_crate_is_local(&self) -> bool {
222 self.0.krate == LOCAL_CRATE
224 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
225 self.1.default_span(tcx)
229 impl Key for (CrateNum, DefId) {
231 fn query_crate_is_local(&self) -> bool {
232 self.0 == LOCAL_CRATE
234 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
235 self.1.default_span(tcx)
239 impl Key for (CrateNum, SimplifiedType) {
241 fn query_crate_is_local(&self) -> bool {
242 self.0 == LOCAL_CRATE
244 fn default_span(&self, _: TyCtxt<'_>) -> Span {
249 impl Key for (DefId, SimplifiedType) {
251 fn query_crate_is_local(&self) -> bool {
252 self.0.krate == LOCAL_CRATE
254 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
255 self.0.default_span(tcx)
259 impl<'tcx> Key for SubstsRef<'tcx> {
261 fn query_crate_is_local(&self) -> bool {
264 fn default_span(&self, _: TyCtxt<'_>) -> Span {
269 impl<'tcx> Key for (DefId, SubstsRef<'tcx>) {
271 fn query_crate_is_local(&self) -> bool {
272 self.0.krate == LOCAL_CRATE
274 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
275 self.0.default_span(tcx)
279 impl<'tcx> Key for (ty::Unevaluated<'tcx, ()>, ty::Unevaluated<'tcx, ()>) {
281 fn query_crate_is_local(&self) -> bool {
282 (self.0).def.did.krate == LOCAL_CRATE
284 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
285 (self.0).def.did.default_span(tcx)
289 impl<'tcx> Key for (LocalDefId, DefId, SubstsRef<'tcx>) {
291 fn query_crate_is_local(&self) -> bool {
294 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
295 self.0.default_span(tcx)
299 impl<'tcx> Key for (ty::ParamEnv<'tcx>, ty::PolyTraitRef<'tcx>) {
301 fn query_crate_is_local(&self) -> bool {
302 self.1.def_id().krate == LOCAL_CRATE
304 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
305 tcx.def_span(self.1.def_id())
309 impl<'tcx> Key for (ty::Const<'tcx>, mir::Field) {
311 fn query_crate_is_local(&self) -> bool {
314 fn default_span(&self, _: TyCtxt<'_>) -> Span {
319 impl<'tcx> Key for mir::interpret::ConstAlloc<'tcx> {
321 fn query_crate_is_local(&self) -> bool {
324 fn default_span(&self, _: TyCtxt<'_>) -> Span {
329 impl<'tcx> Key for ty::PolyTraitRef<'tcx> {
331 fn query_crate_is_local(&self) -> bool {
332 self.def_id().krate == LOCAL_CRATE
334 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
335 tcx.def_span(self.def_id())
339 impl<'tcx> Key for ty::PolyExistentialTraitRef<'tcx> {
341 fn query_crate_is_local(&self) -> bool {
342 self.def_id().krate == LOCAL_CRATE
344 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
345 tcx.def_span(self.def_id())
349 impl<'tcx> Key for (ty::PolyTraitRef<'tcx>, ty::PolyTraitRef<'tcx>) {
351 fn query_crate_is_local(&self) -> bool {
352 self.0.def_id().krate == LOCAL_CRATE
354 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
355 tcx.def_span(self.0.def_id())
359 impl<'tcx> Key for GenericArg<'tcx> {
361 fn query_crate_is_local(&self) -> bool {
364 fn default_span(&self, _: TyCtxt<'_>) -> Span {
369 impl<'tcx> Key for mir::ConstantKind<'tcx> {
371 fn query_crate_is_local(&self) -> bool {
374 fn default_span(&self, _: TyCtxt<'_>) -> Span {
379 impl<'tcx> Key for ty::Const<'tcx> {
381 fn query_crate_is_local(&self) -> bool {
384 fn default_span(&self, _: TyCtxt<'_>) -> Span {
389 impl<'tcx> Key for Ty<'tcx> {
391 fn query_crate_is_local(&self) -> bool {
394 fn default_span(&self, _: TyCtxt<'_>) -> Span {
399 impl<'tcx> Key for (Ty<'tcx>, Ty<'tcx>) {
401 fn query_crate_is_local(&self) -> bool {
404 fn default_span(&self, _: TyCtxt<'_>) -> Span {
409 impl<'tcx> Key for &'tcx ty::List<ty::Predicate<'tcx>> {
411 fn query_crate_is_local(&self) -> bool {
414 fn default_span(&self, _: TyCtxt<'_>) -> Span {
419 impl<'tcx> Key for ty::ParamEnv<'tcx> {
421 fn query_crate_is_local(&self) -> bool {
424 fn default_span(&self, _: TyCtxt<'_>) -> Span {
429 impl<'tcx, T: Key> Key for ty::ParamEnvAnd<'tcx, T> {
431 fn query_crate_is_local(&self) -> bool {
432 self.value.query_crate_is_local()
434 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
435 self.value.default_span(tcx)
439 impl Key for Symbol {
441 fn query_crate_is_local(&self) -> bool {
444 fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
449 impl Key for Option<Symbol> {
451 fn query_crate_is_local(&self) -> bool {
454 fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
459 /// Canonical query goals correspond to abstract trait operations that
460 /// are not tied to any crate in particular.
461 impl<'tcx, T> Key for Canonical<'tcx, T> {
463 fn query_crate_is_local(&self) -> bool {
467 fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
472 impl Key for (Symbol, u32, u32) {
474 fn query_crate_is_local(&self) -> bool {
478 fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
483 impl<'tcx> Key for (DefId, Ty<'tcx>, SubstsRef<'tcx>, ty::ParamEnv<'tcx>) {
485 fn query_crate_is_local(&self) -> bool {
489 fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
494 impl<'tcx> Key for (ty::Predicate<'tcx>, traits::WellFormedLoc) {
496 fn query_crate_is_local(&self) -> bool {
500 fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
505 impl<'tcx> Key for (ty::PolyFnSig<'tcx>, &'tcx ty::List<Ty<'tcx>>) {
507 fn query_crate_is_local(&self) -> bool {
511 fn default_span(&self, _: TyCtxt<'_>) -> Span {
516 impl<'tcx> Key for (ty::Instance<'tcx>, &'tcx ty::List<Ty<'tcx>>) {
518 fn query_crate_is_local(&self) -> bool {
522 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
523 self.0.default_span(tcx)
527 impl<'tcx> Key for (Ty<'tcx>, ty::ValTree<'tcx>) {
529 fn query_crate_is_local(&self) -> bool {
533 fn default_span(&self, _: TyCtxt<'_>) -> Span {