Skip to content

Derived PartialOrd is incorrect when inner values are unordered #81373

Closed
@tmiasko

Description

@tmiasko

In the derived partial_cmp the comparisons stops when inner partial_cmp returns
None. The implementation for lt, le, gt, ge are also provided, but they
continue the comparisons instead as if compared values were considered equal.

This behaviour is also inconsistent with the implementation for tuples / slices:

#[derive(PartialEq, PartialOrd)]
struct P(f64, f64);

fn main() {
    let a = (f64::NAN, 0.0);
    let b = (f64::NAN, 1.0);

    // Tuples:
    assert_eq!(PartialOrd::partial_cmp(&a, &b), None);
    assert!(!(a < b));

    // Arrays:
    let a = [f64::NAN, 0.0];
    let b = [f64::NAN, 1.0];
    assert_eq!(PartialOrd::partial_cmp(&a, &b), None);
    assert!(!(a < b));

    // Derived:
    let a = P(f64::NAN, 0.0);
    let b = P(f64::NAN, 1.0);
    assert_eq!(PartialOrd::partial_cmp(&a, &b), None);
    assert!(!(a < b)); // fails
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)C-bugCategory: This is a bug.P-highHigh priority

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions