Skip to content

Commit b87d4a4

Browse files
authored
Rollup merge of rust-lang#73425 - poliorcetics:zeroed-functions-pointers, r=dtolnay
Mention functions pointers in the documentation Fixes rust-lang#51615. This mentions function pointers in the documentation for `core::mem::zeroed`, adding them to the list of types that are **always** wrong when zeroed, with `&T` and `&mut T`. @rustbot modify labels: T-doc, C-enhancement, T-libs
2 parents c51d0ac + 2da9ca7 commit b87d4a4

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

src/libcore/mem/mod.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -581,11 +581,12 @@ pub const fn needs_drop<T>() -> bool {
581581
/// This means that, for example, the padding byte in `(u8, u16)` is not
582582
/// necessarily zeroed.
583583
///
584-
/// There is no guarantee that an all-zero byte-pattern represents a valid value of
585-
/// some type `T`. For example, the all-zero byte-pattern is not a valid value
586-
/// for reference types (`&T` and `&mut T`). Using `zeroed` on such types
587-
/// causes immediate [undefined behavior][ub] because [the Rust compiler assumes][inv]
588-
/// that there always is a valid value in a variable it considers initialized.
584+
/// There is no guarantee that an all-zero byte-pattern represents a valid value
585+
/// of some type `T`. For example, the all-zero byte-pattern is not a valid value
586+
/// for reference types (`&T`, `&mut T`) and functions pointers. Using `zeroed`
587+
/// on such types causes immediate [undefined behavior][ub] because [the Rust
588+
/// compiler assumes][inv] that there always is a valid value in a variable it
589+
/// considers initialized.
589590
///
590591
/// This has the same effect as [`MaybeUninit::zeroed().assume_init()`][zeroed].
591592
/// It is useful for FFI sometimes, but should generally be avoided.
@@ -612,6 +613,7 @@ pub const fn needs_drop<T>() -> bool {
612613
/// use std::mem;
613614
///
614615
/// let _x: &i32 = unsafe { mem::zeroed() }; // Undefined behavior!
616+
/// let _y: fn() = unsafe { mem::zeroed() }; // And again!
615617
/// ```
616618
#[inline(always)]
617619
#[stable(feature = "rust1", since = "1.0.0")]

0 commit comments

Comments
 (0)