assoc types: type inference works with UFCS but not with method calls

3eeab20
Opened by Jorge Aparicio at 2023-04-05 17:36:36

STR

#![crate_type = "lib"]

trait Tuple {
    type Array: Toa<Item=Self>;
}

trait Toa {
    type Item;

    fn push(&mut self, Self::Item);
    fn with_capacity(usize) -> Self;
}

#[cfg(not(works))]
fn f<T: Tuple>(t: T) -> T::Array {
    let mut toa = Toa::with_capacity(1);
    toa.push(t);  //~ error: the type of this value must be known in this context
    toa
}

#[cfg(works)]
fn g<T: Tuple>(t: T) -> T::Array {
    let mut toa = Toa::with_capacity(1);
    Toa::push(&mut toa, t);
    toa
}

Version

rustc 1.0.0-nightly (134e00be7 2015-02-09 19:01:37 +0000)

cc @nikomatsakis

(Deja Vu, I feel like I've reported/seen a similar problem before, but it didn't involve associated types)

  1. cc me

    Edward Wang at 2015-02-21 07:39:45

  2. I don't think this is really related to associated types. I feel like it's the same as the following test case:

    fn test<I: Iterator<Item=i32>>(i: I) -> Vec<i32> {
        let v = i.collect();
        v.push(1); //~ error: the type of this value must be known in this context
        v
    }
    

    i.e. calling a method on a value whose type can only be inferred "later".

    Geoffry Song at 2015-02-22 02:09:46

  3. Triage: after a small update, the original still reproduces:

    #![crate_type = "lib"]
    
    trait Tuple: Sized {
        type Array: Toa<Item=Self>;
    }
    
    trait Toa {
        type Item;
    
        fn push(&mut self, Self::Item);
        fn with_capacity(usize) -> Self;
    }
    
    #[cfg(not(works))]
    fn f<T: Tuple>(t: T) -> T::Array {
        let mut toa = Toa::with_capacity(1);
        toa.push(t);  //~ error: the type of this value must be known in this context
        toa
    }
    
    #[cfg(works)]
    fn g<T: Tuple>(t: T) -> T::Array {
        let mut toa = Toa::with_capacity(1);
        Toa::push(&mut toa, t);
        toa
    }
    
    

    Steve Klabnik at 2016-05-24 20:31:48

  4. Triage: still reproduces

    Steve Klabnik at 2018-09-24 18:46:53