diff options
Diffstat (limited to 'crates/ra_hir_ty/src/traits/chalk/interner.rs')
-rw-r--r-- | crates/ra_hir_ty/src/traits/chalk/interner.rs | 351 |
1 files changed, 351 insertions, 0 deletions
diff --git a/crates/ra_hir_ty/src/traits/chalk/interner.rs b/crates/ra_hir_ty/src/traits/chalk/interner.rs new file mode 100644 index 000000000..060372819 --- /dev/null +++ b/crates/ra_hir_ty/src/traits/chalk/interner.rs | |||
@@ -0,0 +1,351 @@ | |||
1 | //! Implementation of the Chalk `Interner` trait, which allows customizing the | ||
2 | //! representation of the various objects Chalk deals with (types, goals etc.). | ||
3 | |||
4 | use super::tls; | ||
5 | use chalk_ir::{GenericArg, Goal, GoalData}; | ||
6 | use hir_def::TypeAliasId; | ||
7 | use ra_db::salsa::InternId; | ||
8 | use std::{fmt, sync::Arc}; | ||
9 | |||
10 | #[derive(Debug, Copy, Clone, Hash, PartialOrd, Ord, PartialEq, Eq)] | ||
11 | pub struct Interner; | ||
12 | |||
13 | pub type AssocTypeId = chalk_ir::AssocTypeId<Interner>; | ||
14 | pub type AssociatedTyDatum = chalk_rust_ir::AssociatedTyDatum<Interner>; | ||
15 | pub type TraitId = chalk_ir::TraitId<Interner>; | ||
16 | pub type TraitDatum = chalk_rust_ir::TraitDatum<Interner>; | ||
17 | pub type AdtId = chalk_ir::AdtId<Interner>; | ||
18 | pub type StructDatum = chalk_rust_ir::AdtDatum<Interner>; | ||
19 | pub type ImplId = chalk_ir::ImplId<Interner>; | ||
20 | pub type ImplDatum = chalk_rust_ir::ImplDatum<Interner>; | ||
21 | pub type AssociatedTyValueId = chalk_rust_ir::AssociatedTyValueId<Interner>; | ||
22 | pub type AssociatedTyValue = chalk_rust_ir::AssociatedTyValue<Interner>; | ||
23 | |||
24 | impl chalk_ir::interner::Interner for Interner { | ||
25 | type InternedType = Box<chalk_ir::TyData<Self>>; // FIXME use Arc? | ||
26 | type InternedLifetime = chalk_ir::LifetimeData<Self>; | ||
27 | type InternedConst = Arc<chalk_ir::ConstData<Self>>; | ||
28 | type InternedConcreteConst = (); | ||
29 | type InternedGenericArg = chalk_ir::GenericArgData<Self>; | ||
30 | type InternedGoal = Arc<GoalData<Self>>; | ||
31 | type InternedGoals = Vec<Goal<Self>>; | ||
32 | type InternedSubstitution = Vec<GenericArg<Self>>; | ||
33 | type InternedProgramClause = chalk_ir::ProgramClauseData<Self>; | ||
34 | type InternedProgramClauses = Arc<[chalk_ir::ProgramClause<Self>]>; | ||
35 | type InternedQuantifiedWhereClauses = Vec<chalk_ir::QuantifiedWhereClause<Self>>; | ||
36 | type InternedVariableKinds = Vec<chalk_ir::VariableKind<Self>>; | ||
37 | type InternedCanonicalVarKinds = Vec<chalk_ir::CanonicalVarKind<Self>>; | ||
38 | type DefId = InternId; | ||
39 | type InternedAdtId = crate::TypeCtorId; | ||
40 | type Identifier = TypeAliasId; | ||
41 | |||
42 | fn debug_adt_id(type_kind_id: AdtId, fmt: &mut fmt::Formatter<'_>) -> Option<fmt::Result> { | ||
43 | tls::with_current_program(|prog| Some(prog?.debug_struct_id(type_kind_id, fmt))) | ||
44 | } | ||
45 | |||
46 | fn debug_trait_id(type_kind_id: TraitId, fmt: &mut fmt::Formatter<'_>) -> Option<fmt::Result> { | ||
47 | tls::with_current_program(|prog| Some(prog?.debug_trait_id(type_kind_id, fmt))) | ||
48 | } | ||
49 | |||
50 | fn debug_assoc_type_id(id: AssocTypeId, fmt: &mut fmt::Formatter<'_>) -> Option<fmt::Result> { | ||
51 | tls::with_current_program(|prog| Some(prog?.debug_assoc_type_id(id, fmt))) | ||
52 | } | ||
53 | |||
54 | fn debug_alias( | ||
55 | alias: &chalk_ir::AliasTy<Interner>, | ||
56 | fmt: &mut fmt::Formatter<'_>, | ||
57 | ) -> Option<fmt::Result> { | ||
58 | tls::with_current_program(|prog| Some(prog?.debug_alias(alias, fmt))) | ||
59 | } | ||
60 | |||
61 | fn debug_projection_ty( | ||
62 | proj: &chalk_ir::ProjectionTy<Interner>, | ||
63 | fmt: &mut fmt::Formatter<'_>, | ||
64 | ) -> Option<fmt::Result> { | ||
65 | tls::with_current_program(|prog| Some(prog?.debug_projection_ty(proj, fmt))) | ||
66 | } | ||
67 | |||
68 | fn debug_opaque_ty( | ||
69 | opaque_ty: &chalk_ir::OpaqueTy<Interner>, | ||
70 | fmt: &mut fmt::Formatter<'_>, | ||
71 | ) -> Option<fmt::Result> { | ||
72 | tls::with_current_program(|prog| Some(prog?.debug_opaque_ty(opaque_ty, fmt))) | ||
73 | } | ||
74 | |||
75 | fn debug_opaque_ty_id( | ||
76 | opaque_ty_id: chalk_ir::OpaqueTyId<Self>, | ||
77 | fmt: &mut fmt::Formatter<'_>, | ||
78 | ) -> Option<fmt::Result> { | ||
79 | tls::with_current_program(|prog| Some(prog?.debug_opaque_ty_id(opaque_ty_id, fmt))) | ||
80 | } | ||
81 | |||
82 | fn debug_ty(ty: &chalk_ir::Ty<Interner>, fmt: &mut fmt::Formatter<'_>) -> Option<fmt::Result> { | ||
83 | tls::with_current_program(|prog| Some(prog?.debug_ty(ty, fmt))) | ||
84 | } | ||
85 | |||
86 | fn debug_lifetime( | ||
87 | lifetime: &chalk_ir::Lifetime<Interner>, | ||
88 | fmt: &mut fmt::Formatter<'_>, | ||
89 | ) -> Option<fmt::Result> { | ||
90 | tls::with_current_program(|prog| Some(prog?.debug_lifetime(lifetime, fmt))) | ||
91 | } | ||
92 | |||
93 | fn debug_generic_arg( | ||
94 | parameter: &GenericArg<Interner>, | ||
95 | fmt: &mut fmt::Formatter<'_>, | ||
96 | ) -> Option<fmt::Result> { | ||
97 | tls::with_current_program(|prog| Some(prog?.debug_generic_arg(parameter, fmt))) | ||
98 | } | ||
99 | |||
100 | fn debug_goal(goal: &Goal<Interner>, fmt: &mut fmt::Formatter<'_>) -> Option<fmt::Result> { | ||
101 | tls::with_current_program(|prog| Some(prog?.debug_goal(goal, fmt))) | ||
102 | } | ||
103 | |||
104 | fn debug_goals( | ||
105 | goals: &chalk_ir::Goals<Interner>, | ||
106 | fmt: &mut fmt::Formatter<'_>, | ||
107 | ) -> Option<fmt::Result> { | ||
108 | tls::with_current_program(|prog| Some(prog?.debug_goals(goals, fmt))) | ||
109 | } | ||
110 | |||
111 | fn debug_program_clause_implication( | ||
112 | pci: &chalk_ir::ProgramClauseImplication<Interner>, | ||
113 | fmt: &mut fmt::Formatter<'_>, | ||
114 | ) -> Option<fmt::Result> { | ||
115 | tls::with_current_program(|prog| Some(prog?.debug_program_clause_implication(pci, fmt))) | ||
116 | } | ||
117 | |||
118 | fn debug_application_ty( | ||
119 | application_ty: &chalk_ir::ApplicationTy<Interner>, | ||
120 | fmt: &mut fmt::Formatter<'_>, | ||
121 | ) -> Option<fmt::Result> { | ||
122 | tls::with_current_program(|prog| Some(prog?.debug_application_ty(application_ty, fmt))) | ||
123 | } | ||
124 | |||
125 | fn debug_substitution( | ||
126 | substitution: &chalk_ir::Substitution<Interner>, | ||
127 | fmt: &mut fmt::Formatter<'_>, | ||
128 | ) -> Option<fmt::Result> { | ||
129 | tls::with_current_program(|prog| Some(prog?.debug_substitution(substitution, fmt))) | ||
130 | } | ||
131 | |||
132 | fn debug_separator_trait_ref( | ||
133 | separator_trait_ref: &chalk_ir::SeparatorTraitRef<Interner>, | ||
134 | fmt: &mut fmt::Formatter<'_>, | ||
135 | ) -> Option<fmt::Result> { | ||
136 | tls::with_current_program(|prog| { | ||
137 | Some(prog?.debug_separator_trait_ref(separator_trait_ref, fmt)) | ||
138 | }) | ||
139 | } | ||
140 | |||
141 | fn debug_fn_def_id( | ||
142 | fn_def_id: chalk_ir::FnDefId<Self>, | ||
143 | fmt: &mut fmt::Formatter<'_>, | ||
144 | ) -> Option<fmt::Result> { | ||
145 | tls::with_current_program(|prog| Some(prog?.debug_fn_def_id(fn_def_id, fmt))) | ||
146 | } | ||
147 | fn debug_const( | ||
148 | constant: &chalk_ir::Const<Self>, | ||
149 | fmt: &mut fmt::Formatter<'_>, | ||
150 | ) -> Option<fmt::Result> { | ||
151 | tls::with_current_program(|prog| Some(prog?.debug_const(constant, fmt))) | ||
152 | } | ||
153 | fn debug_variable_kinds( | ||
154 | variable_kinds: &chalk_ir::VariableKinds<Self>, | ||
155 | fmt: &mut fmt::Formatter<'_>, | ||
156 | ) -> Option<fmt::Result> { | ||
157 | tls::with_current_program(|prog| Some(prog?.debug_variable_kinds(variable_kinds, fmt))) | ||
158 | } | ||
159 | fn debug_variable_kinds_with_angles( | ||
160 | variable_kinds: &chalk_ir::VariableKinds<Self>, | ||
161 | fmt: &mut fmt::Formatter<'_>, | ||
162 | ) -> Option<fmt::Result> { | ||
163 | tls::with_current_program(|prog| { | ||
164 | Some(prog?.debug_variable_kinds_with_angles(variable_kinds, fmt)) | ||
165 | }) | ||
166 | } | ||
167 | fn debug_canonical_var_kinds( | ||
168 | canonical_var_kinds: &chalk_ir::CanonicalVarKinds<Self>, | ||
169 | fmt: &mut fmt::Formatter<'_>, | ||
170 | ) -> Option<fmt::Result> { | ||
171 | tls::with_current_program(|prog| { | ||
172 | Some(prog?.debug_canonical_var_kinds(canonical_var_kinds, fmt)) | ||
173 | }) | ||
174 | } | ||
175 | fn debug_program_clause( | ||
176 | clause: &chalk_ir::ProgramClause<Self>, | ||
177 | fmt: &mut fmt::Formatter<'_>, | ||
178 | ) -> Option<fmt::Result> { | ||
179 | tls::with_current_program(|prog| Some(prog?.debug_program_clause(clause, fmt))) | ||
180 | } | ||
181 | fn debug_program_clauses( | ||
182 | clauses: &chalk_ir::ProgramClauses<Self>, | ||
183 | fmt: &mut fmt::Formatter<'_>, | ||
184 | ) -> Option<fmt::Result> { | ||
185 | tls::with_current_program(|prog| Some(prog?.debug_program_clauses(clauses, fmt))) | ||
186 | } | ||
187 | fn debug_quantified_where_clauses( | ||
188 | clauses: &chalk_ir::QuantifiedWhereClauses<Self>, | ||
189 | fmt: &mut fmt::Formatter<'_>, | ||
190 | ) -> Option<fmt::Result> { | ||
191 | tls::with_current_program(|prog| Some(prog?.debug_quantified_where_clauses(clauses, fmt))) | ||
192 | } | ||
193 | |||
194 | fn intern_ty(&self, ty: chalk_ir::TyData<Self>) -> Box<chalk_ir::TyData<Self>> { | ||
195 | Box::new(ty) | ||
196 | } | ||
197 | |||
198 | fn ty_data<'a>(&self, ty: &'a Box<chalk_ir::TyData<Self>>) -> &'a chalk_ir::TyData<Self> { | ||
199 | ty | ||
200 | } | ||
201 | |||
202 | fn intern_lifetime( | ||
203 | &self, | ||
204 | lifetime: chalk_ir::LifetimeData<Self>, | ||
205 | ) -> chalk_ir::LifetimeData<Self> { | ||
206 | lifetime | ||
207 | } | ||
208 | |||
209 | fn lifetime_data<'a>( | ||
210 | &self, | ||
211 | lifetime: &'a chalk_ir::LifetimeData<Self>, | ||
212 | ) -> &'a chalk_ir::LifetimeData<Self> { | ||
213 | lifetime | ||
214 | } | ||
215 | |||
216 | fn intern_const(&self, constant: chalk_ir::ConstData<Self>) -> Arc<chalk_ir::ConstData<Self>> { | ||
217 | Arc::new(constant) | ||
218 | } | ||
219 | |||
220 | fn const_data<'a>( | ||
221 | &self, | ||
222 | constant: &'a Arc<chalk_ir::ConstData<Self>>, | ||
223 | ) -> &'a chalk_ir::ConstData<Self> { | ||
224 | constant | ||
225 | } | ||
226 | |||
227 | fn const_eq(&self, _ty: &Box<chalk_ir::TyData<Self>>, _c1: &(), _c2: &()) -> bool { | ||
228 | true | ||
229 | } | ||
230 | |||
231 | fn intern_generic_arg( | ||
232 | &self, | ||
233 | parameter: chalk_ir::GenericArgData<Self>, | ||
234 | ) -> chalk_ir::GenericArgData<Self> { | ||
235 | parameter | ||
236 | } | ||
237 | |||
238 | fn generic_arg_data<'a>( | ||
239 | &self, | ||
240 | parameter: &'a chalk_ir::GenericArgData<Self>, | ||
241 | ) -> &'a chalk_ir::GenericArgData<Self> { | ||
242 | parameter | ||
243 | } | ||
244 | |||
245 | fn intern_goal(&self, goal: GoalData<Self>) -> Arc<GoalData<Self>> { | ||
246 | Arc::new(goal) | ||
247 | } | ||
248 | |||
249 | fn intern_goals<E>( | ||
250 | &self, | ||
251 | data: impl IntoIterator<Item = Result<Goal<Self>, E>>, | ||
252 | ) -> Result<Self::InternedGoals, E> { | ||
253 | data.into_iter().collect() | ||
254 | } | ||
255 | |||
256 | fn goal_data<'a>(&self, goal: &'a Arc<GoalData<Self>>) -> &'a GoalData<Self> { | ||
257 | goal | ||
258 | } | ||
259 | |||
260 | fn goals_data<'a>(&self, goals: &'a Vec<Goal<Interner>>) -> &'a [Goal<Interner>] { | ||
261 | goals | ||
262 | } | ||
263 | |||
264 | fn intern_substitution<E>( | ||
265 | &self, | ||
266 | data: impl IntoIterator<Item = Result<GenericArg<Self>, E>>, | ||
267 | ) -> Result<Vec<GenericArg<Self>>, E> { | ||
268 | data.into_iter().collect() | ||
269 | } | ||
270 | |||
271 | fn substitution_data<'a>( | ||
272 | &self, | ||
273 | substitution: &'a Vec<GenericArg<Self>>, | ||
274 | ) -> &'a [GenericArg<Self>] { | ||
275 | substitution | ||
276 | } | ||
277 | |||
278 | fn intern_program_clause( | ||
279 | &self, | ||
280 | data: chalk_ir::ProgramClauseData<Self>, | ||
281 | ) -> chalk_ir::ProgramClauseData<Self> { | ||
282 | data | ||
283 | } | ||
284 | |||
285 | fn program_clause_data<'a>( | ||
286 | &self, | ||
287 | clause: &'a chalk_ir::ProgramClauseData<Self>, | ||
288 | ) -> &'a chalk_ir::ProgramClauseData<Self> { | ||
289 | clause | ||
290 | } | ||
291 | |||
292 | fn intern_program_clauses<E>( | ||
293 | &self, | ||
294 | data: impl IntoIterator<Item = Result<chalk_ir::ProgramClause<Self>, E>>, | ||
295 | ) -> Result<Arc<[chalk_ir::ProgramClause<Self>]>, E> { | ||
296 | data.into_iter().collect() | ||
297 | } | ||
298 | |||
299 | fn program_clauses_data<'a>( | ||
300 | &self, | ||
301 | clauses: &'a Arc<[chalk_ir::ProgramClause<Self>]>, | ||
302 | ) -> &'a [chalk_ir::ProgramClause<Self>] { | ||
303 | &clauses | ||
304 | } | ||
305 | |||
306 | fn intern_quantified_where_clauses<E>( | ||
307 | &self, | ||
308 | data: impl IntoIterator<Item = Result<chalk_ir::QuantifiedWhereClause<Self>, E>>, | ||
309 | ) -> Result<Self::InternedQuantifiedWhereClauses, E> { | ||
310 | data.into_iter().collect() | ||
311 | } | ||
312 | |||
313 | fn quantified_where_clauses_data<'a>( | ||
314 | &self, | ||
315 | clauses: &'a Self::InternedQuantifiedWhereClauses, | ||
316 | ) -> &'a [chalk_ir::QuantifiedWhereClause<Self>] { | ||
317 | clauses | ||
318 | } | ||
319 | |||
320 | fn intern_generic_arg_kinds<E>( | ||
321 | &self, | ||
322 | data: impl IntoIterator<Item = Result<chalk_ir::VariableKind<Self>, E>>, | ||
323 | ) -> Result<Self::InternedVariableKinds, E> { | ||
324 | data.into_iter().collect() | ||
325 | } | ||
326 | |||
327 | fn variable_kinds_data<'a>( | ||
328 | &self, | ||
329 | parameter_kinds: &'a Self::InternedVariableKinds, | ||
330 | ) -> &'a [chalk_ir::VariableKind<Self>] { | ||
331 | ¶meter_kinds | ||
332 | } | ||
333 | |||
334 | fn intern_canonical_var_kinds<E>( | ||
335 | &self, | ||
336 | data: impl IntoIterator<Item = Result<chalk_ir::CanonicalVarKind<Self>, E>>, | ||
337 | ) -> Result<Self::InternedCanonicalVarKinds, E> { | ||
338 | data.into_iter().collect() | ||
339 | } | ||
340 | |||
341 | fn canonical_var_kinds_data<'a>( | ||
342 | &self, | ||
343 | canonical_var_kinds: &'a Self::InternedCanonicalVarKinds, | ||
344 | ) -> &'a [chalk_ir::CanonicalVarKind<Self>] { | ||
345 | &canonical_var_kinds | ||
346 | } | ||
347 | } | ||
348 | |||
349 | impl chalk_ir::interner::HasInterner for Interner { | ||
350 | type Interner = Self; | ||
351 | } | ||