Skip to content

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

Closed
@5225225

Description

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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