diff options
Diffstat (limited to 'crates/hir_ty/src/traits/chalk')
-rw-r--r-- | crates/hir_ty/src/traits/chalk/mapping.rs | 184 |
1 files changed, 80 insertions, 104 deletions
diff --git a/crates/hir_ty/src/traits/chalk/mapping.rs b/crates/hir_ty/src/traits/chalk/mapping.rs index 5a3cb7906..60d74e21a 100644 --- a/crates/hir_ty/src/traits/chalk/mapping.rs +++ b/crates/hir_ty/src/traits/chalk/mapping.rs | |||
@@ -16,9 +16,8 @@ use crate::{ | |||
16 | db::HirDatabase, | 16 | db::HirDatabase, |
17 | primitive::UintTy, | 17 | primitive::UintTy, |
18 | traits::{Canonical, Obligation}, | 18 | traits::{Canonical, Obligation}, |
19 | ApplicationTy, CallableDefId, GenericPredicate, InEnvironment, OpaqueTy, OpaqueTyId, | 19 | CallableDefId, FnPointer, FnSig, GenericPredicate, InEnvironment, OpaqueTy, OpaqueTyId, |
20 | ProjectionPredicate, ProjectionTy, Scalar, Substs, TraitEnvironment, TraitRef, Ty, TyKind, | 20 | ProjectionPredicate, ProjectionTy, Scalar, Substs, TraitEnvironment, TraitRef, Ty, TyKind, |
21 | TypeCtor, | ||
22 | }; | 21 | }; |
23 | 22 | ||
24 | use super::interner::*; | 23 | use super::interner::*; |
@@ -28,75 +27,67 @@ impl ToChalk for Ty { | |||
28 | type Chalk = chalk_ir::Ty<Interner>; | 27 | type Chalk = chalk_ir::Ty<Interner>; |
29 | fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::Ty<Interner> { | 28 | fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::Ty<Interner> { |
30 | match self { | 29 | match self { |
31 | Ty::Apply(apply_ty) => match apply_ty.ctor { | 30 | Ty::Ref(m, parameters) => ref_to_chalk(db, m, parameters), |
32 | TypeCtor::Ref(m) => ref_to_chalk(db, m, apply_ty.parameters), | 31 | Ty::Array(parameters) => array_to_chalk(db, parameters), |
33 | TypeCtor::Array => array_to_chalk(db, apply_ty.parameters), | 32 | Ty::Function(FnPointer { sig: FnSig { variadic }, substs, .. }) => { |
34 | TypeCtor::FnPtr { num_args: _, is_varargs } => { | 33 | let substitution = chalk_ir::FnSubst(substs.to_chalk(db).shifted_in(&Interner)); |
35 | let substitution = | 34 | chalk_ir::TyKind::Function(chalk_ir::FnPointer { |
36 | chalk_ir::FnSubst(apply_ty.parameters.to_chalk(db).shifted_in(&Interner)); | 35 | num_binders: 0, |
37 | chalk_ir::TyKind::Function(chalk_ir::FnPointer { | 36 | sig: chalk_ir::FnSig { abi: (), safety: chalk_ir::Safety::Safe, variadic }, |
38 | num_binders: 0, | 37 | substitution, |
39 | sig: chalk_ir::FnSig { | 38 | }) |
40 | abi: (), | 39 | .intern(&Interner) |
41 | safety: chalk_ir::Safety::Safe, | 40 | } |
42 | variadic: is_varargs, | 41 | Ty::AssociatedType(type_alias, substs) => { |
43 | }, | 42 | let assoc_type = TypeAliasAsAssocType(type_alias); |
44 | substitution, | 43 | let assoc_type_id = assoc_type.to_chalk(db); |
45 | }) | 44 | let substitution = substs.to_chalk(db); |
46 | .intern(&Interner) | 45 | chalk_ir::TyKind::AssociatedType(assoc_type_id, substitution).intern(&Interner) |
47 | } | 46 | } |
48 | TypeCtor::AssociatedType(type_alias) => { | ||
49 | let assoc_type = TypeAliasAsAssocType(type_alias); | ||
50 | let assoc_type_id = assoc_type.to_chalk(db); | ||
51 | let substitution = apply_ty.parameters.to_chalk(db); | ||
52 | chalk_ir::TyKind::AssociatedType(assoc_type_id, substitution).intern(&Interner) | ||
53 | } | ||
54 | 47 | ||
55 | TypeCtor::OpaqueType(impl_trait_id) => { | 48 | Ty::OpaqueType(impl_trait_id, substs) => { |
56 | let id = impl_trait_id.to_chalk(db); | 49 | let id = impl_trait_id.to_chalk(db); |
57 | let substitution = apply_ty.parameters.to_chalk(db); | 50 | let substitution = substs.to_chalk(db); |
58 | chalk_ir::TyKind::OpaqueType(id, substitution).intern(&Interner) | 51 | chalk_ir::TyKind::OpaqueType(id, substitution).intern(&Interner) |
59 | } | 52 | } |
60 | 53 | ||
61 | TypeCtor::ForeignType(type_alias) => { | 54 | Ty::ForeignType(type_alias) => { |
62 | let foreign_type = TypeAliasAsForeignType(type_alias); | 55 | let foreign_type = TypeAliasAsForeignType(type_alias); |
63 | let foreign_type_id = foreign_type.to_chalk(db); | 56 | let foreign_type_id = foreign_type.to_chalk(db); |
64 | chalk_ir::TyKind::Foreign(foreign_type_id).intern(&Interner) | 57 | chalk_ir::TyKind::Foreign(foreign_type_id).intern(&Interner) |
65 | } | 58 | } |
66 | 59 | ||
67 | TypeCtor::Scalar(scalar) => chalk_ir::TyKind::Scalar(scalar).intern(&Interner), | 60 | Ty::Scalar(scalar) => chalk_ir::TyKind::Scalar(scalar).intern(&Interner), |
68 | 61 | ||
69 | TypeCtor::Tuple { cardinality } => { | 62 | Ty::Tuple(cardinality, substs) => { |
70 | let substitution = apply_ty.parameters.to_chalk(db); | 63 | let substitution = substs.to_chalk(db); |
71 | chalk_ir::TyKind::Tuple(cardinality.into(), substitution).intern(&Interner) | 64 | chalk_ir::TyKind::Tuple(cardinality.into(), substitution).intern(&Interner) |
72 | } | 65 | } |
73 | TypeCtor::RawPtr(mutability) => { | 66 | Ty::RawPtr(mutability, substs) => { |
74 | let ty = apply_ty.parameters[0].clone().to_chalk(db); | 67 | let ty = substs[0].clone().to_chalk(db); |
75 | chalk_ir::TyKind::Raw(mutability.to_chalk(db), ty).intern(&Interner) | 68 | chalk_ir::TyKind::Raw(mutability.to_chalk(db), ty).intern(&Interner) |
76 | } | 69 | } |
77 | TypeCtor::Slice => { | 70 | Ty::Slice(substs) => { |
78 | chalk_ir::TyKind::Slice(apply_ty.parameters[0].clone().to_chalk(db)) | 71 | chalk_ir::TyKind::Slice(substs[0].clone().to_chalk(db)).intern(&Interner) |
79 | .intern(&Interner) | 72 | } |
80 | } | 73 | Ty::Str => chalk_ir::TyKind::Str.intern(&Interner), |
81 | TypeCtor::Str => chalk_ir::TyKind::Str.intern(&Interner), | 74 | Ty::FnDef(callable_def, substs) => { |
82 | TypeCtor::FnDef(callable_def) => { | 75 | let id = callable_def.to_chalk(db); |
83 | let id = callable_def.to_chalk(db); | 76 | let substitution = substs.to_chalk(db); |
84 | let substitution = apply_ty.parameters.to_chalk(db); | 77 | chalk_ir::TyKind::FnDef(id, substitution).intern(&Interner) |
85 | chalk_ir::TyKind::FnDef(id, substitution).intern(&Interner) | 78 | } |
86 | } | 79 | Ty::Never => chalk_ir::TyKind::Never.intern(&Interner), |
87 | TypeCtor::Never => chalk_ir::TyKind::Never.intern(&Interner), | ||
88 | 80 | ||
89 | TypeCtor::Closure { def, expr } => { | 81 | Ty::Closure(def, expr, substs) => { |
90 | let closure_id = db.intern_closure((def, expr)); | 82 | let closure_id = db.intern_closure((def, expr)); |
91 | let substitution = apply_ty.parameters.to_chalk(db); | 83 | let substitution = substs.to_chalk(db); |
92 | chalk_ir::TyKind::Closure(closure_id.into(), substitution).intern(&Interner) | 84 | chalk_ir::TyKind::Closure(closure_id.into(), substitution).intern(&Interner) |
93 | } | 85 | } |
94 | 86 | ||
95 | TypeCtor::Adt(adt_id) => { | 87 | Ty::Adt(adt_id, substs) => { |
96 | let substitution = apply_ty.parameters.to_chalk(db); | 88 | let substitution = substs.to_chalk(db); |
97 | chalk_ir::TyKind::Adt(chalk_ir::AdtId(adt_id), substitution).intern(&Interner) | 89 | chalk_ir::TyKind::Adt(chalk_ir::AdtId(adt_id), substitution).intern(&Interner) |
98 | } | 90 | } |
99 | }, | ||
100 | Ty::Projection(proj_ty) => { | 91 | Ty::Projection(proj_ty) => { |
101 | let associated_ty_id = TypeAliasAsAssocType(proj_ty.associated_ty).to_chalk(db); | 92 | let associated_ty_id = TypeAliasAsAssocType(proj_ty.associated_ty).to_chalk(db); |
102 | let substitution = proj_ty.parameters.to_chalk(db); | 93 | let substitution = proj_ty.parameters.to_chalk(db); |
@@ -143,9 +134,7 @@ impl ToChalk for Ty { | |||
143 | fn from_chalk(db: &dyn HirDatabase, chalk: chalk_ir::Ty<Interner>) -> Self { | 134 | fn from_chalk(db: &dyn HirDatabase, chalk: chalk_ir::Ty<Interner>) -> Self { |
144 | match chalk.data(&Interner).kind.clone() { | 135 | match chalk.data(&Interner).kind.clone() { |
145 | chalk_ir::TyKind::Error => Ty::Unknown, | 136 | chalk_ir::TyKind::Error => Ty::Unknown, |
146 | chalk_ir::TyKind::Array(ty, _size) => { | 137 | chalk_ir::TyKind::Array(ty, _size) => Ty::Array(Substs::single(from_chalk(db, ty))), |
147 | Ty::apply(TypeCtor::Array, Substs::single(from_chalk(db, ty))) | ||
148 | } | ||
149 | chalk_ir::TyKind::Placeholder(idx) => { | 138 | chalk_ir::TyKind::Placeholder(idx) => { |
150 | assert_eq!(idx.ui, UniverseIndex::ROOT); | 139 | assert_eq!(idx.ui, UniverseIndex::ROOT); |
151 | let interned_id = crate::db::GlobalTypeParamId::from_intern_id( | 140 | let interned_id = crate::db::GlobalTypeParamId::from_intern_id( |
@@ -171,16 +160,14 @@ impl ToChalk for Ty { | |||
171 | .. | 160 | .. |
172 | }) => { | 161 | }) => { |
173 | assert_eq!(num_binders, 0); | 162 | assert_eq!(num_binders, 0); |
174 | let parameters: Substs = from_chalk( | 163 | let substs: Substs = from_chalk( |
175 | db, | 164 | db, |
176 | substitution.0.shifted_out(&Interner).expect("fn ptr should have no binders"), | 165 | substitution.0.shifted_out(&Interner).expect("fn ptr should have no binders"), |
177 | ); | 166 | ); |
178 | Ty::Apply(ApplicationTy { | 167 | Ty::Function(FnPointer { |
179 | ctor: TypeCtor::FnPtr { | 168 | num_args: (substs.len() - 1), |
180 | num_args: (parameters.len() - 1) as u16, | 169 | sig: FnSig { variadic }, |
181 | is_varargs: variadic, | 170 | substs, |
182 | }, | ||
183 | parameters, | ||
184 | }) | 171 | }) |
185 | } | 172 | } |
186 | chalk_ir::TyKind::BoundVar(idx) => Ty::Bound(idx), | 173 | chalk_ir::TyKind::BoundVar(idx) => Ty::Bound(idx), |
@@ -196,60 +183,49 @@ impl ToChalk for Ty { | |||
196 | Ty::Dyn(predicates) | 183 | Ty::Dyn(predicates) |
197 | } | 184 | } |
198 | 185 | ||
199 | chalk_ir::TyKind::Adt(struct_id, subst) => { | 186 | chalk_ir::TyKind::Adt(struct_id, subst) => Ty::Adt(struct_id.0, from_chalk(db, subst)), |
200 | apply_ty_from_chalk(db, TypeCtor::Adt(struct_id.0), subst) | 187 | chalk_ir::TyKind::AssociatedType(type_id, subst) => Ty::AssociatedType( |
201 | } | 188 | from_chalk::<TypeAliasAsAssocType, _>(db, type_id).0, |
202 | chalk_ir::TyKind::AssociatedType(type_id, subst) => apply_ty_from_chalk( | 189 | from_chalk(db, subst), |
203 | db, | ||
204 | TypeCtor::AssociatedType(from_chalk::<TypeAliasAsAssocType, _>(db, type_id).0), | ||
205 | subst, | ||
206 | ), | 190 | ), |
191 | |||
207 | chalk_ir::TyKind::OpaqueType(opaque_type_id, subst) => { | 192 | chalk_ir::TyKind::OpaqueType(opaque_type_id, subst) => { |
208 | apply_ty_from_chalk(db, TypeCtor::OpaqueType(from_chalk(db, opaque_type_id)), subst) | 193 | Ty::OpaqueType(from_chalk(db, opaque_type_id), from_chalk(db, subst)) |
209 | } | 194 | } |
210 | 195 | ||
211 | chalk_ir::TyKind::Scalar(scalar) => Ty::simple(TypeCtor::Scalar(scalar)), | 196 | chalk_ir::TyKind::Scalar(scalar) => Ty::Scalar(scalar), |
212 | chalk_ir::TyKind::Tuple(cardinality, subst) => { | 197 | chalk_ir::TyKind::Tuple(cardinality, subst) => { |
213 | apply_ty_from_chalk(db, TypeCtor::Tuple { cardinality: cardinality as u16 }, subst) | 198 | Ty::Tuple(cardinality, from_chalk(db, subst)) |
214 | } | 199 | } |
215 | chalk_ir::TyKind::Raw(mutability, ty) => { | 200 | chalk_ir::TyKind::Raw(mutability, ty) => { |
216 | Ty::apply_one(TypeCtor::RawPtr(from_chalk(db, mutability)), from_chalk(db, ty)) | 201 | Ty::RawPtr(from_chalk(db, mutability), Substs::single(from_chalk(db, ty))) |
217 | } | 202 | } |
218 | chalk_ir::TyKind::Slice(ty) => Ty::apply_one(TypeCtor::Slice, from_chalk(db, ty)), | 203 | chalk_ir::TyKind::Slice(ty) => Ty::Slice(Substs::single(from_chalk(db, ty))), |
219 | chalk_ir::TyKind::Ref(mutability, _lifetime, ty) => { | 204 | chalk_ir::TyKind::Ref(mutability, _lifetime, ty) => { |
220 | Ty::apply_one(TypeCtor::Ref(from_chalk(db, mutability)), from_chalk(db, ty)) | 205 | Ty::Ref(from_chalk(db, mutability), Substs::single(from_chalk(db, ty))) |
221 | } | 206 | } |
222 | chalk_ir::TyKind::Str => Ty::simple(TypeCtor::Str), | 207 | chalk_ir::TyKind::Str => Ty::Str, |
223 | chalk_ir::TyKind::Never => Ty::simple(TypeCtor::Never), | 208 | chalk_ir::TyKind::Never => Ty::Never, |
224 | 209 | ||
225 | chalk_ir::TyKind::FnDef(fn_def_id, subst) => { | 210 | chalk_ir::TyKind::FnDef(fn_def_id, subst) => { |
226 | let callable_def = from_chalk(db, fn_def_id); | 211 | Ty::FnDef(from_chalk(db, fn_def_id), from_chalk(db, subst)) |
227 | apply_ty_from_chalk(db, TypeCtor::FnDef(callable_def), subst) | ||
228 | } | 212 | } |
229 | 213 | ||
230 | chalk_ir::TyKind::Closure(id, subst) => { | 214 | chalk_ir::TyKind::Closure(id, subst) => { |
231 | let id: crate::db::ClosureId = id.into(); | 215 | let id: crate::db::ClosureId = id.into(); |
232 | let (def, expr) = db.lookup_intern_closure(id); | 216 | let (def, expr) = db.lookup_intern_closure(id); |
233 | apply_ty_from_chalk(db, TypeCtor::Closure { def, expr }, subst) | 217 | Ty::Closure(def, expr, from_chalk(db, subst)) |
234 | } | 218 | } |
235 | 219 | ||
236 | chalk_ir::TyKind::Foreign(foreign_def_id) => Ty::simple(TypeCtor::ForeignType( | 220 | chalk_ir::TyKind::Foreign(foreign_def_id) => { |
237 | from_chalk::<TypeAliasAsForeignType, _>(db, foreign_def_id).0, | 221 | Ty::ForeignType(from_chalk::<TypeAliasAsForeignType, _>(db, foreign_def_id).0) |
238 | )), | 222 | } |
239 | chalk_ir::TyKind::Generator(_, _) => unimplemented!(), // FIXME | 223 | chalk_ir::TyKind::Generator(_, _) => unimplemented!(), // FIXME |
240 | chalk_ir::TyKind::GeneratorWitness(_, _) => unimplemented!(), // FIXME | 224 | chalk_ir::TyKind::GeneratorWitness(_, _) => unimplemented!(), // FIXME |
241 | } | 225 | } |
242 | } | 226 | } |
243 | } | 227 | } |
244 | 228 | ||
245 | fn apply_ty_from_chalk( | ||
246 | db: &dyn HirDatabase, | ||
247 | ctor: TypeCtor, | ||
248 | subst: chalk_ir::Substitution<Interner>, | ||
249 | ) -> Ty { | ||
250 | Ty::Apply(ApplicationTy { ctor, parameters: from_chalk(db, subst) }) | ||
251 | } | ||
252 | |||
253 | /// We currently don't model lifetimes, but Chalk does. So, we have to insert a | 229 | /// We currently don't model lifetimes, but Chalk does. So, we have to insert a |
254 | /// fake lifetime here, because Chalks built-in logic may expect it to be there. | 230 | /// fake lifetime here, because Chalks built-in logic may expect it to be there. |
255 | fn ref_to_chalk( | 231 | fn ref_to_chalk( |