Basically, we need to allow variables in the caller self type to unify with the
impl's declared self type. That requires some more contortions in the variable
handling. I'm looking forward to (hopefully) handling this in a cleaner way when
we switch to Chalk's types and unification code.
This commit is contained in:
Florian Diebold 2020-03-01 14:31:35 +01:00
parent 6db2da4993
commit 336a3c6121
4 changed files with 45 additions and 6 deletions

View file

@ -142,12 +142,21 @@ impl<T> Canonicalized<T> {
pub fn unify(ty1: &Canonical<Ty>, ty2: &Canonical<Ty>) -> Option<Substs> {
let mut table = InferenceTable::new();
let num_vars = ty1.num_vars.max(ty2.num_vars);
let vars =
Substs::builder(ty1.num_vars).fill(std::iter::repeat_with(|| table.new_type_var())).build();
let ty_with_vars = ty1.value.clone().subst_bound_vars(&vars);
if !table.unify(&ty_with_vars, &ty2.value) {
Substs::builder(num_vars).fill(std::iter::repeat_with(|| table.new_type_var())).build();
let ty1_with_vars = ty1.value.clone().subst_bound_vars(&vars);
let ty2_with_vars = ty2.value.clone().subst_bound_vars(&vars);
if !table.unify(&ty1_with_vars, &ty2_with_vars) {
return None;
}
// default any type vars that weren't unified back to their original bound vars
// (kind of hacky)
for (i, var) in vars.iter().enumerate() {
if &*table.resolve_ty_shallow(var) == var {
table.unify(var, &Ty::Bound(i as u32));
}
}
Some(
Substs::builder(ty1.num_vars)
.fill(vars.iter().map(|v| table.resolve_ty_completely(v.clone())))

View file

@ -355,6 +355,10 @@ impl Substs {
Substs(self.0[..std::cmp::min(self.0.len(), n)].into())
}
pub fn suffix(&self, n: usize) -> Substs {
Substs(self.0[self.0.len() - std::cmp::min(self.0.len(), n)..].into())
}
pub fn as_single(&self) -> &Ty {
if self.0.len() != 1 {
panic!("expected substs of len 1, got {:?}", self);

View file

@ -508,10 +508,17 @@ pub(crate) fn inherent_impl_substs(
impl_id: ImplId,
self_ty: &Canonical<Ty>,
) -> Option<Substs> {
let vars = Substs::build_for_def(db, impl_id).fill_with_bound_vars(0).build();
// we create a var for each type parameter of the impl; we need to keep in
// mind here that `self_ty` might have vars of its own
let vars =
Substs::build_for_def(db, impl_id).fill_with_bound_vars(self_ty.num_vars as u32).build();
let self_ty_with_vars = db.impl_self_ty(impl_id).subst(&vars);
let self_ty_with_vars = Canonical { num_vars: vars.len(), value: self_ty_with_vars };
super::infer::unify(&self_ty_with_vars, self_ty)
let self_ty_with_vars =
Canonical { num_vars: vars.len() + self_ty.num_vars, value: self_ty_with_vars };
let substs = super::infer::unify(&self_ty_with_vars, self_ty);
// we only want the substs for the vars we added, not the ones from self_ty
let result = substs.map(|s| s.suffix(vars.len()));
result
}
fn transform_receiver_ty(

View file

@ -1048,6 +1048,25 @@ where
assert_eq!(t, "{unknown}");
}
#[test]
fn method_resolution_3373() {
let t = type_at(
r#"
//- /main.rs
struct A<T>(T);
impl A<i32> {
fn from(v: i32) -> A<i32> { A(v) }
}
fn main() {
A::from(3)<|>;
}
"#,
);
assert_eq!(t, "A<i32>");
}
#[test]
fn method_resolution_slow() {
// this can get quite slow if we set the solver size limit too high