Skip to content

Suggestion to derive traits does not consider needing to implement supertraits #91550

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

Closed
5225225 opened this issue Dec 5, 2021 · 1 comment · Fixed by #93693
Closed

Suggestion to derive traits does not consider needing to implement supertraits #91550

5225225 opened this issue Dec 5, 2021 · 1 comment · Fixed by #93693
Assignees
Labels
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.

Comments

@5225225
Copy link
Contributor

5225225 commented Dec 5, 2021

Given the following code: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=8d2ebff94e06c67049c0ad3f096873ff

use std::collections::HashSet;

struct Value(u32);

fn main() {
    let hs = HashSet::<Value>::new();
    hs.insert(Value(0));
}

The current output is:

   Compiling playground v0.0.1 (/playground)
error[E0599]: the method `insert` exists for struct `HashSet<Value>`, but its trait bounds were not satisfied
 --> src/main.rs:7:8
  |
3 | struct Value(u32);
  | ------------------
  | |
  | doesn't satisfy `Value: Eq`
  | doesn't satisfy `Value: Hash`
...
7 |     hs.insert(Value(0));
  |        ^^^^^^ method cannot be called on `HashSet<Value>` due to unsatisfied trait bounds
  |
  = note: the following trait bounds were not satisfied:
          `Value: Eq`
          `Value: Hash`
help: consider annotating `Value` with `#[derive(Eq, Hash)]`
  |
3 | #[derive(Eq, Hash)]
  |

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

Ideally the output should look like:

   Compiling playground v0.0.1 (/playground)
error[E0599]: the method `insert` exists for struct `HashSet<Value>`, but its trait bounds were not satisfied
 --> src/main.rs:7:8
  |
3 | struct Value(u32);
  | ------------------
  | |
  | doesn't satisfy `Value: Eq`
  | doesn't satisfy `Value: Hash`
...
7 |     hs.insert(Value(0));
  |        ^^^^^^ method cannot be called on `HashSet<Value>` due to unsatisfied trait bounds
  |
  = note: the following trait bounds were not satisfied:
          `Value: Eq`
          `Value: Hash`
help: consider annotating `Value` with `#[derive(PartialEq, Eq, Hash)]`
  |
3 | #[derive(PartialEq, Eq, Hash)]
  |

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

This way, the suggested code will compile without the user needing to know that in order to derive Eq, you must implement PartialEq.

There's also an issue where you need the ::<Value> turbofish in the above code for the suggestion to show up, I'll make a separate issue for that.

@5225225 5225225 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 Dec 5, 2021
@rukai
Copy link
Contributor

rukai commented Dec 5, 2021

Oh, I touched this code recently.
It should be easy to hardcode this for PartialEq and PartialOrd.
here:

traits.push_str(format!(", {}", trait_name).as_str());

@rustbot claim

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Feb 16, 2022
Suggest deriving required supertraits

closes rust-lang#91550

I chose to just hardcode handling for PartialOrd and PartialEq because that should be robust enough and I dont know how to go about doing it generically

r? rust-lang/diagnostics
@bors bors closed this as completed in 351aa1b Feb 17, 2022
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 T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants