diff options
Diffstat (limited to 'crates/ra_hir_ty/src/traits/chalk')
-rw-r--r-- | crates/ra_hir_ty/src/traits/chalk/interner.rs | 353 | ||||
-rw-r--r-- | crates/ra_hir_ty/src/traits/chalk/mapping.rs | 701 | ||||
-rw-r--r-- | crates/ra_hir_ty/src/traits/chalk/tls.rs | 83 |
3 files changed, 1132 insertions, 5 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..2a27f8ed8 --- /dev/null +++ b/crates/ra_hir_ty/src/traits/chalk/interner.rs | |||
@@ -0,0 +1,353 @@ | |||
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 | pub type FnDefId = chalk_ir::FnDefId<Interner>; | ||
24 | pub type FnDefDatum = chalk_rust_ir::FnDefDatum<Interner>; | ||
25 | |||
26 | impl chalk_ir::interner::Interner for Interner { | ||
27 | type InternedType = Box<chalk_ir::TyData<Self>>; // FIXME use Arc? | ||
28 | type InternedLifetime = chalk_ir::LifetimeData<Self>; | ||
29 | type InternedConst = Arc<chalk_ir::ConstData<Self>>; | ||
30 | type InternedConcreteConst = (); | ||
31 | type InternedGenericArg = chalk_ir::GenericArgData<Self>; | ||
32 | type InternedGoal = Arc<GoalData<Self>>; | ||
33 | type InternedGoals = Vec<Goal<Self>>; | ||
34 | type InternedSubstitution = Vec<GenericArg<Self>>; | ||
35 | type InternedProgramClause = chalk_ir::ProgramClauseData<Self>; | ||
36 | type InternedProgramClauses = Arc<[chalk_ir::ProgramClause<Self>]>; | ||
37 | type InternedQuantifiedWhereClauses = Vec<chalk_ir::QuantifiedWhereClause<Self>>; | ||
38 | type InternedVariableKinds = Vec<chalk_ir::VariableKind<Self>>; | ||
39 | type InternedCanonicalVarKinds = Vec<chalk_ir::CanonicalVarKind<Self>>; | ||
40 | type DefId = InternId; | ||
41 | type InternedAdtId = crate::TypeCtorId; | ||
42 | type Identifier = TypeAliasId; | ||
43 | |||
44 | fn debug_adt_id(type_kind_id: AdtId, fmt: &mut fmt::Formatter<'_>) -> Option<fmt::Result> { | ||
45 | tls::with_current_program(|prog| Some(prog?.debug_struct_id(type_kind_id, fmt))) | ||
46 | } | ||
47 | |||
48 | fn debug_trait_id(type_kind_id: TraitId, fmt: &mut fmt::Formatter<'_>) -> Option<fmt::Result> { | ||
49 | tls::with_current_program(|prog| Some(prog?.debug_trait_id(type_kind_id, fmt))) | ||
50 | } | ||
51 | |||
52 | fn debug_assoc_type_id(id: AssocTypeId, fmt: &mut fmt::Formatter<'_>) -> Option<fmt::Result> { | ||
53 | tls::with_current_program(|prog| Some(prog?.debug_assoc_type_id(id, fmt))) | ||
54 | } | ||
55 | |||
56 | fn debug_alias( | ||
57 | alias: &chalk_ir::AliasTy<Interner>, | ||
58 | fmt: &mut fmt::Formatter<'_>, | ||
59 | ) -> Option<fmt::Result> { | ||
60 | tls::with_current_program(|prog| Some(prog?.debug_alias(alias, fmt))) | ||
61 | } | ||
62 | |||
63 | fn debug_projection_ty( | ||
64 | proj: &chalk_ir::ProjectionTy<Interner>, | ||
65 | fmt: &mut fmt::Formatter<'_>, | ||
66 | ) -> Option<fmt::Result> { | ||
67 | tls::with_current_program(|prog| Some(prog?.debug_projection_ty(proj, fmt))) | ||
68 | } | ||
69 | |||
70 | fn debug_opaque_ty( | ||
71 | opaque_ty: &chalk_ir::OpaqueTy<Interner>, | ||
72 | fmt: &mut fmt::Formatter<'_>, | ||
73 | ) -> Option<fmt::Result> { | ||
74 | tls::with_current_program(|prog| Some(prog?.debug_opaque_ty(opaque_ty, fmt))) | ||
75 | } | ||
76 | |||
77 | fn debug_opaque_ty_id( | ||
78 | opaque_ty_id: chalk_ir::OpaqueTyId<Self>, | ||
79 | fmt: &mut fmt::Formatter<'_>, | ||
80 | ) -> Option<fmt::Result> { | ||
81 | tls::with_current_program(|prog| Some(prog?.debug_opaque_ty_id(opaque_ty_id, fmt))) | ||
82 | } | ||
83 | |||
84 | fn debug_ty(ty: &chalk_ir::Ty<Interner>, fmt: &mut fmt::Formatter<'_>) -> Option<fmt::Result> { | ||
85 | tls::with_current_program(|prog| Some(prog?.debug_ty(ty, fmt))) | ||
86 | } | ||
87 | |||
88 | fn debug_lifetime( | ||
89 | lifetime: &chalk_ir::Lifetime<Interner>, | ||
90 | fmt: &mut fmt::Formatter<'_>, | ||
91 | ) -> Option<fmt::Result> { | ||
92 | tls::with_current_program(|prog| Some(prog?.debug_lifetime(lifetime, fmt))) | ||
93 | } | ||
94 | |||
95 | fn debug_generic_arg( | ||
96 | parameter: &GenericArg<Interner>, | ||
97 | fmt: &mut fmt::Formatter<'_>, | ||
98 | ) -> Option<fmt::Result> { | ||
99 | tls::with_current_program(|prog| Some(prog?.debug_generic_arg(parameter, fmt))) | ||
100 | } | ||
101 | |||
102 | fn debug_goal(goal: &Goal<Interner>, fmt: &mut fmt::Formatter<'_>) -> Option<fmt::Result> { | ||
103 | tls::with_current_program(|prog| Some(prog?.debug_goal(goal, fmt))) | ||
104 | } | ||
105 | |||
106 | fn debug_goals( | ||
107 | goals: &chalk_ir::Goals<Interner>, | ||
108 | fmt: &mut fmt::Formatter<'_>, | ||
109 | ) -> Option<fmt::Result> { | ||
110 | tls::with_current_program(|prog| Some(prog?.debug_goals(goals, fmt))) | ||
111 | } | ||
112 | |||
113 | fn debug_program_clause_implication( | ||
114 | pci: &chalk_ir::ProgramClauseImplication<Interner>, | ||
115 | fmt: &mut fmt::Formatter<'_>, | ||
116 | ) -> Option<fmt::Result> { | ||
117 | tls::with_current_program(|prog| Some(prog?.debug_program_clause_implication(pci, fmt))) | ||
118 | } | ||
119 | |||
120 | fn debug_application_ty( | ||
121 | application_ty: &chalk_ir::ApplicationTy<Interner>, | ||
122 | fmt: &mut fmt::Formatter<'_>, | ||
123 | ) -> Option<fmt::Result> { | ||
124 | tls::with_current_program(|prog| Some(prog?.debug_application_ty(application_ty, fmt))) | ||
125 | } | ||
126 | |||
127 | fn debug_substitution( | ||
128 | substitution: &chalk_ir::Substitution<Interner>, | ||
129 | fmt: &mut fmt::Formatter<'_>, | ||
130 | ) -> Option<fmt::Result> { | ||
131 | tls::with_current_program(|prog| Some(prog?.debug_substitution(substitution, fmt))) | ||
132 | } | ||
133 | |||
134 | fn debug_separator_trait_ref( | ||
135 | separator_trait_ref: &chalk_ir::SeparatorTraitRef<Interner>, | ||
136 | fmt: &mut fmt::Formatter<'_>, | ||
137 | ) -> Option<fmt::Result> { | ||
138 | tls::with_current_program(|prog| { | ||
139 | Some(prog?.debug_separator_trait_ref(separator_trait_ref, fmt)) | ||
140 | }) | ||
141 | } | ||
142 | |||
143 | fn debug_fn_def_id( | ||
144 | fn_def_id: chalk_ir::FnDefId<Self>, | ||
145 | fmt: &mut fmt::Formatter<'_>, | ||
146 | ) -> Option<fmt::Result> { | ||
147 | tls::with_current_program(|prog| Some(prog?.debug_fn_def_id(fn_def_id, fmt))) | ||
148 | } | ||
149 | fn debug_const( | ||
150 | constant: &chalk_ir::Const<Self>, | ||
151 | fmt: &mut fmt::Formatter<'_>, | ||
152 | ) -> Option<fmt::Result> { | ||
153 | tls::with_current_program(|prog| Some(prog?.debug_const(constant, fmt))) | ||
154 | } | ||
155 | fn debug_variable_kinds( | ||
156 | variable_kinds: &chalk_ir::VariableKinds<Self>, | ||
157 | fmt: &mut fmt::Formatter<'_>, | ||
158 | ) -> Option<fmt::Result> { | ||
159 | tls::with_current_program(|prog| Some(prog?.debug_variable_kinds(variable_kinds, fmt))) | ||
160 | } | ||
161 | fn debug_variable_kinds_with_angles( | ||
162 | variable_kinds: &chalk_ir::VariableKinds<Self>, | ||
163 | fmt: &mut fmt::Formatter<'_>, | ||
164 | ) -> Option<fmt::Result> { | ||
165 | tls::with_current_program(|prog| { | ||
166 | Some(prog?.debug_variable_kinds_with_angles(variable_kinds, fmt)) | ||
167 | }) | ||
168 | } | ||
169 | fn debug_canonical_var_kinds( | ||
170 | canonical_var_kinds: &chalk_ir::CanonicalVarKinds<Self>, | ||
171 | fmt: &mut fmt::Formatter<'_>, | ||
172 | ) -> Option<fmt::Result> { | ||
173 | tls::with_current_program(|prog| { | ||
174 | Some(prog?.debug_canonical_var_kinds(canonical_var_kinds, fmt)) | ||
175 | }) | ||
176 | } | ||
177 | fn debug_program_clause( | ||
178 | clause: &chalk_ir::ProgramClause<Self>, | ||
179 | fmt: &mut fmt::Formatter<'_>, | ||
180 | ) -> Option<fmt::Result> { | ||
181 | tls::with_current_program(|prog| Some(prog?.debug_program_clause(clause, fmt))) | ||
182 | } | ||
183 | fn debug_program_clauses( | ||
184 | clauses: &chalk_ir::ProgramClauses<Self>, | ||
185 | fmt: &mut fmt::Formatter<'_>, | ||
186 | ) -> Option<fmt::Result> { | ||
187 | tls::with_current_program(|prog| Some(prog?.debug_program_clauses(clauses, fmt))) | ||
188 | } | ||
189 | fn debug_quantified_where_clauses( | ||
190 | clauses: &chalk_ir::QuantifiedWhereClauses<Self>, | ||
191 | fmt: &mut fmt::Formatter<'_>, | ||
192 | ) -> Option<fmt::Result> { | ||
193 | tls::with_current_program(|prog| Some(prog?.debug_quantified_where_clauses(clauses, fmt))) | ||
194 | } | ||
195 | |||
196 | fn intern_ty(&self, ty: chalk_ir::TyData<Self>) -> Box<chalk_ir::TyData<Self>> { | ||
197 | Box::new(ty) | ||
198 | } | ||
199 | |||
200 | fn ty_data<'a>(&self, ty: &'a Box<chalk_ir::TyData<Self>>) -> &'a chalk_ir::TyData<Self> { | ||
201 | ty | ||
202 | } | ||
203 | |||
204 | fn intern_lifetime( | ||
205 | &self, | ||
206 | lifetime: chalk_ir::LifetimeData<Self>, | ||
207 | ) -> chalk_ir::LifetimeData<Self> { | ||
208 | lifetime | ||
209 | } | ||
210 | |||
211 | fn lifetime_data<'a>( | ||
212 | &self, | ||
213 | lifetime: &'a chalk_ir::LifetimeData<Self>, | ||
214 | ) -> &'a chalk_ir::LifetimeData<Self> { | ||
215 | lifetime | ||
216 | } | ||
217 | |||
218 | fn intern_const(&self, constant: chalk_ir::ConstData<Self>) -> Arc<chalk_ir::ConstData<Self>> { | ||
219 | Arc::new(constant) | ||
220 | } | ||
221 | |||
222 | fn const_data<'a>( | ||
223 | &self, | ||
224 | constant: &'a Arc<chalk_ir::ConstData<Self>>, | ||
225 | ) -> &'a chalk_ir::ConstData<Self> { | ||
226 | constant | ||
227 | } | ||
228 | |||
229 | fn const_eq(&self, _ty: &Box<chalk_ir::TyData<Self>>, _c1: &(), _c2: &()) -> bool { | ||
230 | true | ||
231 | } | ||
232 | |||
233 | fn intern_generic_arg( | ||
234 | &self, | ||
235 | parameter: chalk_ir::GenericArgData<Self>, | ||
236 | ) -> chalk_ir::GenericArgData<Self> { | ||
237 | parameter | ||
238 | } | ||
239 | |||
240 | fn generic_arg_data<'a>( | ||
241 | &self, | ||
242 | parameter: &'a chalk_ir::GenericArgData<Self>, | ||
243 | ) -> &'a chalk_ir::GenericArgData<Self> { | ||
244 | parameter | ||
245 | } | ||
246 | |||
247 | fn intern_goal(&self, goal: GoalData<Self>) -> Arc<GoalData<Self>> { | ||
248 | Arc::new(goal) | ||
249 | } | ||
250 | |||
251 | fn intern_goals<E>( | ||
252 | &self, | ||
253 | data: impl IntoIterator<Item = Result<Goal<Self>, E>>, | ||
254 | ) -> Result<Self::InternedGoals, E> { | ||
255 | data.into_iter().collect() | ||
256 | } | ||
257 | |||
258 | fn goal_data<'a>(&self, goal: &'a Arc<GoalData<Self>>) -> &'a GoalData<Self> { | ||
259 | goal | ||
260 | } | ||
261 | |||
262 | fn goals_data<'a>(&self, goals: &'a Vec<Goal<Interner>>) -> &'a [Goal<Interner>] { | ||
263 | goals | ||
264 | } | ||
265 | |||
266 | fn intern_substitution<E>( | ||
267 | &self, | ||
268 | data: impl IntoIterator<Item = Result<GenericArg<Self>, E>>, | ||
269 | ) -> Result<Vec<GenericArg<Self>>, E> { | ||
270 | data.into_iter().collect() | ||
271 | } | ||
272 | |||
273 | fn substitution_data<'a>( | ||
274 | &self, | ||
275 | substitution: &'a Vec<GenericArg<Self>>, | ||
276 | ) -> &'a [GenericArg<Self>] { | ||
277 | substitution | ||
278 | } | ||
279 | |||
280 | fn intern_program_clause( | ||
281 | &self, | ||
282 | data: chalk_ir::ProgramClauseData<Self>, | ||
283 | ) -> chalk_ir::ProgramClauseData<Self> { | ||
284 | data | ||
285 | } | ||
286 | |||
287 | fn program_clause_data<'a>( | ||
288 | &self, | ||
289 | clause: &'a chalk_ir::ProgramClauseData<Self>, | ||
290 | ) -> &'a chalk_ir::ProgramClauseData<Self> { | ||
291 | clause | ||
292 | } | ||
293 | |||
294 | fn intern_program_clauses<E>( | ||
295 | &self, | ||
296 | data: impl IntoIterator<Item = Result<chalk_ir::ProgramClause<Self>, E>>, | ||
297 | ) -> Result<Arc<[chalk_ir::ProgramClause<Self>]>, E> { | ||
298 | data.into_iter().collect() | ||
299 | } | ||
300 | |||
301 | fn program_clauses_data<'a>( | ||
302 | &self, | ||
303 | clauses: &'a Arc<[chalk_ir::ProgramClause<Self>]>, | ||
304 | ) -> &'a [chalk_ir::ProgramClause<Self>] { | ||
305 | &clauses | ||
306 | } | ||
307 | |||
308 | fn intern_quantified_where_clauses<E>( | ||
309 | &self, | ||
310 | data: impl IntoIterator<Item = Result<chalk_ir::QuantifiedWhereClause<Self>, E>>, | ||
311 | ) -> Result<Self::InternedQuantifiedWhereClauses, E> { | ||
312 | data.into_iter().collect() | ||
313 | } | ||
314 | |||
315 | fn quantified_where_clauses_data<'a>( | ||
316 | &self, | ||
317 | clauses: &'a Self::InternedQuantifiedWhereClauses, | ||
318 | ) -> &'a [chalk_ir::QuantifiedWhereClause<Self>] { | ||
319 | clauses | ||
320 | } | ||
321 | |||
322 | fn intern_generic_arg_kinds<E>( | ||
323 | &self, | ||
324 | data: impl IntoIterator<Item = Result<chalk_ir::VariableKind<Self>, E>>, | ||
325 | ) -> Result<Self::InternedVariableKinds, E> { | ||
326 | data.into_iter().collect() | ||
327 | } | ||
328 | |||
329 | fn variable_kinds_data<'a>( | ||
330 | &self, | ||
331 | parameter_kinds: &'a Self::InternedVariableKinds, | ||
332 | ) -> &'a [chalk_ir::VariableKind<Self>] { | ||
333 | ¶meter_kinds | ||
334 | } | ||
335 | |||
336 | fn intern_canonical_var_kinds<E>( | ||
337 | &self, | ||
338 | data: impl IntoIterator<Item = Result<chalk_ir::CanonicalVarKind<Self>, E>>, | ||
339 | ) -> Result<Self::InternedCanonicalVarKinds, E> { | ||
340 | data.into_iter().collect() | ||
341 | } | ||
342 | |||
343 | fn canonical_var_kinds_data<'a>( | ||
344 | &self, | ||
345 | canonical_var_kinds: &'a Self::InternedCanonicalVarKinds, | ||
346 | ) -> &'a [chalk_ir::CanonicalVarKind<Self>] { | ||
347 | &canonical_var_kinds | ||
348 | } | ||
349 | } | ||
350 | |||
351 | impl chalk_ir::interner::HasInterner for Interner { | ||
352 | type Interner = Self; | ||
353 | } | ||
diff --git a/crates/ra_hir_ty/src/traits/chalk/mapping.rs b/crates/ra_hir_ty/src/traits/chalk/mapping.rs new file mode 100644 index 000000000..7082cb095 --- /dev/null +++ b/crates/ra_hir_ty/src/traits/chalk/mapping.rs | |||
@@ -0,0 +1,701 @@ | |||
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::{ | ||
7 | cast::Cast, fold::shift::Shift, interner::HasInterner, PlaceholderIndex, Scalar, TypeName, | ||
8 | UniverseIndex, | ||
9 | }; | ||
10 | |||
11 | use hir_def::{type_ref::Mutability, AssocContainerId, GenericDefId, Lookup, TypeAliasId}; | ||
12 | use ra_db::salsa::InternKey; | ||
13 | |||
14 | use crate::{ | ||
15 | db::HirDatabase, | ||
16 | primitive::{FloatBitness, FloatTy, IntBitness, IntTy, Signedness, Uncertain}, | ||
17 | traits::{builtin, AssocTyValue, Canonical, Impl, Obligation}, | ||
18 | ApplicationTy, CallableDef, GenericPredicate, InEnvironment, ProjectionPredicate, ProjectionTy, | ||
19 | Substs, TraitEnvironment, TraitRef, Ty, TypeCtor, | ||
20 | }; | ||
21 | |||
22 | use super::interner::*; | ||
23 | use super::*; | ||
24 | |||
25 | impl ToChalk for Ty { | ||
26 | type Chalk = chalk_ir::Ty<Interner>; | ||
27 | fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::Ty<Interner> { | ||
28 | match self { | ||
29 | Ty::Apply(apply_ty) => match apply_ty.ctor { | ||
30 | TypeCtor::Ref(m) => ref_to_chalk(db, m, apply_ty.parameters), | ||
31 | TypeCtor::FnPtr { num_args: _ } => { | ||
32 | let substitution = apply_ty.parameters.to_chalk(db).shifted_in(&Interner); | ||
33 | chalk_ir::TyData::Function(chalk_ir::Fn { num_binders: 0, substitution }) | ||
34 | .intern(&Interner) | ||
35 | } | ||
36 | _ => { | ||
37 | let name = apply_ty.ctor.to_chalk(db); | ||
38 | let substitution = apply_ty.parameters.to_chalk(db); | ||
39 | chalk_ir::ApplicationTy { name, substitution }.cast(&Interner).intern(&Interner) | ||
40 | } | ||
41 | }, | ||
42 | Ty::Projection(proj_ty) => { | ||
43 | let associated_ty_id = proj_ty.associated_ty.to_chalk(db); | ||
44 | let substitution = proj_ty.parameters.to_chalk(db); | ||
45 | chalk_ir::AliasTy::Projection(chalk_ir::ProjectionTy { | ||
46 | associated_ty_id, | ||
47 | substitution, | ||
48 | }) | ||
49 | .cast(&Interner) | ||
50 | .intern(&Interner) | ||
51 | } | ||
52 | Ty::Placeholder(id) => { | ||
53 | let interned_id = db.intern_type_param_id(id); | ||
54 | PlaceholderIndex { | ||
55 | ui: UniverseIndex::ROOT, | ||
56 | idx: interned_id.as_intern_id().as_usize(), | ||
57 | } | ||
58 | .to_ty::<Interner>(&Interner) | ||
59 | } | ||
60 | Ty::Bound(idx) => chalk_ir::TyData::BoundVar(idx).intern(&Interner), | ||
61 | Ty::Infer(_infer_ty) => panic!("uncanonicalized infer ty"), | ||
62 | Ty::Dyn(predicates) => { | ||
63 | let where_clauses = chalk_ir::QuantifiedWhereClauses::from( | ||
64 | &Interner, | ||
65 | predicates.iter().filter(|p| !p.is_error()).cloned().map(|p| p.to_chalk(db)), | ||
66 | ); | ||
67 | let bounded_ty = chalk_ir::DynTy { bounds: make_binders(where_clauses, 1) }; | ||
68 | chalk_ir::TyData::Dyn(bounded_ty).intern(&Interner) | ||
69 | } | ||
70 | Ty::Opaque(_) | Ty::Unknown => { | ||
71 | let substitution = chalk_ir::Substitution::empty(&Interner); | ||
72 | let name = TypeName::Error; | ||
73 | chalk_ir::ApplicationTy { name, substitution }.cast(&Interner).intern(&Interner) | ||
74 | } | ||
75 | } | ||
76 | } | ||
77 | fn from_chalk(db: &dyn HirDatabase, chalk: chalk_ir::Ty<Interner>) -> Self { | ||
78 | match chalk.data(&Interner).clone() { | ||
79 | chalk_ir::TyData::Apply(apply_ty) => match apply_ty.name { | ||
80 | TypeName::Error => Ty::Unknown, | ||
81 | TypeName::Ref(m) => ref_from_chalk(db, m, apply_ty.substitution), | ||
82 | _ => { | ||
83 | let ctor = from_chalk(db, apply_ty.name); | ||
84 | let parameters = from_chalk(db, apply_ty.substitution); | ||
85 | Ty::Apply(ApplicationTy { ctor, parameters }) | ||
86 | } | ||
87 | }, | ||
88 | chalk_ir::TyData::Placeholder(idx) => { | ||
89 | assert_eq!(idx.ui, UniverseIndex::ROOT); | ||
90 | let interned_id = crate::db::GlobalTypeParamId::from_intern_id( | ||
91 | crate::salsa::InternId::from(idx.idx), | ||
92 | ); | ||
93 | Ty::Placeholder(db.lookup_intern_type_param_id(interned_id)) | ||
94 | } | ||
95 | chalk_ir::TyData::Alias(chalk_ir::AliasTy::Projection(proj)) => { | ||
96 | let associated_ty = from_chalk(db, proj.associated_ty_id); | ||
97 | let parameters = from_chalk(db, proj.substitution); | ||
98 | Ty::Projection(ProjectionTy { associated_ty, parameters }) | ||
99 | } | ||
100 | chalk_ir::TyData::Alias(chalk_ir::AliasTy::Opaque(_)) => unimplemented!(), | ||
101 | chalk_ir::TyData::Function(chalk_ir::Fn { num_binders: _, substitution }) => { | ||
102 | let parameters: Substs = from_chalk(db, substitution); | ||
103 | Ty::Apply(ApplicationTy { | ||
104 | ctor: TypeCtor::FnPtr { num_args: (parameters.len() - 1) as u16 }, | ||
105 | parameters, | ||
106 | }) | ||
107 | } | ||
108 | chalk_ir::TyData::BoundVar(idx) => Ty::Bound(idx), | ||
109 | chalk_ir::TyData::InferenceVar(_iv) => Ty::Unknown, | ||
110 | chalk_ir::TyData::Dyn(where_clauses) => { | ||
111 | assert_eq!(where_clauses.bounds.binders.len(&Interner), 1); | ||
112 | let predicates = where_clauses | ||
113 | .bounds | ||
114 | .skip_binders() | ||
115 | .iter(&Interner) | ||
116 | .map(|c| from_chalk(db, c.clone())) | ||
117 | .collect(); | ||
118 | Ty::Dyn(predicates) | ||
119 | } | ||
120 | } | ||
121 | } | ||
122 | } | ||
123 | |||
124 | const LIFETIME_PLACEHOLDER: PlaceholderIndex = | ||
125 | PlaceholderIndex { ui: UniverseIndex::ROOT, idx: usize::MAX }; | ||
126 | |||
127 | /// We currently don't model lifetimes, but Chalk does. So, we have to insert a | ||
128 | /// fake lifetime here, because Chalks built-in logic may expect it to be there. | ||
129 | fn ref_to_chalk( | ||
130 | db: &dyn HirDatabase, | ||
131 | mutability: Mutability, | ||
132 | subst: Substs, | ||
133 | ) -> chalk_ir::Ty<Interner> { | ||
134 | let arg = subst[0].clone().to_chalk(db); | ||
135 | let lifetime = LIFETIME_PLACEHOLDER.to_lifetime(&Interner); | ||
136 | chalk_ir::ApplicationTy { | ||
137 | name: TypeName::Ref(mutability.to_chalk(db)), | ||
138 | substitution: chalk_ir::Substitution::from( | ||
139 | &Interner, | ||
140 | vec![lifetime.cast(&Interner), arg.cast(&Interner)], | ||
141 | ), | ||
142 | } | ||
143 | .intern(&Interner) | ||
144 | } | ||
145 | |||
146 | /// Here we remove the lifetime from the type we got from Chalk. | ||
147 | fn ref_from_chalk( | ||
148 | db: &dyn HirDatabase, | ||
149 | mutability: chalk_ir::Mutability, | ||
150 | subst: chalk_ir::Substitution<Interner>, | ||
151 | ) -> Ty { | ||
152 | let tys = subst | ||
153 | .iter(&Interner) | ||
154 | .filter_map(|p| Some(from_chalk(db, p.ty(&Interner)?.clone()))) | ||
155 | .collect(); | ||
156 | Ty::apply(TypeCtor::Ref(from_chalk(db, mutability)), Substs(tys)) | ||
157 | } | ||
158 | |||
159 | impl ToChalk for Substs { | ||
160 | type Chalk = chalk_ir::Substitution<Interner>; | ||
161 | |||
162 | fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::Substitution<Interner> { | ||
163 | chalk_ir::Substitution::from(&Interner, self.iter().map(|ty| ty.clone().to_chalk(db))) | ||
164 | } | ||
165 | |||
166 | fn from_chalk(db: &dyn HirDatabase, parameters: chalk_ir::Substitution<Interner>) -> Substs { | ||
167 | let tys = parameters | ||
168 | .iter(&Interner) | ||
169 | .map(|p| match p.ty(&Interner) { | ||
170 | Some(ty) => from_chalk(db, ty.clone()), | ||
171 | None => unimplemented!(), | ||
172 | }) | ||
173 | .collect(); | ||
174 | Substs(tys) | ||
175 | } | ||
176 | } | ||
177 | |||
178 | impl ToChalk for TraitRef { | ||
179 | type Chalk = chalk_ir::TraitRef<Interner>; | ||
180 | |||
181 | fn to_chalk(self: TraitRef, db: &dyn HirDatabase) -> chalk_ir::TraitRef<Interner> { | ||
182 | let trait_id = self.trait_.to_chalk(db); | ||
183 | let substitution = self.substs.to_chalk(db); | ||
184 | chalk_ir::TraitRef { trait_id, substitution } | ||
185 | } | ||
186 | |||
187 | fn from_chalk(db: &dyn HirDatabase, trait_ref: chalk_ir::TraitRef<Interner>) -> Self { | ||
188 | let trait_ = from_chalk(db, trait_ref.trait_id); | ||
189 | let substs = from_chalk(db, trait_ref.substitution); | ||
190 | TraitRef { trait_, substs } | ||
191 | } | ||
192 | } | ||
193 | |||
194 | impl ToChalk for hir_def::TraitId { | ||
195 | type Chalk = TraitId; | ||
196 | |||
197 | fn to_chalk(self, _db: &dyn HirDatabase) -> TraitId { | ||
198 | chalk_ir::TraitId(self.as_intern_id()) | ||
199 | } | ||
200 | |||
201 | fn from_chalk(_db: &dyn HirDatabase, trait_id: TraitId) -> hir_def::TraitId { | ||
202 | InternKey::from_intern_id(trait_id.0) | ||
203 | } | ||
204 | } | ||
205 | |||
206 | impl ToChalk for TypeCtor { | ||
207 | type Chalk = TypeName<Interner>; | ||
208 | |||
209 | fn to_chalk(self, db: &dyn HirDatabase) -> TypeName<Interner> { | ||
210 | match self { | ||
211 | TypeCtor::AssociatedType(type_alias) => { | ||
212 | let type_id = type_alias.to_chalk(db); | ||
213 | TypeName::AssociatedType(type_id) | ||
214 | } | ||
215 | |||
216 | TypeCtor::Bool => TypeName::Scalar(Scalar::Bool), | ||
217 | TypeCtor::Char => TypeName::Scalar(Scalar::Char), | ||
218 | TypeCtor::Int(Uncertain::Known(int_ty)) => TypeName::Scalar(int_ty_to_chalk(int_ty)), | ||
219 | TypeCtor::Float(Uncertain::Known(FloatTy { bitness: FloatBitness::X32 })) => { | ||
220 | TypeName::Scalar(Scalar::Float(chalk_ir::FloatTy::F32)) | ||
221 | } | ||
222 | TypeCtor::Float(Uncertain::Known(FloatTy { bitness: FloatBitness::X64 })) => { | ||
223 | TypeName::Scalar(Scalar::Float(chalk_ir::FloatTy::F64)) | ||
224 | } | ||
225 | |||
226 | TypeCtor::Tuple { cardinality } => TypeName::Tuple(cardinality.into()), | ||
227 | TypeCtor::RawPtr(mutability) => TypeName::Raw(mutability.to_chalk(db)), | ||
228 | TypeCtor::Slice => TypeName::Slice, | ||
229 | TypeCtor::Ref(mutability) => TypeName::Ref(mutability.to_chalk(db)), | ||
230 | TypeCtor::Str => TypeName::Str, | ||
231 | TypeCtor::FnDef(callable_def) => { | ||
232 | let id = callable_def.to_chalk(db); | ||
233 | TypeName::FnDef(id) | ||
234 | } | ||
235 | TypeCtor::Int(Uncertain::Unknown) | ||
236 | | TypeCtor::Float(Uncertain::Unknown) | ||
237 | | TypeCtor::Adt(_) | ||
238 | | TypeCtor::Array | ||
239 | | TypeCtor::FnPtr { .. } | ||
240 | | TypeCtor::Never | ||
241 | | TypeCtor::Closure { .. } => { | ||
242 | // other TypeCtors get interned and turned into a chalk StructId | ||
243 | let struct_id = db.intern_type_ctor(self).into(); | ||
244 | TypeName::Adt(struct_id) | ||
245 | } | ||
246 | } | ||
247 | } | ||
248 | |||
249 | fn from_chalk(db: &dyn HirDatabase, type_name: TypeName<Interner>) -> TypeCtor { | ||
250 | match type_name { | ||
251 | TypeName::Adt(struct_id) => db.lookup_intern_type_ctor(struct_id.into()), | ||
252 | TypeName::AssociatedType(type_id) => TypeCtor::AssociatedType(from_chalk(db, type_id)), | ||
253 | TypeName::OpaqueType(_) => unreachable!(), | ||
254 | |||
255 | TypeName::Scalar(Scalar::Bool) => TypeCtor::Bool, | ||
256 | TypeName::Scalar(Scalar::Char) => TypeCtor::Char, | ||
257 | TypeName::Scalar(Scalar::Int(int_ty)) => TypeCtor::Int(Uncertain::Known(IntTy { | ||
258 | signedness: Signedness::Signed, | ||
259 | bitness: bitness_from_chalk_int(int_ty), | ||
260 | })), | ||
261 | TypeName::Scalar(Scalar::Uint(uint_ty)) => TypeCtor::Int(Uncertain::Known(IntTy { | ||
262 | signedness: Signedness::Unsigned, | ||
263 | bitness: bitness_from_chalk_uint(uint_ty), | ||
264 | })), | ||
265 | TypeName::Scalar(Scalar::Float(chalk_ir::FloatTy::F32)) => { | ||
266 | TypeCtor::Float(Uncertain::Known(FloatTy { bitness: FloatBitness::X32 })) | ||
267 | } | ||
268 | TypeName::Scalar(Scalar::Float(chalk_ir::FloatTy::F64)) => { | ||
269 | TypeCtor::Float(Uncertain::Known(FloatTy { bitness: FloatBitness::X64 })) | ||
270 | } | ||
271 | TypeName::Tuple(cardinality) => TypeCtor::Tuple { cardinality: cardinality as u16 }, | ||
272 | TypeName::Raw(mutability) => TypeCtor::RawPtr(from_chalk(db, mutability)), | ||
273 | TypeName::Slice => TypeCtor::Slice, | ||
274 | TypeName::Ref(mutability) => TypeCtor::Ref(from_chalk(db, mutability)), | ||
275 | TypeName::Str => TypeCtor::Str, | ||
276 | |||
277 | TypeName::FnDef(fn_def_id) => { | ||
278 | let callable_def = from_chalk(db, fn_def_id); | ||
279 | TypeCtor::FnDef(callable_def) | ||
280 | } | ||
281 | |||
282 | TypeName::Error => { | ||
283 | // this should not be reached, since we don't represent TypeName::Error with TypeCtor | ||
284 | unreachable!() | ||
285 | } | ||
286 | } | ||
287 | } | ||
288 | } | ||
289 | |||
290 | fn bitness_from_chalk_uint(uint_ty: chalk_ir::UintTy) -> IntBitness { | ||
291 | use chalk_ir::UintTy; | ||
292 | |||
293 | match uint_ty { | ||
294 | UintTy::Usize => IntBitness::Xsize, | ||
295 | UintTy::U8 => IntBitness::X8, | ||
296 | UintTy::U16 => IntBitness::X16, | ||
297 | UintTy::U32 => IntBitness::X32, | ||
298 | UintTy::U64 => IntBitness::X64, | ||
299 | UintTy::U128 => IntBitness::X128, | ||
300 | } | ||
301 | } | ||
302 | |||
303 | fn bitness_from_chalk_int(int_ty: chalk_ir::IntTy) -> IntBitness { | ||
304 | use chalk_ir::IntTy; | ||
305 | |||
306 | match int_ty { | ||
307 | IntTy::Isize => IntBitness::Xsize, | ||
308 | IntTy::I8 => IntBitness::X8, | ||
309 | IntTy::I16 => IntBitness::X16, | ||
310 | IntTy::I32 => IntBitness::X32, | ||
311 | IntTy::I64 => IntBitness::X64, | ||
312 | IntTy::I128 => IntBitness::X128, | ||
313 | } | ||
314 | } | ||
315 | |||
316 | fn int_ty_to_chalk(int_ty: IntTy) -> Scalar { | ||
317 | use chalk_ir::{IntTy, UintTy}; | ||
318 | |||
319 | match int_ty.signedness { | ||
320 | Signedness::Signed => Scalar::Int(match int_ty.bitness { | ||
321 | IntBitness::Xsize => IntTy::Isize, | ||
322 | IntBitness::X8 => IntTy::I8, | ||
323 | IntBitness::X16 => IntTy::I16, | ||
324 | IntBitness::X32 => IntTy::I32, | ||
325 | IntBitness::X64 => IntTy::I64, | ||
326 | IntBitness::X128 => IntTy::I128, | ||
327 | }), | ||
328 | Signedness::Unsigned => Scalar::Uint(match int_ty.bitness { | ||
329 | IntBitness::Xsize => UintTy::Usize, | ||
330 | IntBitness::X8 => UintTy::U8, | ||
331 | IntBitness::X16 => UintTy::U16, | ||
332 | IntBitness::X32 => UintTy::U32, | ||
333 | IntBitness::X64 => UintTy::U64, | ||
334 | IntBitness::X128 => UintTy::U128, | ||
335 | }), | ||
336 | } | ||
337 | } | ||
338 | |||
339 | impl ToChalk for Mutability { | ||
340 | type Chalk = chalk_ir::Mutability; | ||
341 | fn to_chalk(self, _db: &dyn HirDatabase) -> Self::Chalk { | ||
342 | match self { | ||
343 | Mutability::Shared => chalk_ir::Mutability::Not, | ||
344 | Mutability::Mut => chalk_ir::Mutability::Mut, | ||
345 | } | ||
346 | } | ||
347 | fn from_chalk(_db: &dyn HirDatabase, chalk: Self::Chalk) -> Self { | ||
348 | match chalk { | ||
349 | chalk_ir::Mutability::Mut => Mutability::Mut, | ||
350 | chalk_ir::Mutability::Not => Mutability::Shared, | ||
351 | } | ||
352 | } | ||
353 | } | ||
354 | |||
355 | impl ToChalk for Impl { | ||
356 | type Chalk = ImplId; | ||
357 | |||
358 | fn to_chalk(self, db: &dyn HirDatabase) -> ImplId { | ||
359 | db.intern_chalk_impl(self).into() | ||
360 | } | ||
361 | |||
362 | fn from_chalk(db: &dyn HirDatabase, impl_id: ImplId) -> Impl { | ||
363 | db.lookup_intern_chalk_impl(impl_id.into()) | ||
364 | } | ||
365 | } | ||
366 | |||
367 | impl ToChalk for CallableDef { | ||
368 | type Chalk = FnDefId; | ||
369 | |||
370 | fn to_chalk(self, db: &dyn HirDatabase) -> FnDefId { | ||
371 | db.intern_callable_def(self).into() | ||
372 | } | ||
373 | |||
374 | fn from_chalk(db: &dyn HirDatabase, fn_def_id: FnDefId) -> CallableDef { | ||
375 | db.lookup_intern_callable_def(fn_def_id.into()) | ||
376 | } | ||
377 | } | ||
378 | |||
379 | impl ToChalk for TypeAliasId { | ||
380 | type Chalk = AssocTypeId; | ||
381 | |||
382 | fn to_chalk(self, _db: &dyn HirDatabase) -> AssocTypeId { | ||
383 | chalk_ir::AssocTypeId(self.as_intern_id()) | ||
384 | } | ||
385 | |||
386 | fn from_chalk(_db: &dyn HirDatabase, type_alias_id: AssocTypeId) -> TypeAliasId { | ||
387 | InternKey::from_intern_id(type_alias_id.0) | ||
388 | } | ||
389 | } | ||
390 | |||
391 | impl ToChalk for AssocTyValue { | ||
392 | type Chalk = AssociatedTyValueId; | ||
393 | |||
394 | fn to_chalk(self, db: &dyn HirDatabase) -> AssociatedTyValueId { | ||
395 | db.intern_assoc_ty_value(self).into() | ||
396 | } | ||
397 | |||
398 | fn from_chalk(db: &dyn HirDatabase, assoc_ty_value_id: AssociatedTyValueId) -> AssocTyValue { | ||
399 | db.lookup_intern_assoc_ty_value(assoc_ty_value_id.into()) | ||
400 | } | ||
401 | } | ||
402 | |||
403 | impl ToChalk for GenericPredicate { | ||
404 | type Chalk = chalk_ir::QuantifiedWhereClause<Interner>; | ||
405 | |||
406 | fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::QuantifiedWhereClause<Interner> { | ||
407 | match self { | ||
408 | GenericPredicate::Implemented(trait_ref) => { | ||
409 | let chalk_trait_ref = trait_ref.to_chalk(db); | ||
410 | let chalk_trait_ref = chalk_trait_ref.shifted_in(&Interner); | ||
411 | make_binders(chalk_ir::WhereClause::Implemented(chalk_trait_ref), 0) | ||
412 | } | ||
413 | GenericPredicate::Projection(projection_pred) => { | ||
414 | let ty = projection_pred.ty.to_chalk(db).shifted_in(&Interner); | ||
415 | let projection = projection_pred.projection_ty.to_chalk(db).shifted_in(&Interner); | ||
416 | let alias = chalk_ir::AliasTy::Projection(projection); | ||
417 | make_binders(chalk_ir::WhereClause::AliasEq(chalk_ir::AliasEq { alias, ty }), 0) | ||
418 | } | ||
419 | GenericPredicate::Error => panic!("tried passing GenericPredicate::Error to Chalk"), | ||
420 | } | ||
421 | } | ||
422 | |||
423 | fn from_chalk( | ||
424 | db: &dyn HirDatabase, | ||
425 | where_clause: chalk_ir::QuantifiedWhereClause<Interner>, | ||
426 | ) -> GenericPredicate { | ||
427 | // we don't produce any where clauses with binders and can't currently deal with them | ||
428 | match where_clause | ||
429 | .skip_binders() | ||
430 | .shifted_out(&Interner) | ||
431 | .expect("unexpected bound vars in where clause") | ||
432 | { | ||
433 | chalk_ir::WhereClause::Implemented(tr) => { | ||
434 | GenericPredicate::Implemented(from_chalk(db, tr)) | ||
435 | } | ||
436 | chalk_ir::WhereClause::AliasEq(projection_eq) => { | ||
437 | let projection_ty = from_chalk( | ||
438 | db, | ||
439 | match projection_eq.alias { | ||
440 | chalk_ir::AliasTy::Projection(p) => p, | ||
441 | _ => unimplemented!(), | ||
442 | }, | ||
443 | ); | ||
444 | let ty = from_chalk(db, projection_eq.ty); | ||
445 | GenericPredicate::Projection(ProjectionPredicate { projection_ty, ty }) | ||
446 | } | ||
447 | } | ||
448 | } | ||
449 | } | ||
450 | |||
451 | impl ToChalk for ProjectionTy { | ||
452 | type Chalk = chalk_ir::ProjectionTy<Interner>; | ||
453 | |||
454 | fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::ProjectionTy<Interner> { | ||
455 | chalk_ir::ProjectionTy { | ||
456 | associated_ty_id: self.associated_ty.to_chalk(db), | ||
457 | substitution: self.parameters.to_chalk(db), | ||
458 | } | ||
459 | } | ||
460 | |||
461 | fn from_chalk( | ||
462 | db: &dyn HirDatabase, | ||
463 | projection_ty: chalk_ir::ProjectionTy<Interner>, | ||
464 | ) -> ProjectionTy { | ||
465 | ProjectionTy { | ||
466 | associated_ty: from_chalk(db, projection_ty.associated_ty_id), | ||
467 | parameters: from_chalk(db, projection_ty.substitution), | ||
468 | } | ||
469 | } | ||
470 | } | ||
471 | |||
472 | impl ToChalk for ProjectionPredicate { | ||
473 | type Chalk = chalk_ir::AliasEq<Interner>; | ||
474 | |||
475 | fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::AliasEq<Interner> { | ||
476 | chalk_ir::AliasEq { | ||
477 | alias: chalk_ir::AliasTy::Projection(self.projection_ty.to_chalk(db)), | ||
478 | ty: self.ty.to_chalk(db), | ||
479 | } | ||
480 | } | ||
481 | |||
482 | fn from_chalk(_db: &dyn HirDatabase, _normalize: chalk_ir::AliasEq<Interner>) -> Self { | ||
483 | unimplemented!() | ||
484 | } | ||
485 | } | ||
486 | |||
487 | impl ToChalk for Obligation { | ||
488 | type Chalk = chalk_ir::DomainGoal<Interner>; | ||
489 | |||
490 | fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::DomainGoal<Interner> { | ||
491 | match self { | ||
492 | Obligation::Trait(tr) => tr.to_chalk(db).cast(&Interner), | ||
493 | Obligation::Projection(pr) => pr.to_chalk(db).cast(&Interner), | ||
494 | } | ||
495 | } | ||
496 | |||
497 | fn from_chalk(_db: &dyn HirDatabase, _goal: chalk_ir::DomainGoal<Interner>) -> Self { | ||
498 | unimplemented!() | ||
499 | } | ||
500 | } | ||
501 | |||
502 | impl<T> ToChalk for Canonical<T> | ||
503 | where | ||
504 | T: ToChalk, | ||
505 | T::Chalk: HasInterner<Interner = Interner>, | ||
506 | { | ||
507 | type Chalk = chalk_ir::Canonical<T::Chalk>; | ||
508 | |||
509 | fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::Canonical<T::Chalk> { | ||
510 | let parameter = chalk_ir::CanonicalVarKind::new( | ||
511 | chalk_ir::VariableKind::Ty, | ||
512 | chalk_ir::UniverseIndex::ROOT, | ||
513 | ); | ||
514 | let value = self.value.to_chalk(db); | ||
515 | chalk_ir::Canonical { | ||
516 | value, | ||
517 | binders: chalk_ir::CanonicalVarKinds::from(&Interner, vec![parameter; self.num_vars]), | ||
518 | } | ||
519 | } | ||
520 | |||
521 | fn from_chalk(db: &dyn HirDatabase, canonical: chalk_ir::Canonical<T::Chalk>) -> Canonical<T> { | ||
522 | Canonical { | ||
523 | num_vars: canonical.binders.len(&Interner), | ||
524 | value: from_chalk(db, canonical.value), | ||
525 | } | ||
526 | } | ||
527 | } | ||
528 | |||
529 | impl ToChalk for Arc<TraitEnvironment> { | ||
530 | type Chalk = chalk_ir::Environment<Interner>; | ||
531 | |||
532 | fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::Environment<Interner> { | ||
533 | let mut clauses = Vec::new(); | ||
534 | for pred in &self.predicates { | ||
535 | if pred.is_error() { | ||
536 | // for env, we just ignore errors | ||
537 | continue; | ||
538 | } | ||
539 | let program_clause: chalk_ir::ProgramClause<Interner> = | ||
540 | pred.clone().to_chalk(db).cast(&Interner); | ||
541 | clauses.push(program_clause.into_from_env_clause(&Interner)); | ||
542 | } | ||
543 | chalk_ir::Environment::new(&Interner).add_clauses(&Interner, clauses) | ||
544 | } | ||
545 | |||
546 | fn from_chalk( | ||
547 | _db: &dyn HirDatabase, | ||
548 | _env: chalk_ir::Environment<Interner>, | ||
549 | ) -> Arc<TraitEnvironment> { | ||
550 | unimplemented!() | ||
551 | } | ||
552 | } | ||
553 | |||
554 | impl<T: ToChalk> ToChalk for InEnvironment<T> | ||
555 | where | ||
556 | T::Chalk: chalk_ir::interner::HasInterner<Interner = Interner>, | ||
557 | { | ||
558 | type Chalk = chalk_ir::InEnvironment<T::Chalk>; | ||
559 | |||
560 | fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::InEnvironment<T::Chalk> { | ||
561 | chalk_ir::InEnvironment { | ||
562 | environment: self.environment.to_chalk(db), | ||
563 | goal: self.value.to_chalk(db), | ||
564 | } | ||
565 | } | ||
566 | |||
567 | fn from_chalk( | ||
568 | db: &dyn HirDatabase, | ||
569 | in_env: chalk_ir::InEnvironment<T::Chalk>, | ||
570 | ) -> InEnvironment<T> { | ||
571 | InEnvironment { | ||
572 | environment: from_chalk(db, in_env.environment), | ||
573 | value: from_chalk(db, in_env.goal), | ||
574 | } | ||
575 | } | ||
576 | } | ||
577 | |||
578 | impl ToChalk for builtin::BuiltinImplData { | ||
579 | type Chalk = ImplDatum; | ||
580 | |||
581 | fn to_chalk(self, db: &dyn HirDatabase) -> ImplDatum { | ||
582 | let impl_type = chalk_rust_ir::ImplType::External; | ||
583 | let where_clauses = self.where_clauses.into_iter().map(|w| w.to_chalk(db)).collect(); | ||
584 | |||
585 | let impl_datum_bound = | ||
586 | chalk_rust_ir::ImplDatumBound { trait_ref: self.trait_ref.to_chalk(db), where_clauses }; | ||
587 | let associated_ty_value_ids = | ||
588 | self.assoc_ty_values.into_iter().map(|v| v.to_chalk(db)).collect(); | ||
589 | chalk_rust_ir::ImplDatum { | ||
590 | binders: make_binders(impl_datum_bound, self.num_vars), | ||
591 | impl_type, | ||
592 | polarity: chalk_rust_ir::Polarity::Positive, | ||
593 | associated_ty_value_ids, | ||
594 | } | ||
595 | } | ||
596 | |||
597 | fn from_chalk(_db: &dyn HirDatabase, _data: ImplDatum) -> Self { | ||
598 | unimplemented!() | ||
599 | } | ||
600 | } | ||
601 | |||
602 | impl ToChalk for builtin::BuiltinImplAssocTyValueData { | ||
603 | type Chalk = AssociatedTyValue; | ||
604 | |||
605 | fn to_chalk(self, db: &dyn HirDatabase) -> AssociatedTyValue { | ||
606 | let ty = self.value.to_chalk(db); | ||
607 | let value_bound = chalk_rust_ir::AssociatedTyValueBound { ty }; | ||
608 | |||
609 | chalk_rust_ir::AssociatedTyValue { | ||
610 | associated_ty_id: self.assoc_ty_id.to_chalk(db), | ||
611 | impl_id: self.impl_.to_chalk(db), | ||
612 | value: make_binders(value_bound, self.num_vars), | ||
613 | } | ||
614 | } | ||
615 | |||
616 | fn from_chalk( | ||
617 | _db: &dyn HirDatabase, | ||
618 | _data: AssociatedTyValue, | ||
619 | ) -> builtin::BuiltinImplAssocTyValueData { | ||
620 | unimplemented!() | ||
621 | } | ||
622 | } | ||
623 | |||
624 | pub(super) fn make_binders<T>(value: T, num_vars: usize) -> chalk_ir::Binders<T> | ||
625 | where | ||
626 | T: HasInterner<Interner = Interner>, | ||
627 | { | ||
628 | chalk_ir::Binders::new( | ||
629 | chalk_ir::VariableKinds::from( | ||
630 | &Interner, | ||
631 | std::iter::repeat(chalk_ir::VariableKind::Ty).take(num_vars), | ||
632 | ), | ||
633 | value, | ||
634 | ) | ||
635 | } | ||
636 | |||
637 | pub(super) fn convert_where_clauses( | ||
638 | db: &dyn HirDatabase, | ||
639 | def: GenericDefId, | ||
640 | substs: &Substs, | ||
641 | ) -> Vec<chalk_ir::QuantifiedWhereClause<Interner>> { | ||
642 | let generic_predicates = db.generic_predicates(def); | ||
643 | let mut result = Vec::with_capacity(generic_predicates.len()); | ||
644 | for pred in generic_predicates.iter() { | ||
645 | if pred.value.is_error() { | ||
646 | // skip errored predicates completely | ||
647 | continue; | ||
648 | } | ||
649 | result.push(pred.clone().subst(substs).to_chalk(db)); | ||
650 | } | ||
651 | result | ||
652 | } | ||
653 | |||
654 | pub(super) fn generic_predicate_to_inline_bound( | ||
655 | db: &dyn HirDatabase, | ||
656 | pred: &GenericPredicate, | ||
657 | self_ty: &Ty, | ||
658 | ) -> Option<chalk_rust_ir::InlineBound<Interner>> { | ||
659 | // An InlineBound is like a GenericPredicate, except the self type is left out. | ||
660 | // We don't have a special type for this, but Chalk does. | ||
661 | match pred { | ||
662 | GenericPredicate::Implemented(trait_ref) => { | ||
663 | if &trait_ref.substs[0] != self_ty { | ||
664 | // we can only convert predicates back to type bounds if they | ||
665 | // have the expected self type | ||
666 | return None; | ||
667 | } | ||
668 | let args_no_self = trait_ref.substs[1..] | ||
669 | .iter() | ||
670 | .map(|ty| ty.clone().to_chalk(db).cast(&Interner)) | ||
671 | .collect(); | ||
672 | let trait_bound = | ||
673 | chalk_rust_ir::TraitBound { trait_id: trait_ref.trait_.to_chalk(db), args_no_self }; | ||
674 | Some(chalk_rust_ir::InlineBound::TraitBound(trait_bound)) | ||
675 | } | ||
676 | GenericPredicate::Projection(proj) => { | ||
677 | if &proj.projection_ty.parameters[0] != self_ty { | ||
678 | return None; | ||
679 | } | ||
680 | let trait_ = match proj.projection_ty.associated_ty.lookup(db.upcast()).container { | ||
681 | AssocContainerId::TraitId(t) => t, | ||
682 | _ => panic!("associated type not in trait"), | ||
683 | }; | ||
684 | let args_no_self = proj.projection_ty.parameters[1..] | ||
685 | .iter() | ||
686 | .map(|ty| ty.clone().to_chalk(db).cast(&Interner)) | ||
687 | .collect(); | ||
688 | let alias_eq_bound = chalk_rust_ir::AliasEqBound { | ||
689 | value: proj.ty.clone().to_chalk(db), | ||
690 | trait_bound: chalk_rust_ir::TraitBound { | ||
691 | trait_id: trait_.to_chalk(db), | ||
692 | args_no_self, | ||
693 | }, | ||
694 | associated_ty_id: proj.projection_ty.associated_ty.to_chalk(db), | ||
695 | parameters: Vec::new(), // FIXME we don't support generic associated types yet | ||
696 | }; | ||
697 | Some(chalk_rust_ir::InlineBound::AliasEqBound(alias_eq_bound)) | ||
698 | } | ||
699 | GenericPredicate::Error => None, | ||
700 | } | ||
701 | } | ||
diff --git a/crates/ra_hir_ty/src/traits/chalk/tls.rs b/crates/ra_hir_ty/src/traits/chalk/tls.rs index 4867cb17e..d88828c7c 100644 --- a/crates/ra_hir_ty/src/traits/chalk/tls.rs +++ b/crates/ra_hir_ty/src/traits/chalk/tls.rs | |||
@@ -1,7 +1,7 @@ | |||
1 | //! Implementation of Chalk debug helper functions using TLS. | 1 | //! Implementation of Chalk debug helper functions using TLS. |
2 | use std::fmt; | 2 | use std::fmt; |
3 | 3 | ||
4 | use chalk_ir::{AliasTy, Goal, Goals, Lifetime, Parameter, ProgramClauseImplication, TypeName}; | 4 | use chalk_ir::{AliasTy, GenericArg, Goal, Goals, Lifetime, ProgramClauseImplication, TypeName}; |
5 | use itertools::Itertools; | 5 | use itertools::Itertools; |
6 | 6 | ||
7 | use super::{from_chalk, Interner}; | 7 | use super::{from_chalk, Interner}; |
@@ -15,10 +15,10 @@ pub struct DebugContext<'a>(&'a (dyn HirDatabase + 'a)); | |||
15 | impl DebugContext<'_> { | 15 | impl DebugContext<'_> { |
16 | pub fn debug_struct_id( | 16 | pub fn debug_struct_id( |
17 | &self, | 17 | &self, |
18 | id: super::StructId, | 18 | id: super::AdtId, |
19 | f: &mut fmt::Formatter<'_>, | 19 | f: &mut fmt::Formatter<'_>, |
20 | ) -> Result<(), fmt::Error> { | 20 | ) -> Result<(), fmt::Error> { |
21 | let type_ctor: TypeCtor = from_chalk(self.0, TypeName::Struct(id)); | 21 | let type_ctor: TypeCtor = from_chalk(self.0, TypeName::Adt(id)); |
22 | match type_ctor { | 22 | match type_ctor { |
23 | TypeCtor::Bool => write!(f, "bool")?, | 23 | TypeCtor::Bool => write!(f, "bool")?, |
24 | TypeCtor::Char => write!(f, "char")?, | 24 | TypeCtor::Char => write!(f, "char")?, |
@@ -188,9 +188,9 @@ impl DebugContext<'_> { | |||
188 | write!(fmt, "{:?}", lifetime.data(&Interner)) | 188 | write!(fmt, "{:?}", lifetime.data(&Interner)) |
189 | } | 189 | } |
190 | 190 | ||
191 | pub fn debug_parameter( | 191 | pub fn debug_generic_arg( |
192 | &self, | 192 | &self, |
193 | parameter: &Parameter<Interner>, | 193 | parameter: &GenericArg<Interner>, |
194 | fmt: &mut fmt::Formatter<'_>, | 194 | fmt: &mut fmt::Formatter<'_>, |
195 | ) -> Result<(), fmt::Error> { | 195 | ) -> Result<(), fmt::Error> { |
196 | write!(fmt, "{:?}", parameter.data(&Interner).inner_debug()) | 196 | write!(fmt, "{:?}", parameter.data(&Interner).inner_debug()) |
@@ -244,6 +244,79 @@ impl DebugContext<'_> { | |||
244 | ) -> Result<(), fmt::Error> { | 244 | ) -> Result<(), fmt::Error> { |
245 | write!(fmt, "{:?}", separator_trait_ref.debug(&Interner)) | 245 | write!(fmt, "{:?}", separator_trait_ref.debug(&Interner)) |
246 | } | 246 | } |
247 | |||
248 | pub fn debug_fn_def_id( | ||
249 | &self, | ||
250 | fn_def_id: chalk_ir::FnDefId<Interner>, | ||
251 | fmt: &mut fmt::Formatter<'_>, | ||
252 | ) -> Result<(), fmt::Error> { | ||
253 | let def: CallableDef = from_chalk(self.0, fn_def_id); | ||
254 | let name = match def { | ||
255 | CallableDef::FunctionId(ff) => self.0.function_data(ff).name.clone(), | ||
256 | CallableDef::StructId(s) => self.0.struct_data(s).name.clone(), | ||
257 | CallableDef::EnumVariantId(e) => { | ||
258 | let enum_data = self.0.enum_data(e.parent); | ||
259 | enum_data.variants[e.local_id].name.clone() | ||
260 | } | ||
261 | }; | ||
262 | match def { | ||
263 | CallableDef::FunctionId(_) => write!(fmt, "{{fn {}}}", name), | ||
264 | CallableDef::StructId(_) | CallableDef::EnumVariantId(_) => { | ||
265 | write!(fmt, "{{ctor {}}}", name) | ||
266 | } | ||
267 | } | ||
268 | } | ||
269 | |||
270 | pub fn debug_const( | ||
271 | &self, | ||
272 | _constant: &chalk_ir::Const<Interner>, | ||
273 | fmt: &mut fmt::Formatter<'_>, | ||
274 | ) -> fmt::Result { | ||
275 | write!(fmt, "const") | ||
276 | } | ||
277 | |||
278 | pub fn debug_variable_kinds( | ||
279 | &self, | ||
280 | variable_kinds: &chalk_ir::VariableKinds<Interner>, | ||
281 | fmt: &mut fmt::Formatter<'_>, | ||
282 | ) -> fmt::Result { | ||
283 | write!(fmt, "{:?}", variable_kinds.as_slice(&Interner)) | ||
284 | } | ||
285 | pub fn debug_variable_kinds_with_angles( | ||
286 | &self, | ||
287 | variable_kinds: &chalk_ir::VariableKinds<Interner>, | ||
288 | fmt: &mut fmt::Formatter<'_>, | ||
289 | ) -> fmt::Result { | ||
290 | write!(fmt, "{:?}", variable_kinds.inner_debug(&Interner)) | ||
291 | } | ||
292 | pub fn debug_canonical_var_kinds( | ||
293 | &self, | ||
294 | canonical_var_kinds: &chalk_ir::CanonicalVarKinds<Interner>, | ||
295 | fmt: &mut fmt::Formatter<'_>, | ||
296 | ) -> fmt::Result { | ||
297 | write!(fmt, "{:?}", canonical_var_kinds.as_slice(&Interner)) | ||
298 | } | ||
299 | pub fn debug_program_clause( | ||
300 | &self, | ||
301 | clause: &chalk_ir::ProgramClause<Interner>, | ||
302 | fmt: &mut fmt::Formatter<'_>, | ||
303 | ) -> fmt::Result { | ||
304 | write!(fmt, "{:?}", clause.data(&Interner)) | ||
305 | } | ||
306 | pub fn debug_program_clauses( | ||
307 | &self, | ||
308 | clauses: &chalk_ir::ProgramClauses<Interner>, | ||
309 | fmt: &mut fmt::Formatter<'_>, | ||
310 | ) -> fmt::Result { | ||
311 | write!(fmt, "{:?}", clauses.as_slice(&Interner)) | ||
312 | } | ||
313 | pub fn debug_quantified_where_clauses( | ||
314 | &self, | ||
315 | clauses: &chalk_ir::QuantifiedWhereClauses<Interner>, | ||
316 | fmt: &mut fmt::Formatter<'_>, | ||
317 | ) -> fmt::Result { | ||
318 | write!(fmt, "{:?}", clauses.as_slice(&Interner)) | ||
319 | } | ||
247 | } | 320 | } |
248 | 321 | ||
249 | mod unsafe_tls { | 322 | mod unsafe_tls { |