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