]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_query_impl/src/keys.rs
Auto merge of #85090 - Aaron1011:type-outlives-global, r=matthewjasper,jackh726
[rust.git] / compiler / rustc_query_impl / src / keys.rs
1 //! Defines the set of legal keys that can be used in queries.
2
3 use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, LOCAL_CRATE};
4 use rustc_middle::infer::canonical::Canonical;
5 use rustc_middle::mir;
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};
11
12 /// The `Key` trait controls what types can legally be used as the key
13 /// for a query.
14 pub trait 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;
18
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;
22 }
23
24 impl Key for () {
25     #[inline(always)]
26     fn query_crate_is_local(&self) -> bool {
27         true
28     }
29
30     fn default_span(&self, _: TyCtxt<'_>) -> Span {
31         DUMMY_SP
32     }
33 }
34
35 impl<'tcx> Key for ty::InstanceDef<'tcx> {
36     #[inline(always)]
37     fn query_crate_is_local(&self) -> bool {
38         true
39     }
40
41     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
42         tcx.def_span(self.def_id())
43     }
44 }
45
46 impl<'tcx> Key for ty::Instance<'tcx> {
47     #[inline(always)]
48     fn query_crate_is_local(&self) -> bool {
49         true
50     }
51
52     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
53         tcx.def_span(self.def_id())
54     }
55 }
56
57 impl<'tcx> Key for mir::interpret::GlobalId<'tcx> {
58     #[inline(always)]
59     fn query_crate_is_local(&self) -> bool {
60         true
61     }
62
63     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
64         self.instance.default_span(tcx)
65     }
66 }
67
68 impl<'tcx> Key for mir::interpret::LitToConstInput<'tcx> {
69     #[inline(always)]
70     fn query_crate_is_local(&self) -> bool {
71         true
72     }
73
74     fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
75         DUMMY_SP
76     }
77 }
78
79 impl Key for CrateNum {
80     #[inline(always)]
81     fn query_crate_is_local(&self) -> bool {
82         *self == LOCAL_CRATE
83     }
84     fn default_span(&self, _: TyCtxt<'_>) -> Span {
85         DUMMY_SP
86     }
87 }
88
89 impl Key for LocalDefId {
90     #[inline(always)]
91     fn query_crate_is_local(&self) -> bool {
92         true
93     }
94     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
95         self.to_def_id().default_span(tcx)
96     }
97 }
98
99 impl Key for DefId {
100     #[inline(always)]
101     fn query_crate_is_local(&self) -> bool {
102         self.krate == LOCAL_CRATE
103     }
104     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
105         tcx.def_span(*self)
106     }
107 }
108
109 impl Key for ty::WithOptConstParam<LocalDefId> {
110     #[inline(always)]
111     fn query_crate_is_local(&self) -> bool {
112         true
113     }
114     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
115         self.did.default_span(tcx)
116     }
117 }
118
119 impl Key for (DefId, DefId) {
120     #[inline(always)]
121     fn query_crate_is_local(&self) -> bool {
122         self.0.krate == LOCAL_CRATE
123     }
124     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
125         self.1.default_span(tcx)
126     }
127 }
128
129 impl Key for (ty::Instance<'tcx>, LocalDefId) {
130     #[inline(always)]
131     fn query_crate_is_local(&self) -> bool {
132         true
133     }
134     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
135         self.0.default_span(tcx)
136     }
137 }
138
139 impl Key for (DefId, LocalDefId) {
140     #[inline(always)]
141     fn query_crate_is_local(&self) -> bool {
142         self.0.krate == LOCAL_CRATE
143     }
144     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
145         self.1.default_span(tcx)
146     }
147 }
148
149 impl Key for (LocalDefId, DefId) {
150     #[inline(always)]
151     fn query_crate_is_local(&self) -> bool {
152         true
153     }
154     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
155         self.0.default_span(tcx)
156     }
157 }
158
159 impl Key for (DefId, Option<Ident>) {
160     #[inline(always)]
161     fn query_crate_is_local(&self) -> bool {
162         self.0.krate == LOCAL_CRATE
163     }
164     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
165         tcx.def_span(self.0)
166     }
167 }
168
169 impl Key for (DefId, LocalDefId, Ident) {
170     #[inline(always)]
171     fn query_crate_is_local(&self) -> bool {
172         self.0.krate == LOCAL_CRATE
173     }
174     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
175         self.1.default_span(tcx)
176     }
177 }
178
179 impl Key for (CrateNum, DefId) {
180     #[inline(always)]
181     fn query_crate_is_local(&self) -> bool {
182         self.0 == LOCAL_CRATE
183     }
184     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
185         self.1.default_span(tcx)
186     }
187 }
188
189 impl Key for (DefId, SimplifiedType) {
190     #[inline(always)]
191     fn query_crate_is_local(&self) -> bool {
192         self.0.krate == LOCAL_CRATE
193     }
194     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
195         self.0.default_span(tcx)
196     }
197 }
198
199 impl<'tcx> Key for SubstsRef<'tcx> {
200     #[inline(always)]
201     fn query_crate_is_local(&self) -> bool {
202         true
203     }
204     fn default_span(&self, _: TyCtxt<'_>) -> Span {
205         DUMMY_SP
206     }
207 }
208
209 impl<'tcx> Key for (DefId, SubstsRef<'tcx>) {
210     #[inline(always)]
211     fn query_crate_is_local(&self) -> bool {
212         self.0.krate == LOCAL_CRATE
213     }
214     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
215         self.0.default_span(tcx)
216     }
217 }
218
219 impl<'tcx> Key
220     for (
221         (ty::WithOptConstParam<DefId>, SubstsRef<'tcx>),
222         (ty::WithOptConstParam<DefId>, SubstsRef<'tcx>),
223     )
224 {
225     #[inline(always)]
226     fn query_crate_is_local(&self) -> bool {
227         (self.0).0.did.krate == LOCAL_CRATE
228     }
229     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
230         (self.0).0.did.default_span(tcx)
231     }
232 }
233
234 impl<'tcx> Key for (LocalDefId, DefId, SubstsRef<'tcx>) {
235     #[inline(always)]
236     fn query_crate_is_local(&self) -> bool {
237         true
238     }
239     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
240         self.0.default_span(tcx)
241     }
242 }
243
244 impl<'tcx> Key for (ty::ParamEnv<'tcx>, ty::PolyTraitRef<'tcx>) {
245     #[inline(always)]
246     fn query_crate_is_local(&self) -> bool {
247         self.1.def_id().krate == LOCAL_CRATE
248     }
249     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
250         tcx.def_span(self.1.def_id())
251     }
252 }
253
254 impl<'tcx> Key for (&'tcx ty::Const<'tcx>, mir::Field) {
255     #[inline(always)]
256     fn query_crate_is_local(&self) -> bool {
257         true
258     }
259     fn default_span(&self, _: TyCtxt<'_>) -> Span {
260         DUMMY_SP
261     }
262 }
263
264 impl<'tcx> Key for mir::interpret::ConstAlloc<'tcx> {
265     #[inline(always)]
266     fn query_crate_is_local(&self) -> bool {
267         true
268     }
269     fn default_span(&self, _: TyCtxt<'_>) -> Span {
270         DUMMY_SP
271     }
272 }
273
274 impl<'tcx> Key for ty::PolyTraitRef<'tcx> {
275     #[inline(always)]
276     fn query_crate_is_local(&self) -> bool {
277         self.def_id().krate == LOCAL_CRATE
278     }
279     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
280         tcx.def_span(self.def_id())
281     }
282 }
283
284 impl<'tcx> Key for GenericArg<'tcx> {
285     #[inline(always)]
286     fn query_crate_is_local(&self) -> bool {
287         true
288     }
289     fn default_span(&self, _: TyCtxt<'_>) -> Span {
290         DUMMY_SP
291     }
292 }
293
294 impl<'tcx> Key for mir::ConstantKind<'tcx> {
295     #[inline(always)]
296     fn query_crate_is_local(&self) -> bool {
297         true
298     }
299     fn default_span(&self, _: TyCtxt<'_>) -> Span {
300         DUMMY_SP
301     }
302 }
303
304 impl<'tcx> Key for &'tcx ty::Const<'tcx> {
305     #[inline(always)]
306     fn query_crate_is_local(&self) -> bool {
307         true
308     }
309     fn default_span(&self, _: TyCtxt<'_>) -> Span {
310         DUMMY_SP
311     }
312 }
313
314 impl<'tcx> Key for Ty<'tcx> {
315     #[inline(always)]
316     fn query_crate_is_local(&self) -> bool {
317         true
318     }
319     fn default_span(&self, _: TyCtxt<'_>) -> Span {
320         DUMMY_SP
321     }
322 }
323
324 impl<'tcx> Key for &'tcx ty::List<ty::Predicate<'tcx>> {
325     #[inline(always)]
326     fn query_crate_is_local(&self) -> bool {
327         true
328     }
329     fn default_span(&self, _: TyCtxt<'_>) -> Span {
330         DUMMY_SP
331     }
332 }
333
334 impl<'tcx> Key for ty::ParamEnv<'tcx> {
335     #[inline(always)]
336     fn query_crate_is_local(&self) -> bool {
337         true
338     }
339     fn default_span(&self, _: TyCtxt<'_>) -> Span {
340         DUMMY_SP
341     }
342 }
343
344 impl<'tcx, T: Key> Key for ty::ParamEnvAnd<'tcx, T> {
345     #[inline(always)]
346     fn query_crate_is_local(&self) -> bool {
347         self.value.query_crate_is_local()
348     }
349     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
350         self.value.default_span(tcx)
351     }
352 }
353
354 impl Key for Symbol {
355     #[inline(always)]
356     fn query_crate_is_local(&self) -> bool {
357         true
358     }
359     fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
360         DUMMY_SP
361     }
362 }
363
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> {
367     #[inline(always)]
368     fn query_crate_is_local(&self) -> bool {
369         true
370     }
371
372     fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
373         DUMMY_SP
374     }
375 }
376
377 impl Key for (Symbol, u32, u32) {
378     #[inline(always)]
379     fn query_crate_is_local(&self) -> bool {
380         true
381     }
382
383     fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
384         DUMMY_SP
385     }
386 }
387
388 impl<'tcx> Key for (DefId, Ty<'tcx>, SubstsRef<'tcx>, ty::ParamEnv<'tcx>) {
389     #[inline(always)]
390     fn query_crate_is_local(&self) -> bool {
391         true
392     }
393
394     fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
395         DUMMY_SP
396     }
397 }