Why does Rust fail to pick the right trait in this case?

Viewed 116

Consider the following code:

use std::ops::Add;

trait Trait
    where Self::Assoc: Add<Self::Assoc, Output=Self::Assoc>
                     + for <'a> Add<&'a Self::Assoc, Output=Self::Assoc>
{
    type Assoc;
    fn get(&self) -> Self::Assoc;
}

fn add1<T: Trait>(x: T, y: T) -> T::Assoc {
    x.get() + y.get()
}

This fails to compile with:

error[E0308]: mismatched types
  --> src/lib.rs:12:15
   |
12 |     x.get() + y.get()
   |               ^^^^^^^
   |               |
   |               expected reference, found associated type
   |               help: consider borrowing here: `&y.get()`
   |
   = note:    expected reference `&<T as Trait>::Assoc`
           found associated type `<T as Trait>::Assoc`

I'm able to work around the issue by explicitly specifying what trait I want to use:

fn add2<T: Trait>(x: T, y: T) -> T::Assoc {
    <T::Assoc as Add<T::Assoc>>::add(x.get(), y.get())
}

But I'm wondering: why is this happening? Also, is there any more compact work around?

Note that the order of the trait bounds matters. If I change them to:

    where Self::Assoc: for <'a> Add<&'a Self::Assoc, Output=Self::Assoc>
                     + Add<Self::Assoc, Output=Self::Assoc>

... then add1 compiles fine, but this fails:

fn add3<T: Trait>(x: T, y: T) -> T::Assoc {
    x.get() + &y.get()
}

Link to playground

0 Answers
Related