From 79209efb0aa847a9e1b174544a60a3c99f8b5231 Mon Sep 17 00:00:00 2001 From: Tim Diekmann Date: Sun, 3 May 2020 04:05:35 +0200 Subject: [PATCH 1/3] Move `Layout` and `LayoutErr` to `core::mem` --- src/libcore/alloc/mod.rs | 21 ++++++++- src/libcore/{alloc => mem}/layout.rs | 51 ++++++++++----------- src/libcore/mem/mod.rs | 4 ++ src/test/ui/layout_err_nonexhaustive.rs | 5 ++ src/test/ui/layout_err_nonexhaustive.stderr | 9 ++++ 5 files changed, 62 insertions(+), 28 deletions(-) rename src/libcore/{alloc => mem}/layout.rs (94%) create mode 100644 src/test/ui/layout_err_nonexhaustive.rs create mode 100644 src/test/ui/layout_err_nonexhaustive.stderr diff --git a/src/libcore/alloc/mod.rs b/src/libcore/alloc/mod.rs index 1346fbd481003..eb6e48753b1df 100644 --- a/src/libcore/alloc/mod.rs +++ b/src/libcore/alloc/mod.rs @@ -3,12 +3,29 @@ #![stable(feature = "alloc_module", since = "1.28.0")] mod global; -mod layout; #[stable(feature = "global_alloc", since = "1.28.0")] pub use self::global::GlobalAlloc; + #[stable(feature = "alloc_layout", since = "1.28.0")] -pub use self::layout::{Layout, LayoutErr}; +#[doc(no_inline)] +/// **This alias is soft-deprecated.** +/// +/// Although using it won’t cause compilation warning, new code should use [`mem::Layout`] directly +/// instead. +/// +/// [`mem::Layout`]: crate::mem::Layout +pub use crate::mem::Layout; + +#[stable(feature = "alloc_layout", since = "1.28.0")] +#[doc(no_inline)] +/// **This alias is soft-deprecated.** +/// +/// Although using it won’t cause compilation warning, new code should use [`mem::LayoutError`] +/// directly instead. +/// +/// [`mem::LayoutError`]: crate::mem::LayoutError +pub use crate::mem::LayoutError as LayoutErr; use crate::fmt; use crate::ptr::{self, NonNull}; diff --git a/src/libcore/alloc/layout.rs b/src/libcore/mem/layout.rs similarity index 94% rename from src/libcore/alloc/layout.rs rename to src/libcore/mem/layout.rs index a09c2387d0de2..d95a5bf7afc3d 100644 --- a/src/libcore/alloc/layout.rs +++ b/src/libcore/mem/layout.rs @@ -39,7 +39,7 @@ pub struct Layout { impl Layout { /// Constructs a `Layout` from a given `size` and `align`, - /// or returns `LayoutErr` if any of the following conditions + /// or returns `LayoutError` if any of the following conditions /// are not met: /// /// * `align` must not be zero, @@ -52,9 +52,9 @@ impl Layout { #[stable(feature = "alloc_layout", since = "1.28.0")] #[rustc_const_unstable(feature = "const_alloc_layout", issue = "67521")] #[inline] - pub const fn from_size_align(size: usize, align: usize) -> Result { + pub const fn from_size_align(size: usize, align: usize) -> Result { if !align.is_power_of_two() { - return Err(LayoutErr { private: () }); + return Err(LayoutError); } // (power-of-two implies align != 0.) @@ -72,7 +72,7 @@ impl Layout { // Above implies that checking for summation overflow is both // necessary and sufficient. if size > usize::MAX - (align - 1) { - return Err(LayoutErr { private: () }); + return Err(LayoutError); } // SAFETY: the conditions for `from_size_align_unchecked` have been @@ -164,7 +164,7 @@ impl Layout { /// [`Layout::from_size_align`](#method.from_size_align). #[stable(feature = "alloc_layout_manipulation", since = "1.44.0")] #[inline] - pub fn align_to(&self, align: usize) -> Result { + pub fn align_to(&self, align: usize) -> Result { Layout::from_size_align(self.size(), cmp::max(self.align(), align)) } @@ -238,16 +238,16 @@ impl Layout { /// layout of the array and `offs` is the distance between the start /// of each element in the array. /// - /// On arithmetic overflow, returns `LayoutErr`. + /// On arithmetic overflow, returns `LayoutError`. #[unstable(feature = "alloc_layout_extra", issue = "55724")] #[inline] - pub fn repeat(&self, n: usize) -> Result<(Self, usize), LayoutErr> { + pub fn repeat(&self, n: usize) -> Result<(Self, usize), LayoutError> { // This cannot overflow. Quoting from the invariant of Layout: // > `size`, when rounded up to the nearest multiple of `align`, // > must not overflow (i.e., the rounded value must be less than // > `usize::MAX`) let padded_size = self.size() + self.padding_needed_for(self.align()); - let alloc_size = padded_size.checked_mul(n).ok_or(LayoutErr { private: () })?; + let alloc_size = padded_size.checked_mul(n).ok_or(LayoutError)?; // SAFETY: self.align is already known to be valid and alloc_size has been // padded already. @@ -271,7 +271,7 @@ impl Layout { /// start of the `next` embedded within the concatenated record /// (assuming that the record itself starts at offset 0). /// - /// On arithmetic overflow, returns `LayoutErr`. + /// On arithmetic overflow, returns `LayoutError`. /// /// # Examples /// @@ -279,8 +279,8 @@ impl Layout { /// the fields from its fields' layouts: /// /// ```rust - /// # use std::alloc::{Layout, LayoutErr}; - /// pub fn repr_c(fields: &[Layout]) -> Result<(Layout, Vec), LayoutErr> { + /// # use std::mem::{Layout, LayoutError}; + /// pub fn repr_c(fields: &[Layout]) -> Result<(Layout, Vec), LayoutError> { /// let mut offsets = Vec::new(); /// let mut layout = Layout::from_size_align(0, 1)?; /// for &field in fields { @@ -301,12 +301,12 @@ impl Layout { /// ``` #[stable(feature = "alloc_layout_manipulation", since = "1.44.0")] #[inline] - pub fn extend(&self, next: Self) -> Result<(Self, usize), LayoutErr> { + pub fn extend(&self, next: Self) -> Result<(Self, usize), LayoutError> { let new_align = cmp::max(self.align(), next.align()); let pad = self.padding_needed_for(next.align()); - let offset = self.size().checked_add(pad).ok_or(LayoutErr { private: () })?; - let new_size = offset.checked_add(next.size()).ok_or(LayoutErr { private: () })?; + let offset = self.size().checked_add(pad).ok_or(LayoutError)?; + let new_size = offset.checked_add(next.size()).ok_or(LayoutError)?; let layout = Layout::from_size_align(new_size, new_align)?; Ok((layout, offset)) @@ -323,11 +323,11 @@ impl Layout { /// guaranteed that all elements in the array will be properly /// aligned. /// - /// On arithmetic overflow, returns `LayoutErr`. + /// On arithmetic overflow, returns `LayoutError`. #[unstable(feature = "alloc_layout_extra", issue = "55724")] #[inline] - pub fn repeat_packed(&self, n: usize) -> Result { - let size = self.size().checked_mul(n).ok_or(LayoutErr { private: () })?; + pub fn repeat_packed(&self, n: usize) -> Result { + let size = self.size().checked_mul(n).ok_or(LayoutError)?; Layout::from_size_align(size, self.align()) } @@ -336,20 +336,20 @@ impl Layout { /// padding is inserted, the alignment of `next` is irrelevant, /// and is not incorporated *at all* into the resulting layout. /// - /// On arithmetic overflow, returns `LayoutErr`. + /// On arithmetic overflow, returns `LayoutError`. #[unstable(feature = "alloc_layout_extra", issue = "55724")] #[inline] - pub fn extend_packed(&self, next: Self) -> Result { - let new_size = self.size().checked_add(next.size()).ok_or(LayoutErr { private: () })?; + pub fn extend_packed(&self, next: Self) -> Result { + let new_size = self.size().checked_add(next.size()).ok_or(LayoutError)?; Layout::from_size_align(new_size, self.align()) } /// Creates a layout describing the record for a `[T; n]`. /// - /// On arithmetic overflow, returns `LayoutErr`. + /// On arithmetic overflow, returns `LayoutError`. #[stable(feature = "alloc_layout_manipulation", since = "1.44.0")] #[inline] - pub fn array(n: usize) -> Result { + pub fn array(n: usize) -> Result { let (layout, offset) = Layout::new::().repeat(n)?; debug_assert_eq!(offset, mem::size_of::()); Ok(layout.pad_to_align()) @@ -361,13 +361,12 @@ impl Layout { /// do not satisfy its documented constraints. #[stable(feature = "alloc_layout", since = "1.28.0")] #[derive(Clone, PartialEq, Eq, Debug)] -pub struct LayoutErr { - private: (), -} +#[non_exhaustive] +pub struct LayoutError; // (we need this for downstream impl of trait Error) #[stable(feature = "alloc_layout", since = "1.28.0")] -impl fmt::Display for LayoutErr { +impl fmt::Display for LayoutError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.write_str("invalid parameters to Layout::from_size_align") } diff --git a/src/libcore/mem/mod.rs b/src/libcore/mem/mod.rs index b1bbcaeab8def..cf09a517caae1 100644 --- a/src/libcore/mem/mod.rs +++ b/src/libcore/mem/mod.rs @@ -25,6 +25,10 @@ pub use maybe_uninit::MaybeUninit; #[doc(inline)] pub use crate::intrinsics::transmute; +mod layout; +#[stable(feature = "alloc_layout", since = "1.28.0")] +pub use layout::{Layout, LayoutError}; + /// Takes ownership and "forgets" about the value **without running its destructor**. /// /// Any resources the value manages, such as heap memory or a file handle, will linger diff --git a/src/test/ui/layout_err_nonexhaustive.rs b/src/test/ui/layout_err_nonexhaustive.rs new file mode 100644 index 0000000000000..b15c326bdbbfa --- /dev/null +++ b/src/test/ui/layout_err_nonexhaustive.rs @@ -0,0 +1,5 @@ +use std::mem::LayoutError; + +fn main() { + let _err = LayoutError; //~ ERROR expected value, found struct `LayoutError` +} diff --git a/src/test/ui/layout_err_nonexhaustive.stderr b/src/test/ui/layout_err_nonexhaustive.stderr new file mode 100644 index 0000000000000..117d6dbcdb937 --- /dev/null +++ b/src/test/ui/layout_err_nonexhaustive.stderr @@ -0,0 +1,9 @@ +error[E0423]: expected value, found struct `LayoutError` + --> $DIR/layout_err_nonexhaustive.rs:4:16 + | +LL | let _err = LayoutError; + | ^^^^^^^^^^^ constructor is not visible here due to private fields + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0423`. From e1dde7f688fcba8e9d4f3ee40d31125c3ff2fc54 Mon Sep 17 00:00:00 2001 From: Tim Diekmann Date: Sun, 3 May 2020 23:14:32 +0200 Subject: [PATCH 2/3] Manually reexport `Layout` in liballoc and libstd for #[doc(no_inline)] --- src/liballoc/alloc.rs | 20 ++++++++++++++++++++ src/libstd/alloc.rs | 20 ++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/src/liballoc/alloc.rs b/src/liballoc/alloc.rs index d31c73cc1bd8d..8ecd2e1240928 100644 --- a/src/liballoc/alloc.rs +++ b/src/liballoc/alloc.rs @@ -9,6 +9,26 @@ use core::ptr::{NonNull, Unique}; #[doc(inline)] pub use core::alloc::*; +#[stable(feature = "alloc_layout", since = "1.28.0")] +#[doc(no_inline)] +/// **This alias is soft-deprecated.** +/// +/// Although using it won’t cause compilation warning, new code should use [`mem::Layout`] directly +/// instead. +/// +/// [`mem::Layout`]: core::mem::Layout +pub use core::mem::Layout; + +#[stable(feature = "alloc_layout", since = "1.28.0")] +#[doc(no_inline)] +/// **This alias is soft-deprecated.** +/// +/// Although using it won’t cause compilation warning, new code should use [`mem::LayoutError`] +/// directly instead. +/// +/// [`mem::LayoutError`]: core::mem::LayoutError +pub use core::mem::LayoutError as LayoutErr; + #[cfg(test)] mod tests; diff --git a/src/libstd/alloc.rs b/src/libstd/alloc.rs index 9840cfa43044f..a57e607fc8d44 100644 --- a/src/libstd/alloc.rs +++ b/src/libstd/alloc.rs @@ -72,6 +72,26 @@ use crate::sys_common::util::dumb_print; #[doc(inline)] pub use alloc_crate::alloc::*; +#[stable(feature = "alloc_layout", since = "1.28.0")] +#[doc(no_inline)] +/// **This alias is soft-deprecated.** +/// +/// Although using it won’t cause compilation warning, new code should use [`mem::Layout`] directly +/// instead. +/// +/// [`mem::Layout`]: crate::mem::Layout +pub use crate::mem::Layout; + +#[stable(feature = "alloc_layout", since = "1.28.0")] +#[doc(no_inline)] +/// **This alias is soft-deprecated.** +/// +/// Although using it won’t cause compilation warning, new code should use [`mem::LayoutError`] +/// directly instead. +/// +/// [`mem::LayoutError`]: crate::mem::LayoutError +pub use crate::mem::LayoutError as LayoutErr; + /// The default memory allocator provided by the operating system. /// /// This is based on `malloc` on Unix platforms and `HeapAlloc` on Windows, From 2ac0def279914237d04114538a266e2bbea93c2d Mon Sep 17 00:00:00 2001 From: Tim Diekmann Date: Mon, 4 May 2020 00:21:44 +0200 Subject: [PATCH 3/3] Use `Layout` and `LayoutError` where possible --- src/liballoc/alloc.rs | 14 ++++++++------ src/liballoc/boxed.rs | 18 ++++++++++-------- src/liballoc/collections/btree/node.rs | 4 ++-- src/liballoc/collections/mod.rs | 6 +++--- src/liballoc/raw_vec.rs | 4 ++-- src/liballoc/rc.rs | 4 ++-- src/liballoc/sync.rs | 4 ++-- src/liballoc/tests/heap.rs | 3 ++- src/libcore/alloc/global.rs | 5 +++-- src/libcore/tests/alloc.rs | 2 +- src/libstd/alloc.rs | 14 ++++++++------ src/libstd/error.rs | 6 +++--- src/libstd/ffi/c_str.rs | 2 +- src/libstd/sys/hermit/alloc.rs | 3 ++- src/libstd/sys/sgx/alloc.rs | 3 ++- src/libstd/sys/unix/alloc.rs | 3 ++- src/libstd/sys/vxworks/alloc.rs | 3 ++- src/libstd/sys/wasi/alloc.rs | 3 ++- src/libstd/sys/wasm/alloc.rs | 3 ++- src/libstd/sys/windows/alloc.rs | 3 ++- src/libstd/sys_common/alloc.rs | 3 ++- src/test/run-make-fulldeps/issue-51671/app.rs | 2 +- src/test/run-make-fulldeps/issue-69368/b.rs | 2 +- .../run-make/wasm-symbols-not-exported/bar.rs | 2 +- src/test/rustdoc/issue-54478-demo-allocator.rs | 1 + .../alloc-error-handler-bad-signature-1.rs | 2 +- src/test/ui/allocator/allocator-args.rs | 3 ++- src/test/ui/allocator/allocator-args.stderr | 2 +- src/test/ui/allocator/auxiliary/custom.rs | 3 ++- src/test/ui/allocator/custom.rs | 3 ++- src/test/ui/allocator/xcrate-use.rs | 3 ++- src/test/ui/allocator/xcrate-use2.rs | 3 ++- src/test/ui/consts/std/alloc.rs | 2 +- src/test/ui/default-alloc-error-hook.rs | 3 ++- .../feature-gate-alloc-error-handler.rs | 2 +- .../ui/missing/missing-alloc_error_handler.rs | 4 ++-- src/test/ui/missing/missing-allocator.rs | 2 +- src/test/ui/realloc-16687.rs | 3 ++- src/test/ui/regions/regions-mock-codegen.rs | 3 ++- 39 files changed, 90 insertions(+), 65 deletions(-) diff --git a/src/liballoc/alloc.rs b/src/liballoc/alloc.rs index 8ecd2e1240928..ca0ec3996c68e 100644 --- a/src/liballoc/alloc.rs +++ b/src/liballoc/alloc.rs @@ -9,24 +9,24 @@ use core::ptr::{NonNull, Unique}; #[doc(inline)] pub use core::alloc::*; -#[stable(feature = "alloc_layout", since = "1.28.0")] -#[doc(no_inline)] /// **This alias is soft-deprecated.** /// /// Although using it won’t cause compilation warning, new code should use [`mem::Layout`] directly /// instead. /// /// [`mem::Layout`]: core::mem::Layout -pub use core::mem::Layout; - #[stable(feature = "alloc_layout", since = "1.28.0")] #[doc(no_inline)] +pub use core::mem::Layout; + /// **This alias is soft-deprecated.** /// /// Although using it won’t cause compilation warning, new code should use [`mem::LayoutError`] /// directly instead. /// /// [`mem::LayoutError`]: core::mem::LayoutError +#[stable(feature = "alloc_layout", since = "1.28.0")] +#[doc(no_inline)] pub use core::mem::LayoutError as LayoutErr; #[cfg(test)] @@ -82,7 +82,8 @@ pub struct Global; /// # Examples /// /// ``` -/// use std::alloc::{alloc, dealloc, Layout}; +/// use std::alloc::{alloc, dealloc}; +/// use std::mem::Layout; /// /// unsafe { /// let layout = Layout::new::(); @@ -164,7 +165,8 @@ pub unsafe fn realloc(ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 /// # Examples /// /// ``` -/// use std::alloc::{alloc_zeroed, dealloc, Layout}; +/// use std::alloc::{alloc_zeroed, dealloc}; +/// use std::mem::Layout; /// /// unsafe { /// let layout = Layout::new::(); diff --git a/src/liballoc/boxed.rs b/src/liballoc/boxed.rs index b3a771a721d22..9aa5f9e441428 100644 --- a/src/liballoc/boxed.rs +++ b/src/liballoc/boxed.rs @@ -122,8 +122,8 @@ //! [`Box::::from_raw(value)`]: struct.Box.html#method.from_raw //! [`Box::::into_raw`]: struct.Box.html#method.into_raw //! [`Global`]: ../alloc/struct.Global.html -//! [`Layout`]: ../alloc/struct.Layout.html -//! [`Layout::for_value(&*value)`]: ../alloc/struct.Layout.html#method.for_value +//! [`Layout`]: ../../core/mem/struct.Layout.html +//! [`Layout::for_value(&*value)`]: ../../core/mem/struct.Layout.html#method.for_value #![stable(feature = "rust1", since = "1.0.0")] @@ -137,7 +137,7 @@ use core::future::Future; use core::hash::{Hash, Hasher}; use core::iter::{FromIterator, FusedIterator, Iterator}; use core::marker::{Unpin, Unsize}; -use core::mem; +use core::mem::{self, Layout}; use core::ops::{ CoerceUnsized, Deref, DerefMut, DispatchFromDyn, Generator, GeneratorState, Receiver, }; @@ -194,7 +194,7 @@ impl Box { /// ``` #[unstable(feature = "new_uninit", issue = "63291")] pub fn new_uninit() -> Box> { - let layout = alloc::Layout::new::>(); + let layout = Layout::new::>(); let ptr = Global .alloc(layout, AllocInit::Uninitialized) .unwrap_or_else(|_| alloc::handle_alloc_error(layout)) @@ -223,7 +223,7 @@ impl Box { /// [zeroed]: ../../std/mem/union.MaybeUninit.html#method.zeroed #[unstable(feature = "new_uninit", issue = "63291")] pub fn new_zeroed() -> Box> { - let layout = alloc::Layout::new::>(); + let layout = Layout::new::>(); let ptr = Global .alloc(layout, AllocInit::Zeroed) .unwrap_or_else(|_| alloc::handle_alloc_error(layout)) @@ -377,7 +377,8 @@ impl Box { /// ``` /// Manually create a `Box` from scratch by using the global allocator: /// ``` - /// use std::alloc::{alloc, Layout}; + /// use std::alloc::alloc; + /// use std::mem::Layout; /// /// unsafe { /// let ptr = alloc(Layout::new::()) as *mut i32; @@ -387,7 +388,7 @@ impl Box { /// ``` /// /// [memory layout]: index.html#memory-layout - /// [`Layout`]: ../alloc/struct.Layout.html + /// [`Layout`]: core::mem::Layout /// [`Box::into_raw`]: struct.Box.html#method.into_raw #[stable(feature = "box_raw", since = "1.4.0")] #[inline] @@ -422,7 +423,8 @@ impl Box { /// Manual cleanup by explicitly running the destructor and deallocating /// the memory: /// ``` - /// use std::alloc::{dealloc, Layout}; + /// use std::alloc::dealloc; + /// use std::mem::Layout; /// use std::ptr; /// /// let x = Box::new(String::from("Hello")); diff --git a/src/liballoc/collections/btree/node.rs b/src/liballoc/collections/btree/node.rs index 5569c293e2f66..1f4d2dcd559db 100644 --- a/src/liballoc/collections/btree/node.rs +++ b/src/liballoc/collections/btree/node.rs @@ -33,11 +33,11 @@ use core::cmp::Ordering; use core::marker::PhantomData; -use core::mem::{self, MaybeUninit}; +use core::mem::{self, Layout, MaybeUninit}; use core::ptr::{self, NonNull, Unique}; use core::slice; -use crate::alloc::{AllocRef, Global, Layout}; +use crate::alloc::{AllocRef, Global}; use crate::boxed::Box; const B: usize = 6; diff --git a/src/liballoc/collections/mod.rs b/src/liballoc/collections/mod.rs index 6b21e54f66aa0..416a0074eed44 100644 --- a/src/liballoc/collections/mod.rs +++ b/src/liballoc/collections/mod.rs @@ -41,8 +41,8 @@ pub use linked_list::LinkedList; #[doc(no_inline)] pub use vec_deque::VecDeque; -use crate::alloc::{Layout, LayoutErr}; use core::fmt::Display; +use core::mem::{Layout, LayoutError}; /// The error type for `try_reserve` methods. #[derive(Clone, PartialEq, Eq, Debug)] @@ -71,9 +71,9 @@ pub enum TryReserveError { } #[unstable(feature = "try_reserve", reason = "new API", issue = "48043")] -impl From for TryReserveError { +impl From for TryReserveError { #[inline] - fn from(_: LayoutErr) -> Self { + fn from(_: LayoutError) -> Self { TryReserveError::CapacityOverflow } } diff --git a/src/liballoc/raw_vec.rs b/src/liballoc/raw_vec.rs index a8e19c9cbaa86..f799c73245870 100644 --- a/src/liballoc/raw_vec.rs +++ b/src/liballoc/raw_vec.rs @@ -3,7 +3,7 @@ use core::alloc::MemoryBlock; use core::cmp; -use core::mem::{self, ManuallyDrop, MaybeUninit}; +use core::mem::{self, Layout, ManuallyDrop, MaybeUninit}; use core::ops::Drop; use core::ptr::{NonNull, Unique}; use core::slice; @@ -11,7 +11,7 @@ use core::slice; use crate::alloc::{ handle_alloc_error, AllocErr, AllocInit::{self, *}, - AllocRef, Global, Layout, + AllocRef, Global, ReallocPlacement::{self, *}, }; use crate::boxed::Box; diff --git a/src/liballoc/rc.rs b/src/liballoc/rc.rs index 2f9505ec79ffa..9248c0a76e420 100644 --- a/src/liballoc/rc.rs +++ b/src/liballoc/rc.rs @@ -245,13 +245,13 @@ use core::hash::{Hash, Hasher}; use core::intrinsics::abort; use core::iter; use core::marker::{self, PhantomData, Unpin, Unsize}; -use core::mem::{self, align_of, align_of_val, forget, size_of_val}; +use core::mem::{self, align_of, align_of_val, forget, size_of_val, Layout}; use core::ops::{CoerceUnsized, Deref, DispatchFromDyn, Receiver}; use core::pin::Pin; use core::ptr::{self, NonNull}; use core::slice::{self, from_raw_parts_mut}; -use crate::alloc::{box_free, handle_alloc_error, AllocInit, AllocRef, Global, Layout}; +use crate::alloc::{box_free, handle_alloc_error, AllocInit, AllocRef, Global}; use crate::string::String; use crate::vec::Vec; diff --git a/src/liballoc/sync.rs b/src/liballoc/sync.rs index a81e0cf7e1d2a..66e3f41d2fb3d 100644 --- a/src/liballoc/sync.rs +++ b/src/liballoc/sync.rs @@ -16,7 +16,7 @@ use core::hash::{Hash, Hasher}; use core::intrinsics::abort; use core::iter; use core::marker::{PhantomData, Unpin, Unsize}; -use core::mem::{self, align_of, align_of_val, size_of_val}; +use core::mem::{self, align_of, align_of_val, size_of_val, Layout}; use core::ops::{CoerceUnsized, Deref, DispatchFromDyn, Receiver}; use core::pin::Pin; use core::ptr::{self, NonNull}; @@ -24,7 +24,7 @@ use core::slice::{self, from_raw_parts_mut}; use core::sync::atomic; use core::sync::atomic::Ordering::{Acquire, Relaxed, Release, SeqCst}; -use crate::alloc::{box_free, handle_alloc_error, AllocInit, AllocRef, Global, Layout}; +use crate::alloc::{box_free, handle_alloc_error, AllocInit, AllocRef, Global}; use crate::boxed::Box; use crate::rc::is_dangling; use crate::string::String; diff --git a/src/liballoc/tests/heap.rs b/src/liballoc/tests/heap.rs index 62f062b83d75d..6f63bf2c95825 100644 --- a/src/liballoc/tests/heap.rs +++ b/src/liballoc/tests/heap.rs @@ -1,4 +1,5 @@ -use std::alloc::{AllocInit, AllocRef, Global, Layout, System}; +use std::alloc::{AllocInit, AllocRef, Global, System}; +use std::mem::Layout; /// Issue #45955 and #62251. #[test] diff --git a/src/libcore/alloc/global.rs b/src/libcore/alloc/global.rs index 147fe696ac02f..20492c135425a 100644 --- a/src/libcore/alloc/global.rs +++ b/src/libcore/alloc/global.rs @@ -1,5 +1,5 @@ -use crate::alloc::Layout; use crate::cmp; +use crate::mem::Layout; use crate::ptr; /// A memory allocator that can be registered as the standard library’s default @@ -21,7 +21,8 @@ use crate::ptr; /// # Example /// /// ```no_run -/// use std::alloc::{GlobalAlloc, Layout, alloc}; +/// use std::alloc::{GlobalAlloc, alloc}; +/// use std::mem::Layout; /// use std::ptr::null_mut; /// /// struct MyAllocator; diff --git a/src/libcore/tests/alloc.rs b/src/libcore/tests/alloc.rs index c8592e40a69a0..7ee292bd8d56e 100644 --- a/src/libcore/tests/alloc.rs +++ b/src/libcore/tests/alloc.rs @@ -1,4 +1,4 @@ -use core::alloc::Layout; +use core::mem::Layout; use core::ptr::NonNull; #[test] diff --git a/src/libstd/alloc.rs b/src/libstd/alloc.rs index a57e607fc8d44..b22c7aa4066b6 100644 --- a/src/libstd/alloc.rs +++ b/src/libstd/alloc.rs @@ -16,7 +16,8 @@ //! to route all default allocation requests to a custom object. //! //! ```rust -//! use std::alloc::{GlobalAlloc, System, Layout}; +//! use std::alloc::{GlobalAlloc, System}; +//! use std::mem::Layout; //! //! struct MyAllocator; //! @@ -72,24 +73,24 @@ use crate::sys_common::util::dumb_print; #[doc(inline)] pub use alloc_crate::alloc::*; -#[stable(feature = "alloc_layout", since = "1.28.0")] -#[doc(no_inline)] /// **This alias is soft-deprecated.** /// /// Although using it won’t cause compilation warning, new code should use [`mem::Layout`] directly /// instead. /// /// [`mem::Layout`]: crate::mem::Layout -pub use crate::mem::Layout; - #[stable(feature = "alloc_layout", since = "1.28.0")] #[doc(no_inline)] +pub use crate::mem::Layout; + /// **This alias is soft-deprecated.** /// /// Although using it won’t cause compilation warning, new code should use [`mem::LayoutError`] /// directly instead. /// /// [`mem::LayoutError`]: crate::mem::LayoutError +#[stable(feature = "alloc_layout", since = "1.28.0")] +#[doc(no_inline)] pub use crate::mem::LayoutError as LayoutErr; /// The default memory allocator provided by the operating system. @@ -116,7 +117,8 @@ pub use crate::mem::LayoutError as LayoutErr; /// keeping track of the number of all bytes allocated: /// /// ```rust -/// use std::alloc::{System, GlobalAlloc, Layout}; +/// use std::alloc::{System, GlobalAlloc}; +/// use std::mem::Layout; /// use std::sync::atomic::{AtomicUsize, Ordering::SeqCst}; /// /// struct Counter; diff --git a/src/libstd/error.rs b/src/libstd/error.rs index 24b57f12e8df4..c46d39e74f32e 100644 --- a/src/libstd/error.rs +++ b/src/libstd/error.rs @@ -15,14 +15,14 @@ use core::array; -use crate::alloc::{AllocErr, LayoutErr}; +use crate::alloc::AllocErr; use crate::any::TypeId; use crate::backtrace::Backtrace; use crate::borrow::Cow; use crate::cell; use crate::char; use crate::fmt::{self, Debug, Display}; -use crate::mem::transmute; +use crate::mem::{transmute, LayoutError}; use crate::num; use crate::str; use crate::string; @@ -407,7 +407,7 @@ impl Error for AllocErr {} reason = "the precise API and guarantees it provides may be tweaked.", issue = "32838" )] -impl Error for LayoutErr {} +impl Error for LayoutError {} #[stable(feature = "rust1", since = "1.0.0")] impl Error for str::ParseBoolError { diff --git a/src/libstd/ffi/c_str.rs b/src/libstd/ffi/c_str.rs index 0a4802fb2c80d..b20e4c3909d79 100644 --- a/src/libstd/ffi/c_str.rs +++ b/src/libstd/ffi/c_str.rs @@ -757,7 +757,7 @@ impl From> for CString { let v: Vec = { // Safety: // - transmuting between `NonZeroU8` and `u8` is sound; - // - `alloc::Layout == alloc::Layout`. + // - `mem::Layout == mem::Layout`. let (ptr, len, cap): (*mut NonZeroU8, _, _) = Vec::into_raw_parts(v); Vec::from_raw_parts(ptr.cast::(), len, cap) }; diff --git a/src/libstd/sys/hermit/alloc.rs b/src/libstd/sys/hermit/alloc.rs index d153914e77e10..f2d315029845e 100644 --- a/src/libstd/sys/hermit/alloc.rs +++ b/src/libstd/sys/hermit/alloc.rs @@ -1,4 +1,5 @@ -use crate::alloc::{GlobalAlloc, Layout, System}; +use crate::alloc::{GlobalAlloc, System}; +use crate::mem::Layout; use crate::ptr; use crate::sys::hermit::abi; diff --git a/src/libstd/sys/sgx/alloc.rs b/src/libstd/sys/sgx/alloc.rs index 40daec758a9fc..fffb7254e5fa0 100644 --- a/src/libstd/sys/sgx/alloc.rs +++ b/src/libstd/sys/sgx/alloc.rs @@ -1,4 +1,5 @@ -use crate::alloc::{GlobalAlloc, Layout, System}; +use crate::alloc::{GlobalAlloc, System}; +use crate::mem::Layout; use super::waitqueue::SpinMutex; diff --git a/src/libstd/sys/unix/alloc.rs b/src/libstd/sys/unix/alloc.rs index 8e193935460eb..eea4b86b64429 100644 --- a/src/libstd/sys/unix/alloc.rs +++ b/src/libstd/sys/unix/alloc.rs @@ -1,4 +1,5 @@ -use crate::alloc::{GlobalAlloc, Layout, System}; +use crate::alloc::{GlobalAlloc, System}; +use crate::mem::Layout; use crate::ptr; use crate::sys_common::alloc::{realloc_fallback, MIN_ALIGN}; diff --git a/src/libstd/sys/vxworks/alloc.rs b/src/libstd/sys/vxworks/alloc.rs index 97a191d7232e0..3f026cf0fb226 100644 --- a/src/libstd/sys/vxworks/alloc.rs +++ b/src/libstd/sys/vxworks/alloc.rs @@ -1,4 +1,5 @@ -use crate::alloc::{GlobalAlloc, Layout, System}; +use crate::alloc::{GlobalAlloc, System}; +use crate::mem::Layout; use crate::ptr; use crate::sys_common::alloc::{realloc_fallback, MIN_ALIGN}; diff --git a/src/libstd/sys/wasi/alloc.rs b/src/libstd/sys/wasi/alloc.rs index bc61416278401..c34ec827a097b 100644 --- a/src/libstd/sys/wasi/alloc.rs +++ b/src/libstd/sys/wasi/alloc.rs @@ -1,4 +1,5 @@ -use crate::alloc::{GlobalAlloc, Layout, System}; +use crate::alloc::{GlobalAlloc, System}; +use crate::mem::Layout; use crate::ptr; use crate::sys_common::alloc::{realloc_fallback, MIN_ALIGN}; use libc; diff --git a/src/libstd/sys/wasm/alloc.rs b/src/libstd/sys/wasm/alloc.rs index 32b8b5bdaea7a..4849561a78a90 100644 --- a/src/libstd/sys/wasm/alloc.rs +++ b/src/libstd/sys/wasm/alloc.rs @@ -16,7 +16,8 @@ //! The crate itself provides a global allocator which on wasm has no //! synchronization as there are no threads! -use crate::alloc::{GlobalAlloc, Layout, System}; +use crate::alloc::{GlobalAlloc, System}; +use crate::mem::Layout; static mut DLMALLOC: dlmalloc::Dlmalloc = dlmalloc::DLMALLOC_INIT; diff --git a/src/libstd/sys/windows/alloc.rs b/src/libstd/sys/windows/alloc.rs index 99b4d6c72a0e3..e7a725e46d813 100644 --- a/src/libstd/sys/windows/alloc.rs +++ b/src/libstd/sys/windows/alloc.rs @@ -1,4 +1,5 @@ -use crate::alloc::{GlobalAlloc, Layout, System}; +use crate::alloc::{GlobalAlloc, System}; +use crate::mem::Layout; use crate::sys::c; use crate::sys_common::alloc::{realloc_fallback, MIN_ALIGN}; diff --git a/src/libstd/sys_common/alloc.rs b/src/libstd/sys_common/alloc.rs index c669410078592..1f2b71e1f7505 100644 --- a/src/libstd/sys_common/alloc.rs +++ b/src/libstd/sys_common/alloc.rs @@ -1,7 +1,8 @@ #![allow(dead_code)] -use crate::alloc::{GlobalAlloc, Layout, System}; +use crate::alloc::{GlobalAlloc, System}; use crate::cmp; +use crate::mem::Layout; use crate::ptr; // The minimum alignment guaranteed by the architecture. This value is used to diff --git a/src/test/run-make-fulldeps/issue-51671/app.rs b/src/test/run-make-fulldeps/issue-51671/app.rs index c13937dcfbe55..73f2ea62c1442 100644 --- a/src/test/run-make-fulldeps/issue-51671/app.rs +++ b/src/test/run-make-fulldeps/issue-51671/app.rs @@ -3,7 +3,7 @@ #![no_main] #![no_std] -use core::alloc::Layout; +use core::mem::Layout; use core::panic::PanicInfo; #[panic_handler] diff --git a/src/test/run-make-fulldeps/issue-69368/b.rs b/src/test/run-make-fulldeps/issue-69368/b.rs index 4d6af0266563b..54bb7145f3035 100644 --- a/src/test/run-make-fulldeps/issue-69368/b.rs +++ b/src/test/run-make-fulldeps/issue-69368/b.rs @@ -3,6 +3,6 @@ #![no_std] #[alloc_error_handler] -pub fn error_handler(_: core::alloc::Layout) -> ! { +pub fn error_handler(_: core::mem::Layout) -> ! { panic!(); } diff --git a/src/test/run-make/wasm-symbols-not-exported/bar.rs b/src/test/run-make/wasm-symbols-not-exported/bar.rs index 6ffbd3ec6900d..b7db3d638d70c 100644 --- a/src/test/run-make/wasm-symbols-not-exported/bar.rs +++ b/src/test/run-make/wasm-symbols-not-exported/bar.rs @@ -25,7 +25,7 @@ pub extern fn foo(a: u32) -> u32 { } #[alloc_error_handler] -fn a(_: core::alloc::Layout) -> ! { +fn a(_: core::mem::Layout) -> ! { loop {} } diff --git a/src/test/rustdoc/issue-54478-demo-allocator.rs b/src/test/rustdoc/issue-54478-demo-allocator.rs index 4811f363bc97a..c38a50b4b263d 100644 --- a/src/test/rustdoc/issue-54478-demo-allocator.rs +++ b/src/test/rustdoc/issue-54478-demo-allocator.rs @@ -18,6 +18,7 @@ //! //! ```rust //! use std::alloc::*; +//! use std::mem::Layout; //! //! #[global_allocator] //! static ALLOC: A = A; diff --git a/src/test/ui/alloc-error/alloc-error-handler-bad-signature-1.rs b/src/test/ui/alloc-error/alloc-error-handler-bad-signature-1.rs index 36041e4b8efde..caa628160084c 100644 --- a/src/test/ui/alloc-error/alloc-error-handler-bad-signature-1.rs +++ b/src/test/ui/alloc-error/alloc-error-handler-bad-signature-1.rs @@ -4,7 +4,7 @@ #![no_std] #![no_main] -use core::alloc::Layout; +use core::mem::Layout; #[alloc_error_handler] fn oom( diff --git a/src/test/ui/allocator/allocator-args.rs b/src/test/ui/allocator/allocator-args.rs index 1033f947c5f5b..1e01f6685fc90 100644 --- a/src/test/ui/allocator/allocator-args.rs +++ b/src/test/ui/allocator/allocator-args.rs @@ -1,4 +1,5 @@ -use std::alloc::{GlobalAlloc, Layout}; +use std::alloc::GlobalAlloc; +use std::mem::Layout; struct A; diff --git a/src/test/ui/allocator/allocator-args.stderr b/src/test/ui/allocator/allocator-args.stderr index dfff2a7e7094d..69ab56bb1e784 100644 --- a/src/test/ui/allocator/allocator-args.stderr +++ b/src/test/ui/allocator/allocator-args.stderr @@ -1,5 +1,5 @@ error: malformed `global_allocator` attribute input - --> $DIR/allocator-args.rs:10:1 + --> $DIR/allocator-args.rs:11:1 | LL | #[global_allocator(malloc)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: must be of the form: `#[global_allocator]` diff --git a/src/test/ui/allocator/auxiliary/custom.rs b/src/test/ui/allocator/auxiliary/custom.rs index b0ec9ab09299f..9a1ec1e0adf97 100644 --- a/src/test/ui/allocator/auxiliary/custom.rs +++ b/src/test/ui/allocator/auxiliary/custom.rs @@ -3,7 +3,8 @@ #![feature(allocator_api)] #![crate_type = "rlib"] -use std::alloc::{GlobalAlloc, System, Layout}; +use std::alloc::{GlobalAlloc, System}; +use std::mem::Layout; use std::sync::atomic::{AtomicUsize, Ordering}; pub struct A(pub AtomicUsize); diff --git a/src/test/ui/allocator/custom.rs b/src/test/ui/allocator/custom.rs index 184e4706a4c86..cb387a91f9706 100644 --- a/src/test/ui/allocator/custom.rs +++ b/src/test/ui/allocator/custom.rs @@ -7,7 +7,8 @@ extern crate helper; -use std::alloc::{self, AllocInit, AllocRef, Global, Layout, System}; +use std::alloc::{self, AllocInit, AllocRef, Global, System}; +use std::mem::Layout; use std::sync::atomic::{AtomicUsize, Ordering}; static HITS: AtomicUsize = AtomicUsize::new(0); diff --git a/src/test/ui/allocator/xcrate-use.rs b/src/test/ui/allocator/xcrate-use.rs index 7de1ab7a55315..331c6727cde15 100644 --- a/src/test/ui/allocator/xcrate-use.rs +++ b/src/test/ui/allocator/xcrate-use.rs @@ -9,7 +9,8 @@ extern crate custom; extern crate helper; -use std::alloc::{AllocInit, AllocRef, Global, Layout, System}; +use std::alloc::{AllocInit, AllocRef, Global, System}; +use std::mem::Layout; use std::sync::atomic::{AtomicUsize, Ordering}; #[global_allocator] diff --git a/src/test/ui/allocator/xcrate-use2.rs b/src/test/ui/allocator/xcrate-use2.rs index d8478fb5eaa41..f4966cad94b4e 100644 --- a/src/test/ui/allocator/xcrate-use2.rs +++ b/src/test/ui/allocator/xcrate-use2.rs @@ -11,7 +11,8 @@ extern crate custom; extern crate custom_as_global; extern crate helper; -use std::alloc::{alloc, dealloc, GlobalAlloc, System, Layout}; +use std::alloc::{alloc, dealloc, GlobalAlloc, System}; +use std::mem::Layout; use std::sync::atomic::{AtomicUsize, Ordering}; static GLOBAL: custom::A = custom::A(AtomicUsize::new(0)); diff --git a/src/test/ui/consts/std/alloc.rs b/src/test/ui/consts/std/alloc.rs index 65ac7e44926d0..95d34a2026698 100644 --- a/src/test/ui/consts/std/alloc.rs +++ b/src/test/ui/consts/std/alloc.rs @@ -1,4 +1,4 @@ -use std::alloc::Layout; +use std::mem::Layout; // ok const LAYOUT_VALID: Layout = unsafe { Layout::from_size_align_unchecked(0x1000, 0x08) }; diff --git a/src/test/ui/default-alloc-error-hook.rs b/src/test/ui/default-alloc-error-hook.rs index 40f61c2b7d5ba..677741bf558e1 100644 --- a/src/test/ui/default-alloc-error-hook.rs +++ b/src/test/ui/default-alloc-error-hook.rs @@ -3,8 +3,9 @@ // ignore-emscripten no processes // ignore-sgx no processes -use std::alloc::{Layout, handle_alloc_error}; +use std::alloc::{handle_alloc_error}; use std::env; +use std::mem::Layout; use std::process::Command; use std::str; diff --git a/src/test/ui/feature-gates/feature-gate-alloc-error-handler.rs b/src/test/ui/feature-gates/feature-gate-alloc-error-handler.rs index ad89096183080..9c348f85e17d0 100644 --- a/src/test/ui/feature-gates/feature-gate-alloc-error-handler.rs +++ b/src/test/ui/feature-gates/feature-gate-alloc-error-handler.rs @@ -3,7 +3,7 @@ #![no_std] #![no_main] -use core::alloc::Layout; +use core::mem::Layout; #[alloc_error_handler] //~ ERROR the `#[alloc_error_handler]` attribute is an experimental feature fn oom(info: Layout) -> ! { diff --git a/src/test/ui/missing/missing-alloc_error_handler.rs b/src/test/ui/missing/missing-alloc_error_handler.rs index ae0c067bb5f3e..da87801b7ce24 100644 --- a/src/test/ui/missing/missing-alloc_error_handler.rs +++ b/src/test/ui/missing/missing-alloc_error_handler.rs @@ -18,6 +18,6 @@ static A: MyAlloc = MyAlloc; struct MyAlloc; unsafe impl core::alloc::GlobalAlloc for MyAlloc { - unsafe fn alloc(&self, _: core::alloc::Layout) -> *mut u8 { 0 as _ } - unsafe fn dealloc(&self, _: *mut u8, _: core::alloc::Layout) {} + unsafe fn alloc(&self, _: core::mem::Layout) -> *mut u8 { 0 as _ } + unsafe fn dealloc(&self, _: *mut u8, _: core::mem::Layout) {} } diff --git a/src/test/ui/missing/missing-allocator.rs b/src/test/ui/missing/missing-allocator.rs index 6d867e2e8b48e..6db01bf566ef2 100644 --- a/src/test/ui/missing/missing-allocator.rs +++ b/src/test/ui/missing/missing-allocator.rs @@ -11,7 +11,7 @@ fn panic(_: &core::panic::PanicInfo) -> ! { } #[alloc_error_handler] -fn oom(_: core::alloc::Layout) -> ! { +fn oom(_: core::mem::Layout) -> ! { loop {} } diff --git a/src/test/ui/realloc-16687.rs b/src/test/ui/realloc-16687.rs index 0687a9ce454cc..cf6e7fdf0e667 100644 --- a/src/test/ui/realloc-16687.rs +++ b/src/test/ui/realloc-16687.rs @@ -6,7 +6,8 @@ #![feature(allocator_api)] -use std::alloc::{handle_alloc_error, AllocInit, AllocRef, Global, Layout, ReallocPlacement}; +use std::alloc::{handle_alloc_error, AllocInit, AllocRef, Global, ReallocPlacement}; +use std::mem::Layout; use std::ptr::{self, NonNull}; fn main() { diff --git a/src/test/ui/regions/regions-mock-codegen.rs b/src/test/ui/regions/regions-mock-codegen.rs index 380310190be01..147eef6c3532d 100644 --- a/src/test/ui/regions/regions-mock-codegen.rs +++ b/src/test/ui/regions/regions-mock-codegen.rs @@ -4,7 +4,8 @@ // pretty-expanded FIXME #23616 #![feature(allocator_api)] -use std::alloc::{handle_alloc_error, AllocInit, AllocRef, Global, Layout}; +use std::alloc::{handle_alloc_error, AllocInit, AllocRef, Global}; +use std::mem::Layout; use std::ptr::NonNull; struct arena(());