Skip to content

required trait implementation for cmp is incorrect #108428

Closed
@gftea

Description

I tried this code:

    #[derive(Eq)]
    struct Person {
        id: u32,
        name: String,
        height: u32,
    }

    impl PartialOrd for Person {
        fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
            Some(self.cmp(other))
        }
    }

     // I intentionaly comment out this code
    // impl Ord for Person {
    //     fn cmp(&self, other: &Self) -> Ordering {
    //         self.height.cmp(&other.height)
    //     }
    // }

    impl PartialEq for Person {
        fn eq(&self, other: &Self) -> bool {
            self.height == other.height
        }
    }

I expected to see this happen:

indicate missing implementation for trait Ord

Instead, this happened:

it indicates missing trait implementation of iterator.

rror[E0599]: the method `cmp` exists for reference `&Person`, but its trait bounds were not satisfied
  --> examples/cmp_eq_ord.rs:39:23
   |
31 |     struct Person {
   |     -------------
   |     |
   |     doesn't satisfy `Person: Iterator`
   |     doesn't satisfy `Person: Ord`
...
39 |             Some(self.cmp(other))
   |                       ^^^ method cannot be called on `&Person` due to unsatisfied trait bounds
   |
   = note: the following trait bounds were not satisfied:
           `Person: Ord`
           which is required by `&Person: Ord`
           `&Person: Iterator`
           which is required by `&mut &Person: Iterator`
           `Person: Iterator`
           which is required by `&mut Person: Iterator`
note: the following trait must be implemented
  --> /home/gftea/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/iter/traits/iterator.rs:67:1
   |
67 | pub trait Iterator {
   | ^^^^^^^^^^^^^^^^^^
help: consider annotating `Person` with `#[derive(Eq, Ord, PartialEq, PartialOrd)]`
   |
31 |     #[derive(Eq, Ord, PartialEq, PartialOrd)]
   |

Meta

rustc --version --verbose:

rustc 1.67.0 (fc594f156 2023-01-24)
binary: rustc
commit-hash: fc594f15669680fa70d255faec3ca3fb507c3405
commit-date: 2023-01-24
host: x86_64-unknown-linux-gnu
release: 1.67.0
LLVM version: 15.0.6
Backtrace

<backtrace>

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-bugCategory: This is a bug.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