You might be missing a type parameter or trait bound

There's no guarantee that, if b1 and b2 are of type B and B: Add, then b1 + b2 will also be of type B. Check documentation for the trait for more info.

For future reference, the solution is to change B: Add to B: Add<Output = B>.

I've opened a PR to make rustc suggest this going forward.

2 Likes

This is now in nightly:

error[E0308]: mismatched types
  --> src/main.rs:12:8
   |
5  | impl<B> Add for A<B>
   |      - this type parameter
...
12 |         Self(self.0 + rhs.0)
   |              ^^^^^^^^^^^^^^ expected type parameter `B`, found associated type
   |
   = note: expected type parameter `B`
             found associated type `<B as std::ops::Add>::Output`
help: consider further restricting this bound
   |
7  |     B: Add + std::ops::Add<Output = B>
   |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^
1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.