Skip to content

Commit 57ddb2d

Browse files
committed
Creating uninitialized integers is UB
1 parent 3e567bc commit 57ddb2d

File tree

2 files changed

+3
-11
lines changed

2 files changed

+3
-11
lines changed

library/core/src/mem/maybe_uninit.rs

-3
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,6 @@ use crate::slice;
5454
/// // The equivalent code with `MaybeUninit<i32>`:
5555
/// let x: i32 = unsafe { MaybeUninit::uninit().assume_init() }; // undefined behavior! ⚠️
5656
/// ```
57-
/// (Notice that the rules around uninitialized integers are not finalized yet, but
58-
/// until they are, it is advisable to avoid them.)
59-
///
6057
/// On top of that, remember that most types have additional invariants beyond merely
6158
/// being considered initialized at the type level. For example, a `1`-initialized [`Vec<T>`]
6259
/// is considered initialized (under the current implementation; this does not constitute

library/core/src/mem/mod.rs

+3-8
Original file line numberDiff line numberDiff line change
@@ -665,14 +665,9 @@ pub unsafe fn zeroed<T>() -> T {
665665
/// correctly: it has the same effect as [`MaybeUninit::uninit().assume_init()`][uninit].
666666
/// As the [`assume_init` documentation][assume_init] explains,
667667
/// [the Rust compiler assumes][inv] that values are properly initialized.
668-
/// As a consequence, calling e.g. `mem::uninitialized::<bool>()` causes immediate
669-
/// undefined behavior for returning a `bool` that is not definitely either `true`
670-
/// or `false`. Worse, truly uninitialized memory like what gets returned here
671-
/// is special in that the compiler knows that it does not have a fixed value.
672-
/// This makes it undefined behavior to have uninitialized data in a variable even
673-
/// if that variable has an integer type.
674-
/// (Notice that the rules around uninitialized integers are not finalized yet, but
675-
/// until they are, it is advisable to avoid them.)
668+
///
669+
/// Therefore, it is immediate undefined behavior to call this function on nearly all types,
670+
/// including integer types and arrays of integer types, and even if the result is unused.
676671
///
677672
/// [uninit]: MaybeUninit::uninit
678673
/// [assume_init]: MaybeUninit::assume_init

0 commit comments

Comments
 (0)