From 15cd51af5e193789e82024ffbe194f4c0dbdfbc3 Mon Sep 17 00:00:00 2001 From: Alexis Bourget Date: Tue, 16 Jun 2020 23:33:23 +0200 Subject: [PATCH 1/3] Mention functions pointers in the documentation --- src/libcore/mem/mod.rs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/libcore/mem/mod.rs b/src/libcore/mem/mod.rs index 8bce980cadd1e..226454561f6fe 100644 --- a/src/libcore/mem/mod.rs +++ b/src/libcore/mem/mod.rs @@ -581,11 +581,12 @@ pub const fn needs_drop() -> bool { /// This means that, for example, the padding byte in `(u8, u16)` is not /// necessarily zeroed. /// -/// There is no guarantee that an all-zero byte-pattern represents a valid value of -/// some type `T`. For example, the all-zero byte-pattern is not a valid value -/// for reference types (`&T` and `&mut T`). Using `zeroed` on such types -/// causes immediate [undefined behavior][ub] because [the Rust compiler assumes][inv] -/// that there always is a valid value in a variable it considers initialized. +/// There is no guarantee that an all-zero byte-pattern represents a valid value +/// of some type `T`. For example, the all-zero byte-pattern is not a valid value +/// for reference types (`&T`, `&mut T` and functions pointers). Using `zeroed` on +/// such types on such types causes immediate [undefined behavior][ub] because +/// [the Rust compiler assumes][inv] that there always is a valid value in a +/// variable it considers initialized. /// /// This has the same effect as [`MaybeUninit::zeroed().assume_init()`][zeroed]. /// It is useful for FFI sometimes, but should generally be avoided. @@ -612,6 +613,7 @@ pub const fn needs_drop() -> bool { /// use std::mem; /// /// let _x: &i32 = unsafe { mem::zeroed() }; // Undefined behavior! +/// let _y: fn() = unsafe { mem::zeroed() }; // And again ! /// ``` #[inline(always)] #[stable(feature = "rust1", since = "1.0.0")] From e75fa896ba53eb5fc5c3dd2741101f377488c2db Mon Sep 17 00:00:00 2001 From: Poliorcetics Date: Wed, 17 Jun 2020 03:30:41 +0200 Subject: [PATCH 2/3] Don't imply function pointers are references Co-authored-by: David Tolnay --- src/libcore/mem/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libcore/mem/mod.rs b/src/libcore/mem/mod.rs index 226454561f6fe..70083ffa4be37 100644 --- a/src/libcore/mem/mod.rs +++ b/src/libcore/mem/mod.rs @@ -583,7 +583,7 @@ pub const fn needs_drop() -> bool { /// /// There is no guarantee that an all-zero byte-pattern represents a valid value /// of some type `T`. For example, the all-zero byte-pattern is not a valid value -/// for reference types (`&T`, `&mut T` and functions pointers). Using `zeroed` on +/// for reference types (`&T`, `&mut T`) and functions pointers. Using `zeroed` on /// such types on such types causes immediate [undefined behavior][ub] because /// [the Rust compiler assumes][inv] that there always is a valid value in a /// variable it considers initialized. @@ -613,7 +613,7 @@ pub const fn needs_drop() -> bool { /// use std::mem; /// /// let _x: &i32 = unsafe { mem::zeroed() }; // Undefined behavior! -/// let _y: fn() = unsafe { mem::zeroed() }; // And again ! +/// let _y: fn() = unsafe { mem::zeroed() }; // And again! /// ``` #[inline(always)] #[stable(feature = "rust1", since = "1.0.0")] From 2da9ca72bcdc6b67fbacf26d9245da367089a113 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Wed, 17 Jun 2020 17:17:07 -0700 Subject: [PATCH 3/3] Remove duplicate sentence fragment from mem::zeroed doc --- src/libcore/mem/mod.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/libcore/mem/mod.rs b/src/libcore/mem/mod.rs index 70083ffa4be37..066bb8b3dc787 100644 --- a/src/libcore/mem/mod.rs +++ b/src/libcore/mem/mod.rs @@ -583,10 +583,10 @@ pub const fn needs_drop() -> bool { /// /// There is no guarantee that an all-zero byte-pattern represents a valid value /// of some type `T`. For example, the all-zero byte-pattern is not a valid value -/// for reference types (`&T`, `&mut T`) and functions pointers. Using `zeroed` on -/// such types on such types causes immediate [undefined behavior][ub] because -/// [the Rust compiler assumes][inv] that there always is a valid value in a -/// variable it considers initialized. +/// for reference types (`&T`, `&mut T`) and functions pointers. Using `zeroed` +/// on such types causes immediate [undefined behavior][ub] because [the Rust +/// compiler assumes][inv] that there always is a valid value in a variable it +/// considers initialized. /// /// This has the same effect as [`MaybeUninit::zeroed().assume_init()`][zeroed]. /// It is useful for FFI sometimes, but should generally be avoided.