1use std::ffi::OsStr;
4use std::fmt::Debug;
5use std::hash::Hash;
6
7use rustc_ast::tokenstream::TokenStream;
8use rustc_data_structures::stable_hasher::HashStable;
9use rustc_hir::def_id::{CrateNum, DefId, LOCAL_CRATE, LocalDefId, LocalModDefId};
10use rustc_hir::hir_id::OwnerId;
11use rustc_span::{DUMMY_SP, Ident, LocalExpnId, Span, Symbol};
12
13use crate::dep_graph::DepNodeIndex;
14use crate::ich::StableHashingContext;
15use crate::infer::canonical::CanonicalQueryInput;
16use crate::mir::mono::CollectionMode;
17use crate::query::{DefIdCache, DefaultCache, SingleCache, VecCache};
18use crate::ty::fast_reject::SimplifiedType;
19use crate::ty::layout::ValidityRequirement;
20use crate::ty::{self, GenericArg, GenericArgsRef, Ty, TyCtxt};
21use crate::{mir, traits};
22
23#[derive(#[automatically_derived]
impl ::core::marker::Copy for LocalCrate { }Copy, #[automatically_derived]
impl ::core::clone::Clone for LocalCrate {
#[inline]
fn clone(&self) -> LocalCrate { *self }
}Clone, #[automatically_derived]
impl ::core::fmt::Debug for LocalCrate {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::write_str(f, "LocalCrate")
}
}Debug)]
25pub struct LocalCrate;
26
27pub trait QueryKeyBounds = Copy + Debug + Eq + Hash + for<'a> HashStable<StableHashingContext<'a>>;
28
29pub trait QueryKey: Sized + QueryKeyBounds {
31 type Cache<V> = DefaultCache<Self, V>;
38
39 fn default_span(&self, tcx: TyCtxt<'_>) -> Span;
42
43 fn key_as_def_id(&self) -> Option<DefId> {
46 None
47 }
48}
49
50pub trait AsLocalQueryKey: QueryKey {
51 type LocalQueryKey;
52
53 fn as_local_key(&self) -> Option<Self::LocalQueryKey>;
56}
57
58impl QueryKey for () {
59 type Cache<V> = SingleCache<V>;
60
61 fn default_span(&self, _: TyCtxt<'_>) -> Span {
62 DUMMY_SP
63 }
64}
65
66impl<'tcx> QueryKey for ty::InstanceKind<'tcx> {
67 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
68 tcx.def_span(self.def_id())
69 }
70}
71
72impl<'tcx> QueryKey for ty::Instance<'tcx> {
73 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
74 tcx.def_span(self.def_id())
75 }
76}
77
78impl<'tcx> QueryKey for mir::interpret::GlobalId<'tcx> {
79 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
80 self.instance.default_span(tcx)
81 }
82}
83
84impl<'tcx> QueryKey for (Ty<'tcx>, Option<ty::ExistentialTraitRef<'tcx>>) {
85 fn default_span(&self, _: TyCtxt<'_>) -> Span {
86 DUMMY_SP
87 }
88}
89
90impl<'tcx> QueryKey for ty::LitToConstInput<'tcx> {
91 fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
92 DUMMY_SP
93 }
94}
95
96impl QueryKey for CrateNum {
97 type Cache<V> = VecCache<Self, V, DepNodeIndex>;
98
99 fn default_span(&self, _: TyCtxt<'_>) -> Span {
100 DUMMY_SP
101 }
102}
103
104impl AsLocalQueryKey for CrateNum {
105 type LocalQueryKey = LocalCrate;
106
107 #[inline(always)]
108 fn as_local_key(&self) -> Option<Self::LocalQueryKey> {
109 (*self == LOCAL_CRATE).then_some(LocalCrate)
110 }
111}
112
113impl QueryKey for OwnerId {
114 type Cache<V> = VecCache<Self, V, DepNodeIndex>;
115
116 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
117 self.to_def_id().default_span(tcx)
118 }
119
120 fn key_as_def_id(&self) -> Option<DefId> {
121 Some(self.to_def_id())
122 }
123}
124
125impl QueryKey for LocalDefId {
126 type Cache<V> = VecCache<Self, V, DepNodeIndex>;
127
128 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
129 self.to_def_id().default_span(tcx)
130 }
131
132 fn key_as_def_id(&self) -> Option<DefId> {
133 Some(self.to_def_id())
134 }
135}
136
137impl QueryKey for DefId {
138 type Cache<V> = DefIdCache<V>;
139
140 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
141 tcx.def_span(*self)
142 }
143
144 #[inline(always)]
145 fn key_as_def_id(&self) -> Option<DefId> {
146 Some(*self)
147 }
148}
149
150impl AsLocalQueryKey for DefId {
151 type LocalQueryKey = LocalDefId;
152
153 #[inline(always)]
154 fn as_local_key(&self) -> Option<Self::LocalQueryKey> {
155 self.as_local()
156 }
157}
158
159impl QueryKey for LocalModDefId {
160 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
161 tcx.def_span(*self)
162 }
163
164 #[inline(always)]
165 fn key_as_def_id(&self) -> Option<DefId> {
166 Some(self.to_def_id())
167 }
168}
169
170impl QueryKey for SimplifiedType {
171 fn default_span(&self, _: TyCtxt<'_>) -> Span {
172 DUMMY_SP
173 }
174}
175
176impl QueryKey for (DefId, DefId) {
177 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
178 self.1.default_span(tcx)
179 }
180}
181
182impl QueryKey for (DefId, Ident) {
183 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
184 tcx.def_span(self.0)
185 }
186
187 #[inline(always)]
188 fn key_as_def_id(&self) -> Option<DefId> {
189 Some(self.0)
190 }
191}
192
193impl QueryKey for (LocalDefId, LocalDefId, Ident) {
194 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
195 self.1.default_span(tcx)
196 }
197}
198
199impl QueryKey for (CrateNum, DefId) {
200 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
201 self.1.default_span(tcx)
202 }
203}
204
205impl AsLocalQueryKey for (CrateNum, DefId) {
206 type LocalQueryKey = DefId;
207
208 #[inline(always)]
209 fn as_local_key(&self) -> Option<Self::LocalQueryKey> {
210 (self.0 == LOCAL_CRATE).then(|| self.1)
211 }
212}
213
214impl QueryKey for (CrateNum, SimplifiedType) {
215 fn default_span(&self, _: TyCtxt<'_>) -> Span {
216 DUMMY_SP
217 }
218}
219
220impl AsLocalQueryKey for (CrateNum, SimplifiedType) {
221 type LocalQueryKey = SimplifiedType;
222
223 #[inline(always)]
224 fn as_local_key(&self) -> Option<Self::LocalQueryKey> {
225 (self.0 == LOCAL_CRATE).then(|| self.1)
226 }
227}
228
229impl QueryKey for (DefId, ty::SizedTraitKind) {
230 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
231 self.0.default_span(tcx)
232 }
233}
234
235impl<'tcx> QueryKey for GenericArgsRef<'tcx> {
236 fn default_span(&self, _: TyCtxt<'_>) -> Span {
237 DUMMY_SP
238 }
239}
240
241impl<'tcx> QueryKey for (DefId, GenericArgsRef<'tcx>) {
242 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
243 self.0.default_span(tcx)
244 }
245}
246
247impl<'tcx> QueryKey for ty::TraitRef<'tcx> {
248 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
249 tcx.def_span(self.def_id)
250 }
251}
252
253impl<'tcx> QueryKey for GenericArg<'tcx> {
254 fn default_span(&self, _: TyCtxt<'_>) -> Span {
255 DUMMY_SP
256 }
257}
258
259impl<'tcx> QueryKey for Ty<'tcx> {
260 fn default_span(&self, _: TyCtxt<'_>) -> Span {
261 DUMMY_SP
262 }
263}
264
265impl<'tcx> QueryKey for (Ty<'tcx>, Ty<'tcx>) {
266 fn default_span(&self, _: TyCtxt<'_>) -> Span {
267 DUMMY_SP
268 }
269}
270
271impl<'tcx> QueryKey for ty::Clauses<'tcx> {
272 fn default_span(&self, _: TyCtxt<'_>) -> Span {
273 DUMMY_SP
274 }
275}
276
277impl<'tcx, T: QueryKey> QueryKey for ty::PseudoCanonicalInput<'tcx, T> {
278 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
279 self.value.default_span(tcx)
280 }
281}
282
283impl QueryKey for Symbol {
284 fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
285 DUMMY_SP
286 }
287}
288
289impl QueryKey for Option<Symbol> {
290 fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
291 DUMMY_SP
292 }
293}
294
295impl<'tcx> QueryKey for &'tcx OsStr {
296 fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
297 DUMMY_SP
298 }
299}
300
301impl<'tcx, T: QueryKeyBounds> QueryKey for CanonicalQueryInput<'tcx, T> {
304 fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
305 DUMMY_SP
306 }
307}
308
309impl<'tcx, T: QueryKeyBounds> QueryKey for (CanonicalQueryInput<'tcx, T>, bool) {
310 fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
311 DUMMY_SP
312 }
313}
314
315impl<'tcx> QueryKey for (Ty<'tcx>, rustc_abi::VariantIdx) {
316 fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
317 DUMMY_SP
318 }
319}
320
321impl<'tcx> QueryKey for (ty::Predicate<'tcx>, traits::WellFormedLoc) {
322 fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
323 DUMMY_SP
324 }
325}
326
327impl<'tcx> QueryKey for (ty::PolyFnSig<'tcx>, &'tcx ty::List<Ty<'tcx>>) {
328 fn default_span(&self, _: TyCtxt<'_>) -> Span {
329 DUMMY_SP
330 }
331}
332
333impl<'tcx> QueryKey for (ty::Instance<'tcx>, &'tcx ty::List<Ty<'tcx>>) {
334 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
335 self.0.default_span(tcx)
336 }
337}
338
339impl<'tcx> QueryKey for ty::Value<'tcx> {
340 fn default_span(&self, _: TyCtxt<'_>) -> Span {
341 DUMMY_SP
342 }
343}
344
345impl<'tcx> QueryKey for (LocalExpnId, &'tcx TokenStream) {
346 fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
347 self.0.expn_data().call_site
348 }
349}
350
351impl<'tcx> QueryKey for (ValidityRequirement, ty::PseudoCanonicalInput<'tcx, Ty<'tcx>>) {
352 fn default_span(&self, _: TyCtxt<'_>) -> Span {
355 DUMMY_SP
356 }
357}
358
359impl<'tcx> QueryKey for (ty::Instance<'tcx>, CollectionMode) {
360 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
361 self.0.default_span(tcx)
362 }
363}