aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFlorian Diebold <[email protected]>2020-07-11 17:33:50 +0100
committerFlorian Diebold <[email protected]>2020-07-12 19:20:31 +0100
commit94f5f69ff4b3185e559339c86f9528b9443ca67d (patch)
tree07cbcae1995f3bb8706cb065812bef7eb10d870b
parent2a4166501d8990d3a489e89af3d92002540c288c (diff)
Use Chalk built-in representation for array types
-rw-r--r--crates/ra_hir_ty/src/traits/chalk/mapping.rs43
1 files changed, 36 insertions, 7 deletions
diff --git a/crates/ra_hir_ty/src/traits/chalk/mapping.rs b/crates/ra_hir_ty/src/traits/chalk/mapping.rs
index 7bdb6264e..7dc9ee759 100644
--- a/crates/ra_hir_ty/src/traits/chalk/mapping.rs
+++ b/crates/ra_hir_ty/src/traits/chalk/mapping.rs
@@ -29,6 +29,7 @@ impl ToChalk for Ty {
29 match self { 29 match self {
30 Ty::Apply(apply_ty) => match apply_ty.ctor { 30 Ty::Apply(apply_ty) => match apply_ty.ctor {
31 TypeCtor::Ref(m) => ref_to_chalk(db, m, apply_ty.parameters), 31 TypeCtor::Ref(m) => ref_to_chalk(db, m, apply_ty.parameters),
32 TypeCtor::Array => array_to_chalk(db, apply_ty.parameters),
32 TypeCtor::FnPtr { num_args: _ } => { 33 TypeCtor::FnPtr { num_args: _ } => {
33 let substitution = apply_ty.parameters.to_chalk(db).shifted_in(&Interner); 34 let substitution = apply_ty.parameters.to_chalk(db).shifted_in(&Interner);
34 chalk_ir::TyData::Function(chalk_ir::Fn { num_binders: 0, substitution }) 35 chalk_ir::TyData::Function(chalk_ir::Fn { num_binders: 0, substitution })
@@ -67,7 +68,7 @@ impl ToChalk for Ty {
67 ); 68 );
68 let bounded_ty = chalk_ir::DynTy { 69 let bounded_ty = chalk_ir::DynTy {
69 bounds: make_binders(where_clauses, 1), 70 bounds: make_binders(where_clauses, 1),
70 lifetime: LIFETIME_PLACEHOLDER.to_lifetime(&Interner), 71 lifetime: FAKE_PLACEHOLDER.to_lifetime(&Interner),
71 }; 72 };
72 chalk_ir::TyData::Dyn(bounded_ty).intern(&Interner) 73 chalk_ir::TyData::Dyn(bounded_ty).intern(&Interner)
73 } 74 }
@@ -92,6 +93,7 @@ impl ToChalk for Ty {
92 chalk_ir::TyData::Apply(apply_ty) => match apply_ty.name { 93 chalk_ir::TyData::Apply(apply_ty) => match apply_ty.name {
93 TypeName::Error => Ty::Unknown, 94 TypeName::Error => Ty::Unknown,
94 TypeName::Ref(m) => ref_from_chalk(db, m, apply_ty.substitution), 95 TypeName::Ref(m) => ref_from_chalk(db, m, apply_ty.substitution),
96 TypeName::Array => array_from_chalk(db, apply_ty.substitution),
95 _ => { 97 _ => {
96 let ctor = from_chalk(db, apply_ty.name); 98 let ctor = from_chalk(db, apply_ty.name);
97 let parameters = from_chalk(db, apply_ty.substitution); 99 let parameters = from_chalk(db, apply_ty.substitution);
@@ -138,7 +140,7 @@ impl ToChalk for Ty {
138 } 140 }
139} 141}
140 142
141const LIFETIME_PLACEHOLDER: PlaceholderIndex = 143const FAKE_PLACEHOLDER: PlaceholderIndex =
142 PlaceholderIndex { ui: UniverseIndex::ROOT, idx: usize::MAX }; 144 PlaceholderIndex { ui: UniverseIndex::ROOT, idx: usize::MAX };
143 145
144/// We currently don't model lifetimes, but Chalk does. So, we have to insert a 146/// We currently don't model lifetimes, but Chalk does. So, we have to insert a
@@ -149,7 +151,7 @@ fn ref_to_chalk(
149 subst: Substs, 151 subst: Substs,
150) -> chalk_ir::Ty<Interner> { 152) -> chalk_ir::Ty<Interner> {
151 let arg = subst[0].clone().to_chalk(db); 153 let arg = subst[0].clone().to_chalk(db);
152 let lifetime = LIFETIME_PLACEHOLDER.to_lifetime(&Interner); 154 let lifetime = FAKE_PLACEHOLDER.to_lifetime(&Interner);
153 chalk_ir::ApplicationTy { 155 chalk_ir::ApplicationTy {
154 name: TypeName::Ref(mutability.to_chalk(db)), 156 name: TypeName::Ref(mutability.to_chalk(db)),
155 substitution: chalk_ir::Substitution::from_iter( 157 substitution: chalk_ir::Substitution::from_iter(
@@ -173,6 +175,35 @@ fn ref_from_chalk(
173 Ty::apply(TypeCtor::Ref(from_chalk(db, mutability)), Substs(tys)) 175 Ty::apply(TypeCtor::Ref(from_chalk(db, mutability)), Substs(tys))
174} 176}
175 177
178/// We currently don't model constants, but Chalk does. So, we have to insert a
179/// fake constant here, because Chalks built-in logic may expect it to be there.
180fn array_to_chalk(db: &dyn HirDatabase, subst: Substs) -> chalk_ir::Ty<Interner> {
181 let arg = subst[0].clone().to_chalk(db);
182 let usize_ty = chalk_ir::ApplicationTy {
183 name: TypeName::Scalar(Scalar::Uint(chalk_ir::UintTy::Usize)),
184 substitution: chalk_ir::Substitution::empty(&Interner),
185 }
186 .intern(&Interner);
187 let const_ = FAKE_PLACEHOLDER.to_const(&Interner, usize_ty);
188 chalk_ir::ApplicationTy {
189 name: TypeName::Array,
190 substitution: chalk_ir::Substitution::from_iter(
191 &Interner,
192 vec![arg.cast(&Interner), const_.cast(&Interner)],
193 ),
194 }
195 .intern(&Interner)
196}
197
198/// Here we remove the const from the type we got from Chalk.
199fn array_from_chalk(db: &dyn HirDatabase, subst: chalk_ir::Substitution<Interner>) -> Ty {
200 let tys = subst
201 .iter(&Interner)
202 .filter_map(|p| Some(from_chalk(db, p.ty(&Interner)?.clone())))
203 .collect();
204 Ty::apply(TypeCtor::Array, Substs(tys))
205}
206
176impl ToChalk for Substs { 207impl ToChalk for Substs {
177 type Chalk = chalk_ir::Substitution<Interner>; 208 type Chalk = chalk_ir::Substitution<Interner>;
178 209
@@ -263,6 +294,7 @@ impl ToChalk for TypeCtor {
263 TypeCtor::Tuple { cardinality } => TypeName::Tuple(cardinality.into()), 294 TypeCtor::Tuple { cardinality } => TypeName::Tuple(cardinality.into()),
264 TypeCtor::RawPtr(mutability) => TypeName::Raw(mutability.to_chalk(db)), 295 TypeCtor::RawPtr(mutability) => TypeName::Raw(mutability.to_chalk(db)),
265 TypeCtor::Slice => TypeName::Slice, 296 TypeCtor::Slice => TypeName::Slice,
297 TypeCtor::Array => TypeName::Array,
266 TypeCtor::Ref(mutability) => TypeName::Ref(mutability.to_chalk(db)), 298 TypeCtor::Ref(mutability) => TypeName::Ref(mutability.to_chalk(db)),
267 TypeCtor::Str => TypeName::Str, 299 TypeCtor::Str => TypeName::Str,
268 TypeCtor::FnDef(callable_def) => { 300 TypeCtor::FnDef(callable_def) => {
@@ -272,10 +304,7 @@ impl ToChalk for TypeCtor {
272 TypeCtor::Never => TypeName::Never, 304 TypeCtor::Never => TypeName::Never,
273 305
274 // FIXME convert these 306 // FIXME convert these
275 TypeCtor::Adt(_) 307 TypeCtor::Adt(_) | TypeCtor::FnPtr { .. } | TypeCtor::Closure { .. } => {
276 | TypeCtor::Array
277 | TypeCtor::FnPtr { .. }
278 | TypeCtor::Closure { .. } => {
279 // other TypeCtors get interned and turned into a chalk StructId 308 // other TypeCtors get interned and turned into a chalk StructId
280 let struct_id = db.intern_type_ctor(self).into(); 309 let struct_id = db.intern_type_ctor(self).into();
281 TypeName::Adt(struct_id) 310 TypeName::Adt(struct_id)