Inference worse in method signature using associated type than with new generic type parameter eq-constrained to the associated type

0e4b2e7
Opened by scottmcm at 2022-12-08 07:43:03

With this signature

    fn try_fold<F, T: Try>(self, init: T::Ok, mut f: F) -> T where
        Self: Sized, F: FnMut(T::Ok, Self::Item) -> T

This code

    let a = [1, 2, 3];
    let sum = a.iter().try_fold(0i8, |acc, &x| acc.checked_add(x));

Gets error[E0619]: the type of this value must be known in this context.

But it works fine with the seemingly-equivalent signature

    fn try_fold<F, U, T: Try<Ok=U>>(self, init: U, mut f: F) -> T where
        Self: Sized, F: FnMut(U, Self::Item) -> T

Since acc and init must have the same type in both cases, shouldn't both signatures work?

Full repro: https://play.rust-lang.org/?gist=f8d2baeae64b17344e1cf27673a2e905&version=nightly

  1. To me this looks like a bug or limitation in our current inference implementation. The two signatures should be equivalent.

    Simon Sapin at 2018-03-17 16:09:55