Trait bound doesn't imply itself?


#1

I have the following code:

trait Foo<'a> where Self: 'a + Sized, &'a Self: std::ops::Add<&'a Self, Output = Self> {}

fn foo<'a, T: Foo<'a>>(t: T) -> T { t }

When I try compiling this program, I get the following error:

error[E0277]: the trait bound `&'a T: std::ops::Add` is not satisfied
 --> src/main.rs:3:1
  |
3 | fn foo<'a, T: Foo<'a>>(t: T) -> T { t }
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ no implementation for `&'a T + &'a T`
  |
  = help: the trait `std::ops::Add` is not implemented for `&'a T`
  = help: consider adding a `where &'a T: std::ops::Add` bound
  = note: required by `Foo`

I’m surprised by this because I would expect the T: Foo<'a> bound to imply &'a T: Add given that it’s in the definition of Foo. What am I getting wrong here?


#2

I think this may get fixed with https://github.com/rust-lang/rfcs/blob/master/text/2089-implied-bounds.md. As it stands, you’re adding a bound on a different type - &Self - and that needs to be repeated today.


#3

OK thanks!


#4

This bug is probably relevant:


#5

Oh good call; thanks!