Why does the call of a generic function is affected by the caller?

trait B<T> {}
impl<T> B<T> for T {}

fn f<K>()
where
    u32: B<K>
{
    get(&1u32); // #1 error
}

fn get<Q>(k: &Q)
where
    u32: B<Q>,
{
}

fn main() {}

In this example, the compiler reports that #1 is an error. The diagnosis says:

= note: expected reference &K
found reference &u32

However, when calling get(&1u32), the type parameter Q of get can be inferred to u32 and the trait bound u32:B<Q> can be satisfied due to the blanket implementations. If I had called get::<K>(&1u32) at #1, I would understand the error.

I wonder how the type parameter of the caller associates with the callee function. I didn't find any clue in the Rust Reference about this point.

where clauses are used to guide inference. Sometimes they're preferred. There's some description in this issue. There might be a more detailed description in the compiler dev guide or such.

You can turbofish it or add another bound.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.