about summary refs log tree commit diff
path: root/compiler/rustc_codegen_llvm/src/errors.rs
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2024-01-22 07:56:41 +0100
committerGitHub <noreply@github.com>2024-01-22 07:56:41 +0100
commit6f7222c1e6c459cf1474d99c306df400bbf97d56 (patch)
tree46b15a3cc66657d9123ac8ca46886c476aee32c4 /compiler/rustc_codegen_llvm/src/errors.rs
parenta58ec8ff03b3269b20104eb7eae407be48ab95a7 (diff)
parent5ec0a21ebeba234d0fcb3d8b5c4d004eeb954998 (diff)
downloadrust-6f7222c1e6c459cf1474d99c306df400bbf97d56.tar.gz
rust-6f7222c1e6c459cf1474d99c306df400bbf97d56.zip
Rollup merge of #118714 - The-Ludwig:explain_ord_derive_enum_field, r=Nilstrieb
 Explanation that fields are being used when deriving `(Partial)Ord` on enums

When deriving `std::cmp::Ord` or `std::cmp::PartialOrd` on enums, their fields are compared if the variants are equal.
This means that the last assertion in the following snipped panics.
```rust
use std::cmp::{PartialEq, Eq, PartialOrd, Ord};

#[derive(PartialEq, Eq, PartialOrd, Ord)]
enum Sizes {
    Small(usize),
    Big(usize),
}

fn main() {
    let a = Sizes::Big(3);
    let b = Sizes::Big(5);
    let c = Sizes::Small(10);
    assert!( c < a);
    assert_eq!(a, c);
}
```

This is more often expected behavior than not, and can be easily circumvented, as discussed in [this thread](https://users.rust-lang.org/t/how-to-sort-enum-variants/52291/4).
But it is addressed nowhere in the documentation, yet.
So I stumbled across this, as I personally did not expect fields being used in `PartialOrd`.
I added the explanation to the documentation.
Diffstat (limited to 'compiler/rustc_codegen_llvm/src/errors.rs')
0 files changed, 0 insertions, 0 deletions