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::ty::fast_reject::SimplifiedType;
7 use rustc_middle::ty::subst::{GenericArg, SubstsRef};
8 use rustc_middle::ty::{self, Ty, TyCtxt};
9 use rustc_span::symbol::{Ident, Symbol};
10 use rustc_span::{Span, DUMMY_SP};
12 /// The `Key` trait controls what types can legally be used as the key
15 /// Given an instance of this key, what crate is it referring to?
16 /// This is used to find the provider.
17 fn query_crate_is_local(&self) -> bool;
19 /// In the event that a cycle occurs, if no explicit span has been
20 /// given for a query with key `self`, what span should we use?
21 fn default_span(&self, tcx: TyCtxt<'_>) -> Span;
26 fn query_crate_is_local(&self) -> bool {
30 fn default_span(&self, _: TyCtxt<'_>) -> Span {
35 impl<'tcx> Key for ty::InstanceDef<'tcx> {
37 fn query_crate_is_local(&self) -> bool {
41 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
42 tcx.def_span(self.def_id())
46 impl<'tcx> Key for ty::Instance<'tcx> {
48 fn query_crate_is_local(&self) -> bool {
52 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
53 tcx.def_span(self.def_id())
57 impl<'tcx> Key for mir::interpret::GlobalId<'tcx> {
59 fn query_crate_is_local(&self) -> bool {
63 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
64 self.instance.default_span(tcx)
68 impl<'tcx> Key for mir::interpret::LitToConstInput<'tcx> {
70 fn query_crate_is_local(&self) -> bool {
74 fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
79 impl Key for CrateNum {
81 fn query_crate_is_local(&self) -> bool {
84 fn default_span(&self, _: TyCtxt<'_>) -> Span {
89 impl Key for LocalDefId {
91 fn query_crate_is_local(&self) -> bool {
94 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
95 self.to_def_id().default_span(tcx)
101 fn query_crate_is_local(&self) -> bool {
102 self.krate == LOCAL_CRATE
104 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
109 impl Key for ty::WithOptConstParam<LocalDefId> {
111 fn query_crate_is_local(&self) -> bool {
114 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
115 self.did.default_span(tcx)
119 impl Key for (DefId, DefId) {
121 fn query_crate_is_local(&self) -> bool {
122 self.0.krate == LOCAL_CRATE
124 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
125 self.1.default_span(tcx)
129 impl Key for (ty::Instance<'tcx>, LocalDefId) {
131 fn query_crate_is_local(&self) -> bool {
134 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
135 self.0.default_span(tcx)
139 impl Key for (DefId, LocalDefId) {
141 fn query_crate_is_local(&self) -> bool {
142 self.0.krate == LOCAL_CRATE
144 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
145 self.1.default_span(tcx)
149 impl Key for (LocalDefId, DefId) {
151 fn query_crate_is_local(&self) -> bool {
154 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
155 self.0.default_span(tcx)
159 impl Key for (DefId, Option<Ident>) {
161 fn query_crate_is_local(&self) -> bool {
162 self.0.krate == LOCAL_CRATE
164 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
169 impl Key for (DefId, LocalDefId, Ident) {
171 fn query_crate_is_local(&self) -> bool {
172 self.0.krate == LOCAL_CRATE
174 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
175 self.1.default_span(tcx)
179 impl Key for (CrateNum, DefId) {
181 fn query_crate_is_local(&self) -> bool {
182 self.0 == LOCAL_CRATE
184 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
185 self.1.default_span(tcx)
189 impl Key for (DefId, SimplifiedType) {
191 fn query_crate_is_local(&self) -> bool {
192 self.0.krate == LOCAL_CRATE
194 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
195 self.0.default_span(tcx)
199 impl<'tcx> Key for SubstsRef<'tcx> {
201 fn query_crate_is_local(&self) -> bool {
204 fn default_span(&self, _: TyCtxt<'_>) -> Span {
209 impl<'tcx> Key for (DefId, SubstsRef<'tcx>) {
211 fn query_crate_is_local(&self) -> bool {
212 self.0.krate == LOCAL_CRATE
214 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
215 self.0.default_span(tcx)
221 (ty::WithOptConstParam<DefId>, SubstsRef<'tcx>),
222 (ty::WithOptConstParam<DefId>, SubstsRef<'tcx>),
226 fn query_crate_is_local(&self) -> bool {
227 (self.0).0.did.krate == LOCAL_CRATE
229 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
230 (self.0).0.did.default_span(tcx)
234 impl<'tcx> Key for (LocalDefId, DefId, SubstsRef<'tcx>) {
236 fn query_crate_is_local(&self) -> bool {
239 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
240 self.0.default_span(tcx)
244 impl<'tcx> Key for (ty::ParamEnv<'tcx>, ty::PolyTraitRef<'tcx>) {
246 fn query_crate_is_local(&self) -> bool {
247 self.1.def_id().krate == LOCAL_CRATE
249 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
250 tcx.def_span(self.1.def_id())
254 impl<'tcx> Key for (&'tcx ty::Const<'tcx>, mir::Field) {
256 fn query_crate_is_local(&self) -> bool {
259 fn default_span(&self, _: TyCtxt<'_>) -> Span {
264 impl<'tcx> Key for mir::interpret::ConstAlloc<'tcx> {
266 fn query_crate_is_local(&self) -> bool {
269 fn default_span(&self, _: TyCtxt<'_>) -> Span {
274 impl<'tcx> Key for ty::PolyTraitRef<'tcx> {
276 fn query_crate_is_local(&self) -> bool {
277 self.def_id().krate == LOCAL_CRATE
279 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
280 tcx.def_span(self.def_id())
284 impl<'tcx> Key for GenericArg<'tcx> {
286 fn query_crate_is_local(&self) -> bool {
289 fn default_span(&self, _: TyCtxt<'_>) -> Span {
294 impl<'tcx> Key for mir::ConstantKind<'tcx> {
296 fn query_crate_is_local(&self) -> bool {
299 fn default_span(&self, _: TyCtxt<'_>) -> Span {
304 impl<'tcx> Key for &'tcx ty::Const<'tcx> {
306 fn query_crate_is_local(&self) -> bool {
309 fn default_span(&self, _: TyCtxt<'_>) -> Span {
314 impl<'tcx> Key for Ty<'tcx> {
316 fn query_crate_is_local(&self) -> bool {
319 fn default_span(&self, _: TyCtxt<'_>) -> Span {
324 impl<'tcx> Key for &'tcx ty::List<ty::Predicate<'tcx>> {
326 fn query_crate_is_local(&self) -> bool {
329 fn default_span(&self, _: TyCtxt<'_>) -> Span {
334 impl<'tcx> Key for ty::ParamEnv<'tcx> {
336 fn query_crate_is_local(&self) -> bool {
339 fn default_span(&self, _: TyCtxt<'_>) -> Span {
344 impl<'tcx, T: Key> Key for ty::ParamEnvAnd<'tcx, T> {
346 fn query_crate_is_local(&self) -> bool {
347 self.value.query_crate_is_local()
349 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
350 self.value.default_span(tcx)
354 impl Key for Symbol {
356 fn query_crate_is_local(&self) -> bool {
359 fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
364 /// Canonical query goals correspond to abstract trait operations that
365 /// are not tied to any crate in particular.
366 impl<'tcx, T> Key for Canonical<'tcx, T> {
368 fn query_crate_is_local(&self) -> bool {
372 fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
377 impl Key for (Symbol, u32, u32) {
379 fn query_crate_is_local(&self) -> bool {
383 fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
388 impl<'tcx> Key for (DefId, Ty<'tcx>, SubstsRef<'tcx>, ty::ParamEnv<'tcx>) {
390 fn query_crate_is_local(&self) -> bool {
394 fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {