]> git.lizzy.rs Git - rust.git/blob - src/librustc/ty/query/keys.rs
Update const_forget.rs
[rust.git] / src / librustc / 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::traits;
6 use crate::ty::fast_reject::SimplifiedType;
7 use crate::ty::query::caches::DefaultCacheSelector;
8 use crate::ty::subst::SubstsRef;
9 use crate::ty::{self, Ty, TyCtxt};
10 use rustc_hir::def_id::{CrateNum, DefId, DefIndex, LOCAL_CRATE};
11 use rustc_span::symbol::Symbol;
12 use rustc_span::{Span, DUMMY_SP};
13
14 /// The `Key` trait controls what types can legally be used as the key
15 /// for a query.
16 pub trait Key {
17     type CacheSelector;
18
19     /// Given an instance of this key, what crate is it referring to?
20     /// This is used to find the provider.
21     fn query_crate(&self) -> CrateNum;
22
23     /// In the event that a cycle occurs, if no explicit span has been
24     /// given for a query with key `self`, what span should we use?
25     fn default_span(&self, tcx: TyCtxt<'_>) -> Span;
26 }
27
28 impl<'tcx> Key for ty::InstanceDef<'tcx> {
29     type CacheSelector = DefaultCacheSelector;
30
31     fn query_crate(&self) -> CrateNum {
32         LOCAL_CRATE
33     }
34
35     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
36         tcx.def_span(self.def_id())
37     }
38 }
39
40 impl<'tcx> Key for ty::Instance<'tcx> {
41     type CacheSelector = DefaultCacheSelector;
42
43     fn query_crate(&self) -> CrateNum {
44         LOCAL_CRATE
45     }
46
47     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
48         tcx.def_span(self.def_id())
49     }
50 }
51
52 impl<'tcx> Key for mir::interpret::GlobalId<'tcx> {
53     type CacheSelector = DefaultCacheSelector;
54
55     fn query_crate(&self) -> CrateNum {
56         self.instance.query_crate()
57     }
58
59     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
60         self.instance.default_span(tcx)
61     }
62 }
63
64 impl<'tcx> Key for mir::interpret::LitToConstInput<'tcx> {
65     type CacheSelector = DefaultCacheSelector;
66
67     fn query_crate(&self) -> CrateNum {
68         LOCAL_CRATE
69     }
70
71     fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
72         DUMMY_SP
73     }
74 }
75
76 impl Key for CrateNum {
77     type CacheSelector = DefaultCacheSelector;
78
79     fn query_crate(&self) -> CrateNum {
80         *self
81     }
82     fn default_span(&self, _: TyCtxt<'_>) -> Span {
83         DUMMY_SP
84     }
85 }
86
87 impl Key for DefIndex {
88     type CacheSelector = DefaultCacheSelector;
89
90     fn query_crate(&self) -> CrateNum {
91         LOCAL_CRATE
92     }
93     fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
94         DUMMY_SP
95     }
96 }
97
98 impl Key for DefId {
99     type CacheSelector = DefaultCacheSelector;
100
101     fn query_crate(&self) -> CrateNum {
102         self.krate
103     }
104     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
105         tcx.def_span(*self)
106     }
107 }
108
109 impl Key for (DefId, DefId) {
110     type CacheSelector = DefaultCacheSelector;
111
112     fn query_crate(&self) -> CrateNum {
113         self.0.krate
114     }
115     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
116         self.1.default_span(tcx)
117     }
118 }
119
120 impl Key for (CrateNum, DefId) {
121     type CacheSelector = DefaultCacheSelector;
122
123     fn query_crate(&self) -> CrateNum {
124         self.0
125     }
126     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
127         self.1.default_span(tcx)
128     }
129 }
130
131 impl Key for (DefId, SimplifiedType) {
132     type CacheSelector = DefaultCacheSelector;
133
134     fn query_crate(&self) -> CrateNum {
135         self.0.krate
136     }
137     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
138         self.0.default_span(tcx)
139     }
140 }
141
142 impl<'tcx> Key for SubstsRef<'tcx> {
143     type CacheSelector = DefaultCacheSelector;
144
145     fn query_crate(&self) -> CrateNum {
146         LOCAL_CRATE
147     }
148     fn default_span(&self, _: TyCtxt<'_>) -> Span {
149         DUMMY_SP
150     }
151 }
152
153 impl<'tcx> Key for (DefId, SubstsRef<'tcx>) {
154     type CacheSelector = DefaultCacheSelector;
155
156     fn query_crate(&self) -> CrateNum {
157         self.0.krate
158     }
159     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
160         self.0.default_span(tcx)
161     }
162 }
163
164 impl<'tcx> Key for (ty::ParamEnv<'tcx>, ty::PolyTraitRef<'tcx>) {
165     type CacheSelector = DefaultCacheSelector;
166
167     fn query_crate(&self) -> CrateNum {
168         self.1.def_id().krate
169     }
170     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
171         tcx.def_span(self.1.def_id())
172     }
173 }
174
175 impl<'tcx> Key for (&'tcx ty::Const<'tcx>, mir::Field) {
176     type CacheSelector = DefaultCacheSelector;
177
178     fn query_crate(&self) -> CrateNum {
179         LOCAL_CRATE
180     }
181     fn default_span(&self, _: TyCtxt<'_>) -> Span {
182         DUMMY_SP
183     }
184 }
185
186 impl<'tcx> Key for ty::PolyTraitRef<'tcx> {
187     type CacheSelector = DefaultCacheSelector;
188
189     fn query_crate(&self) -> CrateNum {
190         self.def_id().krate
191     }
192     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
193         tcx.def_span(self.def_id())
194     }
195 }
196
197 impl<'tcx> Key for &'tcx ty::Const<'tcx> {
198     type CacheSelector = DefaultCacheSelector;
199
200     fn query_crate(&self) -> CrateNum {
201         LOCAL_CRATE
202     }
203     fn default_span(&self, _: TyCtxt<'_>) -> Span {
204         DUMMY_SP
205     }
206 }
207
208 impl<'tcx> Key for Ty<'tcx> {
209     type CacheSelector = DefaultCacheSelector;
210
211     fn query_crate(&self) -> CrateNum {
212         LOCAL_CRATE
213     }
214     fn default_span(&self, _: TyCtxt<'_>) -> Span {
215         DUMMY_SP
216     }
217 }
218
219 impl<'tcx> Key for ty::ParamEnv<'tcx> {
220     type CacheSelector = DefaultCacheSelector;
221
222     fn query_crate(&self) -> CrateNum {
223         LOCAL_CRATE
224     }
225     fn default_span(&self, _: TyCtxt<'_>) -> Span {
226         DUMMY_SP
227     }
228 }
229
230 impl<'tcx, T: Key> Key for ty::ParamEnvAnd<'tcx, T> {
231     type CacheSelector = DefaultCacheSelector;
232
233     fn query_crate(&self) -> CrateNum {
234         self.value.query_crate()
235     }
236     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
237         self.value.default_span(tcx)
238     }
239 }
240
241 impl<'tcx> Key for traits::Environment<'tcx> {
242     type CacheSelector = DefaultCacheSelector;
243
244     fn query_crate(&self) -> CrateNum {
245         LOCAL_CRATE
246     }
247     fn default_span(&self, _: TyCtxt<'_>) -> Span {
248         DUMMY_SP
249     }
250 }
251
252 impl Key for Symbol {
253     type CacheSelector = DefaultCacheSelector;
254
255     fn query_crate(&self) -> CrateNum {
256         LOCAL_CRATE
257     }
258     fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
259         DUMMY_SP
260     }
261 }
262
263 /// Canonical query goals correspond to abstract trait operations that
264 /// are not tied to any crate in particular.
265 impl<'tcx, T> Key for Canonical<'tcx, T> {
266     type CacheSelector = DefaultCacheSelector;
267
268     fn query_crate(&self) -> CrateNum {
269         LOCAL_CRATE
270     }
271
272     fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
273         DUMMY_SP
274     }
275 }
276
277 impl Key for (Symbol, u32, u32) {
278     type CacheSelector = DefaultCacheSelector;
279
280     fn query_crate(&self) -> CrateNum {
281         LOCAL_CRATE
282     }
283
284     fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
285         DUMMY_SP
286     }
287 }