Skip to content

rustc suggests adding a PartialEq bound where another trait is actually required #93927

Closed
@nsunderland1

Description

@nsunderland1

Given the following code (EDIT: see the comment below for a more concise snippet):

struct MyType<T: PartialEq>(T);

impl<T> PartialEq for MyType<T>
where
    T: PartialEq + Eq,
{
    fn eq(&self, other: &Self) -> bool {
        true
    }
}

struct MyStruct<T: PartialEq> {
    a: MyType<T>,
    b: MyType<T>,
}

impl<T> MyStruct<T>
where
    T: PartialEq,
{
    fn cond(&self) -> bool {
        self.b != self.a
    }
}

fn main() {
    let tst = MyStruct {
        a: MyType(1),
        b: MyType(2),
    };
    tst.cond();
}

https://play.rust-lang.org/?version=stable&mode=release&edition=2021&gist=a69190abd7a2b258c588d8e994e69871

The current output is:

error[[E0369]](https://doc.rust-lang.org/stable/error-index.html#E0369): binary operation `!=` cannot be applied to type `MyType<T>`
  [--> src/main.rs:22:16
](https://play.rust-lang.org/?version=stable&mode=release&edition=2021&gist=1dacb528973e6547b8284f3039fadbb1#)   |
22 |         self.b != self.a
   |         ------ ^^ ------ MyType<T>
   |         |
   |         MyType<T>
   |
help: consider further restricting this bound
   |
19 |     T: PartialEq + std::cmp::PartialEq,
   |                  +++++++++++++++++++++

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): binary operation `!=` cannot be applied to type `MyType<T>`
  [--> src/main.rs:22:16
](https://play.rust-lang.org/?version=stable&mode=release&edition=2021&gist=1dacb528973e6547b8284f3039fadbb1#)   |
22 |         self.b != self.a
   |         ------ ^^ ------ MyType<T>
   |         |
   |         MyType<T>
   |
help: consider further restricting this bound
   |
19 |     T: PartialEq + std::cmp::Eq,
   |                  ++++++++++++++

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

!= can't be used here because MyType<T> only implements PartialEq where T implements Eq, but instead of suggesting we add an Eq bound, rustc suggests adding an additional PartialEq bound, despite T already being PartialEq.

This bug is present on both stable and nightly.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsD-incorrectDiagnostics: A diagnostic that is giving misleading or incorrect information.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