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 ?
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
Summary
E. g.
&strshould be compared lexicographically,&u32by dereferencing, etc. Trait objects (&dyn T) should not be comparable at all unlessPartialEqorPartialOrd(depending on which comparison operator is used) is implemented fordyn T.Reproducer
I tried this code:
Does the code make use of any (1.49) nightly feature ?
Godbolt link
https://godbolt.org/z/qqvhTarz5
Actual behavior
The current behaviour is that comparisons on references are miscompiled.
lt_refcompiles to acmp rdi, rsi, which compares the numerical pointer values ofxandy.eq_stris compiled to a componentwise comparison of the two components in the&strfat pointer: the pointer part and the length metadata.eq_trait_objectis compiled to a comparison of only the pointer part of the fat pointer, the vtable metadata is ignored.mainreturns1becausexhas a smaller address thany.Expected behavior
I expected to see that the code is compiled correctly (or not at all in the case of
eq_trait_objectbecause of the missingPartialEqimpl).GCC Version
679aad3