@@ -1733,6 +1733,12 @@ pub(crate) unsafe fn align_offset<T: Sized>(p: *const T, a: usize) -> usize {
17331733/// by their address rather than comparing the values they point to
17341734/// (which is what the `PartialEq for &T` implementation does).
17351735///
1736+ /// When comparing wide pointers, both the address and the metadata are tested for equality.
1737+ /// However, note that comparing trait object pointers (`*const dyn Trait`) is unrealiable: pointers
1738+ /// to values of the same underlying type can compare inequal (because vtables are duplicated in
1739+ /// multiple codegen units), and pointers to values of *different* underlying type can compare equal
1740+ /// (since identical vtables can be deduplicated within a codegen unit).
1741+ ///
17361742/// # Examples
17371743///
17381744/// ```
@@ -1759,41 +1765,6 @@ pub(crate) unsafe fn align_offset<T: Sized>(p: *const T, a: usize) -> usize {
17591765/// assert!(!std::ptr::eq(&a[..2], &a[..3]));
17601766/// assert!(!std::ptr::eq(&a[0..2], &a[1..3]));
17611767/// ```
1762- ///
1763- /// Traits are also compared by their implementation:
1764- ///
1765- /// ```
1766- /// #[repr(transparent)]
1767- /// struct Wrapper { member: i32 }
1768- ///
1769- /// trait Trait {}
1770- /// impl Trait for Wrapper {}
1771- /// impl Trait for i32 {}
1772- ///
1773- /// let wrapper = Wrapper { member: 10 };
1774- ///
1775- /// // Pointers have equal addresses.
1776- /// assert!(std::ptr::eq(
1777- /// &wrapper as *const Wrapper as *const u8,
1778- /// &wrapper.member as *const i32 as *const u8
1779- /// ));
1780- ///
1781- /// // Objects have equal addresses, but `Trait` has different implementations.
1782- /// assert!(!std::ptr::eq(
1783- /// &wrapper as &dyn Trait,
1784- /// &wrapper.member as &dyn Trait,
1785- /// ));
1786- /// assert!(!std::ptr::eq(
1787- /// &wrapper as &dyn Trait as *const dyn Trait,
1788- /// &wrapper.member as &dyn Trait as *const dyn Trait,
1789- /// ));
1790- ///
1791- /// // Converting the reference to a `*const u8` compares by address.
1792- /// assert!(std::ptr::eq(
1793- /// &wrapper as &dyn Trait as *const dyn Trait as *const u8,
1794- /// &wrapper.member as &dyn Trait as *const dyn Trait as *const u8,
1795- /// ));
1796- /// ```
17971768#[ stable( feature = "ptr_eq" , since = "1.17.0" ) ]
17981769#[ inline]
17991770pub fn eq < T : ?Sized > ( a : * const T , b : * const T ) -> bool {
0 commit comments