Skip to content

Illegal Eq derive in an enum with Option<f64> #103157

Closed
@stepantubanov

Description

@stepantubanov

I tried this code:

#[derive(PartialEq, Eq)]
pub enum Value {
    Boolean(Option<bool>),
    Float(Option<f64>),
}

fn main() {
    let a = Value::Float(Some(f64::NAN));
    
    // Eq should always have a == a
    // https://doc.rust-lang.org/std/cmp/trait.Eq.html
    assert!(a == a);
}

I expected to see this happen: code fails to compile.
Instead, this happened: code compiles, and assertion fails.

Changing order of enum variants to make Float go first fixes the issue:

// error[E0277] the trait bound `f64: Eq` is not satisfied
#[derive(PartialEq, Eq)]
                    -- in this derive macro expansion
pub enum Value {
    Float(Option<f64>),
    Boolean(Option<bool>),
}

Meta

rustc --version --verbose:

rustc 1.64.0 (a55dd71d5 2022-09-19)
binary: rustc
commit-hash: a55dd71d5fb0ec5a6a3a9e8c27b2127ba491ce52
commit-date: 2022-09-19
host: aarch64-apple-darwin
release: 1.64.0
LLVM version: 14.0.6

It's reproducible in Playground in Stable, Beta and Nightly.

Metadata

Metadata

Labels

C-bugCategory: This is a bug.P-highHigh priorityregression-from-stable-to-stablePerformance or correctness regression from one stable version to another.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions