diff --git a/src/liballoc/collections/btree/node.rs b/src/liballoc/collections/btree/node.rs index f9b455fe796d1..215689dfc48c9 100644 --- a/src/liballoc/collections/btree/node.rs +++ b/src/liballoc/collections/btree/node.rs @@ -602,7 +602,7 @@ impl<'a, K: 'a, V: 'a, Type> NodeRef, K, V, Type> { } else { unsafe { slice::from_raw_parts_mut( - self.as_leaf_mut().keys.get_mut() as *mut [K] as *mut K, + self.as_leaf_mut().keys.as_mut_ptr() as *mut K, self.len() ) } @@ -613,7 +613,7 @@ impl<'a, K: 'a, V: 'a, Type> NodeRef, K, V, Type> { debug_assert!(!self.is_shared_root()); unsafe { slice::from_raw_parts_mut( - self.as_leaf_mut().vals.get_mut() as *mut [V] as *mut V, + self.as_leaf_mut().vals.as_mut_ptr() as *mut V, self.len() ) } diff --git a/src/libcore/fmt/float.rs b/src/libcore/fmt/float.rs index d01cd012031db..3717a783f2411 100644 --- a/src/libcore/fmt/float.rs +++ b/src/libcore/fmt/float.rs @@ -22,6 +22,9 @@ fn float_to_decimal_common_exact(fmt: &mut Formatter, num: &T, unsafe { let mut buf = MaybeUninit::<[u8; 1024]>::uninitialized(); // enough for f32 and f64 let mut parts = MaybeUninit::<[flt2dec::Part; 4]>::uninitialized(); + // FIXME(#53491): Technically, this is calling `get_mut` on an uninitialized + // `MaybeUninit` (here and elsewhere in this file). Revisit this once + // we decided whether that is valid or not. let formatted = flt2dec::to_exact_fixed_str(flt2dec::strategy::grisu::format_exact, *num, sign, precision, false, buf.get_mut(), parts.get_mut()); diff --git a/src/libcore/mem.rs b/src/libcore/mem.rs index a5603ff6a62e7..e4b2800ae2117 100644 --- a/src/libcore/mem.rs +++ b/src/libcore/mem.rs @@ -1118,6 +1118,9 @@ impl MaybeUninit { /// /// It is up to the caller to guarantee that the `MaybeUninit` really is in an initialized /// state, otherwise this will immediately cause undefined behavior. + // FIXME(#53491): We currently rely on the above being incorrect, i.e., we have references + // to uninitialized data (e.g. in `libcore/fmt/float.rs`). We should make + // a final decision about the rules before stabilization. #[unstable(feature = "maybe_uninit", issue = "53491")] #[inline(always)] pub unsafe fn get_mut(&mut self) -> &mut T { diff --git a/src/libstd/lib.rs b/src/libstd/lib.rs index 5945ba6b09066..78109a1a690e6 100644 --- a/src/libstd/lib.rs +++ b/src/libstd/lib.rs @@ -311,6 +311,7 @@ #![feature(panic_info_message)] #![feature(non_exhaustive)] #![feature(alloc_layout_extra)] +#![feature(maybe_uninit)] #![default_lib_allocator] diff --git a/src/libstd/sys/windows/mutex.rs b/src/libstd/sys/windows/mutex.rs index b0e7331e2b651..2bd5dee63e883 100644 --- a/src/libstd/sys/windows/mutex.rs +++ b/src/libstd/sys/windows/mutex.rs @@ -30,7 +30,7 @@ //! detect recursive locks. use cell::UnsafeCell; -use mem; +use mem::{self, MaybeUninit}; use sync::atomic::{AtomicUsize, Ordering}; use sys::c; use sys::compat; @@ -157,34 +157,34 @@ fn kind() -> Kind { return ret; } -pub struct ReentrantMutex { inner: UnsafeCell } +pub struct ReentrantMutex { inner: UnsafeCell> } unsafe impl Send for ReentrantMutex {} unsafe impl Sync for ReentrantMutex {} impl ReentrantMutex { - pub unsafe fn uninitialized() -> ReentrantMutex { - mem::uninitialized() + pub fn uninitialized() -> ReentrantMutex { + ReentrantMutex { inner: UnsafeCell::new(MaybeUninit::uninitialized()) } } pub unsafe fn init(&mut self) { - c::InitializeCriticalSection(self.inner.get()); + c::InitializeCriticalSection((&mut *self.inner.get()).as_mut_ptr()); } pub unsafe fn lock(&self) { - c::EnterCriticalSection(self.inner.get()); + c::EnterCriticalSection((&mut *self.inner.get()).as_mut_ptr()); } #[inline] pub unsafe fn try_lock(&self) -> bool { - c::TryEnterCriticalSection(self.inner.get()) != 0 + c::TryEnterCriticalSection((&mut *self.inner.get()).as_mut_ptr()) != 0 } pub unsafe fn unlock(&self) { - c::LeaveCriticalSection(self.inner.get()); + c::LeaveCriticalSection((&mut *self.inner.get()).as_mut_ptr()); } pub unsafe fn destroy(&self) { - c::DeleteCriticalSection(self.inner.get()); + c::DeleteCriticalSection((&mut *self.inner.get()).as_mut_ptr()); } }