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;
27 fn query_crate_is_local(&self) -> bool {
31 fn default_span(&self, _: TyCtxt<'_>) -> Span {
36 impl<'tcx> Key for ty::InstanceDef<'tcx> {
38 fn query_crate_is_local(&self) -> bool {
42 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
43 tcx.def_span(self.def_id())
47 impl<'tcx> Key for ty::Instance<'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 mir::interpret::GlobalId<'tcx> {
60 fn query_crate_is_local(&self) -> bool {
64 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
65 self.instance.default_span(tcx)
69 impl<'tcx> Key for mir::interpret::LitToConstInput<'tcx> {
71 fn query_crate_is_local(&self) -> bool {
75 fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
80 impl Key for CrateNum {
82 fn query_crate_is_local(&self) -> bool {
85 fn default_span(&self, _: TyCtxt<'_>) -> Span {
90 impl Key for LocalDefId {
92 fn query_crate_is_local(&self) -> bool {
95 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
96 self.to_def_id().default_span(tcx)
102 fn query_crate_is_local(&self) -> bool {
103 self.krate == LOCAL_CRATE
105 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
110 impl Key for ty::WithOptConstParam<LocalDefId> {
112 fn query_crate_is_local(&self) -> bool {
115 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
116 self.did.default_span(tcx)
120 impl Key for (DefId, DefId) {
122 fn query_crate_is_local(&self) -> bool {
123 self.0.krate == LOCAL_CRATE
125 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
126 self.1.default_span(tcx)
130 impl Key for (ty::Instance<'tcx>, LocalDefId) {
132 fn query_crate_is_local(&self) -> bool {
135 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
136 self.0.default_span(tcx)
140 impl Key for (DefId, LocalDefId) {
142 fn query_crate_is_local(&self) -> bool {
143 self.0.krate == LOCAL_CRATE
145 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
146 self.1.default_span(tcx)
150 impl Key for (LocalDefId, DefId) {
152 fn query_crate_is_local(&self) -> bool {
155 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
156 self.0.default_span(tcx)
160 impl Key for (DefId, Option<Ident>) {
162 fn query_crate_is_local(&self) -> bool {
163 self.0.krate == LOCAL_CRATE
165 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
170 impl Key for (DefId, LocalDefId, Ident) {
172 fn query_crate_is_local(&self) -> bool {
173 self.0.krate == LOCAL_CRATE
175 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
176 self.1.default_span(tcx)
180 impl Key for (CrateNum, DefId) {
182 fn query_crate_is_local(&self) -> bool {
183 self.0 == LOCAL_CRATE
185 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
186 self.1.default_span(tcx)
190 impl Key for (DefId, SimplifiedType) {
192 fn query_crate_is_local(&self) -> bool {
193 self.0.krate == LOCAL_CRATE
195 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
196 self.0.default_span(tcx)
200 impl<'tcx> Key for SubstsRef<'tcx> {
202 fn query_crate_is_local(&self) -> bool {
205 fn default_span(&self, _: TyCtxt<'_>) -> Span {
210 impl<'tcx> Key for (DefId, SubstsRef<'tcx>) {
212 fn query_crate_is_local(&self) -> bool {
213 self.0.krate == LOCAL_CRATE
215 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
216 self.0.default_span(tcx)
222 (ty::WithOptConstParam<DefId>, SubstsRef<'tcx>),
223 (ty::WithOptConstParam<DefId>, SubstsRef<'tcx>),
227 fn query_crate_is_local(&self) -> bool {
228 (self.0).0.did.krate == LOCAL_CRATE
230 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
231 (self.0).0.did.default_span(tcx)
235 impl<'tcx> Key for (LocalDefId, DefId, SubstsRef<'tcx>) {
237 fn query_crate_is_local(&self) -> bool {
240 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
241 self.0.default_span(tcx)
245 impl<'tcx> Key for (ty::ParamEnv<'tcx>, ty::PolyTraitRef<'tcx>) {
247 fn query_crate_is_local(&self) -> bool {
248 self.1.def_id().krate == LOCAL_CRATE
250 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
251 tcx.def_span(self.1.def_id())
255 impl<'tcx> Key for (&'tcx ty::Const<'tcx>, mir::Field) {
257 fn query_crate_is_local(&self) -> bool {
260 fn default_span(&self, _: TyCtxt<'_>) -> Span {
265 impl<'tcx> Key for mir::interpret::ConstAlloc<'tcx> {
267 fn query_crate_is_local(&self) -> bool {
270 fn default_span(&self, _: TyCtxt<'_>) -> Span {
275 impl<'tcx> Key for ty::PolyTraitRef<'tcx> {
277 fn query_crate_is_local(&self) -> bool {
278 self.def_id().krate == LOCAL_CRATE
280 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
281 tcx.def_span(self.def_id())
285 impl<'tcx> Key for GenericArg<'tcx> {
287 fn query_crate_is_local(&self) -> bool {
290 fn default_span(&self, _: TyCtxt<'_>) -> Span {
295 impl<'tcx> Key for mir::ConstantKind<'tcx> {
297 fn query_crate_is_local(&self) -> bool {
300 fn default_span(&self, _: TyCtxt<'_>) -> Span {
305 impl<'tcx> Key for &'tcx ty::Const<'tcx> {
307 fn query_crate_is_local(&self) -> bool {
310 fn default_span(&self, _: TyCtxt<'_>) -> Span {
315 impl<'tcx> Key for Ty<'tcx> {
317 fn query_crate_is_local(&self) -> bool {
320 fn default_span(&self, _: TyCtxt<'_>) -> Span {
325 impl<'tcx> Key for &'tcx ty::List<ty::Predicate<'tcx>> {
327 fn query_crate_is_local(&self) -> bool {
330 fn default_span(&self, _: TyCtxt<'_>) -> Span {
335 impl<'tcx> Key for ty::ParamEnv<'tcx> {
337 fn query_crate_is_local(&self) -> bool {
340 fn default_span(&self, _: TyCtxt<'_>) -> Span {
345 impl<'tcx, T: Key> Key for ty::ParamEnvAnd<'tcx, T> {
347 fn query_crate_is_local(&self) -> bool {
348 self.value.query_crate_is_local()
350 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
351 self.value.default_span(tcx)
355 impl Key for Symbol {
357 fn query_crate_is_local(&self) -> bool {
360 fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
365 /// Canonical query goals correspond to abstract trait operations that
366 /// are not tied to any crate in particular.
367 impl<'tcx, T> Key for Canonical<'tcx, T> {
369 fn query_crate_is_local(&self) -> bool {
373 fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
378 impl Key for (Symbol, u32, u32) {
380 fn query_crate_is_local(&self) -> bool {
384 fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
389 impl<'tcx> Key for (DefId, Ty<'tcx>, SubstsRef<'tcx>, ty::ParamEnv<'tcx>) {
391 fn query_crate_is_local(&self) -> bool {
395 fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
400 impl<'tcx> Key for (ty::Predicate<'tcx>, traits::WellFormedLoc) {
402 fn query_crate_is_local(&self) -> bool {
406 fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {