Skip to content

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

Open
@scottmcm

Description

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

Metadata

Assignees

No one assigned

    Labels

    A-associated-itemsArea: Associated items (types, constants & functions)A-inferenceArea: Type inferenceC-enhancementCategory: An issue proposing an enhancement or a PR with one.T-typesRelevant to the types team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions