Skip to content

Incorrect suggestion to add bound for Add trait #93744

Closed
@compiler-errors

Description

@compiler-errors

Given the following code:

use core::ops::Add;

fn add<A, B, C>(a: A, b: B) -> C {
    a + b
}

The current output is:

error[[E0369]](https://doc.rust-lang.org/stable/error-index.html#E0369): cannot add `B` to `A`
 [--> src/lib.rs:4:7
](https://play.rust-lang.org/#)  |
4 |     a + b
  |     - ^ - B
  |     |
  |     A
  |
help: consider restricting type parameter `A`
  |
3 | fn add<A: std::ops::Add<Output = B>, B, C>(a: A, b: B) -> C {
  |         +++++++++++++++++++++++++++

For more information about this error, try `rustc --explain E0369`.

Ideally the output should look like:

error[[E0369]](https://doc.rust-lang.org/stable/error-index.html#E0369): cannot add `B` to `A`
 [--> src/lib.rs:4:7
](https://play.rust-lang.org/#)  |
4 |     a + b
  |     - ^ - B
  |     |
  |     A
  |
help: consider restricting type parameter `A`
  |
3 | fn add<A: std::ops::Add<B, Output = C>, B, C>(a: A, b: B) -> C {
  |         ++++++++++++++++++++++++++++++

For more information about this error, try `rustc --explain E0369`.

@rustbot label D-invalid-suggestion A-suggestion-diagnostics

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.T-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