Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate uninit_array #101179

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion compiler/rustc_data_structures/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#![feature(extend_one)]
#![feature(hash_raw_entry)]
#![feature(hasher_prefixfree_extras)]
#![feature(maybe_uninit_uninit_array)]
#![feature(maybe_uninit_uninit_array_transpose)]
#![feature(min_specialization)]
#![feature(never_type)]
#![feature(type_alias_impl_trait)]
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_data_structures/src/sip128.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ impl SipHasher128 {
pub fn new_with_keys(key0: u64, key1: u64) -> SipHasher128 {
let mut hasher = SipHasher128 {
nbuf: 0,
buf: MaybeUninit::uninit_array(),
buf: MaybeUninit::uninit().transpose(),
state: State {
v0: key0 ^ 0x736f6d6570736575,
// The XOR with 0xee is only done on 128-bit algorithm version.
Expand Down
7 changes: 3 additions & 4 deletions compiler/rustc_serialize/tests/leb128.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#![feature(maybe_uninit_slice)]
#![feature(maybe_uninit_uninit_array)]
#![feature(maybe_uninit_uninit_array_transpose)]

use rustc_serialize::leb128::*;
use std::mem::MaybeUninit;
Expand All @@ -24,7 +23,7 @@ macro_rules! impl_test_unsigned_leb128 {
let mut stream = Vec::new();

for &x in &values {
let mut buf = MaybeUninit::uninit_array();
let mut buf = MaybeUninit::uninit().transpose();
stream.extend($write_fn_name(&mut buf, x));
}

Expand Down Expand Up @@ -70,7 +69,7 @@ macro_rules! impl_test_signed_leb128 {
let mut stream = Vec::new();

for &x in &values {
let mut buf = MaybeUninit::uninit_array();
let mut buf = MaybeUninit::uninit().transpose();
stream.extend($write_fn_name(&mut buf, x));
}

Expand Down
2 changes: 1 addition & 1 deletion library/alloc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@
#![feature(layout_for_ptr)]
#![feature(maybe_uninit_array_assume_init)]
#![feature(maybe_uninit_slice)]
#![feature(maybe_uninit_uninit_array)]
#![feature(maybe_uninit_uninit_array_transpose)]
#![cfg_attr(test, feature(new_uninit))]
#![feature(nonnull_slice_from_raw_parts)]
#![feature(pattern)]
Expand Down
2 changes: 1 addition & 1 deletion library/alloc/src/vec/into_iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ impl<T, A: Allocator> Iterator for IntoIter<T, A> {

#[inline]
fn next_chunk<const N: usize>(&mut self) -> Result<[T; N], core::array::IntoIter<T, N>> {
let mut raw_ary = MaybeUninit::uninit_array();
let mut raw_ary = MaybeUninit::uninit().transpose();

let len = self.len();

Expand Down
8 changes: 4 additions & 4 deletions library/core/src/array/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ impl<T, const N: usize> IntoIter<T, N> {
/// #![feature(array_into_iter_constructors)]
///
/// #![feature(maybe_uninit_array_assume_init)]
/// #![feature(maybe_uninit_uninit_array)]
/// use std::array::IntoIter;
/// use std::mem::MaybeUninit;
///
Expand All @@ -116,7 +115,7 @@ impl<T, const N: usize> IntoIter<T, N> {
/// fn next_chunk<T: Copy, const N: usize>(
/// it: &mut impl Iterator<Item = T>,
/// ) -> Result<[T; N], IntoIter<T, N>> {
/// let mut buffer = MaybeUninit::uninit_array();
/// let mut buffer = [MaybeUninit::uninit(); N];
scottmcm marked this conversation as resolved.
Show resolved Hide resolved
/// let mut i = 0;
/// while i < N {
/// match it.next() {
Expand Down Expand Up @@ -208,7 +207,7 @@ impl<T, const N: usize> IntoIter<T, N> {
#[unstable(feature = "array_into_iter_constructors", issue = "91583")]
#[rustc_const_unstable(feature = "const_array_into_iter_constructors", issue = "91583")]
pub const fn empty() -> Self {
let buffer = MaybeUninit::uninit_array();
let buffer = MaybeUninit::uninit().transpose();
let initialized = 0..0;

// SAFETY: We're telling it that none of the elements are initialized,
Expand Down Expand Up @@ -387,7 +386,8 @@ impl<T: Clone, const N: usize> Clone for IntoIter<T, N> {
fn clone(&self) -> Self {
// Note, we don't really need to match the exact same alive range, so
// we can just clone into offset 0 regardless of where `self` is.
let mut new = Self { data: MaybeUninit::uninit_array(), alive: IndexRange::zero_to(0) };
let mut new =
Self { data: MaybeUninit::uninit().transpose(), alive: IndexRange::zero_to(0) };

// Clone all alive elements.
for (src, dst) in iter::zip(self.as_slice(), &mut new.data) {
Expand Down
2 changes: 1 addition & 1 deletion library/core/src/array/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -879,7 +879,7 @@ where
}
}

let mut array = MaybeUninit::uninit_array::<N>();
let mut array = MaybeUninit::uninit().transpose();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it worth adding a type annotation?

Suggested change
let mut array = MaybeUninit::uninit().transpose();
let mut array = MaybeUninit::<[???; N]>::uninit().transpose();

There's a lot of inference going on here making the code quite hard to follow.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm, yeah I was wondering about this too. My reasoning for this being ok was that if the compiler manages to infer the types then I don't need to think about them. I'm guessing this is flawed because if you have no constraints at all, then the compiler will happily give you an i32. An interesting question arises: are integers and floats the only two places where the compiler will ever pull a type out of its 🍑 if there are no constraints? If so, my gut feeling is that we should get rid of that behavior (separate discussion, I know) since not having 🍑 types would let you do what I'm doing now where I say, "well, the compiler did some inferences so whatever the types are they must be correct". Or maybe I'm thinking too hard about this and type annotations would make human consumption easier (though I'm still curious about the above questions).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In safe code I tend to follow the same approach -- if the compiler found a type that makes it all work, then that seems right (but I will still sometimes add type annotations to make the code more readable).

In unsafe code though, I don't like trusting the compiler like that. Types are much less meaningful in unsafe code (that's why the code is unsafe to begin with), so IMO type inference should only be relied upon for absolutely obvious cases -- which this one is not.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, will do.

let mut guard = Guard { array_mut: &mut array, initialized: 0 };

for _ in 0..N {
Expand Down
16 changes: 8 additions & 8 deletions library/core/src/fmt/float.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ fn float_to_decimal_common_exact<T>(
where
T: flt2dec::DecodableFloat,
{
let mut buf: [MaybeUninit<u8>; 1024] = MaybeUninit::uninit_array(); // enough for f32 and f64
let mut parts: [MaybeUninit<numfmt::Part<'_>>; 4] = MaybeUninit::uninit_array();
let mut buf = [MaybeUninit::<u8>::uninit(); 1024]; // enough for f32 and f64
let mut parts = [MaybeUninit::<numfmt::Part<'_>>::uninit(); 4];
let formatted = flt2dec::to_exact_fixed_str(
flt2dec::strategy::grisu::format_exact,
*num,
Expand All @@ -61,8 +61,8 @@ where
T: flt2dec::DecodableFloat,
{
// enough for f32 and f64
let mut buf: [MaybeUninit<u8>; flt2dec::MAX_SIG_DIGITS] = MaybeUninit::uninit_array();
let mut parts: [MaybeUninit<numfmt::Part<'_>>; 4] = MaybeUninit::uninit_array();
let mut buf = [MaybeUninit::<u8>::uninit(); flt2dec::MAX_SIG_DIGITS];
let mut parts = [MaybeUninit::<numfmt::Part<'_>>::uninit(); 4];
let formatted = flt2dec::to_shortest_str(
flt2dec::strategy::grisu::format_shortest,
*num,
Expand Down Expand Up @@ -105,8 +105,8 @@ fn float_to_exponential_common_exact<T>(
where
T: flt2dec::DecodableFloat,
{
let mut buf: [MaybeUninit<u8>; 1024] = MaybeUninit::uninit_array(); // enough for f32 and f64
let mut parts: [MaybeUninit<numfmt::Part<'_>>; 6] = MaybeUninit::uninit_array();
let mut buf = [MaybeUninit::<u8>::uninit(); 1024]; // enough for f32 and f64
let mut parts = [MaybeUninit::<numfmt::Part<'_>>::uninit(); 6];
let formatted = flt2dec::to_exact_exp_str(
flt2dec::strategy::grisu::format_exact,
*num,
Expand All @@ -132,8 +132,8 @@ where
T: flt2dec::DecodableFloat,
{
// enough for f32 and f64
let mut buf: [MaybeUninit<u8>; flt2dec::MAX_SIG_DIGITS] = MaybeUninit::uninit_array();
let mut parts: [MaybeUninit<numfmt::Part<'_>>; 6] = MaybeUninit::uninit_array();
let mut buf = [MaybeUninit::<u8>::uninit(); flt2dec::MAX_SIG_DIGITS];
let mut parts = [MaybeUninit::<numfmt::Part<'_>>::uninit(); 6];
let formatted = flt2dec::to_shortest_exp_str(
flt2dec::strategy::grisu::format_shortest,
*num,
Expand Down
3 changes: 1 addition & 2 deletions library/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@
#![feature(const_int_unchecked_arith)]
#![feature(const_intrinsic_forget)]
#![feature(const_likely)]
#![feature(const_maybe_uninit_uninit_array)]
#![feature(const_maybe_uninit_as_mut_ptr)]
#![feature(const_maybe_uninit_assume_init)]
#![feature(const_nonnull_new)]
Expand Down Expand Up @@ -151,7 +150,6 @@
#![feature(const_waker)]
#![feature(core_panic)]
#![feature(duration_consts_float)]
#![feature(maybe_uninit_uninit_array)]
#![feature(ptr_alignment_type)]
#![feature(ptr_metadata)]
#![feature(slice_ptr_get)]
Expand Down Expand Up @@ -217,6 +215,7 @@
#![feature(unboxed_closures)]
#![feature(unsized_fn_params)]
#![feature(asm_const)]
#![feature(const_transmute_copy)]
//
// Target features:
#![feature(arm_target_feature)]
Expand Down
19 changes: 12 additions & 7 deletions library/core/src/mem/maybe_uninit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ use crate::slice;
/// unsafe { mem::transmute::<_, [Vec<u32>; 1000]>(data) }
/// };
///
/// assert_eq!(&data[0], &[42]);
/// assert_eq!(&data[42], &[42]);
/// ```
///
/// You can also work with partially initialized arrays, which could
Expand All @@ -148,10 +148,9 @@ use crate::slice;
/// use std::mem::MaybeUninit;
/// use std::ptr;
///
/// // Create an uninitialized array of `MaybeUninit`. The `assume_init` is
/// // safe because the type we are claiming to have initialized here is a
/// // bunch of `MaybeUninit`s, which do not require initialization.
/// let mut data: [MaybeUninit<String>; 1000] = unsafe { MaybeUninit::uninit().assume_init() };
/// // Create an uninitialized array of `MaybeUninit`.
/// const UNINIT_STRING: MaybeUninit<String> = MaybeUninit::uninit();
/// let mut data = [UNINIT_STRING; 1000];
/// // Count the number of elements we have assigned.
/// let mut data_len: usize = 0;
///
Expand Down Expand Up @@ -348,6 +347,10 @@ impl<T> MaybeUninit<T> {
#[rustc_const_unstable(feature = "const_maybe_uninit_uninit_array", issue = "96097")]
#[must_use]
#[inline(always)]
#[deprecated(
since = "nightly",
note = "Use const array initialization instead or the transpose methods."
)]
pub const fn uninit_array<const N: usize>() -> [Self; N] {
// SAFETY: An uninitialized `[MaybeUninit<_>; LEN]` is valid.
unsafe { MaybeUninit::<[MaybeUninit<T>; N]>::uninit().assume_init() }
Expand Down Expand Up @@ -1297,7 +1300,8 @@ impl<T, const N: usize> MaybeUninit<[T; N]> {
/// let data: [MaybeUninit<u8>; 1000] = MaybeUninit::uninit().transpose();
/// ```
#[unstable(feature = "maybe_uninit_uninit_array_transpose", issue = "96097")]
pub fn transpose(self) -> [MaybeUninit<T>; N] {
#[inline(always)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please just use #[inline] for these. always should be avoided without particular justification, so for this PR please just use #[inline].

Then we can discuss changing it to inline(always) in a subsequent PR if you'd like.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like the scope creep anyway, so lemme just pull this out into a seperate PR.

pub const fn transpose(self) -> [MaybeUninit<T>; N] {
// SAFETY: T and MaybeUninit<T> have the same layout
unsafe { super::transmute_copy(&ManuallyDrop::new(self)) }
}
Expand All @@ -1316,7 +1320,8 @@ impl<T, const N: usize> [MaybeUninit<T>; N] {
/// let data: MaybeUninit<[u8; 1000]> = data.transpose();
/// ```
#[unstable(feature = "maybe_uninit_uninit_array_transpose", issue = "96097")]
pub fn transpose(self) -> MaybeUninit<[T; N]> {
#[inline(always)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(here too)

pub const fn transpose(self) -> MaybeUninit<[T; N]> {
// SAFETY: T and MaybeUninit<T> have the same layout
unsafe { super::transmute_copy(&ManuallyDrop::new(self)) }
}
Expand Down
1 change: 0 additions & 1 deletion library/core/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
#![feature(slice_take)]
#![feature(slice_from_ptr_range)]
#![feature(split_as_slice)]
#![feature(maybe_uninit_uninit_array)]
#![feature(maybe_uninit_array_assume_init)]
#![feature(maybe_uninit_write_slice)]
#![feature(min_specialization)]
Expand Down
2 changes: 1 addition & 1 deletion library/core/tests/mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ fn assume_init_good() {

#[test]
fn uninit_array_assume_init() {
let mut array: [MaybeUninit<i16>; 5] = MaybeUninit::uninit_array();
let mut array = [MaybeUninit::<i16>::uninit(); 5];
array[0].write(3);
array[1].write(1);
array[2].write(4);
Expand Down
3 changes: 1 addition & 2 deletions library/std/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,8 +312,7 @@
#![feature(std_internals)]
#![feature(str_internals)]
#![feature(strict_provenance)]
#![feature(maybe_uninit_uninit_array)]
#![feature(const_maybe_uninit_uninit_array)]
#![feature(maybe_uninit_uninit_array_transpose)]
#![feature(const_waker)]
//
// Library features (alloc):
Expand Down
2 changes: 1 addition & 1 deletion library/std/src/net/display_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub struct DisplayBuffer<const SIZE: usize> {
impl<const SIZE: usize> DisplayBuffer<SIZE> {
#[inline]
pub const fn new() -> Self {
Self { buf: MaybeUninit::uninit_array(), len: 0 }
Self { buf: MaybeUninit::uninit().transpose(), len: 0 }
}

#[inline]
Expand Down
2 changes: 1 addition & 1 deletion library/std/src/sys/windows/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ where
// This initial size also works around `GetFullPathNameW` returning
// incorrect size hints for some short paths:
// https://github.com/dylni/normpath/issues/5
let mut stack_buf: [MaybeUninit<u16>; 512] = MaybeUninit::uninit_array();
let mut stack_buf = [MaybeUninit::<u16>::uninit(); 512];
let mut heap_buf: Vec<MaybeUninit<u16>> = Vec::new();
unsafe {
let mut n = stack_buf.len();
Expand Down