diff options
Diffstat (limited to 'crates/hir_ty/src/traits/chalk')
-rw-r--r-- | crates/hir_ty/src/traits/chalk/interner.rs | 392 | ||||
-rw-r--r-- | crates/hir_ty/src/traits/chalk/mapping.rs | 131 | ||||
-rw-r--r-- | crates/hir_ty/src/traits/chalk/tls.rs | 275 |
3 files changed, 0 insertions, 798 deletions
diff --git a/crates/hir_ty/src/traits/chalk/interner.rs b/crates/hir_ty/src/traits/chalk/interner.rs deleted file mode 100644 index bd9395b7e..000000000 --- a/crates/hir_ty/src/traits/chalk/interner.rs +++ /dev/null | |||
@@ -1,392 +0,0 @@ | |||
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 smallvec::SmallVec; | ||
9 | use std::{fmt, sync::Arc}; | ||
10 | |||
11 | #[derive(Debug, Copy, Clone, Hash, PartialOrd, Ord, PartialEq, Eq)] | ||
12 | pub struct Interner; | ||
13 | |||
14 | pub(crate) type AssocTypeId = chalk_ir::AssocTypeId<Interner>; | ||
15 | pub(crate) type AssociatedTyDatum = chalk_solve::rust_ir::AssociatedTyDatum<Interner>; | ||
16 | pub(crate) type TraitId = chalk_ir::TraitId<Interner>; | ||
17 | pub(crate) type TraitDatum = chalk_solve::rust_ir::TraitDatum<Interner>; | ||
18 | pub(crate) type AdtId = chalk_ir::AdtId<Interner>; | ||
19 | pub(crate) type StructDatum = chalk_solve::rust_ir::AdtDatum<Interner>; | ||
20 | pub(crate) type ImplId = chalk_ir::ImplId<Interner>; | ||
21 | pub(crate) type ImplDatum = chalk_solve::rust_ir::ImplDatum<Interner>; | ||
22 | pub(crate) type AssociatedTyValueId = chalk_solve::rust_ir::AssociatedTyValueId<Interner>; | ||
23 | pub(crate) type AssociatedTyValue = chalk_solve::rust_ir::AssociatedTyValue<Interner>; | ||
24 | pub(crate) type FnDefDatum = chalk_solve::rust_ir::FnDefDatum<Interner>; | ||
25 | pub(crate) type OpaqueTyId = chalk_ir::OpaqueTyId<Interner>; | ||
26 | pub(crate) type OpaqueTyDatum = chalk_solve::rust_ir::OpaqueTyDatum<Interner>; | ||
27 | pub(crate) type Variances = chalk_ir::Variances<Interner>; | ||
28 | |||
29 | impl chalk_ir::interner::Interner for Interner { | ||
30 | type InternedType = Arc<chalk_ir::TyData<Self>>; | ||
31 | type InternedLifetime = chalk_ir::LifetimeData<Self>; | ||
32 | type InternedConst = Arc<chalk_ir::ConstData<Self>>; | ||
33 | type InternedConcreteConst = (); | ||
34 | type InternedGenericArg = chalk_ir::GenericArgData<Self>; | ||
35 | type InternedGoal = Arc<GoalData<Self>>; | ||
36 | type InternedGoals = Vec<Goal<Self>>; | ||
37 | type InternedSubstitution = SmallVec<[GenericArg<Self>; 2]>; | ||
38 | type InternedProgramClause = Arc<chalk_ir::ProgramClauseData<Self>>; | ||
39 | type InternedProgramClauses = Arc<[chalk_ir::ProgramClause<Self>]>; | ||
40 | type InternedQuantifiedWhereClauses = Vec<chalk_ir::QuantifiedWhereClause<Self>>; | ||
41 | type InternedVariableKinds = Vec<chalk_ir::VariableKind<Self>>; | ||
42 | type InternedCanonicalVarKinds = Vec<chalk_ir::CanonicalVarKind<Self>>; | ||
43 | type InternedConstraints = Vec<chalk_ir::InEnvironment<chalk_ir::Constraint<Self>>>; | ||
44 | type InternedVariances = Arc<[chalk_ir::Variance]>; | ||
45 | type DefId = InternId; | ||
46 | type InternedAdtId = hir_def::AdtId; | ||
47 | type Identifier = TypeAliasId; | ||
48 | type FnAbi = (); | ||
49 | |||
50 | fn debug_adt_id(type_kind_id: AdtId, fmt: &mut fmt::Formatter<'_>) -> Option<fmt::Result> { | ||
51 | tls::with_current_program(|prog| Some(prog?.debug_struct_id(type_kind_id, fmt))) | ||
52 | } | ||
53 | |||
54 | fn debug_trait_id(type_kind_id: TraitId, fmt: &mut fmt::Formatter<'_>) -> Option<fmt::Result> { | ||
55 | tls::with_current_program(|prog| Some(prog?.debug_trait_id(type_kind_id, fmt))) | ||
56 | } | ||
57 | |||
58 | fn debug_assoc_type_id(id: AssocTypeId, fmt: &mut fmt::Formatter<'_>) -> Option<fmt::Result> { | ||
59 | tls::with_current_program(|prog| Some(prog?.debug_assoc_type_id(id, fmt))) | ||
60 | } | ||
61 | |||
62 | fn debug_alias( | ||
63 | alias: &chalk_ir::AliasTy<Interner>, | ||
64 | fmt: &mut fmt::Formatter<'_>, | ||
65 | ) -> Option<fmt::Result> { | ||
66 | tls::with_current_program(|prog| Some(prog?.debug_alias(alias, fmt))) | ||
67 | } | ||
68 | |||
69 | fn debug_projection_ty( | ||
70 | proj: &chalk_ir::ProjectionTy<Interner>, | ||
71 | fmt: &mut fmt::Formatter<'_>, | ||
72 | ) -> Option<fmt::Result> { | ||
73 | tls::with_current_program(|prog| Some(prog?.debug_projection_ty(proj, fmt))) | ||
74 | } | ||
75 | |||
76 | fn debug_opaque_ty( | ||
77 | opaque_ty: &chalk_ir::OpaqueTy<Interner>, | ||
78 | fmt: &mut fmt::Formatter<'_>, | ||
79 | ) -> Option<fmt::Result> { | ||
80 | tls::with_current_program(|prog| Some(prog?.debug_opaque_ty(opaque_ty, fmt))) | ||
81 | } | ||
82 | |||
83 | fn debug_opaque_ty_id( | ||
84 | opaque_ty_id: chalk_ir::OpaqueTyId<Self>, | ||
85 | fmt: &mut fmt::Formatter<'_>, | ||
86 | ) -> Option<fmt::Result> { | ||
87 | tls::with_current_program(|prog| Some(prog?.debug_opaque_ty_id(opaque_ty_id, fmt))) | ||
88 | } | ||
89 | |||
90 | fn debug_ty(ty: &chalk_ir::Ty<Interner>, fmt: &mut fmt::Formatter<'_>) -> Option<fmt::Result> { | ||
91 | tls::with_current_program(|prog| Some(prog?.debug_ty(ty, fmt))) | ||
92 | } | ||
93 | |||
94 | fn debug_lifetime( | ||
95 | lifetime: &chalk_ir::Lifetime<Interner>, | ||
96 | fmt: &mut fmt::Formatter<'_>, | ||
97 | ) -> Option<fmt::Result> { | ||
98 | tls::with_current_program(|prog| Some(prog?.debug_lifetime(lifetime, fmt))) | ||
99 | } | ||
100 | |||
101 | fn debug_generic_arg( | ||
102 | parameter: &GenericArg<Interner>, | ||
103 | fmt: &mut fmt::Formatter<'_>, | ||
104 | ) -> Option<fmt::Result> { | ||
105 | tls::with_current_program(|prog| Some(prog?.debug_generic_arg(parameter, fmt))) | ||
106 | } | ||
107 | |||
108 | fn debug_goal(goal: &Goal<Interner>, fmt: &mut fmt::Formatter<'_>) -> Option<fmt::Result> { | ||
109 | tls::with_current_program(|prog| Some(prog?.debug_goal(goal, fmt))) | ||
110 | } | ||
111 | |||
112 | fn debug_goals( | ||
113 | goals: &chalk_ir::Goals<Interner>, | ||
114 | fmt: &mut fmt::Formatter<'_>, | ||
115 | ) -> Option<fmt::Result> { | ||
116 | tls::with_current_program(|prog| Some(prog?.debug_goals(goals, fmt))) | ||
117 | } | ||
118 | |||
119 | fn debug_program_clause_implication( | ||
120 | pci: &chalk_ir::ProgramClauseImplication<Interner>, | ||
121 | fmt: &mut fmt::Formatter<'_>, | ||
122 | ) -> Option<fmt::Result> { | ||
123 | tls::with_current_program(|prog| Some(prog?.debug_program_clause_implication(pci, fmt))) | ||
124 | } | ||
125 | |||
126 | fn debug_substitution( | ||
127 | substitution: &chalk_ir::Substitution<Interner>, | ||
128 | fmt: &mut fmt::Formatter<'_>, | ||
129 | ) -> Option<fmt::Result> { | ||
130 | tls::with_current_program(|prog| Some(prog?.debug_substitution(substitution, fmt))) | ||
131 | } | ||
132 | |||
133 | fn debug_separator_trait_ref( | ||
134 | separator_trait_ref: &chalk_ir::SeparatorTraitRef<Interner>, | ||
135 | fmt: &mut fmt::Formatter<'_>, | ||
136 | ) -> Option<fmt::Result> { | ||
137 | tls::with_current_program(|prog| { | ||
138 | Some(prog?.debug_separator_trait_ref(separator_trait_ref, fmt)) | ||
139 | }) | ||
140 | } | ||
141 | |||
142 | fn debug_fn_def_id( | ||
143 | fn_def_id: chalk_ir::FnDefId<Self>, | ||
144 | fmt: &mut fmt::Formatter<'_>, | ||
145 | ) -> Option<fmt::Result> { | ||
146 | tls::with_current_program(|prog| Some(prog?.debug_fn_def_id(fn_def_id, fmt))) | ||
147 | } | ||
148 | fn debug_const( | ||
149 | constant: &chalk_ir::Const<Self>, | ||
150 | fmt: &mut fmt::Formatter<'_>, | ||
151 | ) -> Option<fmt::Result> { | ||
152 | tls::with_current_program(|prog| Some(prog?.debug_const(constant, fmt))) | ||
153 | } | ||
154 | fn debug_variable_kinds( | ||
155 | variable_kinds: &chalk_ir::VariableKinds<Self>, | ||
156 | fmt: &mut fmt::Formatter<'_>, | ||
157 | ) -> Option<fmt::Result> { | ||
158 | tls::with_current_program(|prog| Some(prog?.debug_variable_kinds(variable_kinds, fmt))) | ||
159 | } | ||
160 | fn debug_variable_kinds_with_angles( | ||
161 | variable_kinds: &chalk_ir::VariableKinds<Self>, | ||
162 | fmt: &mut fmt::Formatter<'_>, | ||
163 | ) -> Option<fmt::Result> { | ||
164 | tls::with_current_program(|prog| { | ||
165 | Some(prog?.debug_variable_kinds_with_angles(variable_kinds, fmt)) | ||
166 | }) | ||
167 | } | ||
168 | fn debug_canonical_var_kinds( | ||
169 | canonical_var_kinds: &chalk_ir::CanonicalVarKinds<Self>, | ||
170 | fmt: &mut fmt::Formatter<'_>, | ||
171 | ) -> Option<fmt::Result> { | ||
172 | tls::with_current_program(|prog| { | ||
173 | Some(prog?.debug_canonical_var_kinds(canonical_var_kinds, fmt)) | ||
174 | }) | ||
175 | } | ||
176 | fn debug_program_clause( | ||
177 | clause: &chalk_ir::ProgramClause<Self>, | ||
178 | fmt: &mut fmt::Formatter<'_>, | ||
179 | ) -> Option<fmt::Result> { | ||
180 | tls::with_current_program(|prog| Some(prog?.debug_program_clause(clause, fmt))) | ||
181 | } | ||
182 | fn debug_program_clauses( | ||
183 | clauses: &chalk_ir::ProgramClauses<Self>, | ||
184 | fmt: &mut fmt::Formatter<'_>, | ||
185 | ) -> Option<fmt::Result> { | ||
186 | tls::with_current_program(|prog| Some(prog?.debug_program_clauses(clauses, fmt))) | ||
187 | } | ||
188 | fn debug_quantified_where_clauses( | ||
189 | clauses: &chalk_ir::QuantifiedWhereClauses<Self>, | ||
190 | fmt: &mut fmt::Formatter<'_>, | ||
191 | ) -> Option<fmt::Result> { | ||
192 | tls::with_current_program(|prog| Some(prog?.debug_quantified_where_clauses(clauses, fmt))) | ||
193 | } | ||
194 | |||
195 | fn intern_ty(&self, kind: chalk_ir::TyKind<Self>) -> Self::InternedType { | ||
196 | let flags = kind.compute_flags(self); | ||
197 | Arc::new(chalk_ir::TyData { kind, flags }) | ||
198 | } | ||
199 | |||
200 | fn ty_data<'a>(&self, ty: &'a Self::InternedType) -> &'a chalk_ir::TyData<Self> { | ||
201 | ty | ||
202 | } | ||
203 | |||
204 | fn intern_lifetime(&self, lifetime: chalk_ir::LifetimeData<Self>) -> Self::InternedLifetime { | ||
205 | lifetime | ||
206 | } | ||
207 | |||
208 | fn lifetime_data<'a>( | ||
209 | &self, | ||
210 | lifetime: &'a Self::InternedLifetime, | ||
211 | ) -> &'a chalk_ir::LifetimeData<Self> { | ||
212 | lifetime | ||
213 | } | ||
214 | |||
215 | fn intern_const(&self, constant: chalk_ir::ConstData<Self>) -> Self::InternedConst { | ||
216 | Arc::new(constant) | ||
217 | } | ||
218 | |||
219 | fn const_data<'a>(&self, constant: &'a Self::InternedConst) -> &'a chalk_ir::ConstData<Self> { | ||
220 | constant | ||
221 | } | ||
222 | |||
223 | fn const_eq( | ||
224 | &self, | ||
225 | _ty: &Self::InternedType, | ||
226 | _c1: &Self::InternedConcreteConst, | ||
227 | _c2: &Self::InternedConcreteConst, | ||
228 | ) -> bool { | ||
229 | true | ||
230 | } | ||
231 | |||
232 | fn intern_generic_arg( | ||
233 | &self, | ||
234 | parameter: chalk_ir::GenericArgData<Self>, | ||
235 | ) -> Self::InternedGenericArg { | ||
236 | parameter | ||
237 | } | ||
238 | |||
239 | fn generic_arg_data<'a>( | ||
240 | &self, | ||
241 | parameter: &'a Self::InternedGenericArg, | ||
242 | ) -> &'a chalk_ir::GenericArgData<Self> { | ||
243 | parameter | ||
244 | } | ||
245 | |||
246 | fn intern_goal(&self, goal: GoalData<Self>) -> Self::InternedGoal { | ||
247 | Arc::new(goal) | ||
248 | } | ||
249 | |||
250 | fn intern_goals<E>( | ||
251 | &self, | ||
252 | data: impl IntoIterator<Item = Result<Goal<Self>, E>>, | ||
253 | ) -> Result<Self::InternedGoals, E> { | ||
254 | data.into_iter().collect() | ||
255 | } | ||
256 | |||
257 | fn goal_data<'a>(&self, goal: &'a Self::InternedGoal) -> &'a GoalData<Self> { | ||
258 | goal | ||
259 | } | ||
260 | |||
261 | fn goals_data<'a>(&self, goals: &'a Self::InternedGoals) -> &'a [Goal<Interner>] { | ||
262 | goals | ||
263 | } | ||
264 | |||
265 | fn intern_substitution<E>( | ||
266 | &self, | ||
267 | data: impl IntoIterator<Item = Result<GenericArg<Self>, E>>, | ||
268 | ) -> Result<Self::InternedSubstitution, E> { | ||
269 | data.into_iter().collect() | ||
270 | } | ||
271 | |||
272 | fn substitution_data<'a>( | ||
273 | &self, | ||
274 | substitution: &'a Self::InternedSubstitution, | ||
275 | ) -> &'a [GenericArg<Self>] { | ||
276 | substitution | ||
277 | } | ||
278 | |||
279 | fn intern_program_clause( | ||
280 | &self, | ||
281 | data: chalk_ir::ProgramClauseData<Self>, | ||
282 | ) -> Self::InternedProgramClause { | ||
283 | Arc::new(data) | ||
284 | } | ||
285 | |||
286 | fn program_clause_data<'a>( | ||
287 | &self, | ||
288 | clause: &'a Self::InternedProgramClause, | ||
289 | ) -> &'a chalk_ir::ProgramClauseData<Self> { | ||
290 | clause | ||
291 | } | ||
292 | |||
293 | fn intern_program_clauses<E>( | ||
294 | &self, | ||
295 | data: impl IntoIterator<Item = Result<chalk_ir::ProgramClause<Self>, E>>, | ||
296 | ) -> Result<Self::InternedProgramClauses, E> { | ||
297 | data.into_iter().collect() | ||
298 | } | ||
299 | |||
300 | fn program_clauses_data<'a>( | ||
301 | &self, | ||
302 | clauses: &'a Self::InternedProgramClauses, | ||
303 | ) -> &'a [chalk_ir::ProgramClause<Self>] { | ||
304 | &clauses | ||
305 | } | ||
306 | |||
307 | fn intern_quantified_where_clauses<E>( | ||
308 | &self, | ||
309 | data: impl IntoIterator<Item = Result<chalk_ir::QuantifiedWhereClause<Self>, E>>, | ||
310 | ) -> Result<Self::InternedQuantifiedWhereClauses, E> { | ||
311 | data.into_iter().collect() | ||
312 | } | ||
313 | |||
314 | fn quantified_where_clauses_data<'a>( | ||
315 | &self, | ||
316 | clauses: &'a Self::InternedQuantifiedWhereClauses, | ||
317 | ) -> &'a [chalk_ir::QuantifiedWhereClause<Self>] { | ||
318 | clauses | ||
319 | } | ||
320 | |||
321 | fn intern_generic_arg_kinds<E>( | ||
322 | &self, | ||
323 | data: impl IntoIterator<Item = Result<chalk_ir::VariableKind<Self>, E>>, | ||
324 | ) -> Result<Self::InternedVariableKinds, E> { | ||
325 | data.into_iter().collect() | ||
326 | } | ||
327 | |||
328 | fn variable_kinds_data<'a>( | ||
329 | &self, | ||
330 | parameter_kinds: &'a Self::InternedVariableKinds, | ||
331 | ) -> &'a [chalk_ir::VariableKind<Self>] { | ||
332 | ¶meter_kinds | ||
333 | } | ||
334 | |||
335 | fn intern_canonical_var_kinds<E>( | ||
336 | &self, | ||
337 | data: impl IntoIterator<Item = Result<chalk_ir::CanonicalVarKind<Self>, E>>, | ||
338 | ) -> Result<Self::InternedCanonicalVarKinds, E> { | ||
339 | data.into_iter().collect() | ||
340 | } | ||
341 | |||
342 | fn canonical_var_kinds_data<'a>( | ||
343 | &self, | ||
344 | canonical_var_kinds: &'a Self::InternedCanonicalVarKinds, | ||
345 | ) -> &'a [chalk_ir::CanonicalVarKind<Self>] { | ||
346 | &canonical_var_kinds | ||
347 | } | ||
348 | |||
349 | fn intern_constraints<E>( | ||
350 | &self, | ||
351 | data: impl IntoIterator<Item = Result<chalk_ir::InEnvironment<chalk_ir::Constraint<Self>>, E>>, | ||
352 | ) -> Result<Self::InternedConstraints, E> { | ||
353 | data.into_iter().collect() | ||
354 | } | ||
355 | |||
356 | fn constraints_data<'a>( | ||
357 | &self, | ||
358 | constraints: &'a Self::InternedConstraints, | ||
359 | ) -> &'a [chalk_ir::InEnvironment<chalk_ir::Constraint<Self>>] { | ||
360 | constraints | ||
361 | } | ||
362 | fn debug_closure_id( | ||
363 | _fn_def_id: chalk_ir::ClosureId<Self>, | ||
364 | _fmt: &mut fmt::Formatter<'_>, | ||
365 | ) -> Option<fmt::Result> { | ||
366 | None | ||
367 | } | ||
368 | fn debug_constraints( | ||
369 | _clauses: &chalk_ir::Constraints<Self>, | ||
370 | _fmt: &mut fmt::Formatter<'_>, | ||
371 | ) -> Option<fmt::Result> { | ||
372 | None | ||
373 | } | ||
374 | |||
375 | fn intern_variances<E>( | ||
376 | &self, | ||
377 | data: impl IntoIterator<Item = Result<chalk_ir::Variance, E>>, | ||
378 | ) -> Result<Self::InternedVariances, E> { | ||
379 | data.into_iter().collect() | ||
380 | } | ||
381 | |||
382 | fn variances_data<'a>( | ||
383 | &self, | ||
384 | variances: &'a Self::InternedVariances, | ||
385 | ) -> &'a [chalk_ir::Variance] { | ||
386 | &variances | ||
387 | } | ||
388 | } | ||
389 | |||
390 | impl chalk_ir::interner::HasInterner for Interner { | ||
391 | type Interner = Self; | ||
392 | } | ||
diff --git a/crates/hir_ty/src/traits/chalk/mapping.rs b/crates/hir_ty/src/traits/chalk/mapping.rs deleted file mode 100644 index 7818f6387..000000000 --- a/crates/hir_ty/src/traits/chalk/mapping.rs +++ /dev/null | |||
@@ -1,131 +0,0 @@ | |||
1 | //! This module contains the implementations of the `ToChalk` trait, which | ||
2 | //! handles conversion between our data types and their corresponding types in | ||
3 | //! Chalk (in both directions); plus some helper functions for more specialized | ||
4 | //! conversions. | ||
5 | |||
6 | use chalk_ir::cast::Cast; | ||
7 | use chalk_solve::rust_ir; | ||
8 | |||
9 | use base_db::salsa::InternKey; | ||
10 | use hir_def::{GenericDefId, TypeAliasId}; | ||
11 | |||
12 | use crate::{ | ||
13 | db::HirDatabase, AliasTy, CallableDefId, ProjectionTyExt, QuantifiedWhereClause, Substitution, | ||
14 | Ty, WhereClause, | ||
15 | }; | ||
16 | |||
17 | use super::interner::*; | ||
18 | use super::*; | ||
19 | |||
20 | impl ToChalk for hir_def::TraitId { | ||
21 | type Chalk = TraitId; | ||
22 | |||
23 | fn to_chalk(self, _db: &dyn HirDatabase) -> TraitId { | ||
24 | chalk_ir::TraitId(self.as_intern_id()) | ||
25 | } | ||
26 | |||
27 | fn from_chalk(_db: &dyn HirDatabase, trait_id: TraitId) -> hir_def::TraitId { | ||
28 | InternKey::from_intern_id(trait_id.0) | ||
29 | } | ||
30 | } | ||
31 | |||
32 | impl ToChalk for hir_def::ImplId { | ||
33 | type Chalk = ImplId; | ||
34 | |||
35 | fn to_chalk(self, _db: &dyn HirDatabase) -> ImplId { | ||
36 | chalk_ir::ImplId(self.as_intern_id()) | ||
37 | } | ||
38 | |||
39 | fn from_chalk(_db: &dyn HirDatabase, impl_id: ImplId) -> hir_def::ImplId { | ||
40 | InternKey::from_intern_id(impl_id.0) | ||
41 | } | ||
42 | } | ||
43 | |||
44 | impl ToChalk for CallableDefId { | ||
45 | type Chalk = FnDefId; | ||
46 | |||
47 | fn to_chalk(self, db: &dyn HirDatabase) -> FnDefId { | ||
48 | db.intern_callable_def(self).into() | ||
49 | } | ||
50 | |||
51 | fn from_chalk(db: &dyn HirDatabase, fn_def_id: FnDefId) -> CallableDefId { | ||
52 | db.lookup_intern_callable_def(fn_def_id.into()) | ||
53 | } | ||
54 | } | ||
55 | |||
56 | pub(crate) struct TypeAliasAsValue(pub(crate) TypeAliasId); | ||
57 | |||
58 | impl ToChalk for TypeAliasAsValue { | ||
59 | type Chalk = AssociatedTyValueId; | ||
60 | |||
61 | fn to_chalk(self, _db: &dyn HirDatabase) -> AssociatedTyValueId { | ||
62 | rust_ir::AssociatedTyValueId(self.0.as_intern_id()) | ||
63 | } | ||
64 | |||
65 | fn from_chalk( | ||
66 | _db: &dyn HirDatabase, | ||
67 | assoc_ty_value_id: AssociatedTyValueId, | ||
68 | ) -> TypeAliasAsValue { | ||
69 | TypeAliasAsValue(TypeAliasId::from_intern_id(assoc_ty_value_id.0)) | ||
70 | } | ||
71 | } | ||
72 | |||
73 | pub(super) fn convert_where_clauses( | ||
74 | db: &dyn HirDatabase, | ||
75 | def: GenericDefId, | ||
76 | substs: &Substitution, | ||
77 | ) -> Vec<chalk_ir::QuantifiedWhereClause<Interner>> { | ||
78 | let generic_predicates = db.generic_predicates(def); | ||
79 | let mut result = Vec::with_capacity(generic_predicates.len()); | ||
80 | for pred in generic_predicates.iter() { | ||
81 | result.push(pred.clone().substitute(&Interner, substs)); | ||
82 | } | ||
83 | result | ||
84 | } | ||
85 | |||
86 | pub(super) fn generic_predicate_to_inline_bound( | ||
87 | db: &dyn HirDatabase, | ||
88 | pred: &QuantifiedWhereClause, | ||
89 | self_ty: &Ty, | ||
90 | ) -> Option<chalk_ir::Binders<rust_ir::InlineBound<Interner>>> { | ||
91 | // An InlineBound is like a GenericPredicate, except the self type is left out. | ||
92 | // We don't have a special type for this, but Chalk does. | ||
93 | let self_ty_shifted_in = self_ty.clone().shifted_in_from(&Interner, DebruijnIndex::ONE); | ||
94 | let (pred, binders) = pred.as_ref().into_value_and_skipped_binders(); | ||
95 | match pred { | ||
96 | WhereClause::Implemented(trait_ref) => { | ||
97 | if trait_ref.self_type_parameter(&Interner) != self_ty_shifted_in { | ||
98 | // we can only convert predicates back to type bounds if they | ||
99 | // have the expected self type | ||
100 | return None; | ||
101 | } | ||
102 | let args_no_self = trait_ref.substitution.interned()[1..] | ||
103 | .iter() | ||
104 | .map(|ty| ty.clone().cast(&Interner)) | ||
105 | .collect(); | ||
106 | let trait_bound = rust_ir::TraitBound { trait_id: trait_ref.trait_id, args_no_self }; | ||
107 | Some(chalk_ir::Binders::new(binders, rust_ir::InlineBound::TraitBound(trait_bound))) | ||
108 | } | ||
109 | WhereClause::AliasEq(AliasEq { alias: AliasTy::Projection(projection_ty), ty }) => { | ||
110 | if projection_ty.self_type_parameter(&Interner) != self_ty_shifted_in { | ||
111 | return None; | ||
112 | } | ||
113 | let trait_ = projection_ty.trait_(db); | ||
114 | let args_no_self = projection_ty.substitution.interned()[1..] | ||
115 | .iter() | ||
116 | .map(|ty| ty.clone().cast(&Interner)) | ||
117 | .collect(); | ||
118 | let alias_eq_bound = rust_ir::AliasEqBound { | ||
119 | value: ty.clone(), | ||
120 | trait_bound: rust_ir::TraitBound { trait_id: trait_.to_chalk(db), args_no_self }, | ||
121 | associated_ty_id: projection_ty.associated_ty_id, | ||
122 | parameters: Vec::new(), // FIXME we don't support generic associated types yet | ||
123 | }; | ||
124 | Some(chalk_ir::Binders::new( | ||
125 | binders, | ||
126 | rust_ir::InlineBound::AliasEqBound(alias_eq_bound), | ||
127 | )) | ||
128 | } | ||
129 | _ => None, | ||
130 | } | ||
131 | } | ||
diff --git a/crates/hir_ty/src/traits/chalk/tls.rs b/crates/hir_ty/src/traits/chalk/tls.rs deleted file mode 100644 index 8892a63a9..000000000 --- a/crates/hir_ty/src/traits/chalk/tls.rs +++ /dev/null | |||
@@ -1,275 +0,0 @@ | |||
1 | //! Implementation of Chalk debug helper functions using TLS. | ||
2 | use std::fmt; | ||
3 | |||
4 | use chalk_ir::{AliasTy, GenericArg, Goal, Goals, Lifetime, ProgramClauseImplication}; | ||
5 | use itertools::Itertools; | ||
6 | |||
7 | use super::{from_chalk, Interner}; | ||
8 | use crate::{db::HirDatabase, from_assoc_type_id, CallableDefId}; | ||
9 | use hir_def::{AdtId, AssocContainerId, Lookup, TypeAliasId}; | ||
10 | |||
11 | pub(crate) use unsafe_tls::{set_current_program, with_current_program}; | ||
12 | |||
13 | pub(crate) struct DebugContext<'a>(&'a dyn HirDatabase); | ||
14 | |||
15 | impl DebugContext<'_> { | ||
16 | pub(crate) fn debug_struct_id( | ||
17 | &self, | ||
18 | id: super::AdtId, | ||
19 | f: &mut fmt::Formatter<'_>, | ||
20 | ) -> Result<(), fmt::Error> { | ||
21 | let name = match id.0 { | ||
22 | AdtId::StructId(it) => self.0.struct_data(it).name.clone(), | ||
23 | AdtId::UnionId(it) => self.0.union_data(it).name.clone(), | ||
24 | AdtId::EnumId(it) => self.0.enum_data(it).name.clone(), | ||
25 | }; | ||
26 | write!(f, "{}", name) | ||
27 | } | ||
28 | |||
29 | pub(crate) fn debug_trait_id( | ||
30 | &self, | ||
31 | id: super::TraitId, | ||
32 | fmt: &mut fmt::Formatter<'_>, | ||
33 | ) -> Result<(), fmt::Error> { | ||
34 | let trait_: hir_def::TraitId = from_chalk(self.0, id); | ||
35 | let trait_data = self.0.trait_data(trait_); | ||
36 | write!(fmt, "{}", trait_data.name) | ||
37 | } | ||
38 | |||
39 | pub(crate) fn debug_assoc_type_id( | ||
40 | &self, | ||
41 | id: super::AssocTypeId, | ||
42 | fmt: &mut fmt::Formatter<'_>, | ||
43 | ) -> Result<(), fmt::Error> { | ||
44 | let type_alias: TypeAliasId = from_assoc_type_id(id); | ||
45 | let type_alias_data = self.0.type_alias_data(type_alias); | ||
46 | let trait_ = match type_alias.lookup(self.0.upcast()).container { | ||
47 | AssocContainerId::TraitId(t) => t, | ||
48 | _ => panic!("associated type not in trait"), | ||
49 | }; | ||
50 | let trait_data = self.0.trait_data(trait_); | ||
51 | write!(fmt, "{}::{}", trait_data.name, type_alias_data.name) | ||
52 | } | ||
53 | |||
54 | pub(crate) fn debug_opaque_ty_id( | ||
55 | &self, | ||
56 | opaque_ty_id: chalk_ir::OpaqueTyId<Interner>, | ||
57 | fmt: &mut fmt::Formatter<'_>, | ||
58 | ) -> Result<(), fmt::Error> { | ||
59 | fmt.debug_struct("OpaqueTyId").field("index", &opaque_ty_id.0).finish() | ||
60 | } | ||
61 | |||
62 | pub(crate) fn debug_alias( | ||
63 | &self, | ||
64 | alias_ty: &AliasTy<Interner>, | ||
65 | fmt: &mut fmt::Formatter<'_>, | ||
66 | ) -> Result<(), fmt::Error> { | ||
67 | match alias_ty { | ||
68 | AliasTy::Projection(projection_ty) => self.debug_projection_ty(projection_ty, fmt), | ||
69 | AliasTy::Opaque(opaque_ty) => self.debug_opaque_ty(opaque_ty, fmt), | ||
70 | } | ||
71 | } | ||
72 | |||
73 | pub(crate) fn debug_projection_ty( | ||
74 | &self, | ||
75 | projection_ty: &chalk_ir::ProjectionTy<Interner>, | ||
76 | fmt: &mut fmt::Formatter<'_>, | ||
77 | ) -> Result<(), fmt::Error> { | ||
78 | let type_alias = from_assoc_type_id(projection_ty.associated_ty_id); | ||
79 | let type_alias_data = self.0.type_alias_data(type_alias); | ||
80 | let trait_ = match type_alias.lookup(self.0.upcast()).container { | ||
81 | AssocContainerId::TraitId(t) => t, | ||
82 | _ => panic!("associated type not in trait"), | ||
83 | }; | ||
84 | let trait_data = self.0.trait_data(trait_); | ||
85 | let params = projection_ty.substitution.as_slice(&Interner); | ||
86 | write!(fmt, "<{:?} as {}", ¶ms[0], trait_data.name,)?; | ||
87 | if params.len() > 1 { | ||
88 | write!( | ||
89 | fmt, | ||
90 | "<{}>", | ||
91 | ¶ms[1..].iter().format_with(", ", |x, f| f(&format_args!("{:?}", x))), | ||
92 | )?; | ||
93 | } | ||
94 | write!(fmt, ">::{}", type_alias_data.name) | ||
95 | } | ||
96 | |||
97 | pub(crate) fn debug_opaque_ty( | ||
98 | &self, | ||
99 | opaque_ty: &chalk_ir::OpaqueTy<Interner>, | ||
100 | fmt: &mut fmt::Formatter<'_>, | ||
101 | ) -> Result<(), fmt::Error> { | ||
102 | write!(fmt, "{:?}", opaque_ty.opaque_ty_id) | ||
103 | } | ||
104 | |||
105 | pub(crate) fn debug_ty( | ||
106 | &self, | ||
107 | ty: &chalk_ir::Ty<Interner>, | ||
108 | fmt: &mut fmt::Formatter<'_>, | ||
109 | ) -> Result<(), fmt::Error> { | ||
110 | write!(fmt, "{:?}", ty.data(&Interner)) | ||
111 | } | ||
112 | |||
113 | pub(crate) fn debug_lifetime( | ||
114 | &self, | ||
115 | lifetime: &Lifetime<Interner>, | ||
116 | fmt: &mut fmt::Formatter<'_>, | ||
117 | ) -> Result<(), fmt::Error> { | ||
118 | write!(fmt, "{:?}", lifetime.data(&Interner)) | ||
119 | } | ||
120 | |||
121 | pub(crate) fn debug_generic_arg( | ||
122 | &self, | ||
123 | parameter: &GenericArg<Interner>, | ||
124 | fmt: &mut fmt::Formatter<'_>, | ||
125 | ) -> Result<(), fmt::Error> { | ||
126 | write!(fmt, "{:?}", parameter.data(&Interner).inner_debug()) | ||
127 | } | ||
128 | |||
129 | pub(crate) fn debug_goal( | ||
130 | &self, | ||
131 | goal: &Goal<Interner>, | ||
132 | fmt: &mut fmt::Formatter<'_>, | ||
133 | ) -> Result<(), fmt::Error> { | ||
134 | let goal_data = goal.data(&Interner); | ||
135 | write!(fmt, "{:?}", goal_data) | ||
136 | } | ||
137 | |||
138 | pub(crate) fn debug_goals( | ||
139 | &self, | ||
140 | goals: &Goals<Interner>, | ||
141 | fmt: &mut fmt::Formatter<'_>, | ||
142 | ) -> Result<(), fmt::Error> { | ||
143 | write!(fmt, "{:?}", goals.debug(&Interner)) | ||
144 | } | ||
145 | |||
146 | pub(crate) fn debug_program_clause_implication( | ||
147 | &self, | ||
148 | pci: &ProgramClauseImplication<Interner>, | ||
149 | fmt: &mut fmt::Formatter<'_>, | ||
150 | ) -> Result<(), fmt::Error> { | ||
151 | write!(fmt, "{:?}", pci.debug(&Interner)) | ||
152 | } | ||
153 | |||
154 | pub(crate) fn debug_substitution( | ||
155 | &self, | ||
156 | substitution: &chalk_ir::Substitution<Interner>, | ||
157 | fmt: &mut fmt::Formatter<'_>, | ||
158 | ) -> Result<(), fmt::Error> { | ||
159 | write!(fmt, "{:?}", substitution.debug(&Interner)) | ||
160 | } | ||
161 | |||
162 | pub(crate) fn debug_separator_trait_ref( | ||
163 | &self, | ||
164 | separator_trait_ref: &chalk_ir::SeparatorTraitRef<Interner>, | ||
165 | fmt: &mut fmt::Formatter<'_>, | ||
166 | ) -> Result<(), fmt::Error> { | ||
167 | write!(fmt, "{:?}", separator_trait_ref.debug(&Interner)) | ||
168 | } | ||
169 | |||
170 | pub(crate) fn debug_fn_def_id( | ||
171 | &self, | ||
172 | fn_def_id: chalk_ir::FnDefId<Interner>, | ||
173 | fmt: &mut fmt::Formatter<'_>, | ||
174 | ) -> Result<(), fmt::Error> { | ||
175 | let def: CallableDefId = from_chalk(self.0, fn_def_id); | ||
176 | let name = match def { | ||
177 | CallableDefId::FunctionId(ff) => self.0.function_data(ff).name.clone(), | ||
178 | CallableDefId::StructId(s) => self.0.struct_data(s).name.clone(), | ||
179 | CallableDefId::EnumVariantId(e) => { | ||
180 | let enum_data = self.0.enum_data(e.parent); | ||
181 | enum_data.variants[e.local_id].name.clone() | ||
182 | } | ||
183 | }; | ||
184 | match def { | ||
185 | CallableDefId::FunctionId(_) => write!(fmt, "{{fn {}}}", name), | ||
186 | CallableDefId::StructId(_) | CallableDefId::EnumVariantId(_) => { | ||
187 | write!(fmt, "{{ctor {}}}", name) | ||
188 | } | ||
189 | } | ||
190 | } | ||
191 | |||
192 | pub(crate) fn debug_const( | ||
193 | &self, | ||
194 | _constant: &chalk_ir::Const<Interner>, | ||
195 | fmt: &mut fmt::Formatter<'_>, | ||
196 | ) -> fmt::Result { | ||
197 | write!(fmt, "const") | ||
198 | } | ||
199 | |||
200 | pub(crate) fn debug_variable_kinds( | ||
201 | &self, | ||
202 | variable_kinds: &chalk_ir::VariableKinds<Interner>, | ||
203 | fmt: &mut fmt::Formatter<'_>, | ||
204 | ) -> fmt::Result { | ||
205 | write!(fmt, "{:?}", variable_kinds.as_slice(&Interner)) | ||
206 | } | ||
207 | pub(crate) fn debug_variable_kinds_with_angles( | ||
208 | &self, | ||
209 | variable_kinds: &chalk_ir::VariableKinds<Interner>, | ||
210 | fmt: &mut fmt::Formatter<'_>, | ||
211 | ) -> fmt::Result { | ||
212 | write!(fmt, "{:?}", variable_kinds.inner_debug(&Interner)) | ||
213 | } | ||
214 | pub(crate) fn debug_canonical_var_kinds( | ||
215 | &self, | ||
216 | canonical_var_kinds: &chalk_ir::CanonicalVarKinds<Interner>, | ||
217 | fmt: &mut fmt::Formatter<'_>, | ||
218 | ) -> fmt::Result { | ||
219 | write!(fmt, "{:?}", canonical_var_kinds.as_slice(&Interner)) | ||
220 | } | ||
221 | pub(crate) fn debug_program_clause( | ||
222 | &self, | ||
223 | clause: &chalk_ir::ProgramClause<Interner>, | ||
224 | fmt: &mut fmt::Formatter<'_>, | ||
225 | ) -> fmt::Result { | ||
226 | write!(fmt, "{:?}", clause.data(&Interner)) | ||
227 | } | ||
228 | pub(crate) fn debug_program_clauses( | ||
229 | &self, | ||
230 | clauses: &chalk_ir::ProgramClauses<Interner>, | ||
231 | fmt: &mut fmt::Formatter<'_>, | ||
232 | ) -> fmt::Result { | ||
233 | write!(fmt, "{:?}", clauses.as_slice(&Interner)) | ||
234 | } | ||
235 | pub(crate) fn debug_quantified_where_clauses( | ||
236 | &self, | ||
237 | clauses: &chalk_ir::QuantifiedWhereClauses<Interner>, | ||
238 | fmt: &mut fmt::Formatter<'_>, | ||
239 | ) -> fmt::Result { | ||
240 | write!(fmt, "{:?}", clauses.as_slice(&Interner)) | ||
241 | } | ||
242 | } | ||
243 | |||
244 | mod unsafe_tls { | ||
245 | use super::DebugContext; | ||
246 | use crate::db::HirDatabase; | ||
247 | use scoped_tls::scoped_thread_local; | ||
248 | |||
249 | scoped_thread_local!(static PROGRAM: DebugContext); | ||
250 | |||
251 | pub(crate) fn with_current_program<R>( | ||
252 | op: impl for<'a> FnOnce(Option<&'a DebugContext<'a>>) -> R, | ||
253 | ) -> R { | ||
254 | if PROGRAM.is_set() { | ||
255 | PROGRAM.with(|prog| op(Some(prog))) | ||
256 | } else { | ||
257 | op(None) | ||
258 | } | ||
259 | } | ||
260 | |||
261 | pub(crate) fn set_current_program<OP, R>(p: &dyn HirDatabase, op: OP) -> R | ||
262 | where | ||
263 | OP: FnOnce() -> R, | ||
264 | { | ||
265 | let ctx = DebugContext(p); | ||
266 | // we're transmuting the lifetime in the DebugContext to static. This is | ||
267 | // fine because we only keep the reference for the lifetime of this | ||
268 | // function, *and* the only way to access the context is through | ||
269 | // `with_current_program`, which hides the lifetime through the `for` | ||
270 | // type. | ||
271 | let static_p: &DebugContext<'static> = | ||
272 | unsafe { std::mem::transmute::<&DebugContext, &DebugContext<'static>>(&ctx) }; | ||
273 | PROGRAM.set(static_p, || op()) | ||
274 | } | ||
275 | } | ||