Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect suggestion to add bound for Add trait #93744

Closed
compiler-errors opened this issue Feb 7, 2022 · 2 comments
Closed

Incorrect suggestion to add bound for Add trait #93744

compiler-errors opened this issue Feb 7, 2022 · 2 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@compiler-errors
Copy link
Member

compiler-errors commented Feb 7, 2022

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

@compiler-errors compiler-errors added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Feb 7, 2022
@rustbot rustbot added A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. labels Feb 7, 2022
@compiler-errors
Copy link
Member Author

@rustbot claim

@compiler-errors
Copy link
Member Author

Actually I probably won't get around to fixing this. Someone can steal it :)

@rustbot release-assignment

willcrichton added a commit to willcrichton/rust that referenced this issue Apr 26, 2022
to fix incorrect suggestion for trait bounds involving binary operators.
Fixes rust-lang#93927, rust-lang#92347, rust-lang#93744.
bors added a commit to rust-lang-ci/rust that referenced this issue Apr 26, 2022
…-binops, r=estebank

Fix incorrect suggestion for trait bounds involving binary operators

This PR fixes rust-lang#93927, rust-lang#92347, rust-lang#93744 by replacing the bespoke trait-suggestion logic in `op.rs` with a more common code path.

The downside is that this fix causes some suggestions to not include an `Output=` type, reducing their usefulness.

Note that this causes one case in the `missing-bounds.rs` test to fail rustfix. So I would need to move that code into a separate non-fix test if this PR is otherwise acceptable.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants