From 33452b0587ad543c6d7cc0f41daad6d4be71842f Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Wed, 17 Jul 2019 09:33:08 +0200 Subject: [PATCH 1/3] warn about deprecated-in-future in most of libstd --- src/libstd/lib.rs | 2 +- src/libstd/sync/mod.rs | 2 +- src/libstd/sys/cloudabi/mod.rs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/libstd/lib.rs b/src/libstd/lib.rs index f394195d77aca..79b2f09f40af1 100644 --- a/src/libstd/lib.rs +++ b/src/libstd/lib.rs @@ -205,7 +205,7 @@ // Don't link to std. We are std. #![no_std] -//#![warn(deprecated_in_future)] // FIXME: std still has quite a few uses of `mem::uninitialized` +#![warn(deprecated_in_future)] #![warn(missing_docs)] #![warn(missing_debug_implementations)] #![deny(intra_doc_link_resolution_failure)] // rustdoc is run without -D warnings diff --git a/src/libstd/sync/mod.rs b/src/libstd/sync/mod.rs index fd6e46fd61dc5..5d4cdb16472cb 100644 --- a/src/libstd/sync/mod.rs +++ b/src/libstd/sync/mod.rs @@ -163,7 +163,7 @@ pub use self::condvar::{Condvar, WaitTimeoutResult}; #[stable(feature = "rust1", since = "1.0.0")] pub use self::mutex::{Mutex, MutexGuard}; #[stable(feature = "rust1", since = "1.0.0")] -#[allow(deprecated)] +#[allow(deprecated_in_future)] pub use self::once::{Once, OnceState, ONCE_INIT}; #[stable(feature = "rust1", since = "1.0.0")] pub use crate::sys_common::poison::{PoisonError, TryLockError, TryLockResult, LockResult}; diff --git a/src/libstd/sys/cloudabi/mod.rs b/src/libstd/sys/cloudabi/mod.rs index 3fef7552259c8..77a52a8743d95 100644 --- a/src/libstd/sys/cloudabi/mod.rs +++ b/src/libstd/sys/cloudabi/mod.rs @@ -1,4 +1,4 @@ -#![allow(deprecated)] // mem::uninitialized +#![allow(deprecated_in_future)] // mem::uninitialized; becomes `deprecated` when nightly is 1.39 use crate::io::ErrorKind; use crate::mem; From 13ed0cf9e86a4fdbf75152849353050fea5d4461 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Wed, 17 Jul 2019 09:51:58 +0200 Subject: [PATCH 2/3] do not use mem::uninitialized in std::io --- src/libcore/fmt/float.rs | 7 ++++--- src/libstd/io/util.rs | 24 ++++++++++-------------- src/libstd/lib.rs | 1 + 3 files changed, 15 insertions(+), 17 deletions(-) diff --git a/src/libcore/fmt/float.rs b/src/libcore/fmt/float.rs index 4bd7d3b4b22e3..a2fff913ac720 100644 --- a/src/libcore/fmt/float.rs +++ b/src/libcore/fmt/float.rs @@ -12,10 +12,11 @@ fn float_to_decimal_common_exact(fmt: &mut Formatter<'_>, num: &T, unsafe { let mut buf = MaybeUninit::<[u8; 1024]>::uninit(); // enough for f32 and f64 let mut parts = MaybeUninit::<[flt2dec::Part<'_>; 4]>::uninit(); - // FIXME(#53491): Technically, this is calling `get_mut` on an uninitialized - // `MaybeUninit` (here and elsewhere in this file). Revisit this once + // FIXME(#53491): 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. - // Using `freeze` is *not enough*; `flt2dec::Part` is an enum! + // We can do this only because we are libstd and coupled to the compiler. + // (FWIW, using `freeze` would not be enough; `flt2dec::Part` is an enum!) 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/libstd/io/util.rs b/src/libstd/io/util.rs index 1efccb53b7551..20979e02a4373 100644 --- a/src/libstd/io/util.rs +++ b/src/libstd/io/util.rs @@ -2,7 +2,7 @@ use crate::fmt; use crate::io::{self, Read, Initializer, Write, ErrorKind, BufRead, IoSlice, IoSliceMut}; -use crate::mem; +use crate::mem::MaybeUninit; /// Copies the entire contents of a reader into a writer. /// @@ -43,27 +43,23 @@ use crate::mem; pub fn copy(reader: &mut R, writer: &mut W) -> io::Result where R: Read, W: Write { - let mut buf = unsafe { - // This is still technically undefined behavior due to creating a reference - // to uninitialized data, but within libstd we can rely on more guarantees - // than if this code were in an external lib - - // FIXME: This should probably be changed to an array of `MaybeUninit` - // once the `mem::MaybeUninit` slice APIs stabilize - let mut buf: mem::MaybeUninit<[u8; super::DEFAULT_BUF_SIZE]> = mem::MaybeUninit::uninit(); - reader.initializer().initialize(&mut *buf.as_mut_ptr()); - buf.assume_init() - }; + let mut buf = MaybeUninit::<[u8; super::DEFAULT_BUF_SIZE]>::uninit(); + // FIXME(#53491): This is calling `get_mut` and `get_ref` on an uninitialized + // `MaybeUninit`. Revisit this once we decided whether that is valid or not. + // This is still technically undefined behavior due to creating a reference + // to uninitialized data, but within libstd we can rely on more guarantees + // than if this code were in an external lib + unsafe { reader.initializer().initialize(buf.get_mut()); } let mut written = 0; loop { - let len = match reader.read(&mut buf) { + let len = match reader.read(unsafe { buf.get_mut() }) { Ok(0) => return Ok(written), Ok(len) => len, Err(ref e) if e.kind() == ErrorKind::Interrupted => continue, Err(e) => return Err(e), }; - writer.write_all(&buf[..len])?; + writer.write_all(unsafe { &buf.get_ref()[..len] })?; written += len as u64; } } diff --git a/src/libstd/lib.rs b/src/libstd/lib.rs index 79b2f09f40af1..49fb4be39b451 100644 --- a/src/libstd/lib.rs +++ b/src/libstd/lib.rs @@ -272,6 +272,7 @@ #![feature(libc)] #![feature(link_args)] #![feature(linkage)] +#![feature(maybe_uninit_ref)] #![feature(mem_take)] #![feature(needs_panic_runtime)] #![feature(never_type)] From 7c1e4054787f99dff36aa66318b7589f78dfc7d9 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Wed, 17 Jul 2019 10:34:34 +0200 Subject: [PATCH 3/3] ONCE_INIT is deprecated-in-future only for bootstrap --- src/libstd/io/util.rs | 2 +- src/libstd/sync/mod.rs | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/libstd/io/util.rs b/src/libstd/io/util.rs index 20979e02a4373..33cc87eb79555 100644 --- a/src/libstd/io/util.rs +++ b/src/libstd/io/util.rs @@ -48,7 +48,7 @@ pub fn copy(reader: &mut R, writer: &mut W) -> io::Result< // `MaybeUninit`. Revisit this once we decided whether that is valid or not. // This is still technically undefined behavior due to creating a reference // to uninitialized data, but within libstd we can rely on more guarantees - // than if this code were in an external lib + // than if this code were in an external lib. unsafe { reader.initializer().initialize(buf.get_mut()); } let mut written = 0; diff --git a/src/libstd/sync/mod.rs b/src/libstd/sync/mod.rs index 5d4cdb16472cb..e29faf18d83e5 100644 --- a/src/libstd/sync/mod.rs +++ b/src/libstd/sync/mod.rs @@ -163,7 +163,8 @@ pub use self::condvar::{Condvar, WaitTimeoutResult}; #[stable(feature = "rust1", since = "1.0.0")] pub use self::mutex::{Mutex, MutexGuard}; #[stable(feature = "rust1", since = "1.0.0")] -#[allow(deprecated_in_future)] +#[cfg_attr(bootstrap, allow(deprecated_in_future))] +#[allow(deprecated)] pub use self::once::{Once, OnceState, ONCE_INIT}; #[stable(feature = "rust1", since = "1.0.0")] pub use crate::sys_common::poison::{PoisonError, TryLockError, TryLockResult, LockResult};