Skip to content

Suggested bounds restriction suggests duplicate traitΒ #107335

Closed
@edward-shen

Description

@edward-shen

Code

pub trait TryAdd<Rhs = Self> {
    type Error;
    type Output;

    fn try_add(self, rhs: Rhs) -> Result<Self::Output, Self::Error>;
}

impl<T: TryAdd> TryAdd for Option<T> {
    type Error = <T as TryAdd>::Error;
    type Output = Option<<T as TryAdd>::Output>;

    fn try_add(self, rhs: Self) -> Result<Self::Output, Self::Error> {
        Ok(self.or(rhs))
    }
}

Current output

error[E0308]: mismatched types
  --> src/lib.rs:14:12
   |
9  | impl<T: TryAdd> TryAdd for Option<T> {
   |      - this type parameter
...
14 |         Ok(self.or(rhs))
   |         -- ^^^^^^^^^^^^ expected associated type, found type parameter `T`
   |         |
   |         arguments to this enum variant are incorrect
   |
   = note: expected enum `Option<<T as TryAdd>::Output>`
              found enum `Option<T>`
help: the type constructed contains `Option<T>` due to the type of the argument passed
  --> src/lib.rs:14:9
   |
14 |         Ok(self.or(rhs))
   |         ^^^------------^
   |            |
   |            this argument influences the type of `Ok`
note: tuple variant defined here
  --> /rustc/c18a5e8a5b1afb0d7a582fe9ebad4c1996c90da3/library/core/src/result.rs:507:5
help: consider further restricting this bound
   |
9  | impl<T: TryAdd + TryAdd<Output = T>> TryAdd for Option<T> {
   |                ++++++++++++++++++++

For more information about this error, try `rustc --explain E0308`.
error: could not compile `playground` due to previous error

Desired output

error[E0308]: mismatched types
  --> src/lib.rs:14:12
   |
9  | impl<T: TryAdd> TryAdd for Option<T> {
   |      - this type parameter
...
14 |         Ok(self.or(rhs))
   |         -- ^^^^^^^^^^^^ expected associated type, found type parameter `T`
   |         |
   |         arguments to this enum variant are incorrect
   |
   = note: expected enum `Option<<T as TryAdd>::Output>`
              found enum `Option<T>`
help: the type constructed contains `Option<T>` due to the type of the argument passed
  --> src/lib.rs:14:9
   |
14 |         Ok(self.or(rhs))
   |         ^^^------------^
   |            |
   |            this argument influences the type of `Ok`
note: tuple variant defined here
  --> /rustc/c18a5e8a5b1afb0d7a582fe9ebad4c1996c90da3/library/core/src/result.rs:507:5
help: consider further restricting this bound
   |
9  | impl<T: TryAdd<Output = T>> TryAdd for Option<T> {
   |         ++++++++++++++++++

For more information about this error, try `rustc --explain E0308`.
error: could not compile `playground` due to previous error

Rationale and extra context

While the diagnostic produces valid code, I think this diagnostic could be improved. I don't think there's ever a valid scenario where a human would write MyTrait + MyTrait<Output = T> over MyTrait<Output = T>.

Other cases

No response

Anything else?

Reproduces on playground.

Tested on stable 1.66.1 and nightly 1.69.0-nightly (2023-01-25 c18a5e8)

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions