Normalize associated types during inference

This commit is contained in:
Florian Diebold 2019-08-07 22:06:09 +02:00
parent 22724f37f3
commit 6265497523
3 changed files with 25 additions and 9 deletions

View file

@ -245,7 +245,8 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
&self.resolver,
type_ref,
);
self.insert_type_vars(ty)
let ty = self.insert_type_vars(ty);
self.normalize_associated_types_in(ty)
}
fn unify_substs(&mut self, substs1: &Substs, substs2: &Substs, depth: usize) -> bool {
@ -411,6 +412,25 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
ty
}
fn normalize_associated_types_in(&mut self, ty: Ty) -> Ty {
ty.fold(&mut |ty| match ty {
Ty::Projection(proj_ty) => self.normalize_projection_ty(proj_ty),
Ty::UnselectedProjection(proj_ty) => {
// FIXME
Ty::UnselectedProjection(proj_ty)
}
_ => ty,
})
}
fn normalize_projection_ty(&mut self, proj_ty: ProjectionTy) -> Ty {
let var = self.new_type_var();
let predicate = ProjectionPredicate { projection_ty: proj_ty.clone(), ty: var.clone() };
let obligation = Obligation::Projection(predicate);
self.obligations.push(obligation);
var
}
/// Resolves the type completely; type variables without known type are
/// replaced by Ty::Unknown.
fn resolve_ty_completely(&mut self, tv_stack: &mut Vec<TypeVarId>, ty: Ty) -> Ty {

View file

@ -117,11 +117,6 @@ impl Ty {
return Ty::Unknown;
}
};
eprintln!(
"assoc ty: {:?}, parameters: {:?}",
associated_ty.name(db),
trait_ref.substs
);
// FIXME handle type parameters on the segment
Ty::Projection(ProjectionTy { associated_ty, parameters: trait_ref.substs })
} else {

View file

@ -402,11 +402,12 @@ where
&self,
projection: &'p chalk_ir::ProjectionTy,
) -> (Arc<AssociatedTyDatum>, &'p [Parameter], &'p [Parameter]) {
debug!("split_projection {:?}", projection);
unimplemented!()
let proj_ty: ProjectionTy = from_chalk(self.db, projection.clone());
debug!("split_projection {:?} = {}", projection, proj_ty.display(self.db));
// we don't support GATs, so I think this should always be correct currently
(self.db.associated_ty_data(projection.associated_ty_id), &projection.parameters, &[])
}
fn custom_clauses(&self) -> Vec<chalk_ir::ProgramClause> {
debug!("custom_clauses");
vec![]
}
fn all_structs(&self) -> Vec<chalk_ir::StructId> {