From a4f12344c68530d1f42c5b00c10ab417137c0491 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Tue, 27 Nov 2018 16:12:08 +0100 Subject: add comments explaining our uses of get_ref/get_mut for MaybeUninit --- src/libcore/fmt/float.rs | 3 +++ src/libcore/mem.rs | 6 ++++++ 2 files changed, 9 insertions(+) (limited to 'src/libcore') diff --git a/src/libcore/fmt/float.rs b/src/libcore/fmt/float.rs index d01cd012031..0c1ac90dfd2 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: 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 a5603ff6a62..626db7806df 100644 --- a/src/libcore/mem.rs +++ b/src/libcore/mem.rs @@ -1106,6 +1106,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: We currently rely on the above being incorrect, i.e., we have references + // to uninitialized data (e.g. in `libstd/sys/windows/mutex.rs`). We should make + // a final decision about the rules before stabilization. #[unstable(feature = "maybe_uninit", issue = "53491")] #[inline(always)] pub unsafe fn get_ref(&self) -> &T { @@ -1118,6 +1121,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: 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 { -- cgit 1.4.1-3-g733a5 From 12d90aa949f34712a374984bfaf88a5bf2f08685 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Wed, 28 Nov 2018 09:29:56 +0100 Subject: put the MaybeUninit inside the UnsafeCell --- src/libcore/mem.rs | 3 --- src/libstd/sys/windows/mutex.rs | 17 +++++++---------- 2 files changed, 7 insertions(+), 13 deletions(-) (limited to 'src/libcore') diff --git a/src/libcore/mem.rs b/src/libcore/mem.rs index 626db7806df..b61a5c973a7 100644 --- a/src/libcore/mem.rs +++ b/src/libcore/mem.rs @@ -1106,9 +1106,6 @@ 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: We currently rely on the above being incorrect, i.e., we have references - // to uninitialized data (e.g. in `libstd/sys/windows/mutex.rs`). We should make - // a final decision about the rules before stabilization. #[unstable(feature = "maybe_uninit", issue = "53491")] #[inline(always)] pub unsafe fn get_ref(&self) -> &T { diff --git a/src/libstd/sys/windows/mutex.rs b/src/libstd/sys/windows/mutex.rs index 0c228f5097e..38ba0c7e035 100644 --- a/src/libstd/sys/windows/mutex.rs +++ b/src/libstd/sys/windows/mutex.rs @@ -157,37 +157,34 @@ fn kind() -> Kind { return ret; } -pub struct ReentrantMutex { inner: MaybeUninit> } +pub struct ReentrantMutex { inner: UnsafeCell> } unsafe impl Send for ReentrantMutex {} unsafe impl Sync for ReentrantMutex {} impl ReentrantMutex { pub fn uninitialized() -> ReentrantMutex { - ReentrantMutex { inner: MaybeUninit::uninitialized() } + ReentrantMutex { inner: UnsafeCell::new(MaybeUninit::uninitialized()) } } pub unsafe fn init(&mut self) { - // FIXME: Technically, this is calling `get_ref` on an uninitialized - // `MaybeUninit`. Revisit this once we decided whether that is valid - // or not. - c::InitializeCriticalSection(self.inner.get_ref().get()); + c::InitializeCriticalSection(self.inner.get().as_mut_ptr()); } pub unsafe fn lock(&self) { - c::EnterCriticalSection(self.inner.get_ref().get()); + c::EnterCriticalSection(self.inner.get().get_ref()); } #[inline] pub unsafe fn try_lock(&self) -> bool { - c::TryEnterCriticalSection(self.inner.get_ref().get()) != 0 + c::TryEnterCriticalSection(self.inner.get().get_ref()) != 0 } pub unsafe fn unlock(&self) { - c::LeaveCriticalSection(self.inner.get_ref().get()); + c::LeaveCriticalSection(self.inner.get().get_ref()); } pub unsafe fn destroy(&self) { - c::DeleteCriticalSection(self.inner.get_ref().get()); + c::DeleteCriticalSection(self.inner.get().get_ref()); } } -- cgit 1.4.1-3-g733a5 From f4f8b211a8650feff62c69a1dfb5156f692003c3 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Sun, 2 Dec 2018 12:29:54 +0100 Subject: let FIXME refer to tracking issue --- src/libcore/fmt/float.rs | 2 +- src/libcore/mem.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'src/libcore') diff --git a/src/libcore/fmt/float.rs b/src/libcore/fmt/float.rs index 0c1ac90dfd2..3717a783f24 100644 --- a/src/libcore/fmt/float.rs +++ b/src/libcore/fmt/float.rs @@ -22,7 +22,7 @@ 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: Technically, this is calling `get_mut` on an 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, diff --git a/src/libcore/mem.rs b/src/libcore/mem.rs index b61a5c973a7..e4b2800ae21 100644 --- a/src/libcore/mem.rs +++ b/src/libcore/mem.rs @@ -1118,7 +1118,7 @@ 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: We currently rely on the above being incorrect, i.e., we have references + // 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")] -- cgit 1.4.1-3-g733a5