diff options
Diffstat (limited to 'crates/ra_hir_ty/src/traits')
-rw-r--r-- | crates/ra_hir_ty/src/traits/chalk.rs | 899 | ||||
-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 |
4 files changed, 1227 insertions, 809 deletions
diff --git a/crates/ra_hir_ty/src/traits/chalk.rs b/crates/ra_hir_ty/src/traits/chalk.rs index 5870618a0..5b0f12a3c 100644 --- a/crates/ra_hir_ty/src/traits/chalk.rs +++ b/crates/ra_hir_ty/src/traits/chalk.rs | |||
@@ -1,301 +1,29 @@ | |||
1 | //! Conversion code from/to Chalk. | 1 | //! Conversion code from/to Chalk. |
2 | use std::{fmt, sync::Arc}; | 2 | use std::sync::Arc; |
3 | 3 | ||
4 | use log::debug; | 4 | use log::debug; |
5 | 5 | ||
6 | use chalk_ir::{ | 6 | use chalk_ir::{fold::shift::Shift, GenericArg, TypeName}; |
7 | cast::Cast, fold::shift::Shift, interner::HasInterner, Goal, GoalData, Parameter, | ||
8 | PlaceholderIndex, TypeName, UniverseIndex, | ||
9 | }; | ||
10 | 7 | ||
11 | use hir_def::{AssocContainerId, AssocItemId, GenericDefId, HasModule, Lookup, TypeAliasId}; | 8 | use hir_def::{ |
12 | use ra_db::{ | 9 | lang_item::{lang_attr, LangItemTarget}, |
13 | salsa::{InternId, InternKey}, | 10 | AssocContainerId, AssocItemId, HasModule, Lookup, TypeAliasId, |
14 | CrateId, | ||
15 | }; | 11 | }; |
12 | use ra_db::{salsa::InternKey, CrateId}; | ||
16 | 13 | ||
17 | use super::{builtin, AssocTyValue, Canonical, ChalkContext, Impl, Obligation}; | 14 | use super::{builtin, AssocTyValue, ChalkContext, Impl}; |
18 | use crate::{ | 15 | use crate::{ |
19 | db::HirDatabase, display::HirDisplay, method_resolution::TyFingerprint, utils::generics, | 16 | db::HirDatabase, display::HirDisplay, method_resolution::TyFingerprint, utils::generics, |
20 | ApplicationTy, DebruijnIndex, GenericPredicate, ProjectionTy, Substs, TraitRef, Ty, TypeCtor, | 17 | CallableDef, DebruijnIndex, GenericPredicate, Substs, Ty, TypeCtor, |
21 | }; | 18 | }; |
19 | use chalk_rust_ir::WellKnownTrait; | ||
20 | use mapping::{convert_where_clauses, generic_predicate_to_inline_bound, make_binders}; | ||
22 | 21 | ||
23 | pub(super) mod tls; | 22 | pub use self::interner::*; |
24 | |||
25 | #[derive(Debug, Copy, Clone, Hash, PartialOrd, Ord, PartialEq, Eq)] | ||
26 | pub struct Interner; | ||
27 | |||
28 | impl chalk_ir::interner::Interner for Interner { | ||
29 | type InternedType = Box<chalk_ir::TyData<Self>>; | ||
30 | type InternedLifetime = chalk_ir::LifetimeData<Self>; | ||
31 | type InternedParameter = chalk_ir::ParameterData<Self>; | ||
32 | type InternedGoal = Arc<GoalData<Self>>; | ||
33 | type InternedGoals = Vec<Goal<Self>>; | ||
34 | type InternedSubstitution = Vec<Parameter<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 InternedParameterKinds = Vec<chalk_ir::ParameterKind<()>>; | ||
39 | type InternedCanonicalVarKinds = Vec<chalk_ir::ParameterKind<UniverseIndex>>; | ||
40 | type Identifier = TypeAliasId; | ||
41 | type DefId = InternId; | ||
42 | |||
43 | fn debug_struct_id( | ||
44 | type_kind_id: StructId, | ||
45 | fmt: &mut fmt::Formatter<'_>, | ||
46 | ) -> Option<fmt::Result> { | ||
47 | tls::with_current_program(|prog| Some(prog?.debug_struct_id(type_kind_id, fmt))) | ||
48 | } | ||
49 | |||
50 | fn debug_trait_id(type_kind_id: TraitId, fmt: &mut fmt::Formatter<'_>) -> Option<fmt::Result> { | ||
51 | tls::with_current_program(|prog| Some(prog?.debug_trait_id(type_kind_id, fmt))) | ||
52 | } | ||
53 | |||
54 | fn debug_assoc_type_id(id: AssocTypeId, fmt: &mut fmt::Formatter<'_>) -> Option<fmt::Result> { | ||
55 | tls::with_current_program(|prog| Some(prog?.debug_assoc_type_id(id, fmt))) | ||
56 | } | ||
57 | |||
58 | fn debug_alias( | ||
59 | alias: &chalk_ir::AliasTy<Interner>, | ||
60 | fmt: &mut fmt::Formatter<'_>, | ||
61 | ) -> Option<fmt::Result> { | ||
62 | tls::with_current_program(|prog| Some(prog?.debug_alias(alias, fmt))) | ||
63 | } | ||
64 | |||
65 | fn debug_projection_ty( | ||
66 | proj: &chalk_ir::ProjectionTy<Interner>, | ||
67 | fmt: &mut fmt::Formatter<'_>, | ||
68 | ) -> Option<fmt::Result> { | ||
69 | tls::with_current_program(|prog| Some(prog?.debug_projection_ty(proj, fmt))) | ||
70 | } | ||
71 | |||
72 | fn debug_opaque_ty( | ||
73 | opaque_ty: &chalk_ir::OpaqueTy<Interner>, | ||
74 | fmt: &mut fmt::Formatter<'_>, | ||
75 | ) -> Option<fmt::Result> { | ||
76 | tls::with_current_program(|prog| Some(prog?.debug_opaque_ty(opaque_ty, fmt))) | ||
77 | } | ||
78 | |||
79 | fn debug_opaque_ty_id( | ||
80 | opaque_ty_id: chalk_ir::OpaqueTyId<Self>, | ||
81 | fmt: &mut fmt::Formatter<'_>, | ||
82 | ) -> Option<fmt::Result> { | ||
83 | tls::with_current_program(|prog| Some(prog?.debug_opaque_ty_id(opaque_ty_id, fmt))) | ||
84 | } | ||
85 | |||
86 | fn debug_ty(ty: &chalk_ir::Ty<Interner>, fmt: &mut fmt::Formatter<'_>) -> Option<fmt::Result> { | ||
87 | tls::with_current_program(|prog| Some(prog?.debug_ty(ty, fmt))) | ||
88 | } | ||
89 | |||
90 | fn debug_lifetime( | ||
91 | lifetime: &chalk_ir::Lifetime<Interner>, | ||
92 | fmt: &mut fmt::Formatter<'_>, | ||
93 | ) -> Option<fmt::Result> { | ||
94 | tls::with_current_program(|prog| Some(prog?.debug_lifetime(lifetime, fmt))) | ||
95 | } | ||
96 | |||
97 | fn debug_parameter( | ||
98 | parameter: &Parameter<Interner>, | ||
99 | fmt: &mut fmt::Formatter<'_>, | ||
100 | ) -> Option<fmt::Result> { | ||
101 | tls::with_current_program(|prog| Some(prog?.debug_parameter(parameter, fmt))) | ||
102 | } | ||
103 | |||
104 | fn debug_goal(goal: &Goal<Interner>, fmt: &mut fmt::Formatter<'_>) -> Option<fmt::Result> { | ||
105 | tls::with_current_program(|prog| Some(prog?.debug_goal(goal, fmt))) | ||
106 | } | ||
107 | |||
108 | fn debug_goals( | ||
109 | goals: &chalk_ir::Goals<Interner>, | ||
110 | fmt: &mut fmt::Formatter<'_>, | ||
111 | ) -> Option<fmt::Result> { | ||
112 | tls::with_current_program(|prog| Some(prog?.debug_goals(goals, fmt))) | ||
113 | } | ||
114 | |||
115 | fn debug_program_clause_implication( | ||
116 | pci: &chalk_ir::ProgramClauseImplication<Interner>, | ||
117 | fmt: &mut fmt::Formatter<'_>, | ||
118 | ) -> Option<fmt::Result> { | ||
119 | tls::with_current_program(|prog| Some(prog?.debug_program_clause_implication(pci, fmt))) | ||
120 | } | ||
121 | |||
122 | fn debug_application_ty( | ||
123 | application_ty: &chalk_ir::ApplicationTy<Interner>, | ||
124 | fmt: &mut fmt::Formatter<'_>, | ||
125 | ) -> Option<fmt::Result> { | ||
126 | tls::with_current_program(|prog| Some(prog?.debug_application_ty(application_ty, fmt))) | ||
127 | } | ||
128 | |||
129 | fn debug_substitution( | ||
130 | substitution: &chalk_ir::Substitution<Interner>, | ||
131 | fmt: &mut fmt::Formatter<'_>, | ||
132 | ) -> Option<fmt::Result> { | ||
133 | tls::with_current_program(|prog| Some(prog?.debug_substitution(substitution, fmt))) | ||
134 | } | ||
135 | |||
136 | fn debug_separator_trait_ref( | ||
137 | separator_trait_ref: &chalk_ir::SeparatorTraitRef<Interner>, | ||
138 | fmt: &mut fmt::Formatter<'_>, | ||
139 | ) -> Option<fmt::Result> { | ||
140 | tls::with_current_program(|prog| { | ||
141 | Some(prog?.debug_separator_trait_ref(separator_trait_ref, fmt)) | ||
142 | }) | ||
143 | } | ||
144 | |||
145 | fn intern_ty(&self, ty: chalk_ir::TyData<Self>) -> Box<chalk_ir::TyData<Self>> { | ||
146 | Box::new(ty) | ||
147 | } | ||
148 | |||
149 | fn ty_data<'a>(&self, ty: &'a Box<chalk_ir::TyData<Self>>) -> &'a chalk_ir::TyData<Self> { | ||
150 | ty | ||
151 | } | ||
152 | |||
153 | fn intern_lifetime( | ||
154 | &self, | ||
155 | lifetime: chalk_ir::LifetimeData<Self>, | ||
156 | ) -> chalk_ir::LifetimeData<Self> { | ||
157 | lifetime | ||
158 | } | ||
159 | |||
160 | fn lifetime_data<'a>( | ||
161 | &self, | ||
162 | lifetime: &'a chalk_ir::LifetimeData<Self>, | ||
163 | ) -> &'a chalk_ir::LifetimeData<Self> { | ||
164 | lifetime | ||
165 | } | ||
166 | |||
167 | fn intern_parameter( | ||
168 | &self, | ||
169 | parameter: chalk_ir::ParameterData<Self>, | ||
170 | ) -> chalk_ir::ParameterData<Self> { | ||
171 | parameter | ||
172 | } | ||
173 | |||
174 | fn parameter_data<'a>( | ||
175 | &self, | ||
176 | parameter: &'a chalk_ir::ParameterData<Self>, | ||
177 | ) -> &'a chalk_ir::ParameterData<Self> { | ||
178 | parameter | ||
179 | } | ||
180 | |||
181 | fn intern_goal(&self, goal: GoalData<Self>) -> Arc<GoalData<Self>> { | ||
182 | Arc::new(goal) | ||
183 | } | ||
184 | |||
185 | fn intern_goals<E>( | ||
186 | &self, | ||
187 | data: impl IntoIterator<Item = Result<Goal<Self>, E>>, | ||
188 | ) -> Result<Self::InternedGoals, E> { | ||
189 | data.into_iter().collect() | ||
190 | } | ||
191 | |||
192 | fn goal_data<'a>(&self, goal: &'a Arc<GoalData<Self>>) -> &'a GoalData<Self> { | ||
193 | goal | ||
194 | } | ||
195 | |||
196 | fn goals_data<'a>(&self, goals: &'a Vec<Goal<Interner>>) -> &'a [Goal<Interner>] { | ||
197 | goals | ||
198 | } | ||
199 | |||
200 | fn intern_substitution<E>( | ||
201 | &self, | ||
202 | data: impl IntoIterator<Item = Result<Parameter<Self>, E>>, | ||
203 | ) -> Result<Vec<Parameter<Self>>, E> { | ||
204 | data.into_iter().collect() | ||
205 | } | ||
206 | |||
207 | fn substitution_data<'a>( | ||
208 | &self, | ||
209 | substitution: &'a Vec<Parameter<Self>>, | ||
210 | ) -> &'a [Parameter<Self>] { | ||
211 | substitution | ||
212 | } | ||
213 | |||
214 | fn intern_program_clause( | ||
215 | &self, | ||
216 | data: chalk_ir::ProgramClauseData<Self>, | ||
217 | ) -> chalk_ir::ProgramClauseData<Self> { | ||
218 | data | ||
219 | } | ||
220 | |||
221 | fn program_clause_data<'a>( | ||
222 | &self, | ||
223 | clause: &'a chalk_ir::ProgramClauseData<Self>, | ||
224 | ) -> &'a chalk_ir::ProgramClauseData<Self> { | ||
225 | clause | ||
226 | } | ||
227 | |||
228 | fn intern_program_clauses<E>( | ||
229 | &self, | ||
230 | data: impl IntoIterator<Item = Result<chalk_ir::ProgramClause<Self>, E>>, | ||
231 | ) -> Result<Arc<[chalk_ir::ProgramClause<Self>]>, E> { | ||
232 | data.into_iter().collect() | ||
233 | } | ||
234 | |||
235 | fn program_clauses_data<'a>( | ||
236 | &self, | ||
237 | clauses: &'a Arc<[chalk_ir::ProgramClause<Self>]>, | ||
238 | ) -> &'a [chalk_ir::ProgramClause<Self>] { | ||
239 | &clauses | ||
240 | } | ||
241 | |||
242 | fn intern_quantified_where_clauses<E>( | ||
243 | &self, | ||
244 | data: impl IntoIterator<Item = Result<chalk_ir::QuantifiedWhereClause<Self>, E>>, | ||
245 | ) -> Result<Self::InternedQuantifiedWhereClauses, E> { | ||
246 | data.into_iter().collect() | ||
247 | } | ||
248 | |||
249 | fn quantified_where_clauses_data<'a>( | ||
250 | &self, | ||
251 | clauses: &'a Self::InternedQuantifiedWhereClauses, | ||
252 | ) -> &'a [chalk_ir::QuantifiedWhereClause<Self>] { | ||
253 | clauses | ||
254 | } | ||
255 | |||
256 | fn intern_parameter_kinds<E>( | ||
257 | &self, | ||
258 | data: impl IntoIterator<Item = Result<chalk_ir::ParameterKind<()>, E>>, | ||
259 | ) -> Result<Self::InternedParameterKinds, E> { | ||
260 | data.into_iter().collect() | ||
261 | } | ||
262 | 23 | ||
263 | fn parameter_kinds_data<'a>( | 24 | pub(super) mod tls; |
264 | &self, | 25 | mod interner; |
265 | parameter_kinds: &'a Self::InternedParameterKinds, | 26 | mod mapping; |
266 | ) -> &'a [chalk_ir::ParameterKind<()>] { | ||
267 | ¶meter_kinds | ||
268 | } | ||
269 | |||
270 | fn intern_canonical_var_kinds<E>( | ||
271 | &self, | ||
272 | data: impl IntoIterator<Item = Result<chalk_ir::ParameterKind<UniverseIndex>, E>>, | ||
273 | ) -> Result<Self::InternedCanonicalVarKinds, E> { | ||
274 | data.into_iter().collect() | ||
275 | } | ||
276 | |||
277 | fn canonical_var_kinds_data<'a>( | ||
278 | &self, | ||
279 | canonical_var_kinds: &'a Self::InternedCanonicalVarKinds, | ||
280 | ) -> &'a [chalk_ir::ParameterKind<UniverseIndex>] { | ||
281 | &canonical_var_kinds | ||
282 | } | ||
283 | } | ||
284 | |||
285 | impl chalk_ir::interner::HasInterner for Interner { | ||
286 | type Interner = Self; | ||
287 | } | ||
288 | |||
289 | pub type AssocTypeId = chalk_ir::AssocTypeId<Interner>; | ||
290 | pub type AssociatedTyDatum = chalk_rust_ir::AssociatedTyDatum<Interner>; | ||
291 | pub type TraitId = chalk_ir::TraitId<Interner>; | ||
292 | pub type TraitDatum = chalk_rust_ir::TraitDatum<Interner>; | ||
293 | pub type StructId = chalk_ir::StructId<Interner>; | ||
294 | pub type StructDatum = chalk_rust_ir::StructDatum<Interner>; | ||
295 | pub type ImplId = chalk_ir::ImplId<Interner>; | ||
296 | pub type ImplDatum = chalk_rust_ir::ImplDatum<Interner>; | ||
297 | pub type AssociatedTyValueId = chalk_rust_ir::AssociatedTyValueId<Interner>; | ||
298 | pub type AssociatedTyValue = chalk_rust_ir::AssociatedTyValue<Interner>; | ||
299 | 27 | ||
300 | pub(super) trait ToChalk { | 28 | pub(super) trait ToChalk { |
301 | type Chalk; | 29 | type Chalk; |
@@ -310,508 +38,6 @@ where | |||
310 | T::from_chalk(db, chalk) | 38 | T::from_chalk(db, chalk) |
311 | } | 39 | } |
312 | 40 | ||
313 | impl ToChalk for Ty { | ||
314 | type Chalk = chalk_ir::Ty<Interner>; | ||
315 | fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::Ty<Interner> { | ||
316 | match self { | ||
317 | Ty::Apply(apply_ty) => { | ||
318 | let name = apply_ty.ctor.to_chalk(db); | ||
319 | let substitution = apply_ty.parameters.to_chalk(db); | ||
320 | chalk_ir::ApplicationTy { name, substitution }.cast(&Interner).intern(&Interner) | ||
321 | } | ||
322 | Ty::Projection(proj_ty) => { | ||
323 | let associated_ty_id = proj_ty.associated_ty.to_chalk(db); | ||
324 | let substitution = proj_ty.parameters.to_chalk(db); | ||
325 | chalk_ir::AliasTy::Projection(chalk_ir::ProjectionTy { | ||
326 | associated_ty_id, | ||
327 | substitution, | ||
328 | }) | ||
329 | .cast(&Interner) | ||
330 | .intern(&Interner) | ||
331 | } | ||
332 | Ty::Placeholder(id) => { | ||
333 | let interned_id = db.intern_type_param_id(id); | ||
334 | PlaceholderIndex { | ||
335 | ui: UniverseIndex::ROOT, | ||
336 | idx: interned_id.as_intern_id().as_usize(), | ||
337 | } | ||
338 | .to_ty::<Interner>(&Interner) | ||
339 | } | ||
340 | Ty::Bound(idx) => chalk_ir::TyData::BoundVar(idx).intern(&Interner), | ||
341 | Ty::Infer(_infer_ty) => panic!("uncanonicalized infer ty"), | ||
342 | Ty::Dyn(predicates) => { | ||
343 | let where_clauses = chalk_ir::QuantifiedWhereClauses::from( | ||
344 | &Interner, | ||
345 | predicates.iter().filter(|p| !p.is_error()).cloned().map(|p| p.to_chalk(db)), | ||
346 | ); | ||
347 | let bounded_ty = chalk_ir::DynTy { bounds: make_binders(where_clauses, 1) }; | ||
348 | chalk_ir::TyData::Dyn(bounded_ty).intern(&Interner) | ||
349 | } | ||
350 | Ty::Opaque(_) | Ty::Unknown => { | ||
351 | let substitution = chalk_ir::Substitution::empty(&Interner); | ||
352 | let name = TypeName::Error; | ||
353 | chalk_ir::ApplicationTy { name, substitution }.cast(&Interner).intern(&Interner) | ||
354 | } | ||
355 | } | ||
356 | } | ||
357 | fn from_chalk(db: &dyn HirDatabase, chalk: chalk_ir::Ty<Interner>) -> Self { | ||
358 | match chalk.data(&Interner).clone() { | ||
359 | chalk_ir::TyData::Apply(apply_ty) => match apply_ty.name { | ||
360 | TypeName::Error => Ty::Unknown, | ||
361 | _ => { | ||
362 | let ctor = from_chalk(db, apply_ty.name); | ||
363 | let parameters = from_chalk(db, apply_ty.substitution); | ||
364 | Ty::Apply(ApplicationTy { ctor, parameters }) | ||
365 | } | ||
366 | }, | ||
367 | chalk_ir::TyData::Placeholder(idx) => { | ||
368 | assert_eq!(idx.ui, UniverseIndex::ROOT); | ||
369 | let interned_id = crate::db::GlobalTypeParamId::from_intern_id( | ||
370 | crate::salsa::InternId::from(idx.idx), | ||
371 | ); | ||
372 | Ty::Placeholder(db.lookup_intern_type_param_id(interned_id)) | ||
373 | } | ||
374 | chalk_ir::TyData::Alias(chalk_ir::AliasTy::Projection(proj)) => { | ||
375 | let associated_ty = from_chalk(db, proj.associated_ty_id); | ||
376 | let parameters = from_chalk(db, proj.substitution); | ||
377 | Ty::Projection(ProjectionTy { associated_ty, parameters }) | ||
378 | } | ||
379 | chalk_ir::TyData::Alias(chalk_ir::AliasTy::Opaque(_)) => unimplemented!(), | ||
380 | chalk_ir::TyData::Function(_) => unimplemented!(), | ||
381 | chalk_ir::TyData::BoundVar(idx) => Ty::Bound(idx), | ||
382 | chalk_ir::TyData::InferenceVar(_iv) => Ty::Unknown, | ||
383 | chalk_ir::TyData::Dyn(where_clauses) => { | ||
384 | assert_eq!(where_clauses.bounds.binders.len(&Interner), 1); | ||
385 | let predicates = where_clauses | ||
386 | .bounds | ||
387 | .skip_binders() | ||
388 | .iter(&Interner) | ||
389 | .map(|c| from_chalk(db, c.clone())) | ||
390 | .collect(); | ||
391 | Ty::Dyn(predicates) | ||
392 | } | ||
393 | } | ||
394 | } | ||
395 | } | ||
396 | |||
397 | impl ToChalk for Substs { | ||
398 | type Chalk = chalk_ir::Substitution<Interner>; | ||
399 | |||
400 | fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::Substitution<Interner> { | ||
401 | chalk_ir::Substitution::from(&Interner, self.iter().map(|ty| ty.clone().to_chalk(db))) | ||
402 | } | ||
403 | |||
404 | fn from_chalk(db: &dyn HirDatabase, parameters: chalk_ir::Substitution<Interner>) -> Substs { | ||
405 | let tys = parameters | ||
406 | .iter(&Interner) | ||
407 | .map(|p| match p.ty(&Interner) { | ||
408 | Some(ty) => from_chalk(db, ty.clone()), | ||
409 | None => unimplemented!(), | ||
410 | }) | ||
411 | .collect(); | ||
412 | Substs(tys) | ||
413 | } | ||
414 | } | ||
415 | |||
416 | impl ToChalk for TraitRef { | ||
417 | type Chalk = chalk_ir::TraitRef<Interner>; | ||
418 | |||
419 | fn to_chalk(self: TraitRef, db: &dyn HirDatabase) -> chalk_ir::TraitRef<Interner> { | ||
420 | let trait_id = self.trait_.to_chalk(db); | ||
421 | let substitution = self.substs.to_chalk(db); | ||
422 | chalk_ir::TraitRef { trait_id, substitution } | ||
423 | } | ||
424 | |||
425 | fn from_chalk(db: &dyn HirDatabase, trait_ref: chalk_ir::TraitRef<Interner>) -> Self { | ||
426 | let trait_ = from_chalk(db, trait_ref.trait_id); | ||
427 | let substs = from_chalk(db, trait_ref.substitution); | ||
428 | TraitRef { trait_, substs } | ||
429 | } | ||
430 | } | ||
431 | |||
432 | impl ToChalk for hir_def::TraitId { | ||
433 | type Chalk = TraitId; | ||
434 | |||
435 | fn to_chalk(self, _db: &dyn HirDatabase) -> TraitId { | ||
436 | chalk_ir::TraitId(self.as_intern_id()) | ||
437 | } | ||
438 | |||
439 | fn from_chalk(_db: &dyn HirDatabase, trait_id: TraitId) -> hir_def::TraitId { | ||
440 | InternKey::from_intern_id(trait_id.0) | ||
441 | } | ||
442 | } | ||
443 | |||
444 | impl ToChalk for TypeCtor { | ||
445 | type Chalk = TypeName<Interner>; | ||
446 | |||
447 | fn to_chalk(self, db: &dyn HirDatabase) -> TypeName<Interner> { | ||
448 | match self { | ||
449 | TypeCtor::AssociatedType(type_alias) => { | ||
450 | let type_id = type_alias.to_chalk(db); | ||
451 | TypeName::AssociatedType(type_id) | ||
452 | } | ||
453 | _ => { | ||
454 | // other TypeCtors get interned and turned into a chalk StructId | ||
455 | let struct_id = db.intern_type_ctor(self).into(); | ||
456 | TypeName::Struct(struct_id) | ||
457 | } | ||
458 | } | ||
459 | } | ||
460 | |||
461 | fn from_chalk(db: &dyn HirDatabase, type_name: TypeName<Interner>) -> TypeCtor { | ||
462 | match type_name { | ||
463 | TypeName::Struct(struct_id) => db.lookup_intern_type_ctor(struct_id.into()), | ||
464 | TypeName::AssociatedType(type_id) => TypeCtor::AssociatedType(from_chalk(db, type_id)), | ||
465 | TypeName::OpaqueType(_) => unreachable!(), | ||
466 | |||
467 | TypeName::Scalar(_) => unreachable!(), | ||
468 | TypeName::Tuple(_) => unreachable!(), | ||
469 | TypeName::Raw(_) => unreachable!(), | ||
470 | TypeName::Slice => unreachable!(), | ||
471 | TypeName::Ref(_) => unreachable!(), | ||
472 | TypeName::Str => unreachable!(), | ||
473 | |||
474 | TypeName::Error => { | ||
475 | // this should not be reached, since we don't represent TypeName::Error with TypeCtor | ||
476 | unreachable!() | ||
477 | } | ||
478 | } | ||
479 | } | ||
480 | } | ||
481 | |||
482 | impl ToChalk for Impl { | ||
483 | type Chalk = ImplId; | ||
484 | |||
485 | fn to_chalk(self, db: &dyn HirDatabase) -> ImplId { | ||
486 | db.intern_chalk_impl(self).into() | ||
487 | } | ||
488 | |||
489 | fn from_chalk(db: &dyn HirDatabase, impl_id: ImplId) -> Impl { | ||
490 | db.lookup_intern_chalk_impl(impl_id.into()) | ||
491 | } | ||
492 | } | ||
493 | |||
494 | impl ToChalk for TypeAliasId { | ||
495 | type Chalk = AssocTypeId; | ||
496 | |||
497 | fn to_chalk(self, _db: &dyn HirDatabase) -> AssocTypeId { | ||
498 | chalk_ir::AssocTypeId(self.as_intern_id()) | ||
499 | } | ||
500 | |||
501 | fn from_chalk(_db: &dyn HirDatabase, type_alias_id: AssocTypeId) -> TypeAliasId { | ||
502 | InternKey::from_intern_id(type_alias_id.0) | ||
503 | } | ||
504 | } | ||
505 | |||
506 | impl ToChalk for AssocTyValue { | ||
507 | type Chalk = AssociatedTyValueId; | ||
508 | |||
509 | fn to_chalk(self, db: &dyn HirDatabase) -> AssociatedTyValueId { | ||
510 | db.intern_assoc_ty_value(self).into() | ||
511 | } | ||
512 | |||
513 | fn from_chalk(db: &dyn HirDatabase, assoc_ty_value_id: AssociatedTyValueId) -> AssocTyValue { | ||
514 | db.lookup_intern_assoc_ty_value(assoc_ty_value_id.into()) | ||
515 | } | ||
516 | } | ||
517 | |||
518 | impl ToChalk for GenericPredicate { | ||
519 | type Chalk = chalk_ir::QuantifiedWhereClause<Interner>; | ||
520 | |||
521 | fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::QuantifiedWhereClause<Interner> { | ||
522 | match self { | ||
523 | GenericPredicate::Implemented(trait_ref) => { | ||
524 | let chalk_trait_ref = trait_ref.to_chalk(db); | ||
525 | let chalk_trait_ref = chalk_trait_ref.shifted_in(&Interner); | ||
526 | make_binders(chalk_ir::WhereClause::Implemented(chalk_trait_ref), 0) | ||
527 | } | ||
528 | GenericPredicate::Projection(projection_pred) => { | ||
529 | let ty = projection_pred.ty.to_chalk(db).shifted_in(&Interner); | ||
530 | let projection = projection_pred.projection_ty.to_chalk(db).shifted_in(&Interner); | ||
531 | let alias = chalk_ir::AliasTy::Projection(projection); | ||
532 | make_binders(chalk_ir::WhereClause::AliasEq(chalk_ir::AliasEq { alias, ty }), 0) | ||
533 | } | ||
534 | GenericPredicate::Error => panic!("tried passing GenericPredicate::Error to Chalk"), | ||
535 | } | ||
536 | } | ||
537 | |||
538 | fn from_chalk( | ||
539 | db: &dyn HirDatabase, | ||
540 | where_clause: chalk_ir::QuantifiedWhereClause<Interner>, | ||
541 | ) -> GenericPredicate { | ||
542 | // we don't produce any where clauses with binders and can't currently deal with them | ||
543 | match where_clause | ||
544 | .skip_binders() | ||
545 | .shifted_out(&Interner) | ||
546 | .expect("unexpected bound vars in where clause") | ||
547 | { | ||
548 | chalk_ir::WhereClause::Implemented(tr) => { | ||
549 | GenericPredicate::Implemented(from_chalk(db, tr)) | ||
550 | } | ||
551 | chalk_ir::WhereClause::AliasEq(projection_eq) => { | ||
552 | let projection_ty = from_chalk( | ||
553 | db, | ||
554 | match projection_eq.alias { | ||
555 | chalk_ir::AliasTy::Projection(p) => p, | ||
556 | _ => unimplemented!(), | ||
557 | }, | ||
558 | ); | ||
559 | let ty = from_chalk(db, projection_eq.ty); | ||
560 | GenericPredicate::Projection(super::ProjectionPredicate { projection_ty, ty }) | ||
561 | } | ||
562 | } | ||
563 | } | ||
564 | } | ||
565 | |||
566 | impl ToChalk for ProjectionTy { | ||
567 | type Chalk = chalk_ir::ProjectionTy<Interner>; | ||
568 | |||
569 | fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::ProjectionTy<Interner> { | ||
570 | chalk_ir::ProjectionTy { | ||
571 | associated_ty_id: self.associated_ty.to_chalk(db), | ||
572 | substitution: self.parameters.to_chalk(db), | ||
573 | } | ||
574 | } | ||
575 | |||
576 | fn from_chalk( | ||
577 | db: &dyn HirDatabase, | ||
578 | projection_ty: chalk_ir::ProjectionTy<Interner>, | ||
579 | ) -> ProjectionTy { | ||
580 | ProjectionTy { | ||
581 | associated_ty: from_chalk(db, projection_ty.associated_ty_id), | ||
582 | parameters: from_chalk(db, projection_ty.substitution), | ||
583 | } | ||
584 | } | ||
585 | } | ||
586 | |||
587 | impl ToChalk for super::ProjectionPredicate { | ||
588 | type Chalk = chalk_ir::AliasEq<Interner>; | ||
589 | |||
590 | fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::AliasEq<Interner> { | ||
591 | chalk_ir::AliasEq { | ||
592 | alias: chalk_ir::AliasTy::Projection(self.projection_ty.to_chalk(db)), | ||
593 | ty: self.ty.to_chalk(db), | ||
594 | } | ||
595 | } | ||
596 | |||
597 | fn from_chalk(_db: &dyn HirDatabase, _normalize: chalk_ir::AliasEq<Interner>) -> Self { | ||
598 | unimplemented!() | ||
599 | } | ||
600 | } | ||
601 | |||
602 | impl ToChalk for Obligation { | ||
603 | type Chalk = chalk_ir::DomainGoal<Interner>; | ||
604 | |||
605 | fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::DomainGoal<Interner> { | ||
606 | match self { | ||
607 | Obligation::Trait(tr) => tr.to_chalk(db).cast(&Interner), | ||
608 | Obligation::Projection(pr) => pr.to_chalk(db).cast(&Interner), | ||
609 | } | ||
610 | } | ||
611 | |||
612 | fn from_chalk(_db: &dyn HirDatabase, _goal: chalk_ir::DomainGoal<Interner>) -> Self { | ||
613 | unimplemented!() | ||
614 | } | ||
615 | } | ||
616 | |||
617 | impl<T> ToChalk for Canonical<T> | ||
618 | where | ||
619 | T: ToChalk, | ||
620 | T::Chalk: HasInterner<Interner = Interner>, | ||
621 | { | ||
622 | type Chalk = chalk_ir::Canonical<T::Chalk>; | ||
623 | |||
624 | fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::Canonical<T::Chalk> { | ||
625 | let parameter = chalk_ir::ParameterKind::Ty(chalk_ir::UniverseIndex::ROOT); | ||
626 | let value = self.value.to_chalk(db); | ||
627 | chalk_ir::Canonical { | ||
628 | value, | ||
629 | binders: chalk_ir::CanonicalVarKinds::from(&Interner, vec![parameter; self.num_vars]), | ||
630 | } | ||
631 | } | ||
632 | |||
633 | fn from_chalk(db: &dyn HirDatabase, canonical: chalk_ir::Canonical<T::Chalk>) -> Canonical<T> { | ||
634 | Canonical { | ||
635 | num_vars: canonical.binders.len(&Interner), | ||
636 | value: from_chalk(db, canonical.value), | ||
637 | } | ||
638 | } | ||
639 | } | ||
640 | |||
641 | impl ToChalk for Arc<super::TraitEnvironment> { | ||
642 | type Chalk = chalk_ir::Environment<Interner>; | ||
643 | |||
644 | fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::Environment<Interner> { | ||
645 | let mut clauses = Vec::new(); | ||
646 | for pred in &self.predicates { | ||
647 | if pred.is_error() { | ||
648 | // for env, we just ignore errors | ||
649 | continue; | ||
650 | } | ||
651 | let program_clause: chalk_ir::ProgramClause<Interner> = | ||
652 | pred.clone().to_chalk(db).cast(&Interner); | ||
653 | clauses.push(program_clause.into_from_env_clause(&Interner)); | ||
654 | } | ||
655 | chalk_ir::Environment::new(&Interner).add_clauses(&Interner, clauses) | ||
656 | } | ||
657 | |||
658 | fn from_chalk( | ||
659 | _db: &dyn HirDatabase, | ||
660 | _env: chalk_ir::Environment<Interner>, | ||
661 | ) -> Arc<super::TraitEnvironment> { | ||
662 | unimplemented!() | ||
663 | } | ||
664 | } | ||
665 | |||
666 | impl<T: ToChalk> ToChalk for super::InEnvironment<T> | ||
667 | where | ||
668 | T::Chalk: chalk_ir::interner::HasInterner<Interner = Interner>, | ||
669 | { | ||
670 | type Chalk = chalk_ir::InEnvironment<T::Chalk>; | ||
671 | |||
672 | fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::InEnvironment<T::Chalk> { | ||
673 | chalk_ir::InEnvironment { | ||
674 | environment: self.environment.to_chalk(db), | ||
675 | goal: self.value.to_chalk(db), | ||
676 | } | ||
677 | } | ||
678 | |||
679 | fn from_chalk( | ||
680 | db: &dyn HirDatabase, | ||
681 | in_env: chalk_ir::InEnvironment<T::Chalk>, | ||
682 | ) -> super::InEnvironment<T> { | ||
683 | super::InEnvironment { | ||
684 | environment: from_chalk(db, in_env.environment), | ||
685 | value: from_chalk(db, in_env.goal), | ||
686 | } | ||
687 | } | ||
688 | } | ||
689 | |||
690 | impl ToChalk for builtin::BuiltinImplData { | ||
691 | type Chalk = ImplDatum; | ||
692 | |||
693 | fn to_chalk(self, db: &dyn HirDatabase) -> ImplDatum { | ||
694 | let impl_type = chalk_rust_ir::ImplType::External; | ||
695 | let where_clauses = self.where_clauses.into_iter().map(|w| w.to_chalk(db)).collect(); | ||
696 | |||
697 | let impl_datum_bound = | ||
698 | chalk_rust_ir::ImplDatumBound { trait_ref: self.trait_ref.to_chalk(db), where_clauses }; | ||
699 | let associated_ty_value_ids = | ||
700 | self.assoc_ty_values.into_iter().map(|v| v.to_chalk(db)).collect(); | ||
701 | chalk_rust_ir::ImplDatum { | ||
702 | binders: make_binders(impl_datum_bound, self.num_vars), | ||
703 | impl_type, | ||
704 | polarity: chalk_rust_ir::Polarity::Positive, | ||
705 | associated_ty_value_ids, | ||
706 | } | ||
707 | } | ||
708 | |||
709 | fn from_chalk(_db: &dyn HirDatabase, _data: ImplDatum) -> Self { | ||
710 | unimplemented!() | ||
711 | } | ||
712 | } | ||
713 | |||
714 | impl ToChalk for builtin::BuiltinImplAssocTyValueData { | ||
715 | type Chalk = AssociatedTyValue; | ||
716 | |||
717 | fn to_chalk(self, db: &dyn HirDatabase) -> AssociatedTyValue { | ||
718 | let ty = self.value.to_chalk(db); | ||
719 | let value_bound = chalk_rust_ir::AssociatedTyValueBound { ty }; | ||
720 | |||
721 | chalk_rust_ir::AssociatedTyValue { | ||
722 | associated_ty_id: self.assoc_ty_id.to_chalk(db), | ||
723 | impl_id: self.impl_.to_chalk(db), | ||
724 | value: make_binders(value_bound, self.num_vars), | ||
725 | } | ||
726 | } | ||
727 | |||
728 | fn from_chalk( | ||
729 | _db: &dyn HirDatabase, | ||
730 | _data: AssociatedTyValue, | ||
731 | ) -> builtin::BuiltinImplAssocTyValueData { | ||
732 | unimplemented!() | ||
733 | } | ||
734 | } | ||
735 | |||
736 | fn make_binders<T>(value: T, num_vars: usize) -> chalk_ir::Binders<T> | ||
737 | where | ||
738 | T: HasInterner<Interner = Interner>, | ||
739 | { | ||
740 | chalk_ir::Binders::new( | ||
741 | chalk_ir::ParameterKinds::from( | ||
742 | &Interner, | ||
743 | std::iter::repeat(chalk_ir::ParameterKind::Ty(())).take(num_vars), | ||
744 | ), | ||
745 | value, | ||
746 | ) | ||
747 | } | ||
748 | |||
749 | fn convert_where_clauses( | ||
750 | db: &dyn HirDatabase, | ||
751 | def: GenericDefId, | ||
752 | substs: &Substs, | ||
753 | ) -> Vec<chalk_ir::QuantifiedWhereClause<Interner>> { | ||
754 | let generic_predicates = db.generic_predicates(def); | ||
755 | let mut result = Vec::with_capacity(generic_predicates.len()); | ||
756 | for pred in generic_predicates.iter() { | ||
757 | if pred.value.is_error() { | ||
758 | // skip errored predicates completely | ||
759 | continue; | ||
760 | } | ||
761 | result.push(pred.clone().subst(substs).to_chalk(db)); | ||
762 | } | ||
763 | result | ||
764 | } | ||
765 | |||
766 | fn generic_predicate_to_inline_bound( | ||
767 | db: &dyn HirDatabase, | ||
768 | pred: &GenericPredicate, | ||
769 | self_ty: &Ty, | ||
770 | ) -> Option<chalk_rust_ir::InlineBound<Interner>> { | ||
771 | // An InlineBound is like a GenericPredicate, except the self type is left out. | ||
772 | // We don't have a special type for this, but Chalk does. | ||
773 | match pred { | ||
774 | GenericPredicate::Implemented(trait_ref) => { | ||
775 | if &trait_ref.substs[0] != self_ty { | ||
776 | // we can only convert predicates back to type bounds if they | ||
777 | // have the expected self type | ||
778 | return None; | ||
779 | } | ||
780 | let args_no_self = trait_ref.substs[1..] | ||
781 | .iter() | ||
782 | .map(|ty| ty.clone().to_chalk(db).cast(&Interner)) | ||
783 | .collect(); | ||
784 | let trait_bound = | ||
785 | chalk_rust_ir::TraitBound { trait_id: trait_ref.trait_.to_chalk(db), args_no_self }; | ||
786 | Some(chalk_rust_ir::InlineBound::TraitBound(trait_bound)) | ||
787 | } | ||
788 | GenericPredicate::Projection(proj) => { | ||
789 | if &proj.projection_ty.parameters[0] != self_ty { | ||
790 | return None; | ||
791 | } | ||
792 | let trait_ = match proj.projection_ty.associated_ty.lookup(db.upcast()).container { | ||
793 | AssocContainerId::TraitId(t) => t, | ||
794 | _ => panic!("associated type not in trait"), | ||
795 | }; | ||
796 | let args_no_self = proj.projection_ty.parameters[1..] | ||
797 | .iter() | ||
798 | .map(|ty| ty.clone().to_chalk(db).cast(&Interner)) | ||
799 | .collect(); | ||
800 | let alias_eq_bound = chalk_rust_ir::AliasEqBound { | ||
801 | value: proj.ty.clone().to_chalk(db), | ||
802 | trait_bound: chalk_rust_ir::TraitBound { | ||
803 | trait_id: trait_.to_chalk(db), | ||
804 | args_no_self, | ||
805 | }, | ||
806 | associated_ty_id: proj.projection_ty.associated_ty.to_chalk(db), | ||
807 | parameters: Vec::new(), // FIXME we don't support generic associated types yet | ||
808 | }; | ||
809 | Some(chalk_rust_ir::InlineBound::AliasEqBound(alias_eq_bound)) | ||
810 | } | ||
811 | GenericPredicate::Error => None, | ||
812 | } | ||
813 | } | ||
814 | |||
815 | impl<'a> chalk_solve::RustIrDatabase<Interner> for ChalkContext<'a> { | 41 | impl<'a> chalk_solve::RustIrDatabase<Interner> for ChalkContext<'a> { |
816 | fn associated_ty_data(&self, id: AssocTypeId) -> Arc<AssociatedTyDatum> { | 42 | fn associated_ty_data(&self, id: AssocTypeId) -> Arc<AssociatedTyDatum> { |
817 | self.db.associated_ty_data(id) | 43 | self.db.associated_ty_data(id) |
@@ -819,16 +45,24 @@ impl<'a> chalk_solve::RustIrDatabase<Interner> for ChalkContext<'a> { | |||
819 | fn trait_datum(&self, trait_id: TraitId) -> Arc<TraitDatum> { | 45 | fn trait_datum(&self, trait_id: TraitId) -> Arc<TraitDatum> { |
820 | self.db.trait_datum(self.krate, trait_id) | 46 | self.db.trait_datum(self.krate, trait_id) |
821 | } | 47 | } |
822 | fn struct_datum(&self, struct_id: StructId) -> Arc<StructDatum> { | 48 | fn adt_datum(&self, struct_id: AdtId) -> Arc<StructDatum> { |
823 | self.db.struct_datum(self.krate, struct_id) | 49 | self.db.struct_datum(self.krate, struct_id) |
824 | } | 50 | } |
825 | fn impl_datum(&self, impl_id: ImplId) -> Arc<ImplDatum> { | 51 | fn impl_datum(&self, impl_id: ImplId) -> Arc<ImplDatum> { |
826 | self.db.impl_datum(self.krate, impl_id) | 52 | self.db.impl_datum(self.krate, impl_id) |
827 | } | 53 | } |
54 | |||
55 | fn fn_def_datum( | ||
56 | &self, | ||
57 | fn_def_id: chalk_ir::FnDefId<Interner>, | ||
58 | ) -> Arc<chalk_rust_ir::FnDefDatum<Interner>> { | ||
59 | self.db.fn_def_datum(self.krate, fn_def_id) | ||
60 | } | ||
61 | |||
828 | fn impls_for_trait( | 62 | fn impls_for_trait( |
829 | &self, | 63 | &self, |
830 | trait_id: TraitId, | 64 | trait_id: TraitId, |
831 | parameters: &[Parameter<Interner>], | 65 | parameters: &[GenericArg<Interner>], |
832 | ) -> Vec<ImplId> { | 66 | ) -> Vec<ImplId> { |
833 | debug!("impls_for_trait {:?}", trait_id); | 67 | debug!("impls_for_trait {:?}", trait_id); |
834 | let trait_: hir_def::TraitId = from_chalk(self.db, trait_id); | 68 | let trait_: hir_def::TraitId = from_chalk(self.db, trait_id); |
@@ -859,7 +93,7 @@ impl<'a> chalk_solve::RustIrDatabase<Interner> for ChalkContext<'a> { | |||
859 | debug!("impls_for_trait returned {} impls", result.len()); | 93 | debug!("impls_for_trait returned {} impls", result.len()); |
860 | result | 94 | result |
861 | } | 95 | } |
862 | fn impl_provided_for(&self, auto_trait_id: TraitId, struct_id: StructId) -> bool { | 96 | fn impl_provided_for(&self, auto_trait_id: TraitId, struct_id: AdtId) -> bool { |
863 | debug!("impl_provided_for {:?}, {:?}", auto_trait_id, struct_id); | 97 | debug!("impl_provided_for {:?}, {:?}", auto_trait_id, struct_id); |
864 | false // FIXME | 98 | false // FIXME |
865 | } | 99 | } |
@@ -878,10 +112,15 @@ impl<'a> chalk_solve::RustIrDatabase<Interner> for ChalkContext<'a> { | |||
878 | } | 112 | } |
879 | fn well_known_trait_id( | 113 | fn well_known_trait_id( |
880 | &self, | 114 | &self, |
881 | _well_known_trait: chalk_rust_ir::WellKnownTrait, | 115 | well_known_trait: chalk_rust_ir::WellKnownTrait, |
882 | ) -> Option<chalk_ir::TraitId<Interner>> { | 116 | ) -> Option<chalk_ir::TraitId<Interner>> { |
883 | // FIXME tell Chalk about well-known traits (here and in trait_datum) | 117 | let lang_attr = lang_attr_from_well_known_trait(well_known_trait); |
884 | None | 118 | let lang_items = self.db.crate_lang_items(self.krate); |
119 | let trait_ = match lang_items.target(lang_attr) { | ||
120 | Some(LangItemTarget::TraitId(trait_)) => trait_, | ||
121 | _ => return None, | ||
122 | }; | ||
123 | Some(trait_.to_chalk(self.db)) | ||
885 | } | 124 | } |
886 | 125 | ||
887 | fn program_clauses_for_env( | 126 | fn program_clauses_for_env( |
@@ -983,7 +222,8 @@ pub(crate) fn trait_datum_query( | |||
983 | let associated_ty_ids = | 222 | let associated_ty_ids = |
984 | trait_data.associated_types().map(|type_alias| type_alias.to_chalk(db)).collect(); | 223 | trait_data.associated_types().map(|type_alias| type_alias.to_chalk(db)).collect(); |
985 | let trait_datum_bound = chalk_rust_ir::TraitDatumBound { where_clauses }; | 224 | let trait_datum_bound = chalk_rust_ir::TraitDatumBound { where_clauses }; |
986 | let well_known = None; // FIXME set this (depending on lang items) | 225 | let well_known = |
226 | lang_attr(db.upcast(), trait_).and_then(|name| well_known_trait_from_lang_attr(&name)); | ||
987 | let trait_datum = TraitDatum { | 227 | let trait_datum = TraitDatum { |
988 | id: trait_id, | 228 | id: trait_id, |
989 | binders: make_binders(trait_datum_bound, bound_vars.len()), | 229 | binders: make_binders(trait_datum_bound, bound_vars.len()), |
@@ -994,13 +234,32 @@ pub(crate) fn trait_datum_query( | |||
994 | Arc::new(trait_datum) | 234 | Arc::new(trait_datum) |
995 | } | 235 | } |
996 | 236 | ||
237 | fn well_known_trait_from_lang_attr(name: &str) -> Option<WellKnownTrait> { | ||
238 | Some(match name { | ||
239 | "sized" => WellKnownTrait::SizedTrait, | ||
240 | "copy" => WellKnownTrait::CopyTrait, | ||
241 | "clone" => WellKnownTrait::CloneTrait, | ||
242 | "drop" => WellKnownTrait::DropTrait, | ||
243 | _ => return None, | ||
244 | }) | ||
245 | } | ||
246 | |||
247 | fn lang_attr_from_well_known_trait(attr: WellKnownTrait) -> &'static str { | ||
248 | match attr { | ||
249 | WellKnownTrait::SizedTrait => "sized", | ||
250 | WellKnownTrait::CopyTrait => "copy", | ||
251 | WellKnownTrait::CloneTrait => "clone", | ||
252 | WellKnownTrait::DropTrait => "drop", | ||
253 | } | ||
254 | } | ||
255 | |||
997 | pub(crate) fn struct_datum_query( | 256 | pub(crate) fn struct_datum_query( |
998 | db: &dyn HirDatabase, | 257 | db: &dyn HirDatabase, |
999 | krate: CrateId, | 258 | krate: CrateId, |
1000 | struct_id: StructId, | 259 | struct_id: AdtId, |
1001 | ) -> Arc<StructDatum> { | 260 | ) -> Arc<StructDatum> { |
1002 | debug!("struct_datum {:?}", struct_id); | 261 | debug!("struct_datum {:?}", struct_id); |
1003 | let type_ctor: TypeCtor = from_chalk(db, TypeName::Struct(struct_id)); | 262 | let type_ctor: TypeCtor = from_chalk(db, TypeName::Adt(struct_id)); |
1004 | debug!("struct {:?} = {:?}", struct_id, type_ctor); | 263 | debug!("struct {:?} = {:?}", struct_id, type_ctor); |
1005 | let num_params = type_ctor.num_ty_params(db); | 264 | let num_params = type_ctor.num_ty_params(db); |
1006 | let upstream = type_ctor.krate(db) != Some(krate); | 265 | let upstream = type_ctor.krate(db) != Some(krate); |
@@ -1012,12 +271,12 @@ pub(crate) fn struct_datum_query( | |||
1012 | convert_where_clauses(db, generic_def, &bound_vars) | 271 | convert_where_clauses(db, generic_def, &bound_vars) |
1013 | }) | 272 | }) |
1014 | .unwrap_or_else(Vec::new); | 273 | .unwrap_or_else(Vec::new); |
1015 | let flags = chalk_rust_ir::StructFlags { | 274 | let flags = chalk_rust_ir::AdtFlags { |
1016 | upstream, | 275 | upstream, |
1017 | // FIXME set fundamental flag correctly | 276 | // FIXME set fundamental flag correctly |
1018 | fundamental: false, | 277 | fundamental: false, |
1019 | }; | 278 | }; |
1020 | let struct_datum_bound = chalk_rust_ir::StructDatumBound { | 279 | let struct_datum_bound = chalk_rust_ir::AdtDatumBound { |
1021 | fields: Vec::new(), // FIXME add fields (only relevant for auto traits) | 280 | fields: Vec::new(), // FIXME add fields (only relevant for auto traits) |
1022 | where_clauses, | 281 | where_clauses, |
1023 | }; | 282 | }; |
@@ -1145,15 +404,47 @@ fn type_alias_associated_ty_value( | |||
1145 | Arc::new(value) | 404 | Arc::new(value) |
1146 | } | 405 | } |
1147 | 406 | ||
1148 | impl From<StructId> for crate::TypeCtorId { | 407 | pub(crate) fn fn_def_datum_query( |
1149 | fn from(struct_id: StructId) -> Self { | 408 | db: &dyn HirDatabase, |
1150 | InternKey::from_intern_id(struct_id.0) | 409 | _krate: CrateId, |
410 | fn_def_id: FnDefId, | ||
411 | ) -> Arc<FnDefDatum> { | ||
412 | let callable_def: CallableDef = from_chalk(db, fn_def_id); | ||
413 | let generic_params = generics(db.upcast(), callable_def.into()); | ||
414 | let sig = db.callable_item_signature(callable_def); | ||
415 | let bound_vars = Substs::bound_vars(&generic_params, DebruijnIndex::INNERMOST); | ||
416 | let where_clauses = convert_where_clauses(db, callable_def.into(), &bound_vars); | ||
417 | let bound = chalk_rust_ir::FnDefDatumBound { | ||
418 | // Note: Chalk doesn't actually use this information yet as far as I am aware, but we provide it anyway | ||
419 | argument_types: sig.value.params().iter().map(|ty| ty.clone().to_chalk(db)).collect(), | ||
420 | return_type: sig.value.ret().clone().to_chalk(db), | ||
421 | where_clauses, | ||
422 | }; | ||
423 | let datum = FnDefDatum { id: fn_def_id, binders: make_binders(bound, sig.num_binders) }; | ||
424 | Arc::new(datum) | ||
425 | } | ||
426 | |||
427 | impl From<AdtId> for crate::TypeCtorId { | ||
428 | fn from(struct_id: AdtId) -> Self { | ||
429 | struct_id.0 | ||
1151 | } | 430 | } |
1152 | } | 431 | } |
1153 | 432 | ||
1154 | impl From<crate::TypeCtorId> for StructId { | 433 | impl From<crate::TypeCtorId> for AdtId { |
1155 | fn from(type_ctor_id: crate::TypeCtorId) -> Self { | 434 | fn from(type_ctor_id: crate::TypeCtorId) -> Self { |
1156 | chalk_ir::StructId(type_ctor_id.as_intern_id()) | 435 | chalk_ir::AdtId(type_ctor_id) |
436 | } | ||
437 | } | ||
438 | |||
439 | impl From<FnDefId> for crate::CallableDefId { | ||
440 | fn from(fn_def_id: FnDefId) -> Self { | ||
441 | InternKey::from_intern_id(fn_def_id.0) | ||
442 | } | ||
443 | } | ||
444 | |||
445 | impl From<crate::CallableDefId> for FnDefId { | ||
446 | fn from(callable_def_id: crate::CallableDefId) -> Self { | ||
447 | chalk_ir::FnDefId(callable_def_id.as_intern_id()) | ||
1157 | } | 448 | } |
1158 | } | 449 | } |
1159 | 450 | ||
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 { |