From 1383657a46a2b4052ceaf066942145b3799d8677 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Sat, 10 Feb 2024 14:58:37 +0100 Subject: [PATCH] add note on comparing vtables / function pointers --- library/core/src/primitive_docs.rs | 5 +++++ library/core/src/ptr/metadata.rs | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/library/core/src/primitive_docs.rs b/library/core/src/primitive_docs.rs index bf47d767a92..807a84d09f2 100644 --- a/library/core/src/primitive_docs.rs +++ b/library/core/src/primitive_docs.rs @@ -1676,6 +1676,11 @@ mod prim_ref {} /// * [`UnwindSafe`] /// * [`RefUnwindSafe`] /// +/// Note that while this type implements `PartialEq`, comparing function pointers is unreliable: +/// pointers to the same function can compare inequal (because functions are duplicated in multiple +/// codegen units), and pointers to *different* functions can compare equal (since identical +/// functions can be deduplicated within a codegen unit). +/// /// [`Hash`]: hash::Hash /// [`Pointer`]: fmt::Pointer /// [`UnwindSafe`]: panic::UnwindSafe diff --git a/library/core/src/ptr/metadata.rs b/library/core/src/ptr/metadata.rs index a6a390db043..c7b0fe5694a 100644 --- a/library/core/src/ptr/metadata.rs +++ b/library/core/src/ptr/metadata.rs @@ -175,6 +175,11 @@ impl Clone for PtrComponents { /// /// It is possible to name this struct with a type parameter that is not a `dyn` trait object /// (for example `DynMetadata`) but not to obtain a meaningful value of that struct. +/// +/// Note that while this type implements `PartialEq`, comparing vtable pointers is unreliable: +/// pointers to vtables of the same type for the same trait can compare inequal (because vtables are +/// duplicated in multiple codegen units), and pointers to vtables of *different* types/traits can +/// compare equal (since identical vtables can be deduplicated within a codegen unit). #[lang = "dyn_metadata"] pub struct DynMetadata { vtable_ptr: &'static VTable,