Compiler fails to infer generic parameter passed as an argument

Viewed 92

I am having an issue with type inference when calling a generic function from another generic function. Normally I could resolve this via turbofish, but the function I'm calling has an impl Trait argument so this isn't possible:

trait B: Ord {
    type A;
}

type Map<Foo> = std::collections::BTreeMap<u8, <Foo as B>::A>;

fn f1<Foo: B>(a: &Map<Foo>, b: impl core::fmt::Display) -> Map<Foo> {
    unimplemented!()
}

fn f2<Foo: B>(a: &Map<Foo>) {
    let _ = f1(a, "test");
}

fn main() {}

This gives the error:

error[E0282]: type annotations needed
  --> src/main.rs:12:13
   |
12 |     let _ = f1(a, "test");
   |             ^^ cannot infer type for type parameter `Foo` declared on the function `f1`

Why can't the compiler infer the type here? I am aware this can be fixed by making b a standard generic and using a turbofish, but this change would cause a lot of headache in the codebase.

0 Answers
Related