Skip to content

Misleading documentation for derived Ord/PartialOrd implementation for enums #75620

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
robinkrahl opened this issue Aug 17, 2020 · 0 comments · Fixed by #75826
Closed

Misleading documentation for derived Ord/PartialOrd implementation for enums #75620

robinkrahl opened this issue Aug 17, 2020 · 0 comments · Fixed by #75826
Labels
A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools C-bug Category: This is a bug. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.

Comments

@robinkrahl
Copy link
Contributor

The documentation for the Ord and PartialOrd traits says:

When derived on enums, variants are ordered by their top-to-bottom declaration order.

But apparently, the derived implementation actually orders the variants by their discriminant. The discriminants do not necessarily match the declaration order.

I tried this code (gist, Rust Playground):

#[derive(Debug, Eq, Ord, PartialEq, PartialOrd)]
enum A {
    V1,
    V2,
}

#[derive(Debug, Eq, Ord, PartialEq, PartialOrd)]
#[repr(u8)]
enum B {
    V1 = 1,
    V2 = 0,
}

fn main() {
    let mut a = vec![A::V1, A::V2];
    let mut b = vec![B::V1, B::V2];
    a.sort();
    b.sort();
    println!("a = {:?}", a);
    println!("b = {:?}", b);
}

Both enums have the same declaration order, so the output should be:

a = [V1, V2]
b = [V1, V2]

Instead, this happened:

a = [V1, V2]
b = [V2, V1]

Meta

$ rustc --version --verbose
rustc 1.45.2 (d3fb005a3 2020-07-31)
binary: rustc
commit-hash: d3fb005a39e62501b8b0b356166e515ae24e2e54
commit-date: 2020-07-31
host: x86_64-unknown-linux-gnu
release: 1.45.2
LLVM version: 10.0

Reproduced with both the stable and the nightly compiler on the Rust Playground

@robinkrahl robinkrahl added the C-bug Category: This is a bug. label Aug 17, 2020
@JohnTitor JohnTitor added E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools labels Aug 19, 2020
ayushmishra2005 pushed a commit to ayushmishra2005/rust that referenced this issue Aug 23, 2020
JohnTitor added a commit to JohnTitor/rust that referenced this issue Aug 24, 2020
…ation_for_derived_Ord_PartialOrd, r=KodrAus

Corrected Misleading documentation for derived Ord/PartialOrd implementation

Corrected Misleading documentation for derived Ord/PartialOrd implementation

Fixes rust-lang#75620
@bors bors closed this as completed in 427e969 Aug 24, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools C-bug Category: This is a bug. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants