Skip to content

Commit 3cf469a

Browse files
committedAug 30, 2022
Auto merge of #98919 - 5225225:stricter-invalid-value, r=RalfJung
Strengthen invalid_value lint to forbid uninit primitives, adjust docs to say that's UB For context: rust-lang/rust#66151 (comment) This does not make it a FCW, but it does explicitly state in the docs that uninit integers are UB. This also doesn't affect any runtime behavior, uninit u32's will still successfully be created through mem::uninitialized.
2 parents 349241e + ae4961a commit 3cf469a

File tree

2 files changed

+5
-8
lines changed

2 files changed

+5
-8
lines changed
 

‎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

‎core/src/mem/mod.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -665,14 +665,14 @@ 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
668+
///
669+
/// Truly uninitialized memory like what gets returned here
671670
/// is special in that the compiler knows that it does not have a fixed value.
672671
/// This makes it undefined behavior to have uninitialized data in a variable even
673672
/// 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.)
673+
///
674+
/// Therefore, it is immediate undefined behavior to call this function on nearly all types,
675+
/// including integer types and arrays of integer types, and even if the result is unused.
676676
///
677677
/// [uninit]: MaybeUninit::uninit
678678
/// [assume_init]: MaybeUninit::assume_init

0 commit comments

Comments
 (0)
Please sign in to comment.