]> git.lizzy.rs Git - rust.git/blob - src/librustc_middle/ty/query/keys.rs
improve naming
[rust.git] / src / librustc_middle / ty / query / keys.rs
1 //! Defines the set of legal keys that can be used in queries.
2
3 use crate::infer::canonical::Canonical;
4 use crate::mir;
5 use crate::ty::fast_reject::SimplifiedType;
6 use crate::ty::subst::{GenericArg, SubstsRef};
7 use crate::ty::{self, Ty, TyCtxt};
8 use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, LOCAL_CRATE};
9 use rustc_query_system::query::DefaultCacheSelector;
10 use rustc_span::symbol::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     type CacheSelector;
17
18     /// Given an instance of this key, what crate is it referring to?
19     /// This is used to find the provider.
20     fn query_crate(&self) -> CrateNum;
21
22     /// In the event that a cycle occurs, if no explicit span has been
23     /// given for a query with key `self`, what span should we use?
24     fn default_span(&self, tcx: TyCtxt<'_>) -> Span;
25 }
26
27 impl<'tcx> Key for ty::InstanceDef<'tcx> {
28     type CacheSelector = DefaultCacheSelector;
29
30     fn query_crate(&self) -> CrateNum {
31         LOCAL_CRATE
32     }
33
34     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
35         tcx.def_span(self.def_id())
36     }
37 }
38
39 impl<'tcx> Key for ty::Instance<'tcx> {
40     type CacheSelector = DefaultCacheSelector;
41
42     fn query_crate(&self) -> CrateNum {
43         LOCAL_CRATE
44     }
45
46     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
47         tcx.def_span(self.def_id())
48     }
49 }
50
51 impl<'tcx> Key for mir::interpret::GlobalId<'tcx> {
52     type CacheSelector = DefaultCacheSelector;
53
54     fn query_crate(&self) -> CrateNum {
55         self.instance.query_crate()
56     }
57
58     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
59         self.instance.default_span(tcx)
60     }
61 }
62
63 impl<'tcx> Key for mir::interpret::LitToConstInput<'tcx> {
64     type CacheSelector = DefaultCacheSelector;
65
66     fn query_crate(&self) -> CrateNum {
67         LOCAL_CRATE
68     }
69
70     fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
71         DUMMY_SP
72     }
73 }
74
75 impl Key for CrateNum {
76     type CacheSelector = DefaultCacheSelector;
77
78     fn query_crate(&self) -> CrateNum {
79         *self
80     }
81     fn default_span(&self, _: TyCtxt<'_>) -> Span {
82         DUMMY_SP
83     }
84 }
85
86 impl Key for LocalDefId {
87     type CacheSelector = DefaultCacheSelector;
88
89     fn query_crate(&self) -> CrateNum {
90         self.to_def_id().query_crate()
91     }
92     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
93         self.to_def_id().default_span(tcx)
94     }
95 }
96
97 impl Key for DefId {
98     type CacheSelector = DefaultCacheSelector;
99
100     fn query_crate(&self) -> CrateNum {
101         self.krate
102     }
103     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
104         tcx.def_span(*self)
105     }
106 }
107
108 impl Key for ty::WithOptConstParam<LocalDefId> {
109     type CacheSelector = DefaultCacheSelector;
110
111     fn query_crate(&self) -> CrateNum {
112         self.did.query_crate()
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     type CacheSelector = DefaultCacheSelector;
121
122     fn query_crate(&self) -> CrateNum {
123         self.0.krate
124     }
125     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
126         self.1.default_span(tcx)
127     }
128 }
129
130 impl Key for (DefId, LocalDefId) {
131     type CacheSelector = DefaultCacheSelector;
132
133     fn query_crate(&self) -> CrateNum {
134         self.0.krate
135     }
136     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
137         self.1.default_span(tcx)
138     }
139 }
140
141 impl Key for (LocalDefId, DefId) {
142     type CacheSelector = DefaultCacheSelector;
143
144     fn query_crate(&self) -> CrateNum {
145         LOCAL_CRATE
146     }
147     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
148         self.0.default_span(tcx)
149     }
150 }
151
152 impl Key for (CrateNum, DefId) {
153     type CacheSelector = DefaultCacheSelector;
154
155     fn query_crate(&self) -> CrateNum {
156         self.0
157     }
158     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
159         self.1.default_span(tcx)
160     }
161 }
162
163 impl Key for (DefId, SimplifiedType) {
164     type CacheSelector = DefaultCacheSelector;
165
166     fn query_crate(&self) -> CrateNum {
167         self.0.krate
168     }
169     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
170         self.0.default_span(tcx)
171     }
172 }
173
174 impl<'tcx> Key for SubstsRef<'tcx> {
175     type CacheSelector = DefaultCacheSelector;
176
177     fn query_crate(&self) -> CrateNum {
178         LOCAL_CRATE
179     }
180     fn default_span(&self, _: TyCtxt<'_>) -> Span {
181         DUMMY_SP
182     }
183 }
184
185 impl<'tcx> Key for (DefId, SubstsRef<'tcx>) {
186     type CacheSelector = DefaultCacheSelector;
187
188     fn query_crate(&self) -> CrateNum {
189         self.0.krate
190     }
191     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
192         self.0.default_span(tcx)
193     }
194 }
195
196 impl<'tcx> Key for (LocalDefId, DefId, SubstsRef<'tcx>) {
197     type CacheSelector = DefaultCacheSelector;
198
199     fn query_crate(&self) -> CrateNum {
200         LOCAL_CRATE
201     }
202     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
203         self.0.default_span(tcx)
204     }
205 }
206
207 impl<'tcx> Key for (ty::ParamEnv<'tcx>, ty::PolyTraitRef<'tcx>) {
208     type CacheSelector = DefaultCacheSelector;
209
210     fn query_crate(&self) -> CrateNum {
211         self.1.def_id().krate
212     }
213     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
214         tcx.def_span(self.1.def_id())
215     }
216 }
217
218 impl<'tcx> Key for (&'tcx ty::Const<'tcx>, mir::Field) {
219     type CacheSelector = DefaultCacheSelector;
220
221     fn query_crate(&self) -> CrateNum {
222         LOCAL_CRATE
223     }
224     fn default_span(&self, _: TyCtxt<'_>) -> Span {
225         DUMMY_SP
226     }
227 }
228
229 impl<'tcx> Key for ty::PolyTraitRef<'tcx> {
230     type CacheSelector = DefaultCacheSelector;
231
232     fn query_crate(&self) -> CrateNum {
233         self.def_id().krate
234     }
235     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
236         tcx.def_span(self.def_id())
237     }
238 }
239
240 impl<'tcx> Key for GenericArg<'tcx> {
241     type CacheSelector = DefaultCacheSelector;
242
243     fn query_crate(&self) -> CrateNum {
244         LOCAL_CRATE
245     }
246     fn default_span(&self, _: TyCtxt<'_>) -> Span {
247         DUMMY_SP
248     }
249 }
250
251 impl<'tcx> Key for &'tcx ty::Const<'tcx> {
252     type CacheSelector = DefaultCacheSelector;
253
254     fn query_crate(&self) -> CrateNum {
255         LOCAL_CRATE
256     }
257     fn default_span(&self, _: TyCtxt<'_>) -> Span {
258         DUMMY_SP
259     }
260 }
261
262 impl<'tcx> Key for Ty<'tcx> {
263     type CacheSelector = DefaultCacheSelector;
264
265     fn query_crate(&self) -> CrateNum {
266         LOCAL_CRATE
267     }
268     fn default_span(&self, _: TyCtxt<'_>) -> Span {
269         DUMMY_SP
270     }
271 }
272
273 impl<'tcx> Key for ty::ParamEnv<'tcx> {
274     type CacheSelector = DefaultCacheSelector;
275
276     fn query_crate(&self) -> CrateNum {
277         LOCAL_CRATE
278     }
279     fn default_span(&self, _: TyCtxt<'_>) -> Span {
280         DUMMY_SP
281     }
282 }
283
284 impl<'tcx, T: Key> Key for ty::ParamEnvAnd<'tcx, T> {
285     type CacheSelector = DefaultCacheSelector;
286
287     fn query_crate(&self) -> CrateNum {
288         self.value.query_crate()
289     }
290     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
291         self.value.default_span(tcx)
292     }
293 }
294
295 impl Key for Symbol {
296     type CacheSelector = DefaultCacheSelector;
297
298     fn query_crate(&self) -> CrateNum {
299         LOCAL_CRATE
300     }
301     fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
302         DUMMY_SP
303     }
304 }
305
306 /// Canonical query goals correspond to abstract trait operations that
307 /// are not tied to any crate in particular.
308 impl<'tcx, T> Key for Canonical<'tcx, T> {
309     type CacheSelector = DefaultCacheSelector;
310
311     fn query_crate(&self) -> CrateNum {
312         LOCAL_CRATE
313     }
314
315     fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
316         DUMMY_SP
317     }
318 }
319
320 impl Key for (Symbol, u32, u32) {
321     type CacheSelector = DefaultCacheSelector;
322
323     fn query_crate(&self) -> CrateNum {
324         LOCAL_CRATE
325     }
326
327     fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
328         DUMMY_SP
329     }
330 }
331
332 impl<'tcx> Key for (DefId, Ty<'tcx>, SubstsRef<'tcx>, ty::ParamEnv<'tcx>) {
333     type CacheSelector = DefaultCacheSelector;
334
335     fn query_crate(&self) -> CrateNum {
336         LOCAL_CRATE
337     }
338
339     fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
340         DUMMY_SP
341     }
342 }