diff options
Diffstat (limited to 'crates/hir_ty/src/traits/chalk/mapping.rs')
-rw-r--r-- | crates/hir_ty/src/traits/chalk/mapping.rs | 369 |
1 files changed, 164 insertions, 205 deletions
diff --git a/crates/hir_ty/src/traits/chalk/mapping.rs b/crates/hir_ty/src/traits/chalk/mapping.rs index be3301313..86cbc4c7e 100644 --- a/crates/hir_ty/src/traits/chalk/mapping.rs +++ b/crates/hir_ty/src/traits/chalk/mapping.rs | |||
@@ -4,7 +4,7 @@ | |||
4 | //! conversions. | 4 | //! conversions. |
5 | 5 | ||
6 | use chalk_ir::{ | 6 | use chalk_ir::{ |
7 | cast::Cast, fold::shift::Shift, interner::HasInterner, PlaceholderIndex, Scalar, TypeName, | 7 | cast::Cast, fold::shift::Shift, interner::HasInterner, LifetimeData, PlaceholderIndex, Scalar, |
8 | UniverseIndex, | 8 | UniverseIndex, |
9 | }; | 9 | }; |
10 | use chalk_solve::rust_ir; | 10 | use chalk_solve::rust_ir; |
@@ -32,7 +32,7 @@ impl ToChalk for Ty { | |||
32 | TypeCtor::Array => array_to_chalk(db, apply_ty.parameters), | 32 | TypeCtor::Array => array_to_chalk(db, apply_ty.parameters), |
33 | TypeCtor::FnPtr { num_args: _, is_varargs } => { | 33 | TypeCtor::FnPtr { num_args: _, is_varargs } => { |
34 | let substitution = apply_ty.parameters.to_chalk(db).shifted_in(&Interner); | 34 | let substitution = apply_ty.parameters.to_chalk(db).shifted_in(&Interner); |
35 | chalk_ir::TyData::Function(chalk_ir::FnPointer { | 35 | chalk_ir::TyKind::Function(chalk_ir::FnPointer { |
36 | num_binders: 0, | 36 | num_binders: 0, |
37 | sig: chalk_ir::FnSig { | 37 | sig: chalk_ir::FnSig { |
38 | abi: (), | 38 | abi: (), |
@@ -43,10 +43,68 @@ impl ToChalk for Ty { | |||
43 | }) | 43 | }) |
44 | .intern(&Interner) | 44 | .intern(&Interner) |
45 | } | 45 | } |
46 | _ => { | 46 | TypeCtor::AssociatedType(type_alias) => { |
47 | let name = apply_ty.ctor.to_chalk(db); | 47 | let assoc_type = TypeAliasAsAssocType(type_alias); |
48 | let assoc_type_id = assoc_type.to_chalk(db); | ||
48 | let substitution = apply_ty.parameters.to_chalk(db); | 49 | let substitution = apply_ty.parameters.to_chalk(db); |
49 | chalk_ir::ApplicationTy { name, substitution }.cast(&Interner).intern(&Interner) | 50 | chalk_ir::TyKind::AssociatedType(assoc_type_id, substitution).intern(&Interner) |
51 | } | ||
52 | |||
53 | TypeCtor::OpaqueType(impl_trait_id) => { | ||
54 | let id = impl_trait_id.to_chalk(db); | ||
55 | let substitution = apply_ty.parameters.to_chalk(db); | ||
56 | chalk_ir::TyKind::OpaqueType(id, substitution).intern(&Interner) | ||
57 | } | ||
58 | |||
59 | TypeCtor::ForeignType(type_alias) => { | ||
60 | let foreign_type = TypeAliasAsForeignType(type_alias); | ||
61 | let foreign_type_id = foreign_type.to_chalk(db); | ||
62 | chalk_ir::TyKind::Foreign(foreign_type_id).intern(&Interner) | ||
63 | } | ||
64 | |||
65 | TypeCtor::Bool => chalk_ir::TyKind::Scalar(Scalar::Bool).intern(&Interner), | ||
66 | TypeCtor::Char => chalk_ir::TyKind::Scalar(Scalar::Char).intern(&Interner), | ||
67 | TypeCtor::Int(int_ty) => { | ||
68 | chalk_ir::TyKind::Scalar(int_ty_to_chalk(int_ty)).intern(&Interner) | ||
69 | } | ||
70 | TypeCtor::Float(FloatTy { bitness: FloatBitness::X32 }) => { | ||
71 | chalk_ir::TyKind::Scalar(Scalar::Float(chalk_ir::FloatTy::F32)) | ||
72 | .intern(&Interner) | ||
73 | } | ||
74 | TypeCtor::Float(FloatTy { bitness: FloatBitness::X64 }) => { | ||
75 | chalk_ir::TyKind::Scalar(Scalar::Float(chalk_ir::FloatTy::F64)) | ||
76 | .intern(&Interner) | ||
77 | } | ||
78 | |||
79 | TypeCtor::Tuple { cardinality } => { | ||
80 | let substitution = apply_ty.parameters.to_chalk(db); | ||
81 | chalk_ir::TyKind::Tuple(cardinality.into(), substitution).intern(&Interner) | ||
82 | } | ||
83 | TypeCtor::RawPtr(mutability) => { | ||
84 | let ty = apply_ty.parameters[0].clone().to_chalk(db); | ||
85 | chalk_ir::TyKind::Raw(mutability.to_chalk(db), ty).intern(&Interner) | ||
86 | } | ||
87 | TypeCtor::Slice => { | ||
88 | chalk_ir::TyKind::Slice(apply_ty.parameters[0].clone().to_chalk(db)) | ||
89 | .intern(&Interner) | ||
90 | } | ||
91 | TypeCtor::Str => chalk_ir::TyKind::Str.intern(&Interner), | ||
92 | TypeCtor::FnDef(callable_def) => { | ||
93 | let id = callable_def.to_chalk(db); | ||
94 | let substitution = apply_ty.parameters.to_chalk(db); | ||
95 | chalk_ir::TyKind::FnDef(id, substitution).intern(&Interner) | ||
96 | } | ||
97 | TypeCtor::Never => chalk_ir::TyKind::Never.intern(&Interner), | ||
98 | |||
99 | TypeCtor::Closure { def, expr } => { | ||
100 | let closure_id = db.intern_closure((def, expr)); | ||
101 | let substitution = apply_ty.parameters.to_chalk(db); | ||
102 | chalk_ir::TyKind::Closure(closure_id.into(), substitution).intern(&Interner) | ||
103 | } | ||
104 | |||
105 | TypeCtor::Adt(adt_id) => { | ||
106 | let substitution = apply_ty.parameters.to_chalk(db); | ||
107 | chalk_ir::TyKind::Adt(chalk_ir::AdtId(adt_id), substitution).intern(&Interner) | ||
50 | } | 108 | } |
51 | }, | 109 | }, |
52 | Ty::Projection(proj_ty) => { | 110 | Ty::Projection(proj_ty) => { |
@@ -67,7 +125,7 @@ impl ToChalk for Ty { | |||
67 | } | 125 | } |
68 | .to_ty::<Interner>(&Interner) | 126 | .to_ty::<Interner>(&Interner) |
69 | } | 127 | } |
70 | Ty::Bound(idx) => chalk_ir::TyData::BoundVar(idx).intern(&Interner), | 128 | Ty::Bound(idx) => chalk_ir::TyKind::BoundVar(idx).intern(&Interner), |
71 | Ty::Infer(_infer_ty) => panic!("uncanonicalized infer ty"), | 129 | Ty::Infer(_infer_ty) => panic!("uncanonicalized infer ty"), |
72 | Ty::Dyn(predicates) => { | 130 | Ty::Dyn(predicates) => { |
73 | let where_clauses = chalk_ir::QuantifiedWhereClauses::from_iter( | 131 | let where_clauses = chalk_ir::QuantifiedWhereClauses::from_iter( |
@@ -76,57 +134,47 @@ impl ToChalk for Ty { | |||
76 | ); | 134 | ); |
77 | let bounded_ty = chalk_ir::DynTy { | 135 | let bounded_ty = chalk_ir::DynTy { |
78 | bounds: make_binders(where_clauses, 1), | 136 | bounds: make_binders(where_clauses, 1), |
79 | lifetime: FAKE_PLACEHOLDER.to_lifetime(&Interner), | 137 | lifetime: LifetimeData::Static.intern(&Interner), |
80 | }; | 138 | }; |
81 | chalk_ir::TyData::Dyn(bounded_ty).intern(&Interner) | 139 | chalk_ir::TyKind::Dyn(bounded_ty).intern(&Interner) |
82 | } | 140 | } |
83 | Ty::Opaque(opaque_ty) => { | 141 | Ty::Opaque(opaque_ty) => { |
84 | let opaque_ty_id = opaque_ty.opaque_ty_id.to_chalk(db); | 142 | let opaque_ty_id = opaque_ty.opaque_ty_id.to_chalk(db); |
85 | let substitution = opaque_ty.parameters.to_chalk(db); | 143 | let substitution = opaque_ty.parameters.to_chalk(db); |
86 | chalk_ir::TyData::Alias(chalk_ir::AliasTy::Opaque(chalk_ir::OpaqueTy { | 144 | chalk_ir::TyKind::Alias(chalk_ir::AliasTy::Opaque(chalk_ir::OpaqueTy { |
87 | opaque_ty_id, | 145 | opaque_ty_id, |
88 | substitution, | 146 | substitution, |
89 | })) | 147 | })) |
90 | .intern(&Interner) | 148 | .intern(&Interner) |
91 | } | 149 | } |
92 | Ty::Unknown => { | 150 | Ty::Unknown => chalk_ir::TyKind::Error.intern(&Interner), |
93 | let substitution = chalk_ir::Substitution::empty(&Interner); | ||
94 | let name = TypeName::Error; | ||
95 | chalk_ir::ApplicationTy { name, substitution }.cast(&Interner).intern(&Interner) | ||
96 | } | ||
97 | } | 151 | } |
98 | } | 152 | } |
99 | fn from_chalk(db: &dyn HirDatabase, chalk: chalk_ir::Ty<Interner>) -> Self { | 153 | fn from_chalk(db: &dyn HirDatabase, chalk: chalk_ir::Ty<Interner>) -> Self { |
100 | match chalk.data(&Interner).clone() { | 154 | match chalk.data(&Interner).kind.clone() { |
101 | chalk_ir::TyData::Apply(apply_ty) => match apply_ty.name { | 155 | chalk_ir::TyKind::Error => Ty::Unknown, |
102 | TypeName::Error => Ty::Unknown, | 156 | chalk_ir::TyKind::Array(ty, _size) => { |
103 | TypeName::Ref(m) => ref_from_chalk(db, m, apply_ty.substitution), | 157 | Ty::apply(TypeCtor::Array, Substs::single(from_chalk(db, ty))) |
104 | TypeName::Array => array_from_chalk(db, apply_ty.substitution), | 158 | } |
105 | _ => { | 159 | chalk_ir::TyKind::Placeholder(idx) => { |
106 | let ctor = from_chalk(db, apply_ty.name); | ||
107 | let parameters = from_chalk(db, apply_ty.substitution); | ||
108 | Ty::Apply(ApplicationTy { ctor, parameters }) | ||
109 | } | ||
110 | }, | ||
111 | chalk_ir::TyData::Placeholder(idx) => { | ||
112 | assert_eq!(idx.ui, UniverseIndex::ROOT); | 160 | assert_eq!(idx.ui, UniverseIndex::ROOT); |
113 | let interned_id = crate::db::GlobalTypeParamId::from_intern_id( | 161 | let interned_id = crate::db::GlobalTypeParamId::from_intern_id( |
114 | crate::salsa::InternId::from(idx.idx), | 162 | crate::salsa::InternId::from(idx.idx), |
115 | ); | 163 | ); |
116 | Ty::Placeholder(db.lookup_intern_type_param_id(interned_id)) | 164 | Ty::Placeholder(db.lookup_intern_type_param_id(interned_id)) |
117 | } | 165 | } |
118 | chalk_ir::TyData::Alias(chalk_ir::AliasTy::Projection(proj)) => { | 166 | chalk_ir::TyKind::Alias(chalk_ir::AliasTy::Projection(proj)) => { |
119 | let associated_ty = | 167 | let associated_ty = |
120 | from_chalk::<TypeAliasAsAssocType, _>(db, proj.associated_ty_id).0; | 168 | from_chalk::<TypeAliasAsAssocType, _>(db, proj.associated_ty_id).0; |
121 | let parameters = from_chalk(db, proj.substitution); | 169 | let parameters = from_chalk(db, proj.substitution); |
122 | Ty::Projection(ProjectionTy { associated_ty, parameters }) | 170 | Ty::Projection(ProjectionTy { associated_ty, parameters }) |
123 | } | 171 | } |
124 | chalk_ir::TyData::Alias(chalk_ir::AliasTy::Opaque(opaque_ty)) => { | 172 | chalk_ir::TyKind::Alias(chalk_ir::AliasTy::Opaque(opaque_ty)) => { |
125 | let impl_trait_id = from_chalk(db, opaque_ty.opaque_ty_id); | 173 | let impl_trait_id = from_chalk(db, opaque_ty.opaque_ty_id); |
126 | let parameters = from_chalk(db, opaque_ty.substitution); | 174 | let parameters = from_chalk(db, opaque_ty.substitution); |
127 | Ty::Opaque(OpaqueTy { opaque_ty_id: impl_trait_id, parameters }) | 175 | Ty::Opaque(OpaqueTy { opaque_ty_id: impl_trait_id, parameters }) |
128 | } | 176 | } |
129 | chalk_ir::TyData::Function(chalk_ir::FnPointer { | 177 | chalk_ir::TyKind::Function(chalk_ir::FnPointer { |
130 | num_binders, | 178 | num_binders, |
131 | sig: chalk_ir::FnSig { variadic, .. }, | 179 | sig: chalk_ir::FnSig { variadic, .. }, |
132 | substitution, | 180 | substitution, |
@@ -145,9 +193,9 @@ impl ToChalk for Ty { | |||
145 | parameters, | 193 | parameters, |
146 | }) | 194 | }) |
147 | } | 195 | } |
148 | chalk_ir::TyData::BoundVar(idx) => Ty::Bound(idx), | 196 | chalk_ir::TyKind::BoundVar(idx) => Ty::Bound(idx), |
149 | chalk_ir::TyData::InferenceVar(_iv, _kind) => Ty::Unknown, | 197 | chalk_ir::TyKind::InferenceVar(_iv, _kind) => Ty::Unknown, |
150 | chalk_ir::TyData::Dyn(where_clauses) => { | 198 | chalk_ir::TyKind::Dyn(where_clauses) => { |
151 | assert_eq!(where_clauses.bounds.binders.len(&Interner), 1); | 199 | assert_eq!(where_clauses.bounds.binders.len(&Interner), 1); |
152 | let predicates = where_clauses | 200 | let predicates = where_clauses |
153 | .bounds | 201 | .bounds |
@@ -157,12 +205,75 @@ impl ToChalk for Ty { | |||
157 | .collect(); | 205 | .collect(); |
158 | Ty::Dyn(predicates) | 206 | Ty::Dyn(predicates) |
159 | } | 207 | } |
208 | |||
209 | chalk_ir::TyKind::Adt(struct_id, subst) => { | ||
210 | apply_ty_from_chalk(db, TypeCtor::Adt(struct_id.0), subst) | ||
211 | } | ||
212 | chalk_ir::TyKind::AssociatedType(type_id, subst) => apply_ty_from_chalk( | ||
213 | db, | ||
214 | TypeCtor::AssociatedType(from_chalk::<TypeAliasAsAssocType, _>(db, type_id).0), | ||
215 | subst, | ||
216 | ), | ||
217 | chalk_ir::TyKind::OpaqueType(opaque_type_id, subst) => { | ||
218 | apply_ty_from_chalk(db, TypeCtor::OpaqueType(from_chalk(db, opaque_type_id)), subst) | ||
219 | } | ||
220 | |||
221 | chalk_ir::TyKind::Scalar(Scalar::Bool) => Ty::simple(TypeCtor::Bool), | ||
222 | chalk_ir::TyKind::Scalar(Scalar::Char) => Ty::simple(TypeCtor::Char), | ||
223 | chalk_ir::TyKind::Scalar(Scalar::Int(int_ty)) => Ty::simple(TypeCtor::Int(IntTy { | ||
224 | signedness: Signedness::Signed, | ||
225 | bitness: bitness_from_chalk_int(int_ty), | ||
226 | })), | ||
227 | chalk_ir::TyKind::Scalar(Scalar::Uint(uint_ty)) => Ty::simple(TypeCtor::Int(IntTy { | ||
228 | signedness: Signedness::Unsigned, | ||
229 | bitness: bitness_from_chalk_uint(uint_ty), | ||
230 | })), | ||
231 | chalk_ir::TyKind::Scalar(Scalar::Float(chalk_ir::FloatTy::F32)) => { | ||
232 | Ty::simple(TypeCtor::Float(FloatTy { bitness: FloatBitness::X32 })) | ||
233 | } | ||
234 | chalk_ir::TyKind::Scalar(Scalar::Float(chalk_ir::FloatTy::F64)) => { | ||
235 | Ty::simple(TypeCtor::Float(FloatTy { bitness: FloatBitness::X64 })) | ||
236 | } | ||
237 | chalk_ir::TyKind::Tuple(cardinality, subst) => { | ||
238 | apply_ty_from_chalk(db, TypeCtor::Tuple { cardinality: cardinality as u16 }, subst) | ||
239 | } | ||
240 | chalk_ir::TyKind::Raw(mutability, ty) => { | ||
241 | Ty::apply_one(TypeCtor::RawPtr(from_chalk(db, mutability)), from_chalk(db, ty)) | ||
242 | } | ||
243 | chalk_ir::TyKind::Slice(ty) => Ty::apply_one(TypeCtor::Slice, from_chalk(db, ty)), | ||
244 | chalk_ir::TyKind::Ref(mutability, _lifetime, ty) => { | ||
245 | Ty::apply_one(TypeCtor::Ref(from_chalk(db, mutability)), from_chalk(db, ty)) | ||
246 | } | ||
247 | chalk_ir::TyKind::Str => Ty::simple(TypeCtor::Str), | ||
248 | chalk_ir::TyKind::Never => Ty::simple(TypeCtor::Never), | ||
249 | |||
250 | chalk_ir::TyKind::FnDef(fn_def_id, subst) => { | ||
251 | let callable_def = from_chalk(db, fn_def_id); | ||
252 | apply_ty_from_chalk(db, TypeCtor::FnDef(callable_def), subst) | ||
253 | } | ||
254 | |||
255 | chalk_ir::TyKind::Closure(id, subst) => { | ||
256 | let id: crate::db::ClosureId = id.into(); | ||
257 | let (def, expr) = db.lookup_intern_closure(id); | ||
258 | apply_ty_from_chalk(db, TypeCtor::Closure { def, expr }, subst) | ||
259 | } | ||
260 | |||
261 | chalk_ir::TyKind::Foreign(foreign_def_id) => Ty::simple(TypeCtor::ForeignType( | ||
262 | from_chalk::<TypeAliasAsForeignType, _>(db, foreign_def_id).0, | ||
263 | )), | ||
264 | chalk_ir::TyKind::Generator(_, _) => unimplemented!(), // FIXME | ||
265 | chalk_ir::TyKind::GeneratorWitness(_, _) => unimplemented!(), // FIXME | ||
160 | } | 266 | } |
161 | } | 267 | } |
162 | } | 268 | } |
163 | 269 | ||
164 | const FAKE_PLACEHOLDER: PlaceholderIndex = | 270 | fn apply_ty_from_chalk( |
165 | PlaceholderIndex { ui: UniverseIndex::ROOT, idx: usize::MAX }; | 271 | db: &dyn HirDatabase, |
272 | ctor: TypeCtor, | ||
273 | subst: chalk_ir::Substitution<Interner>, | ||
274 | ) -> Ty { | ||
275 | Ty::Apply(ApplicationTy { ctor, parameters: from_chalk(db, subst) }) | ||
276 | } | ||
166 | 277 | ||
167 | /// We currently don't model lifetimes, but Chalk does. So, we have to insert a | 278 | /// We currently don't model lifetimes, but Chalk does. So, we have to insert a |
168 | /// fake lifetime here, because Chalks built-in logic may expect it to be there. | 279 | /// fake lifetime here, because Chalks built-in logic may expect it to be there. |
@@ -172,57 +283,22 @@ fn ref_to_chalk( | |||
172 | subst: Substs, | 283 | subst: Substs, |
173 | ) -> chalk_ir::Ty<Interner> { | 284 | ) -> chalk_ir::Ty<Interner> { |
174 | let arg = subst[0].clone().to_chalk(db); | 285 | let arg = subst[0].clone().to_chalk(db); |
175 | let lifetime = FAKE_PLACEHOLDER.to_lifetime(&Interner); | 286 | let lifetime = LifetimeData::Static.intern(&Interner); |
176 | chalk_ir::ApplicationTy { | 287 | chalk_ir::TyKind::Ref(mutability.to_chalk(db), lifetime, arg).intern(&Interner) |
177 | name: TypeName::Ref(mutability.to_chalk(db)), | ||
178 | substitution: chalk_ir::Substitution::from_iter( | ||
179 | &Interner, | ||
180 | vec![lifetime.cast(&Interner), arg.cast(&Interner)], | ||
181 | ), | ||
182 | } | ||
183 | .intern(&Interner) | ||
184 | } | ||
185 | |||
186 | /// Here we remove the lifetime from the type we got from Chalk. | ||
187 | fn ref_from_chalk( | ||
188 | db: &dyn HirDatabase, | ||
189 | mutability: chalk_ir::Mutability, | ||
190 | subst: chalk_ir::Substitution<Interner>, | ||
191 | ) -> Ty { | ||
192 | let tys = subst | ||
193 | .iter(&Interner) | ||
194 | .filter_map(|p| Some(from_chalk(db, p.ty(&Interner)?.clone()))) | ||
195 | .collect(); | ||
196 | Ty::apply(TypeCtor::Ref(from_chalk(db, mutability)), Substs(tys)) | ||
197 | } | 288 | } |
198 | 289 | ||
199 | /// We currently don't model constants, but Chalk does. So, we have to insert a | 290 | /// We currently don't model constants, but Chalk does. So, we have to insert a |
200 | /// fake constant here, because Chalks built-in logic may expect it to be there. | 291 | /// fake constant here, because Chalks built-in logic may expect it to be there. |
201 | fn array_to_chalk(db: &dyn HirDatabase, subst: Substs) -> chalk_ir::Ty<Interner> { | 292 | fn array_to_chalk(db: &dyn HirDatabase, subst: Substs) -> chalk_ir::Ty<Interner> { |
202 | let arg = subst[0].clone().to_chalk(db); | 293 | let arg = subst[0].clone().to_chalk(db); |
203 | let usize_ty = chalk_ir::ApplicationTy { | 294 | let usize_ty = |
204 | name: TypeName::Scalar(Scalar::Uint(chalk_ir::UintTy::Usize)), | 295 | chalk_ir::TyKind::Scalar(Scalar::Uint(chalk_ir::UintTy::Usize)).intern(&Interner); |
205 | substitution: chalk_ir::Substitution::empty(&Interner), | 296 | let const_ = chalk_ir::ConstData { |
297 | ty: usize_ty, | ||
298 | value: chalk_ir::ConstValue::Concrete(chalk_ir::ConcreteConst { interned: () }), | ||
206 | } | 299 | } |
207 | .intern(&Interner); | 300 | .intern(&Interner); |
208 | let const_ = FAKE_PLACEHOLDER.to_const(&Interner, usize_ty); | 301 | chalk_ir::TyKind::Array(arg, const_).intern(&Interner) |
209 | chalk_ir::ApplicationTy { | ||
210 | name: TypeName::Array, | ||
211 | substitution: chalk_ir::Substitution::from_iter( | ||
212 | &Interner, | ||
213 | vec![arg.cast(&Interner), const_.cast(&Interner)], | ||
214 | ), | ||
215 | } | ||
216 | .intern(&Interner) | ||
217 | } | ||
218 | |||
219 | /// Here we remove the const from the type we got from Chalk. | ||
220 | fn array_from_chalk(db: &dyn HirDatabase, subst: chalk_ir::Substitution<Interner>) -> Ty { | ||
221 | let tys = subst | ||
222 | .iter(&Interner) | ||
223 | .filter_map(|p| Some(from_chalk(db, p.ty(&Interner)?.clone()))) | ||
224 | .collect(); | ||
225 | Ty::apply(TypeCtor::Array, Substs(tys)) | ||
226 | } | 302 | } |
227 | 303 | ||
228 | impl ToChalk for Substs { | 304 | impl ToChalk for Substs { |
@@ -287,124 +363,6 @@ impl ToChalk for OpaqueTyId { | |||
287 | } | 363 | } |
288 | } | 364 | } |
289 | 365 | ||
290 | impl ToChalk for TypeCtor { | ||
291 | type Chalk = TypeName<Interner>; | ||
292 | |||
293 | fn to_chalk(self, db: &dyn HirDatabase) -> TypeName<Interner> { | ||
294 | match self { | ||
295 | TypeCtor::AssociatedType(type_alias) => { | ||
296 | let assoc_type = TypeAliasAsAssocType(type_alias); | ||
297 | let assoc_type_id = assoc_type.to_chalk(db); | ||
298 | TypeName::AssociatedType(assoc_type_id) | ||
299 | } | ||
300 | |||
301 | TypeCtor::OpaqueType(impl_trait_id) => { | ||
302 | let id = impl_trait_id.to_chalk(db); | ||
303 | TypeName::OpaqueType(id) | ||
304 | } | ||
305 | |||
306 | TypeCtor::ForeignType(type_alias) => { | ||
307 | let foreign_type = TypeAliasAsForeignType(type_alias); | ||
308 | let foreign_type_id = foreign_type.to_chalk(db); | ||
309 | TypeName::Foreign(foreign_type_id) | ||
310 | } | ||
311 | |||
312 | TypeCtor::Bool => TypeName::Scalar(Scalar::Bool), | ||
313 | TypeCtor::Char => TypeName::Scalar(Scalar::Char), | ||
314 | TypeCtor::Int(int_ty) => TypeName::Scalar(int_ty_to_chalk(int_ty)), | ||
315 | TypeCtor::Float(FloatTy { bitness: FloatBitness::X32 }) => { | ||
316 | TypeName::Scalar(Scalar::Float(chalk_ir::FloatTy::F32)) | ||
317 | } | ||
318 | TypeCtor::Float(FloatTy { bitness: FloatBitness::X64 }) => { | ||
319 | TypeName::Scalar(Scalar::Float(chalk_ir::FloatTy::F64)) | ||
320 | } | ||
321 | |||
322 | TypeCtor::Tuple { cardinality } => TypeName::Tuple(cardinality.into()), | ||
323 | TypeCtor::RawPtr(mutability) => TypeName::Raw(mutability.to_chalk(db)), | ||
324 | TypeCtor::Slice => TypeName::Slice, | ||
325 | TypeCtor::Array => TypeName::Array, | ||
326 | TypeCtor::Ref(mutability) => TypeName::Ref(mutability.to_chalk(db)), | ||
327 | TypeCtor::Str => TypeName::Str, | ||
328 | TypeCtor::FnDef(callable_def) => { | ||
329 | let id = callable_def.to_chalk(db); | ||
330 | TypeName::FnDef(id) | ||
331 | } | ||
332 | TypeCtor::Never => TypeName::Never, | ||
333 | |||
334 | TypeCtor::Closure { def, expr } => { | ||
335 | let closure_id = db.intern_closure((def, expr)); | ||
336 | TypeName::Closure(closure_id.into()) | ||
337 | } | ||
338 | |||
339 | TypeCtor::Adt(adt_id) => TypeName::Adt(chalk_ir::AdtId(adt_id)), | ||
340 | |||
341 | TypeCtor::FnPtr { .. } => { | ||
342 | // This should not be reached, since Chalk doesn't represent | ||
343 | // function pointers with TypeName | ||
344 | unreachable!() | ||
345 | } | ||
346 | } | ||
347 | } | ||
348 | |||
349 | fn from_chalk(db: &dyn HirDatabase, type_name: TypeName<Interner>) -> TypeCtor { | ||
350 | match type_name { | ||
351 | TypeName::Adt(struct_id) => TypeCtor::Adt(struct_id.0), | ||
352 | TypeName::AssociatedType(type_id) => { | ||
353 | TypeCtor::AssociatedType(from_chalk::<TypeAliasAsAssocType, _>(db, type_id).0) | ||
354 | } | ||
355 | TypeName::OpaqueType(opaque_type_id) => { | ||
356 | TypeCtor::OpaqueType(from_chalk(db, opaque_type_id)) | ||
357 | } | ||
358 | |||
359 | TypeName::Scalar(Scalar::Bool) => TypeCtor::Bool, | ||
360 | TypeName::Scalar(Scalar::Char) => TypeCtor::Char, | ||
361 | TypeName::Scalar(Scalar::Int(int_ty)) => TypeCtor::Int(IntTy { | ||
362 | signedness: Signedness::Signed, | ||
363 | bitness: bitness_from_chalk_int(int_ty), | ||
364 | }), | ||
365 | TypeName::Scalar(Scalar::Uint(uint_ty)) => TypeCtor::Int(IntTy { | ||
366 | signedness: Signedness::Unsigned, | ||
367 | bitness: bitness_from_chalk_uint(uint_ty), | ||
368 | }), | ||
369 | TypeName::Scalar(Scalar::Float(chalk_ir::FloatTy::F32)) => { | ||
370 | TypeCtor::Float(FloatTy { bitness: FloatBitness::X32 }) | ||
371 | } | ||
372 | TypeName::Scalar(Scalar::Float(chalk_ir::FloatTy::F64)) => { | ||
373 | TypeCtor::Float(FloatTy { bitness: FloatBitness::X64 }) | ||
374 | } | ||
375 | TypeName::Tuple(cardinality) => TypeCtor::Tuple { cardinality: cardinality as u16 }, | ||
376 | TypeName::Raw(mutability) => TypeCtor::RawPtr(from_chalk(db, mutability)), | ||
377 | TypeName::Slice => TypeCtor::Slice, | ||
378 | TypeName::Ref(mutability) => TypeCtor::Ref(from_chalk(db, mutability)), | ||
379 | TypeName::Str => TypeCtor::Str, | ||
380 | TypeName::Never => TypeCtor::Never, | ||
381 | |||
382 | TypeName::FnDef(fn_def_id) => { | ||
383 | let callable_def = from_chalk(db, fn_def_id); | ||
384 | TypeCtor::FnDef(callable_def) | ||
385 | } | ||
386 | TypeName::Array => TypeCtor::Array, | ||
387 | |||
388 | TypeName::Closure(id) => { | ||
389 | let id: crate::db::ClosureId = id.into(); | ||
390 | let (def, expr) = db.lookup_intern_closure(id); | ||
391 | TypeCtor::Closure { def, expr } | ||
392 | } | ||
393 | |||
394 | TypeName::Foreign(foreign_def_id) => { | ||
395 | TypeCtor::ForeignType(from_chalk::<TypeAliasAsForeignType, _>(db, foreign_def_id).0) | ||
396 | } | ||
397 | |||
398 | TypeName::Error => { | ||
399 | // this should not be reached, since we don't represent TypeName::Error with TypeCtor | ||
400 | unreachable!() | ||
401 | } | ||
402 | TypeName::Generator(_) => unimplemented!(), // FIXME | ||
403 | TypeName::GeneratorWitness(_) => unimplemented!(), // FIXME | ||
404 | } | ||
405 | } | ||
406 | } | ||
407 | |||
408 | fn bitness_from_chalk_uint(uint_ty: chalk_ir::UintTy) -> IntBitness { | 366 | fn bitness_from_chalk_uint(uint_ty: chalk_ir::UintTy) -> IntBitness { |
409 | use chalk_ir::UintTy; | 367 | use chalk_ir::UintTy; |
410 | 368 | ||
@@ -506,7 +464,7 @@ impl ToChalk for CallableDefId { | |||
506 | } | 464 | } |
507 | } | 465 | } |
508 | 466 | ||
509 | pub struct TypeAliasAsAssocType(pub TypeAliasId); | 467 | pub(crate) struct TypeAliasAsAssocType(pub(crate) TypeAliasId); |
510 | 468 | ||
511 | impl ToChalk for TypeAliasAsAssocType { | 469 | impl ToChalk for TypeAliasAsAssocType { |
512 | type Chalk = AssocTypeId; | 470 | type Chalk = AssocTypeId; |
@@ -520,7 +478,7 @@ impl ToChalk for TypeAliasAsAssocType { | |||
520 | } | 478 | } |
521 | } | 479 | } |
522 | 480 | ||
523 | pub struct TypeAliasAsForeignType(pub TypeAliasId); | 481 | pub(crate) struct TypeAliasAsForeignType(pub(crate) TypeAliasId); |
524 | 482 | ||
525 | impl ToChalk for TypeAliasAsForeignType { | 483 | impl ToChalk for TypeAliasAsForeignType { |
526 | type Chalk = ForeignDefId; | 484 | type Chalk = ForeignDefId; |
@@ -534,7 +492,7 @@ impl ToChalk for TypeAliasAsForeignType { | |||
534 | } | 492 | } |
535 | } | 493 | } |
536 | 494 | ||
537 | pub struct TypeAliasAsValue(pub TypeAliasId); | 495 | pub(crate) struct TypeAliasAsValue(pub(crate) TypeAliasId); |
538 | 496 | ||
539 | impl ToChalk for TypeAliasAsValue { | 497 | impl ToChalk for TypeAliasAsValue { |
540 | type Chalk = AssociatedTyValueId; | 498 | type Chalk = AssociatedTyValueId; |
@@ -676,9 +634,9 @@ where | |||
676 | .kinds | 634 | .kinds |
677 | .iter() | 635 | .iter() |
678 | .map(|k| match k { | 636 | .map(|k| match k { |
679 | TyKind::General => chalk_ir::TyKind::General, | 637 | TyKind::General => chalk_ir::TyVariableKind::General, |
680 | TyKind::Integer => chalk_ir::TyKind::Integer, | 638 | TyKind::Integer => chalk_ir::TyVariableKind::Integer, |
681 | TyKind::Float => chalk_ir::TyKind::Float, | 639 | TyKind::Float => chalk_ir::TyVariableKind::Float, |
682 | }) | 640 | }) |
683 | .map(|tk| { | 641 | .map(|tk| { |
684 | chalk_ir::CanonicalVarKind::new( | 642 | chalk_ir::CanonicalVarKind::new( |
@@ -699,9 +657,9 @@ where | |||
699 | .iter(&Interner) | 657 | .iter(&Interner) |
700 | .map(|k| match k.kind { | 658 | .map(|k| match k.kind { |
701 | chalk_ir::VariableKind::Ty(tk) => match tk { | 659 | chalk_ir::VariableKind::Ty(tk) => match tk { |
702 | chalk_ir::TyKind::General => TyKind::General, | 660 | chalk_ir::TyVariableKind::General => TyKind::General, |
703 | chalk_ir::TyKind::Integer => TyKind::Integer, | 661 | chalk_ir::TyVariableKind::Integer => TyKind::Integer, |
704 | chalk_ir::TyKind::Float => TyKind::Float, | 662 | chalk_ir::TyVariableKind::Float => TyKind::Float, |
705 | }, | 663 | }, |
706 | chalk_ir::VariableKind::Lifetime => panic!("unexpected lifetime from Chalk"), | 664 | chalk_ir::VariableKind::Lifetime => panic!("unexpected lifetime from Chalk"), |
707 | chalk_ir::VariableKind::Const(_) => panic!("unexpected const from Chalk"), | 665 | chalk_ir::VariableKind::Const(_) => panic!("unexpected const from Chalk"), |
@@ -767,7 +725,8 @@ where | |||
767 | chalk_ir::Binders::new( | 725 | chalk_ir::Binders::new( |
768 | chalk_ir::VariableKinds::from_iter( | 726 | chalk_ir::VariableKinds::from_iter( |
769 | &Interner, | 727 | &Interner, |
770 | std::iter::repeat(chalk_ir::VariableKind::Ty(chalk_ir::TyKind::General)).take(num_vars), | 728 | std::iter::repeat(chalk_ir::VariableKind::Ty(chalk_ir::TyVariableKind::General)) |
729 | .take(num_vars), | ||
771 | ), | 730 | ), |
772 | value, | 731 | value, |
773 | ) | 732 | ) |