Skip to content

Commit 8660eba

Browse files
committed
Auto merge of #56275 - RalfJung:win-mutex, r=SimonSapin
use MaybeUninit instead of mem::uninitialized for Windows Mutex I hope this builds, I do not have a Windows machine to test...
2 parents 9abc231 + ebe69c0 commit 8660eba

File tree

5 files changed

+18
-11
lines changed

5 files changed

+18
-11
lines changed

src/liballoc/collections/btree/node.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,7 @@ impl<'a, K: 'a, V: 'a, Type> NodeRef<marker::Mut<'a>, K, V, Type> {
602602
} else {
603603
unsafe {
604604
slice::from_raw_parts_mut(
605-
self.as_leaf_mut().keys.get_mut() as *mut [K] as *mut K,
605+
self.as_leaf_mut().keys.as_mut_ptr() as *mut K,
606606
self.len()
607607
)
608608
}
@@ -613,7 +613,7 @@ impl<'a, K: 'a, V: 'a, Type> NodeRef<marker::Mut<'a>, K, V, Type> {
613613
debug_assert!(!self.is_shared_root());
614614
unsafe {
615615
slice::from_raw_parts_mut(
616-
self.as_leaf_mut().vals.get_mut() as *mut [V] as *mut V,
616+
self.as_leaf_mut().vals.as_mut_ptr() as *mut V,
617617
self.len()
618618
)
619619
}

src/libcore/fmt/float.rs

+3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ fn float_to_decimal_common_exact<T>(fmt: &mut Formatter, num: &T,
2222
unsafe {
2323
let mut buf = MaybeUninit::<[u8; 1024]>::uninitialized(); // enough for f32 and f64
2424
let mut parts = MaybeUninit::<[flt2dec::Part; 4]>::uninitialized();
25+
// FIXME(#53491): Technically, this is calling `get_mut` on an uninitialized
26+
// `MaybeUninit` (here and elsewhere in this file). Revisit this once
27+
// we decided whether that is valid or not.
2528
let formatted = flt2dec::to_exact_fixed_str(flt2dec::strategy::grisu::format_exact,
2629
*num, sign, precision,
2730
false, buf.get_mut(), parts.get_mut());

src/libcore/mem.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1118,6 +1118,9 @@ impl<T> MaybeUninit<T> {
11181118
///
11191119
/// It is up to the caller to guarantee that the `MaybeUninit` really is in an initialized
11201120
/// state, otherwise this will immediately cause undefined behavior.
1121+
// FIXME(#53491): We currently rely on the above being incorrect, i.e., we have references
1122+
// to uninitialized data (e.g. in `libcore/fmt/float.rs`). We should make
1123+
// a final decision about the rules before stabilization.
11211124
#[unstable(feature = "maybe_uninit", issue = "53491")]
11221125
#[inline(always)]
11231126
pub unsafe fn get_mut(&mut self) -> &mut T {

src/libstd/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,7 @@
311311
#![feature(panic_info_message)]
312312
#![feature(non_exhaustive)]
313313
#![feature(alloc_layout_extra)]
314+
#![feature(maybe_uninit)]
314315

315316
#![default_lib_allocator]
316317

src/libstd/sys/windows/mutex.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
//! detect recursive locks.
3131
3232
use cell::UnsafeCell;
33-
use mem;
33+
use mem::{self, MaybeUninit};
3434
use sync::atomic::{AtomicUsize, Ordering};
3535
use sys::c;
3636
use sys::compat;
@@ -157,34 +157,34 @@ fn kind() -> Kind {
157157
return ret;
158158
}
159159

160-
pub struct ReentrantMutex { inner: UnsafeCell<c::CRITICAL_SECTION> }
160+
pub struct ReentrantMutex { inner: UnsafeCell<MaybeUninit<c::CRITICAL_SECTION>> }
161161

162162
unsafe impl Send for ReentrantMutex {}
163163
unsafe impl Sync for ReentrantMutex {}
164164

165165
impl ReentrantMutex {
166-
pub unsafe fn uninitialized() -> ReentrantMutex {
167-
mem::uninitialized()
166+
pub fn uninitialized() -> ReentrantMutex {
167+
ReentrantMutex { inner: UnsafeCell::new(MaybeUninit::uninitialized()) }
168168
}
169169

170170
pub unsafe fn init(&mut self) {
171-
c::InitializeCriticalSection(self.inner.get());
171+
c::InitializeCriticalSection((&mut *self.inner.get()).as_mut_ptr());
172172
}
173173

174174
pub unsafe fn lock(&self) {
175-
c::EnterCriticalSection(self.inner.get());
175+
c::EnterCriticalSection((&mut *self.inner.get()).as_mut_ptr());
176176
}
177177

178178
#[inline]
179179
pub unsafe fn try_lock(&self) -> bool {
180-
c::TryEnterCriticalSection(self.inner.get()) != 0
180+
c::TryEnterCriticalSection((&mut *self.inner.get()).as_mut_ptr()) != 0
181181
}
182182

183183
pub unsafe fn unlock(&self) {
184-
c::LeaveCriticalSection(self.inner.get());
184+
c::LeaveCriticalSection((&mut *self.inner.get()).as_mut_ptr());
185185
}
186186

187187
pub unsafe fn destroy(&self) {
188-
c::DeleteCriticalSection(self.inner.get());
188+
c::DeleteCriticalSection((&mut *self.inner.get()).as_mut_ptr());
189189
}
190190
}

0 commit comments

Comments
 (0)