Skip to content

Commit d280538

Browse files
committed
Clean up Chalk mapping a bit
1 parent 5df65e5 commit d280538

File tree

1 file changed

+37
-69
lines changed

1 file changed

+37
-69
lines changed

crates/hir_ty/src/traits/chalk/mapping.rs

Lines changed: 37 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,9 @@ use base_db::salsa::InternKey;
1010
use hir_def::{GenericDefId, TypeAliasId};
1111

1212
use crate::{
13-
chalk_ext::ProjectionTyExt, db::HirDatabase, dummy_usize_const, static_lifetime, AliasTy,
14-
CallableDefId, Canonical, Const, DomainGoal, FnPointer, GenericArg, InEnvironment, Lifetime,
15-
OpaqueTy, ProjectionTy, QuantifiedWhereClause, Substitution, TraitRef, Ty, TypeWalk,
16-
WhereClause,
13+
chalk_ext::ProjectionTyExt, db::HirDatabase, static_lifetime, AliasTy, CallableDefId,
14+
Canonical, DomainGoal, FnPointer, GenericArg, InEnvironment, OpaqueTy, ProjectionTy,
15+
QuantifiedWhereClause, Substitution, TraitRef, Ty, TypeWalk, WhereClause,
1716
};
1817

1918
use super::interner::*;
@@ -23,16 +22,16 @@ impl ToChalk for Ty {
2322
type Chalk = chalk_ir::Ty<Interner>;
2423
fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::Ty<Interner> {
2524
match self.into_inner() {
26-
TyKind::Ref(m, lt, ty) => ref_to_chalk(db, m, lt, ty),
27-
TyKind::Array(ty, size) => array_to_chalk(db, ty, size),
28-
TyKind::Function(FnPointer { sig, substitution: substs, .. }) => {
25+
TyKind::Ref(m, lt, ty) => {
26+
chalk_ir::TyKind::Ref(m, lt, ty.to_chalk(db)).intern(&Interner)
27+
}
28+
TyKind::Array(ty, size) => {
29+
chalk_ir::TyKind::Array(ty.to_chalk(db), size).intern(&Interner)
30+
}
31+
TyKind::Function(FnPointer { sig, substitution: substs, num_binders }) => {
2932
let substitution = chalk_ir::FnSubst(substs.0.to_chalk(db));
30-
chalk_ir::TyKind::Function(chalk_ir::FnPointer {
31-
num_binders: 0,
32-
sig,
33-
substitution,
34-
})
35-
.intern(&Interner)
33+
chalk_ir::TyKind::Function(chalk_ir::FnPointer { num_binders, sig, substitution })
34+
.intern(&Interner)
3635
}
3736
TyKind::AssociatedType(assoc_type_id, substs) => {
3837
let substitution = substs.to_chalk(db);
@@ -74,22 +73,13 @@ impl ToChalk for Ty {
7473
chalk_ir::TyKind::Adt(adt_id, substitution).intern(&Interner)
7574
}
7675
TyKind::Alias(AliasTy::Projection(proj_ty)) => {
77-
let associated_ty_id = proj_ty.associated_ty_id;
78-
let substitution = proj_ty.substitution.to_chalk(db);
79-
chalk_ir::AliasTy::Projection(chalk_ir::ProjectionTy {
80-
associated_ty_id,
81-
substitution,
82-
})
83-
.cast(&Interner)
84-
.intern(&Interner)
85-
}
86-
TyKind::Alias(AliasTy::Opaque(opaque_ty)) => {
87-
let opaque_ty_id = opaque_ty.opaque_ty_id;
88-
let substitution = opaque_ty.substitution.to_chalk(db);
89-
chalk_ir::AliasTy::Opaque(chalk_ir::OpaqueTy { opaque_ty_id, substitution })
76+
chalk_ir::AliasTy::Projection(proj_ty.to_chalk(db))
9077
.cast(&Interner)
9178
.intern(&Interner)
9279
}
80+
TyKind::Alias(AliasTy::Opaque(opaque_ty)) => {
81+
chalk_ir::AliasTy::Opaque(opaque_ty.to_chalk(db)).cast(&Interner).intern(&Interner)
82+
}
9383
TyKind::Placeholder(idx) => idx.to_ty::<Interner>(&Interner),
9484
TyKind::BoundVar(idx) => chalk_ir::TyKind::BoundVar(idx).intern(&Interner),
9585
TyKind::InferenceVar(..) => panic!("uncanonicalized infer ty"),
@@ -101,7 +91,7 @@ impl ToChalk for Ty {
10191
);
10292
let bounded_ty = chalk_ir::DynTy {
10393
bounds: chalk_ir::Binders::new(binders, where_clauses),
104-
lifetime: static_lifetime(),
94+
lifetime: dyn_ty.lifetime,
10595
};
10696
chalk_ir::TyKind::Dyn(bounded_ty).intern(&Interner)
10797
}
@@ -114,17 +104,10 @@ impl ToChalk for Ty {
114104
chalk_ir::TyKind::Array(ty, size) => TyKind::Array(from_chalk(db, ty), size),
115105
chalk_ir::TyKind::Placeholder(idx) => TyKind::Placeholder(idx),
116106
chalk_ir::TyKind::Alias(chalk_ir::AliasTy::Projection(proj)) => {
117-
let associated_ty = proj.associated_ty_id;
118-
let parameters = from_chalk(db, proj.substitution);
119-
TyKind::Alias(AliasTy::Projection(ProjectionTy {
120-
associated_ty_id: associated_ty,
121-
substitution: parameters,
122-
}))
107+
TyKind::Alias(AliasTy::Projection(from_chalk(db, proj)))
123108
}
124109
chalk_ir::TyKind::Alias(chalk_ir::AliasTy::Opaque(opaque_ty)) => {
125-
let opaque_ty_id = opaque_ty.opaque_ty_id;
126-
let parameters = from_chalk(db, opaque_ty.substitution);
127-
TyKind::Alias(AliasTy::Opaque(OpaqueTy { opaque_ty_id, substitution: parameters }))
110+
TyKind::Alias(AliasTy::Opaque(from_chalk(db, opaque_ty)))
128111
}
129112
chalk_ir::TyKind::Function(chalk_ir::FnPointer {
130113
num_binders,
@@ -138,18 +121,19 @@ impl ToChalk for Ty {
138121
}
139122
chalk_ir::TyKind::BoundVar(idx) => TyKind::BoundVar(idx),
140123
chalk_ir::TyKind::InferenceVar(_iv, _kind) => TyKind::Error,
141-
chalk_ir::TyKind::Dyn(where_clauses) => {
142-
assert_eq!(where_clauses.bounds.binders.len(&Interner), 1);
143-
let bounds = where_clauses
144-
.bounds
145-
.skip_binders()
146-
.iter(&Interner)
147-
.map(|c| from_chalk(db, c.clone()));
124+
chalk_ir::TyKind::Dyn(dyn_ty) => {
125+
assert_eq!(dyn_ty.bounds.binders.len(&Interner), 1);
126+
let (bounds, binders) = dyn_ty.bounds.into_value_and_skipped_binders();
127+
let where_clauses = crate::QuantifiedWhereClauses::from_iter(
128+
&Interner,
129+
bounds.interned().iter().cloned().map(|p| from_chalk(db, p)),
130+
);
148131
TyKind::Dyn(crate::DynTy {
149-
bounds: crate::Binders::new(
150-
where_clauses.bounds.binders.clone(),
151-
crate::QuantifiedWhereClauses::from_iter(&Interner, bounds),
152-
),
132+
bounds: crate::Binders::new(binders, where_clauses),
133+
// HACK: we sometimes get lifetime variables back in solutions
134+
// from Chalk, and don't have the infrastructure to substitute
135+
// them yet. So for now we just turn them into 'static right
136+
// when we get them
153137
lifetime: static_lifetime(),
154138
})
155139
}
@@ -169,8 +153,12 @@ impl ToChalk for Ty {
169153
}
170154
chalk_ir::TyKind::Raw(mutability, ty) => TyKind::Raw(mutability, from_chalk(db, ty)),
171155
chalk_ir::TyKind::Slice(ty) => TyKind::Slice(from_chalk(db, ty)),
172-
chalk_ir::TyKind::Ref(mutability, lifetime, ty) => {
173-
TyKind::Ref(mutability, lifetime, from_chalk(db, ty))
156+
chalk_ir::TyKind::Ref(mutability, _lifetime, ty) => {
157+
// HACK: we sometimes get lifetime variables back in solutions
158+
// from Chalk, and don't have the infrastructure to substitute
159+
// them yet. So for now we just turn them into 'static right
160+
// when we get them
161+
TyKind::Ref(mutability, static_lifetime(), from_chalk(db, ty))
174162
}
175163
chalk_ir::TyKind::Str => TyKind::Str,
176164
chalk_ir::TyKind::Never => TyKind::Never,
@@ -189,26 +177,6 @@ impl ToChalk for Ty {
189177
}
190178
}
191179

192-
/// We currently don't model lifetimes, but Chalk does. So, we have to insert a
193-
/// fake lifetime here, because Chalks built-in logic may expect it to be there.
194-
fn ref_to_chalk(
195-
db: &dyn HirDatabase,
196-
mutability: chalk_ir::Mutability,
197-
_lifetime: Lifetime,
198-
ty: Ty,
199-
) -> chalk_ir::Ty<Interner> {
200-
let arg = ty.to_chalk(db);
201-
let lifetime = static_lifetime();
202-
chalk_ir::TyKind::Ref(mutability, lifetime, arg).intern(&Interner)
203-
}
204-
205-
/// We currently don't model constants, but Chalk does. So, we have to insert a
206-
/// fake constant here, because Chalks built-in logic may expect it to be there.
207-
fn array_to_chalk(db: &dyn HirDatabase, ty: Ty, _: Const) -> chalk_ir::Ty<Interner> {
208-
let arg = ty.to_chalk(db);
209-
chalk_ir::TyKind::Array(arg, dummy_usize_const()).intern(&Interner)
210-
}
211-
212180
impl ToChalk for GenericArg {
213181
type Chalk = chalk_ir::GenericArg<Interner>;
214182

0 commit comments

Comments
 (0)