From 33189b4e5007cf3f10e613b406ea0fe37afcb6f9 Mon Sep 17 00:00:00 2001 From: Jules Bertholet Date: Mon, 6 Jul 2026 22:31:19 -0400 Subject: [PATCH] Clarify preconditions of raw size/align methods The current documentation doesn't guarantee that these methods are safe to call for pointers derived from references to new unsized types that Rust may add in the future. Co-authored-by: Crystal Durham --- library/core/src/alloc/layout.rs | 4 +++- library/core/src/mem/alignment.rs | 4 +++- library/core/src/mem/mod.rs | 8 ++++++-- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/library/core/src/alloc/layout.rs b/library/core/src/alloc/layout.rs index 66f5310db8310..9c0517c3ee2f8 100644 --- a/library/core/src/alloc/layout.rs +++ b/library/core/src/alloc/layout.rs @@ -226,7 +226,9 @@ impl Layout { /// /// # Safety /// - /// This function is only safe to call if the following conditions hold: + /// This function is safe to call if the pointer is safe to reborrow as `&T` + /// (in which case you could also call [`for_value`][Self::for_value]). + /// Otherwise, the following conditions must hold: /// /// - If `T` is `Sized`, this function is always safe to call. /// - If the unsized tail of `T` is: diff --git a/library/core/src/mem/alignment.rs b/library/core/src/mem/alignment.rs index 8f453685a6f92..4b0d7e96a38d1 100644 --- a/library/core/src/mem/alignment.rs +++ b/library/core/src/mem/alignment.rs @@ -114,7 +114,9 @@ impl Alignment { /// /// # Safety /// - /// This function is only safe to call if the following conditions hold: + /// This function is safe to call if the pointer is safe to reborrow as `&T` + /// (in which case you could also call [`of_val`][Self::of_val]). + /// Otherwise, the following conditions must hold: /// /// - If `T` is `Sized`, this function is always safe to call. /// - If the unsized tail of `T` is: diff --git a/library/core/src/mem/mod.rs b/library/core/src/mem/mod.rs index e24b220acb43b..9c453d9607b29 100644 --- a/library/core/src/mem/mod.rs +++ b/library/core/src/mem/mod.rs @@ -419,7 +419,9 @@ pub const fn size_of_val(val: &T) -> usize { /// /// # Safety /// -/// This function is only safe to call if the following conditions hold: +/// This function is safe to call if the pointer is safe to reborrow as `&T` +/// (in which case you could also call [`size_of_val`]). +/// Otherwise, the following conditions must hold: /// /// - If `T` is `Sized`, this function is always safe to call. /// - If the unsized tail of `T` is: @@ -592,7 +594,9 @@ pub const fn align_of_val(val: &T) -> usize { /// /// # Safety /// -/// This function is only safe to call if the following conditions hold: +/// This function is safe to call if the pointer is safe to reborrow as `&T` +/// (in which case you could also call [`align_of_val`]). +/// Otherwise, the following conditions must hold: /// /// - If `T` is `Sized`, this function is always safe to call. /// - If the unsized tail of `T` is: