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