]> git.lizzy.rs Git - rust.git/blob - src/librustc/ty/query/keys.rs
96a98b45f1f694c5fdc0103e82f65ad2f026adbd
[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::hir::def_id::{CrateNum, DefId, LOCAL_CRATE, DefIndex};
5 use crate::traits;
6 use crate::ty::{self, Ty, TyCtxt};
7 use crate::ty::subst::SubstsRef;
8 use crate::ty::fast_reject::SimplifiedType;
9 use crate::mir;
10
11 use std::fmt::Debug;
12 use std::hash::Hash;
13 use syntax_pos::{Span, DUMMY_SP};
14 use syntax_pos::symbol::InternedString;
15
16 /// The `Key` trait controls what types can legally be used as the key
17 /// for a query.
18 pub(super) trait Key: Clone + Hash + Eq + Debug {
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     fn query_crate(&self) -> CrateNum {
30         LOCAL_CRATE
31     }
32
33     fn default_span(&self, tcx: TyCtxt<'_, '_>) -> Span {
34         tcx.def_span(self.def_id())
35     }
36 }
37
38 impl<'tcx> Key for ty::Instance<'tcx> {
39     fn query_crate(&self) -> CrateNum {
40         LOCAL_CRATE
41     }
42
43     fn default_span(&self, tcx: TyCtxt<'_, '_>) -> Span {
44         tcx.def_span(self.def_id())
45     }
46 }
47
48 impl<'tcx> Key for mir::interpret::GlobalId<'tcx> {
49     fn query_crate(&self) -> CrateNum {
50         self.instance.query_crate()
51     }
52
53     fn default_span(&self, tcx: TyCtxt<'_, '_>) -> Span {
54         self.instance.default_span(tcx)
55     }
56 }
57
58 impl Key for CrateNum {
59     fn query_crate(&self) -> CrateNum {
60         *self
61     }
62     fn default_span(&self, _: TyCtxt<'_, '_>) -> Span {
63         DUMMY_SP
64     }
65 }
66
67 impl Key for DefIndex {
68     fn query_crate(&self) -> CrateNum {
69         LOCAL_CRATE
70     }
71     fn default_span(&self, _tcx: TyCtxt<'_, '_>) -> Span {
72         DUMMY_SP
73     }
74 }
75
76 impl Key for DefId {
77     fn query_crate(&self) -> CrateNum {
78         self.krate
79     }
80     fn default_span(&self, tcx: TyCtxt<'_, '_>) -> Span {
81         tcx.def_span(*self)
82     }
83 }
84
85 impl Key for (DefId, DefId) {
86     fn query_crate(&self) -> CrateNum {
87         self.0.krate
88     }
89     fn default_span(&self, tcx: TyCtxt<'_, '_>) -> Span {
90         self.1.default_span(tcx)
91     }
92 }
93
94 impl Key for (CrateNum, DefId) {
95     fn query_crate(&self) -> CrateNum {
96         self.0
97     }
98     fn default_span(&self, tcx: TyCtxt<'_, '_>) -> Span {
99         self.1.default_span(tcx)
100     }
101 }
102
103 impl Key for (DefId, SimplifiedType) {
104     fn query_crate(&self) -> CrateNum {
105         self.0.krate
106     }
107     fn default_span(&self, tcx: TyCtxt<'_, '_>) -> Span {
108         self.0.default_span(tcx)
109     }
110 }
111
112 impl<'tcx> Key for (DefId, SubstsRef<'tcx>) {
113     fn query_crate(&self) -> CrateNum {
114         self.0.krate
115     }
116     fn default_span(&self, tcx: TyCtxt<'_, '_>) -> Span {
117         self.0.default_span(tcx)
118     }
119 }
120
121 impl<'tcx> Key for (ty::ParamEnv<'tcx>, ty::PolyTraitRef<'tcx>) {
122     fn query_crate(&self) -> CrateNum {
123         self.1.def_id().krate
124     }
125     fn default_span(&self, tcx: TyCtxt<'_, '_>) -> Span {
126         tcx.def_span(self.1.def_id())
127     }
128 }
129
130 impl<'tcx> Key for (&'tcx ty::Const<'tcx>, mir::Field) {
131     fn query_crate(&self) -> CrateNum {
132         LOCAL_CRATE
133     }
134     fn default_span(&self, _: TyCtxt<'_, '_>) -> Span {
135         DUMMY_SP
136     }
137 }
138
139 impl<'tcx> Key for ty::PolyTraitRef<'tcx>{
140     fn query_crate(&self) -> CrateNum {
141         self.def_id().krate
142     }
143     fn default_span(&self, tcx: TyCtxt<'_, '_>) -> Span {
144         tcx.def_span(self.def_id())
145     }
146 }
147
148 impl<'tcx> Key for ty::Const<'tcx> {
149     fn query_crate(&self) -> CrateNum {
150         LOCAL_CRATE
151     }
152     fn default_span(&self, _: TyCtxt<'_, '_>) -> Span {
153         DUMMY_SP
154     }
155 }
156
157 impl<'tcx> Key for Ty<'tcx> {
158     fn query_crate(&self) -> CrateNum {
159         LOCAL_CRATE
160     }
161     fn default_span(&self, _: TyCtxt<'_, '_>) -> Span {
162         DUMMY_SP
163     }
164 }
165
166 impl<'tcx> Key for ty::ParamEnv<'tcx> {
167     fn query_crate(&self) -> CrateNum {
168         LOCAL_CRATE
169     }
170     fn default_span(&self, _: TyCtxt<'_, '_>) -> Span {
171         DUMMY_SP
172     }
173 }
174
175 impl<'tcx, T: Key> Key for ty::ParamEnvAnd<'tcx, T> {
176     fn query_crate(&self) -> CrateNum {
177         self.value.query_crate()
178     }
179     fn default_span(&self, tcx: TyCtxt<'_, '_>) -> Span {
180         self.value.default_span(tcx)
181     }
182 }
183
184 impl<'tcx> Key for traits::Environment<'tcx> {
185     fn query_crate(&self) -> CrateNum {
186         LOCAL_CRATE
187     }
188     fn default_span(&self, _: TyCtxt<'_, '_>) -> Span {
189         DUMMY_SP
190     }
191 }
192
193 impl Key for InternedString {
194     fn query_crate(&self) -> CrateNum {
195         LOCAL_CRATE
196     }
197     fn default_span(&self, _tcx: TyCtxt<'_, '_>) -> Span {
198         DUMMY_SP
199     }
200 }
201
202 /// Canonical query goals correspond to abstract trait operations that
203 /// are not tied to any crate in particular.
204 impl<'tcx, T> Key for Canonical<'tcx, T>
205 where
206     T: Debug + Hash + Clone + Eq,
207 {
208     fn query_crate(&self) -> CrateNum {
209         LOCAL_CRATE
210     }
211
212     fn default_span(&self, _tcx: TyCtxt<'_, '_>) -> Span {
213         DUMMY_SP
214     }
215 }