Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clarify preconditions of raw size/align methods #103372

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions library/core/src/alloc/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,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 [`Layout::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:
Expand All @@ -186,7 +188,8 @@ impl Layout {
/// call, but may panic or otherwise return the wrong value, as the
/// extern type's layout is not known. This is the same behavior as
/// [`Layout::for_value`] on a reference to an extern type tail.
/// - otherwise, it is conservatively not allowed to call this function.
/// - otherwise, it is conservatively allowed to call this function
/// only when it would be safe to reborrow `t` as a shared reference.
///
/// [trait object]: ../../book/ch17-02-trait-objects.html
/// [extern type]: ../../unstable-book/language-features/extern-types.html
Expand Down
14 changes: 10 additions & 4 deletions library/core/src/mem/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,9 @@ pub const fn size_of_val<T: ?Sized>(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:
Expand All @@ -366,7 +368,8 @@ pub const fn size_of_val<T: ?Sized>(val: &T) -> usize {
/// call, but may panic or otherwise return the wrong value, as the
/// extern type's layout is not known. This is the same behavior as
/// [`size_of_val`] on a reference to a type with an extern type tail.
/// - otherwise, it is conservatively not allowed to call this function.
/// - otherwise, it is conservatively allowed to call this function
/// only when it would be safe to reborrow `val` as a shared reference.
///
/// [trait object]: ../../book/ch17-02-trait-objects.html
/// [extern type]: ../../unstable-book/language-features/extern-types.html
Expand Down Expand Up @@ -497,7 +500,9 @@ pub const fn align_of_val<T: ?Sized>(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:
Expand All @@ -512,7 +517,8 @@ pub const fn align_of_val<T: ?Sized>(val: &T) -> usize {
/// call, but may panic or otherwise return the wrong value, as the
/// extern type's layout is not known. This is the same behavior as
/// [`align_of_val`] on a reference to a type with an extern type tail.
/// - otherwise, it is conservatively not allowed to call this function.
/// - otherwise, it is conservatively allowed to call this function
/// only when it would be safe to reborrow `val` as a shared reference.
///
/// [trait object]: ../../book/ch17-02-trait-objects.html
/// [extern type]: ../../unstable-book/language-features/extern-types.html
Expand Down