@@ -2536,47 +2536,46 @@ pub fn addr_eq<T: PointeeSized, U: PointeeSized>(p: *const T, q: *const U) -> bo
25362536/// This is the same as `f == g`, but using this function makes clear that the potentially
25372537/// surprising semantics of function pointer comparison are involved.
25382538///
2539- /// There are **very few guarantees** about how functions are compiled and they have no intrinsic
2539+ /// There are **basically no guarantees** about how functions are compiled as they have no intrinsic
25402540/// “identity”; in particular, this comparison:
25412541///
2542- /// * May return `true` unexpectedly, in cases where functions are equivalent.
2542+ /// * May return `true` unexpectedly, even in cases where functions are not logically equivalent,
2543+ /// as long as they compile to the same machine code.
25432544///
25442545/// For example, the following program is likely (but not guaranteed) to print `(true, true)`
25452546/// when compiled with optimization:
25462547///
25472548/// ```
2549+ /// use std::hint::assert_unchecked;
2550+ ///
25482551/// let f: fn(i32) -> i32 = |x| x;
2549- /// let g: fn(i32) -> i32 = |x| x + 0; // different closure, different body
2550- /// let h: fn(u32) -> u32 = |x| x + 0; // different signature too
2551- /// dbg!(std::ptr::fn_addr_eq(f, g), std::ptr::fn_addr_eq(f, h)); // not guaranteed to be equal
2552+ /// let g: fn(u32) -> u32 = |x| x + 0; // different type, different body
2553+ /// let h: unsafe fn(i32) -> i32 = |x| unsafe { assert_unchecked(x != 0); x }; // different unsafe preconditions
2554+ /// // These three functions are allowed but not guaranteed to compare equal.
2555+ /// dbg!(std::ptr::fn_addr_eq(f, g), std::ptr::fn_addr_eq(f, h));
25522556/// ```
25532557///
2558+ /// In other words, functions that compare equal can have different behavior! Even if
2559+ /// `fn_addr_eq(f, h)` is true, it is still undefined behavior to call `h(0)`, while `f(0)` is
2560+ /// safe to call.
2561+ ///
2562+ /// The explanation for this is that function pointers have [provenance][crate::ptr#provenance].
2563+ /// If functions have the same assembly and get merged, their provenance decides which variant of
2564+ /// the function will be called. `f` and `h` therefore end up with the same address but different
2565+ /// provenance, and `fn_addr_eq` only compares the address.
2566+ ///
25542567/// * May return `false` in any case.
25552568///
25562569/// This is particularly likely with generic functions but may happen with any function.
25572570/// (From an implementation perspective, this is possible because functions may sometimes be
25582571/// processed more than once by the compiler, resulting in duplicate machine code.)
25592572///
2560- /// Despite these false positives and false negatives, this comparison can still be useful.
2561- /// Specifically, if
2562- ///
2563- /// * `T` is the same type as `U`, `T` is a [subtype] of `U`, or `U` is a [subtype] of `T`, and
2564- /// * `ptr::fn_addr_eq(f, g)` returns true,
2565- ///
2566- /// then calling `f` and calling `g` will be equivalent.
2567- ///
2568- ///
2569- /// # Examples
2570- ///
2571- /// ```
2572- /// use std::ptr;
2573- ///
2574- /// fn a() { println!("a"); }
2575- /// fn b() { println!("b"); }
2576- /// assert!(!ptr::fn_addr_eq(a as fn(), b as fn()));
2577- /// ```
2578- ///
2579- /// [subtype]: https://doc.rust-lang.org/reference/subtyping.html
2573+ /// These limitations imply that comparing function pointers (via this function or via `==`) is only
2574+ /// useful in extremely niche circumstances. For instance, if a library maintains a set of
2575+ /// callbacks, and doesn't guarantee which of them are actually called (i.e., callbacks can be
2576+ /// spuriously skipped), then it can be legitimate to deduplicate callbacks based on `fn_addr_eq`.
2577+ /// This can never introduce undefined behavior because it only skips executing some of the
2578+ /// registered callbacks.
25802579#[ stable( feature = "ptr_fn_addr_eq" , since = "1.85.0" ) ]
25812580#[ inline( always) ]
25822581#[ must_use = "function pointer comparison produces a value" ]
0 commit comments