Skip to content

Commit f6ce646

Browse files
committed
Stabilize const_maybe_uninit_zeroed
Make `MaybeUninit::zeroed` const stable. Newly stable API: // core::mem impl<T> MaybeUninit<T> { pub const fn zeroed() -> MaybeUninit<T>; } Use of `const_mut_refs` should be acceptable since we do not leak the mutability. Tracking issue: rust-lang#91850
1 parent 2ba4eb2 commit f6ce646

File tree

3 files changed

+32
-7
lines changed

3 files changed

+32
-7
lines changed

library/alloc/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,6 @@
113113
#![feature(const_eval_select)]
114114
#![feature(const_maybe_uninit_as_mut_ptr)]
115115
#![feature(const_maybe_uninit_write)]
116-
#![feature(const_maybe_uninit_zeroed)]
117116
#![feature(const_pin)]
118117
#![feature(const_refs_to_cell)]
119118
#![feature(const_size_of_val)]

library/core/src/mem/maybe_uninit.rs

+11-6
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,9 @@ impl<T> MaybeUninit<T> {
374374
/// assert_eq!(x, (0, false));
375375
/// ```
376376
///
377+
/// This can be used in const contexts, such as to indicate the end of static arrays for
378+
/// plugin registration.
379+
///
377380
/// *Incorrect* usage of this function: calling `x.zeroed().assume_init()`
378381
/// when `0` is not a valid bit-pattern for the type:
379382
///
@@ -387,17 +390,19 @@ impl<T> MaybeUninit<T> {
387390
/// // Inside a pair, we create a `NotZero` that does not have a valid discriminant.
388391
/// // This is undefined behavior. ⚠️
389392
/// ```
390-
#[stable(feature = "maybe_uninit", since = "1.36.0")]
391-
#[rustc_const_unstable(feature = "const_maybe_uninit_zeroed", issue = "91850")]
392-
#[must_use]
393393
#[inline]
394+
#[must_use]
394395
#[rustc_diagnostic_item = "maybe_uninit_zeroed"]
396+
#[stable(feature = "maybe_uninit", since = "1.36.0")]
397+
// These are OK to allow since we do not leak &mut to user-visible API
398+
#[rustc_allow_const_fn_unstable(const_mut_refs)]
399+
#[rustc_allow_const_fn_unstable(const_ptr_write)]
400+
#[rustc_allow_const_fn_unstable(const_maybe_uninit_as_mut_ptr)]
401+
#[rustc_const_stable(feature = "const_maybe_uninit_zeroed", since = "CURRENT_RUSTC_VERSION")]
395402
pub const fn zeroed() -> MaybeUninit<T> {
396403
let mut u = MaybeUninit::<T>::uninit();
397404
// SAFETY: `u.as_mut_ptr()` points to allocated memory.
398-
unsafe {
399-
u.as_mut_ptr().write_bytes(0u8, 1);
400-
}
405+
unsafe { u.as_mut_ptr().write_bytes(0u8, 1) };
401406
u
402407
}
403408

library/core/tests/mem.rs

+21
Original file line numberDiff line numberDiff line change
@@ -565,3 +565,24 @@ fn offset_of_addr() {
565565
assert_eq!(ptr::addr_of!(base).addr() + offset_of!(Foo, z.0), ptr::addr_of!(base.z.0).addr());
566566
assert_eq!(ptr::addr_of!(base).addr() + offset_of!(Foo, z.1), ptr::addr_of!(base.z.1).addr());
567567
}
568+
569+
#[test]
570+
fn const_maybe_uninit_zeroed() {
571+
// Sanity check for `MaybeUninit::zeroed` in a realistic const situation (plugin array term)
572+
#[repr(C)]
573+
struct Foo {
574+
a: Option<&'static str>,
575+
b: Bar,
576+
c: f32,
577+
d: *const u8,
578+
}
579+
#[repr(C)]
580+
struct Bar(usize);
581+
struct FooPtr(*const Foo);
582+
unsafe impl Sync for FooPtr {}
583+
584+
static UNINIT: FooPtr = FooPtr([unsafe { MaybeUninit::zeroed().assume_init() }].as_ptr());
585+
const SIZE: usize = size_of::<Foo>();
586+
587+
assert_eq!(unsafe { (*UNINIT.0.cast::<[[u8; SIZE]; 1]>())[0] }, [0u8; SIZE]);
588+
}

0 commit comments

Comments
 (0)