Those 2 usizes combined are the “pointer” that’s being compared for equality by ptr_eq— You’ve described the memory layout of &dyn Trait; the vtable pointer lives alongside the data pointer in Rust, instead of beside the data allocation.
I think the problem is that, if a and b point to the same object there’s no guarantee that the vtable part of the pointer will be equal. If different crates separately cast the same underlying reference into a trait object reference, they will each use their own copy of the vtable.
Furthermore vtables for different types could have the same address after being merged together.
the lint also seems to say that. Does it mean if two different Types implement the same trait, they might somehow share the same vtable? sounds pretty confusing.
Comparing trait objects pointers compares an vtable addresses which are not guaranteed to be unique and could vary between different code generation units. Furthermore vtables for different types could have the same address after being merged together.
Okay, I think I completely misread this. The first part states:
Comparing trait objects pointers compares an vtable addresses which are not guaranteed to be unique
I incorrectly interpreted this as:
a = Rc::new(Cat::new()) as Rc<AnimalT>;
b = Rc::new(Cat::new()) as Rc<AnimalT>;
I incorrectly interpreted this as a and b not necessairly unique, i.e. it is possible that Rc::ptr_eq(a, b) returns true. Which makes no sense at all to me.
I think the problem you are pointing to is that for a given trait, when compiled in different crates, there might be multiple DIFFERENT vtables (i.e. vtables themselves not uniquje), i.e. the problem of:
let a = Rc::new(Cat::new())
let b: Rc<dyn AnimalT> = crate_xyz::get_animal(&a);
let c: Rc<dyn AnimalT> = crate_foo::get_animal(&a);
Now, we would expect that Rc::ptr_eq(b, c) to return true, but you are saying taht this might be false if there ar emultiple vtables created for AnimalT.
Yes, it's possible if they implement the trait the same way, are the same size and have the same drop behavior, the compiler could merge vtables. This is trivially the case for types that differ only in lifetime parameters, but it could happen with other types as well.
In summary, a pointer to a trait object is a struct of unspecified layout that contains two usize pointers:
a pointer to the object
a pointer to a not-necessarily-unique vtable for that trait, and possibly for other traits that have identical vtables, or where one trait's vtable is a layout-identical subset of another's.
As a result
Distinct traits in the same compilation unit can share a common vtable when all of the methods for one trait, including destructors, are methods for the other trait.
Trait objects that are created in different compilation units for a shared trait may reference different vtables whose contents and methods, as used by the trait, are functionally identical.
Therefore, within 2-usize trait object pointers, comparing the two internal usize object pointers for equality can determine whether they are referring to the same trait object,
but comparing the two internal usize vtable pointers for equality cannot determine whether they are referring to the same trait.
A 1-usize zero-offset field reference is not a 2-usize trait-object reference. Thus the types of the two objects are different, even if their addresses are the same. Even if the first field of the trait object is a recursive reference to another trait object of the identical trait, thus sharing the same vtable. there's a type-apparent operational difference in comparing the 1-usize field address to the 2-usize trait object pointer.
struct Inner(...);
#[repr(transparent)]
struct Outer(pub Inner);
impl Trait for Inner { ... }
impl Trait for Outer { ... }
fn foo(x: Outer) {
let dyn_inner: &dyn Trait = &x.0;
let dyn_outer: &dyn Trait = &x;
/* ... */
}
Here dyn_inner and dyn_outer are both of type &dyn Trait. The object pointer portion of each of these is the same, but the vtable portion will differ.
Though the two trait objects reside at the same address, they shouldn’t ever be considered the same object, and the different vtable pointers shouldn’t be considered interchangable.
We must compare both parts of the fat pointer to test for equality
If they’re both equal, the two pointers refer to the same object (Edit: maybe not, if the two trait implementations ended up with the same vtable. At that point, is a wrapper struct meaningfully different from the field it’s wrapping?)
If the object data address is different, the two pointers refer to different objects
If the object data address is equal but the vtable address is not equal, the equality test is inconclusive: they might be conceptually the same object and they might not.
I thought of the same thing, but I couldn't make the comparison actually return true. I think it is allowed to, though. Obviously this wouldn't be possible with Rc, since you can't nest Rcs like that, but with & I don't see any reason why the compiler wouldn't be allowed to make dyn_inner and dyn_outer value-wise equivalent.
I also can't think of any way to make dyn_inner and dyn_outer have different behavior, despite that the underlying types are different, so maybe it's purely an academic question. Incidentally, I don't think that the repr(transparent) is necessary here.
With an equal vtable pointer, I can’t think of a way to make them behave differently either. But the way I read @TomP’s original statement, it sounded like ignoring the vtable pointer and comparing the data pointer was sufficient to determine if the two trait objects are equivalent (which is important b/c a single trait impl may have multiple equivalent vtable pointers).
It’s relatively trivial to get multiple trait object references that behave differently if you allow the vtable portion to differ, though: (Playground)
So, if the vtable pointers are different, there’s no general test that can show object equivalence.
Aside, re: repr. With the current compiler it’s not necessary. Including it future-proofs the example against compiler changes, since memory layout is part if the unstable ABI.
// in another crate
use ::parent_crate::*;
pub fn fatten (it: &'_ Struct) -> &'_ dyn Trait { it }
fn main ()
{
assert_eq!(
::parent_crate::fatten(&STATIC) as *const dyn Trait,
crate::fatten(&STATIC) as *const dyn Trait,
);
}
Then the above assertion could fail: ::parent_crate::fatten will append a &'static TraitVTable to it when fattening the pointer, and so will crate::fatten, but nothing guarantees that these different compilation units end up using the same&'static TraitVTable for that, hence appending a distinct "second usize" each.
EDIT: I managed to trigger this on the playground, using constvs. non-const evaluation, yielding a hilarious error message: