Skip to content

Commit e6f1dc2

Browse files
authored
Rollup merge of rust-lang#120880 - RalfJung:vtable-fnptr-partialeq, r=cuviper
add note on comparing vtables / function pointers Fixes rust-lang#99388 Fixes rust-lang#117047
2 parents 5d7b839 + 0b06390 commit e6f1dc2

File tree

3 files changed

+16
-0
lines changed

3 files changed

+16
-0
lines changed

core/src/primitive_docs.rs

+5
Original file line numberDiff line numberDiff line change
@@ -1660,6 +1660,11 @@ mod prim_ref {}
16601660
/// * [`UnwindSafe`]
16611661
/// * [`RefUnwindSafe`]
16621662
///
1663+
/// Note that while this type implements `PartialEq`, comparing function pointers is unreliable:
1664+
/// pointers to the same function can compare inequal (because functions are duplicated in multiple
1665+
/// codegen units), and pointers to *different* functions can compare equal (since identical
1666+
/// functions can be deduplicated within a codegen unit).
1667+
///
16631668
/// [`Hash`]: hash::Hash
16641669
/// [`Pointer`]: fmt::Pointer
16651670
/// [`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,

core/src/task/wake.rs

+6
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,12 @@ impl RawWaker {
9999
/// [`RawWaker`] implementation. Calling one of the contained functions using
100100
/// any other `data` pointer will cause undefined behavior.
101101
///
102+
/// Note that while this type implements `PartialEq`, comparing function pointers, and hence
103+
/// comparing structs like this that contain function pointers, is unreliable: pointers to the same
104+
/// function can compare inequal (because functions are duplicated in multiple codegen units), and
105+
/// pointers to *different* functions can compare equal (since identical functions can be
106+
/// deduplicated within a codegen unit).
107+
///
102108
/// # Thread safety
103109
/// If the [`RawWaker`] will be used to construct a [`Waker`] then
104110
/// these functions must all be thread-safe (even though [`RawWaker`] is

0 commit comments

Comments
 (0)