Skip to content

References are compared by pointer value, not according to their PartialEq/PartialOrd instances #4537

@narpfel

Description

@narpfel

Summary

E. g. &str should be compared lexicographically, &u32 by dereferencing, etc. Trait objects (&dyn T) should not be comparable at all unless PartialEq or PartialOrd (depending on which comparison operator is used) is implemented for dyn T.

Reproducer

I tried this code:

#![feature(no_core)]
#![no_core]

#[inline(never)]
pub fn lt_ref(x: &u32, y: &u32) -> bool {
    x < y
}

#[inline(never)]
pub fn eq_str(s: &str) -> bool {
    s == "hello world"
}

trait T {}

#[inline(never)]
pub fn eq_trait_object(a: &dyn T, b: &dyn T) -> bool {
    a == b
}

pub fn main() -> i32 {
    let x = &42;
    let y = &27;
    lt_ref(x, y) as i32
}

Does the code make use of any (1.49) nightly feature ?

  • Nightly

Godbolt link

https://godbolt.org/z/qqvhTarz5

Actual behavior

The current behaviour is that comparisons on references are miscompiled.

  • lt_ref compiles to a cmp rdi, rsi, which compares the numerical pointer values of x and y.
  • eq_str is compiled to a componentwise comparison of the two components in the &str fat pointer: the pointer part and the length metadata.
  • eq_trait_object is compiled to a comparison of only the pointer part of the fat pointer, the vtable metadata is ignored.
  • main returns 1 because x has a smaller address than y.

Expected behavior

I expected to see that the code is compiled correctly (or not at all in the case of eq_trait_object because of the missing PartialEq impl).

GCC Version

679aad3

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions