#Shared reference pointer equality unexpectedly optimized to false
11 messages · Page 1 of 1 (latest)
?eval ```rust
pub struct Interned<'arena, T: ?Sized>(&'arena T);
impl<T: ?Sized> PartialEq for Interned<', T> {
fn eq(&self, other: &Self) -> bool {
core::ptr::eq(self, other)
}
}
impl<T: ?Sized> Eq for Interned<', T> {}
pub fn main() {
let s1: &'static str = "abcd";
let i1 = Interned(s1);
let i2 = Interned(s1);
assert_eq!(
core::ptr::from_ref(i1.0).addr(),
core::ptr::from_ref(i2.0).addr()
);
assert!(i1 == i2);
}```
thread 'main' (11) panicked at src/main.rs:20:5:
assertion failed: i1 == i2
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Even though the first assertion passes, the PartialEq impl is optimized to always return false, which is not what I would expect from reading https://doc.rust-lang.org/stable/core/ptr/fn.eq.html. Am I missing something super obvious?
?godbolt ```rust
pub struct Interned<'arena, T: ?Sized>(&'arena T);
impl<T: ?Sized> PartialEq for Interned<', T> {
fn eq(&self, other: &Self) -> bool {
core::ptr::eq(self, other)
}
}
impl<T: ?Sized> Eq for Interned<', T> {}
pub fn main() {
let s1: &'static str = "abcd";
let i1 = Interned(s1);
let i2 = Interned(s1);
assert_eq!(
core::ptr::from_ref(i1.0).addr(),
core::ptr::from_ref(i2.0).addr()
);
assert!(i1 == i2);
}```
main:
push rax
lea rdi, [rip + .Lanon.fad58de7366495db4650cfefac2fcd61.1]
lea rdx, [rip + .Lanon.fad58de7366495db4650cfefac2fcd61.2]
mov esi, 26
call qword ptr [rip + core::panicking::panic@GOTPCREL]
.Lanon.fad58de7366495db4650cfefac2fcd61.0:
.asciz "/app/example.rs"
.Lanon.fad58de7366495db4650cfefac2fcd61.1:
.ascii "assertion failed: i1 == i2"
.Lanon.fad58de7366495db4650cfefac2fcd61.2:
.quad .Lanon.fad58de7366495db4650cfefac2fcd61.0
.asciz "\017\000\000\000\000\000\000\000\024\000\000\000\005\000\000"
Shared reference pointer equality unexpectedly optimized to false
You're comparing the addresses of i1 and i2, aka the struct holding the reference to the string. You need to do
core::ptr::eq(self.0, other.0)