aboutsummaryrefslogtreecommitdiff
path: root/crates/hir_ty/src/traits/chalk
diff options
context:
space:
mode:
Diffstat (limited to 'crates/hir_ty/src/traits/chalk')
-rw-r--r--crates/hir_ty/src/traits/chalk/mapping.rs189
1 files changed, 85 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..297ddeabd 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, GenericPredicate, InEnvironment, OpaqueTy, OpaqueTyId, ProjectionPredicate,
20 ProjectionPredicate, ProjectionTy, Scalar, Substs, TraitEnvironment, TraitRef, Ty, TyKind, 20 ProjectionTy, Scalar, Substs, TraitEnvironment, TraitRef, Ty, TyKind,
21 TypeCtor,
22}; 21};
23 22
24use super::interner::*; 23use super::interner::*;
@@ -28,75 +27,71 @@ 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::FnPtr { num_args: _, is_varargs, 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 {
38 num_binders: 0, 37 abi: (),
39 sig: chalk_ir::FnSig { 38 safety: chalk_ir::Safety::Safe,
40 abi: (), 39 variadic: is_varargs,
41 safety: chalk_ir::Safety::Safe, 40 },
42 variadic: is_varargs, 41 substitution,
43 }, 42 })
44 substitution, 43 .intern(&Interner)
45 }) 44 }
46 .intern(&Interner) 45 Ty::AssociatedType(type_alias, substs) => {
47 } 46 let assoc_type = TypeAliasAsAssocType(type_alias);
48 TypeCtor::AssociatedType(type_alias) => { 47 let assoc_type_id = assoc_type.to_chalk(db);
49 let assoc_type = TypeAliasAsAssocType(type_alias); 48 let substitution = substs.to_chalk(db);
50 let assoc_type_id = assoc_type.to_chalk(db); 49 chalk_ir::TyKind::AssociatedType(assoc_type_id, substitution).intern(&Interner)
51 let substitution = apply_ty.parameters.to_chalk(db); 50 }
52 chalk_ir::TyKind::AssociatedType(assoc_type_id, substitution).intern(&Interner)
53 }
54 51
55 TypeCtor::OpaqueType(impl_trait_id) => { 52 Ty::OpaqueType(impl_trait_id, substs) => {
56 let id = impl_trait_id.to_chalk(db); 53 let id = impl_trait_id.to_chalk(db);
57 let substitution = apply_ty.parameters.to_chalk(db); 54 let substitution = substs.to_chalk(db);
58 chalk_ir::TyKind::OpaqueType(id, substitution).intern(&Interner) 55 chalk_ir::TyKind::OpaqueType(id, substitution).intern(&Interner)
59 } 56 }
60 57
61 TypeCtor::ForeignType(type_alias) => { 58 Ty::ForeignType(type_alias, _) => {
62 let foreign_type = TypeAliasAsForeignType(type_alias); 59 let foreign_type = TypeAliasAsForeignType(type_alias);
63 let foreign_type_id = foreign_type.to_chalk(db); 60 let foreign_type_id = foreign_type.to_chalk(db);
64 chalk_ir::TyKind::Foreign(foreign_type_id).intern(&Interner) 61 chalk_ir::TyKind::Foreign(foreign_type_id).intern(&Interner)
65 } 62 }
66 63
67 TypeCtor::Scalar(scalar) => chalk_ir::TyKind::Scalar(scalar).intern(&Interner), 64 Ty::Scalar(scalar) => chalk_ir::TyKind::Scalar(scalar).intern(&Interner),
68 65
69 TypeCtor::Tuple { cardinality } => { 66 Ty::Tuple { cardinality, substs } => {
70 let substitution = apply_ty.parameters.to_chalk(db); 67 let substitution = substs.to_chalk(db);
71 chalk_ir::TyKind::Tuple(cardinality.into(), substitution).intern(&Interner) 68 chalk_ir::TyKind::Tuple(cardinality.into(), substitution).intern(&Interner)
72 } 69 }
73 TypeCtor::RawPtr(mutability) => { 70 Ty::RawPtr(mutability, substs) => {
74 let ty = apply_ty.parameters[0].clone().to_chalk(db); 71 let ty = substs[0].clone().to_chalk(db);
75 chalk_ir::TyKind::Raw(mutability.to_chalk(db), ty).intern(&Interner) 72 chalk_ir::TyKind::Raw(mutability.to_chalk(db), ty).intern(&Interner)
76 } 73 }
77 TypeCtor::Slice => { 74 Ty::Slice(substs) => {
78 chalk_ir::TyKind::Slice(apply_ty.parameters[0].clone().to_chalk(db)) 75 chalk_ir::TyKind::Slice(substs[0].clone().to_chalk(db)).intern(&Interner)
79 .intern(&Interner) 76 }
80 } 77 Ty::Str => chalk_ir::TyKind::Str.intern(&Interner),
81 TypeCtor::Str => chalk_ir::TyKind::Str.intern(&Interner), 78 Ty::FnDef(callable_def, substs) => {
82 TypeCtor::FnDef(callable_def) => { 79 let id = callable_def.to_chalk(db);
83 let id = callable_def.to_chalk(db); 80 let substitution = substs.to_chalk(db);
84 let substitution = apply_ty.parameters.to_chalk(db); 81 chalk_ir::TyKind::FnDef(id, substitution).intern(&Interner)
85 chalk_ir::TyKind::FnDef(id, substitution).intern(&Interner) 82 }
86 } 83 Ty::Never => chalk_ir::TyKind::Never.intern(&Interner),
87 TypeCtor::Never => chalk_ir::TyKind::Never.intern(&Interner),
88 84
89 TypeCtor::Closure { def, expr } => { 85 Ty::Closure { def, expr, substs } => {
90 let closure_id = db.intern_closure((def, expr)); 86 let closure_id = db.intern_closure((def, expr));
91 let substitution = apply_ty.parameters.to_chalk(db); 87 let substitution = substs.to_chalk(db);
92 chalk_ir::TyKind::Closure(closure_id.into(), substitution).intern(&Interner) 88 chalk_ir::TyKind::Closure(closure_id.into(), substitution).intern(&Interner)
93 } 89 }
94 90
95 TypeCtor::Adt(adt_id) => { 91 Ty::Adt(adt_id, substs) => {
96 let substitution = apply_ty.parameters.to_chalk(db); 92 let substitution = substs.to_chalk(db);
97 chalk_ir::TyKind::Adt(chalk_ir::AdtId(adt_id), substitution).intern(&Interner) 93 chalk_ir::TyKind::Adt(chalk_ir::AdtId(adt_id), substitution).intern(&Interner)
98 } 94 }
99 },
100 Ty::Projection(proj_ty) => { 95 Ty::Projection(proj_ty) => {
101 let associated_ty_id = TypeAliasAsAssocType(proj_ty.associated_ty).to_chalk(db); 96 let associated_ty_id = TypeAliasAsAssocType(proj_ty.associated_ty).to_chalk(db);
102 let substitution = proj_ty.parameters.to_chalk(db); 97 let substitution = proj_ty.parameters.to_chalk(db);
@@ -143,9 +138,7 @@ impl ToChalk for Ty {
143 fn from_chalk(db: &dyn HirDatabase, chalk: chalk_ir::Ty<Interner>) -> Self { 138 fn from_chalk(db: &dyn HirDatabase, chalk: chalk_ir::Ty<Interner>) -> Self {
144 match chalk.data(&Interner).kind.clone() { 139 match chalk.data(&Interner).kind.clone() {
145 chalk_ir::TyKind::Error => Ty::Unknown, 140 chalk_ir::TyKind::Error => Ty::Unknown,
146 chalk_ir::TyKind::Array(ty, _size) => { 141 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) => { 142 chalk_ir::TyKind::Placeholder(idx) => {
150 assert_eq!(idx.ui, UniverseIndex::ROOT); 143 assert_eq!(idx.ui, UniverseIndex::ROOT);
151 let interned_id = crate::db::GlobalTypeParamId::from_intern_id( 144 let interned_id = crate::db::GlobalTypeParamId::from_intern_id(
@@ -175,13 +168,11 @@ impl ToChalk for Ty {
175 db, 168 db,
176 substitution.0.shifted_out(&Interner).expect("fn ptr should have no binders"), 169 substitution.0.shifted_out(&Interner).expect("fn ptr should have no binders"),
177 ); 170 );
178 Ty::Apply(ApplicationTy { 171 Ty::FnPtr {
179 ctor: TypeCtor::FnPtr { 172 num_args: (parameters.len() - 1) as u16,
180 num_args: (parameters.len() - 1) as u16, 173 is_varargs: variadic,
181 is_varargs: variadic, 174 substs: parameters,
182 }, 175 }
183 parameters,
184 })
185 } 176 }
186 chalk_ir::TyKind::BoundVar(idx) => Ty::Bound(idx), 177 chalk_ir::TyKind::BoundVar(idx) => Ty::Bound(idx),
187 chalk_ir::TyKind::InferenceVar(_iv, _kind) => Ty::Unknown, 178 chalk_ir::TyKind::InferenceVar(_iv, _kind) => Ty::Unknown,
@@ -196,60 +187,50 @@ impl ToChalk for Ty {
196 Ty::Dyn(predicates) 187 Ty::Dyn(predicates)
197 } 188 }
198 189
199 chalk_ir::TyKind::Adt(struct_id, subst) => { 190 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) 191 chalk_ir::TyKind::AssociatedType(type_id, subst) => Ty::AssociatedType(
201 } 192 from_chalk::<TypeAliasAsAssocType, _>(db, type_id).0,
202 chalk_ir::TyKind::AssociatedType(type_id, subst) => apply_ty_from_chalk( 193 from_chalk(db, subst),
203 db,
204 TypeCtor::AssociatedType(from_chalk::<TypeAliasAsAssocType, _>(db, type_id).0),
205 subst,
206 ), 194 ),
195
207 chalk_ir::TyKind::OpaqueType(opaque_type_id, subst) => { 196 chalk_ir::TyKind::OpaqueType(opaque_type_id, subst) => {
208 apply_ty_from_chalk(db, TypeCtor::OpaqueType(from_chalk(db, opaque_type_id)), subst) 197 Ty::OpaqueType(from_chalk(db, opaque_type_id), from_chalk(db, subst))
209 } 198 }
210 199
211 chalk_ir::TyKind::Scalar(scalar) => Ty::simple(TypeCtor::Scalar(scalar)), 200 chalk_ir::TyKind::Scalar(scalar) => Ty::Scalar(scalar),
212 chalk_ir::TyKind::Tuple(cardinality, subst) => { 201 chalk_ir::TyKind::Tuple(cardinality, subst) => {
213 apply_ty_from_chalk(db, TypeCtor::Tuple { cardinality: cardinality as u16 }, subst) 202 Ty::Tuple { cardinality: cardinality as u16, substs: from_chalk(db, subst) }
214 } 203 }
215 chalk_ir::TyKind::Raw(mutability, ty) => { 204 chalk_ir::TyKind::Raw(mutability, ty) => {
216 Ty::apply_one(TypeCtor::RawPtr(from_chalk(db, mutability)), from_chalk(db, ty)) 205 Ty::RawPtr(from_chalk(db, mutability), Substs::single(from_chalk(db, ty)))
217 } 206 }
218 chalk_ir::TyKind::Slice(ty) => Ty::apply_one(TypeCtor::Slice, from_chalk(db, ty)), 207 chalk_ir::TyKind::Slice(ty) => Ty::Slice(Substs::single(from_chalk(db, ty))),
219 chalk_ir::TyKind::Ref(mutability, _lifetime, ty) => { 208 chalk_ir::TyKind::Ref(mutability, _lifetime, ty) => {
220 Ty::apply_one(TypeCtor::Ref(from_chalk(db, mutability)), from_chalk(db, ty)) 209 Ty::Ref(from_chalk(db, mutability), Substs::single(from_chalk(db, ty)))
221 } 210 }
222 chalk_ir::TyKind::Str => Ty::simple(TypeCtor::Str), 211 chalk_ir::TyKind::Str => Ty::Str,
223 chalk_ir::TyKind::Never => Ty::simple(TypeCtor::Never), 212 chalk_ir::TyKind::Never => Ty::Never,
224 213
225 chalk_ir::TyKind::FnDef(fn_def_id, subst) => { 214 chalk_ir::TyKind::FnDef(fn_def_id, subst) => {
226 let callable_def = from_chalk(db, fn_def_id); 215 Ty::FnDef(from_chalk(db, fn_def_id), from_chalk(db, subst))
227 apply_ty_from_chalk(db, TypeCtor::FnDef(callable_def), subst)
228 } 216 }
229 217
230 chalk_ir::TyKind::Closure(id, subst) => { 218 chalk_ir::TyKind::Closure(id, subst) => {
231 let id: crate::db::ClosureId = id.into(); 219 let id: crate::db::ClosureId = id.into();
232 let (def, expr) = db.lookup_intern_closure(id); 220 let (def, expr) = db.lookup_intern_closure(id);
233 apply_ty_from_chalk(db, TypeCtor::Closure { def, expr }, subst) 221 Ty::Closure { def, expr, substs: from_chalk(db, subst) }
234 } 222 }
235 223
236 chalk_ir::TyKind::Foreign(foreign_def_id) => Ty::simple(TypeCtor::ForeignType( 224 chalk_ir::TyKind::Foreign(foreign_def_id) => Ty::ForeignType(
237 from_chalk::<TypeAliasAsForeignType, _>(db, foreign_def_id).0, 225 from_chalk::<TypeAliasAsForeignType, _>(db, foreign_def_id).0,
238 )), 226 Substs::empty(),
227 ),
239 chalk_ir::TyKind::Generator(_, _) => unimplemented!(), // FIXME 228 chalk_ir::TyKind::Generator(_, _) => unimplemented!(), // FIXME
240 chalk_ir::TyKind::GeneratorWitness(_, _) => unimplemented!(), // FIXME 229 chalk_ir::TyKind::GeneratorWitness(_, _) => unimplemented!(), // FIXME
241 } 230 }
242 } 231 }
243} 232}
244 233
245fn 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 234/// 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. 235/// fake lifetime here, because Chalks built-in logic may expect it to be there.
255fn ref_to_chalk( 236fn ref_to_chalk(