Skip to content

Error in derived PartialOrd for structs based on types with incomparable elements.  #49650

Closed
@0ndorio

Description

@0ndorio

If a type deriving PartialOrd wraps another type which contains incomparable elements - and therefore only defines an implementation of PartialOrd and not a Ord - the derived implementations of the le and ge functions are flawed.

The leads to a state where the internal type returns false as result of the expressions a <= b and b >= a for two incomparable elements a and b while the wrapper returns true. [playground]

If we look into the attached output of cargo-expand it becomes clear that both implementations ignore the the underlying type only implements PartialOrd and may contain incomparable elements which can't be checked by (a < b) || !(b < a) or (a > b) || !(a > b).

Expanded: Less or Equal

    #[inline]
    fn le(&self, __arg_0: &StructWithoutOrd) -> bool {
        match *__arg_0 {
            StructWithoutOrd(ref __self_1_0) => match *self {
                StructWithoutOrd(ref __self_0_0) => {
                    (*__self_0_0) < (*__self_1_0) || !((*__self_1_0) < (*__self_0_0)) && true
                }   
            },  
        }   
    }

Expanded: Greater or Equal

    #[inline]
    fn ge(&self, __arg_0: &StructWithoutOrd) -> bool {
        match *__arg_0 {
            StructWithoutOrd(ref __self_1_0) => match *self {
                StructWithoutOrd(ref __self_0_0) => {
                    (*__self_0_0) > (*__self_1_0) || !((*__self_1_0) > (*__self_0_0)) && true
                }   
            },  
        }   
    }

Metadata

Metadata

Assignees

Labels

C-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