]> git.lizzy.rs Git - rust.git/blob - src/librustc/ty/query/keys.rs
Revert previous attempt at detecting unsatisfiable predicates
[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::subst::SubstsRef;
8 use crate::ty::{self, Ty, TyCtxt};
9 use rustc_hir::def_id::{CrateNum, DefId, DefIndex, LOCAL_CRATE};
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(super) 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(&self) -> CrateNum;
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<'tcx> Key for ty::InstanceDef<'tcx> {
26     fn query_crate(&self) -> CrateNum {
27         LOCAL_CRATE
28     }
29
30     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
31         tcx.def_span(self.def_id())
32     }
33 }
34
35 impl<'tcx> Key for ty::Instance<'tcx> {
36     fn query_crate(&self) -> CrateNum {
37         LOCAL_CRATE
38     }
39
40     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
41         tcx.def_span(self.def_id())
42     }
43 }
44
45 impl<'tcx> Key for mir::interpret::GlobalId<'tcx> {
46     fn query_crate(&self) -> CrateNum {
47         self.instance.query_crate()
48     }
49
50     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
51         self.instance.default_span(tcx)
52     }
53 }
54
55 impl<'tcx> Key for mir::interpret::LitToConstInput<'tcx> {
56     fn query_crate(&self) -> CrateNum {
57         LOCAL_CRATE
58     }
59
60     fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
61         DUMMY_SP
62     }
63 }
64
65 impl Key for CrateNum {
66     fn query_crate(&self) -> CrateNum {
67         *self
68     }
69     fn default_span(&self, _: TyCtxt<'_>) -> Span {
70         DUMMY_SP
71     }
72 }
73
74 impl Key for DefIndex {
75     fn query_crate(&self) -> CrateNum {
76         LOCAL_CRATE
77     }
78     fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
79         DUMMY_SP
80     }
81 }
82
83 impl Key for DefId {
84     fn query_crate(&self) -> CrateNum {
85         self.krate
86     }
87     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
88         tcx.def_span(*self)
89     }
90 }
91
92 impl Key for (DefId, DefId) {
93     fn query_crate(&self) -> CrateNum {
94         self.0.krate
95     }
96     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
97         self.1.default_span(tcx)
98     }
99 }
100
101 impl Key for (CrateNum, DefId) {
102     fn query_crate(&self) -> CrateNum {
103         self.0
104     }
105     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
106         self.1.default_span(tcx)
107     }
108 }
109
110 impl Key for (DefId, SimplifiedType) {
111     fn query_crate(&self) -> CrateNum {
112         self.0.krate
113     }
114     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
115         self.0.default_span(tcx)
116     }
117 }
118
119 impl<'tcx> Key for (DefId, SubstsRef<'tcx>) {
120     fn query_crate(&self) -> CrateNum {
121         self.0.krate
122     }
123     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
124         self.0.default_span(tcx)
125     }
126 }
127
128 impl<'tcx> Key for (ty::ParamEnv<'tcx>, ty::PolyTraitRef<'tcx>) {
129     fn query_crate(&self) -> CrateNum {
130         self.1.def_id().krate
131     }
132     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
133         tcx.def_span(self.1.def_id())
134     }
135 }
136
137 impl<'tcx> Key for (&'tcx ty::Const<'tcx>, mir::Field) {
138     fn query_crate(&self) -> CrateNum {
139         LOCAL_CRATE
140     }
141     fn default_span(&self, _: TyCtxt<'_>) -> Span {
142         DUMMY_SP
143     }
144 }
145
146 impl<'tcx> Key for ty::PolyTraitRef<'tcx> {
147     fn query_crate(&self) -> CrateNum {
148         self.def_id().krate
149     }
150     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
151         tcx.def_span(self.def_id())
152     }
153 }
154
155 impl<'tcx> Key for &'tcx ty::Const<'tcx> {
156     fn query_crate(&self) -> CrateNum {
157         LOCAL_CRATE
158     }
159     fn default_span(&self, _: TyCtxt<'_>) -> Span {
160         DUMMY_SP
161     }
162 }
163
164 impl<'tcx> Key for Ty<'tcx> {
165     fn query_crate(&self) -> CrateNum {
166         LOCAL_CRATE
167     }
168     fn default_span(&self, _: TyCtxt<'_>) -> Span {
169         DUMMY_SP
170     }
171 }
172
173 impl<'tcx> Key for ty::ParamEnv<'tcx> {
174     fn query_crate(&self) -> CrateNum {
175         LOCAL_CRATE
176     }
177     fn default_span(&self, _: TyCtxt<'_>) -> Span {
178         DUMMY_SP
179     }
180 }
181
182 impl<'tcx, T: Key> Key for ty::ParamEnvAnd<'tcx, T> {
183     fn query_crate(&self) -> CrateNum {
184         self.value.query_crate()
185     }
186     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
187         self.value.default_span(tcx)
188     }
189 }
190
191 impl<'tcx> Key for traits::Environment<'tcx> {
192     fn query_crate(&self) -> CrateNum {
193         LOCAL_CRATE
194     }
195     fn default_span(&self, _: TyCtxt<'_>) -> Span {
196         DUMMY_SP
197     }
198 }
199
200 impl Key for Symbol {
201     fn query_crate(&self) -> CrateNum {
202         LOCAL_CRATE
203     }
204     fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
205         DUMMY_SP
206     }
207 }
208
209 /// Canonical query goals correspond to abstract trait operations that
210 /// are not tied to any crate in particular.
211 impl<'tcx, T> Key for Canonical<'tcx, T> {
212     fn query_crate(&self) -> CrateNum {
213         LOCAL_CRATE
214     }
215
216     fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
217         DUMMY_SP
218     }
219 }
220
221 impl Key for (Symbol, u32, u32) {
222     fn query_crate(&self) -> CrateNum {
223         LOCAL_CRATE
224     }
225
226     fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
227         DUMMY_SP
228     }
229 }