Skip to content

Commit 2ed3ac0

Browse files
committed
add note on comparing vtables / function pointers
1 parent 69b5218 commit 2ed3ac0

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

core/src/primitive_docs.rs

+5
Original file line numberDiff line numberDiff line change
@@ -1676,6 +1676,11 @@ mod prim_ref {}
16761676
/// * [`UnwindSafe`]
16771677
/// * [`RefUnwindSafe`]
16781678
///
1679+
/// Note that while this type implements `PartialEq`, comparing function pointers is unreliable:
1680+
/// pointers to the same function can compare inequal (because functions are duplicated in multiple
1681+
/// codegen units), and pointers to *different* functions can compare equal (since identical
1682+
/// functions can be deduplicated within a codegen unit).
1683+
///
16791684
/// [`Hash`]: hash::Hash
16801685
/// [`Pointer`]: fmt::Pointer
16811686
/// [`UnwindSafe`]: panic::UnwindSafe

core/src/ptr/metadata.rs

+5
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,11 @@ impl<T: ?Sized> Clone for PtrComponents<T> {
175175
///
176176
/// It is possible to name this struct with a type parameter that is not a `dyn` trait object
177177
/// (for example `DynMetadata<u64>`) but not to obtain a meaningful value of that struct.
178+
///
179+
/// Note that while this type implements `PartialEq`, comparing vtable pointers is unreliable:
180+
/// pointers to vtables of the same type for the same trait can compare inequal (because vtables are
181+
/// duplicated in multiple codegen units), and pointers to vtables of *different* types/traits can
182+
/// compare equal (since identical vtables can be deduplicated within a codegen unit).
178183
#[lang = "dyn_metadata"]
179184
pub struct DynMetadata<Dyn: ?Sized> {
180185
vtable_ptr: &'static VTable,

0 commit comments

Comments
 (0)