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