From ba8bd1b8efd114e0f2610760ff13173863e743de Mon Sep 17 00:00:00 2001 From: Ben Kimock Date: Fri, 21 Jun 2024 12:28:03 -0400 Subject: [PATCH 1/5] Polymorphize RawVec --- library/alloc/src/boxed.rs | 14 +- .../alloc/src/collections/vec_deque/mod.rs | 74 +++-- library/alloc/src/raw_vec.rs | 266 +++++++++--------- library/alloc/src/raw_vec/tests.rs | 98 ++++--- library/alloc/src/vec/in_place_drop.rs | 5 +- library/alloc/src/vec/into_iter.rs | 6 +- library/alloc/src/vec/mod.rs | 71 +++-- library/alloc/src/vec/spec_from_elem.rs | 20 +- .../alloc/src/vec/spec_from_iter_nested.rs | 4 +- library/alloc/src/vec/splice.rs | 3 +- library/core/src/mem/mod.rs | 5 + src/etc/gdb_providers.py | 7 + tests/debuginfo/strings-and-strs.rs | 2 +- .../inline_shims.drop.Inline.panic-abort.diff | 12 +- ...inline_shims.drop.Inline.panic-unwind.diff | 30 +- ..._to_slice.PreCodegen.after.panic-abort.mir | 63 ++--- ...to_slice.PreCodegen.after.panic-unwind.mir | 63 ++--- ...Vec_i32_.AddMovesForPackedDrops.before.mir | 14 +- 18 files changed, 420 insertions(+), 337 deletions(-) diff --git a/library/alloc/src/boxed.rs b/library/alloc/src/boxed.rs index f299aa0124dbe..84c1172ce3698 100644 --- a/library/alloc/src/boxed.rs +++ b/library/alloc/src/boxed.rs @@ -675,7 +675,7 @@ impl Box<[T]> { #[unstable(feature = "new_uninit", issue = "63291")] #[must_use] pub fn new_uninit_slice(len: usize) -> Box<[mem::MaybeUninit]> { - unsafe { RawVec::with_capacity(len).into_box(len) } + unsafe { RawVec::with_capacity(len, T::LAYOUT).into_box(len) } } /// Constructs a new boxed slice with uninitialized contents, with the memory @@ -700,7 +700,7 @@ impl Box<[T]> { #[unstable(feature = "new_uninit", issue = "63291")] #[must_use] pub fn new_zeroed_slice(len: usize) -> Box<[mem::MaybeUninit]> { - unsafe { RawVec::with_capacity_zeroed(len).into_box(len) } + unsafe { RawVec::with_capacity_zeroed(len, T::LAYOUT).into_box(len) } } /// Constructs a new boxed slice with uninitialized contents. Returns an error if @@ -727,7 +727,7 @@ impl Box<[T]> { #[inline] pub fn try_new_uninit_slice(len: usize) -> Result]>, AllocError> { let ptr = if T::IS_ZST || len == 0 { - NonNull::dangling() + NonNull::::dangling() } else { let layout = match Layout::array::>(len) { Ok(l) => l, @@ -761,7 +761,7 @@ impl Box<[T]> { #[inline] pub fn try_new_zeroed_slice(len: usize) -> Result]>, AllocError> { let ptr = if T::IS_ZST || len == 0 { - NonNull::dangling() + NonNull::::dangling() } else { let layout = match Layout::array::>(len) { Ok(l) => l, @@ -801,7 +801,7 @@ impl Box<[T], A> { // #[unstable(feature = "new_uninit", issue = "63291")] #[must_use] pub fn new_uninit_slice_in(len: usize, alloc: A) -> Box<[mem::MaybeUninit], A> { - unsafe { RawVec::with_capacity_in(len, alloc).into_box(len) } + unsafe { RawVec::with_capacity_in(len, alloc, T::LAYOUT).into_box(len) } } /// Constructs a new boxed slice with uninitialized contents in the provided allocator, @@ -829,7 +829,7 @@ impl Box<[T], A> { // #[unstable(feature = "new_uninit", issue = "63291")] #[must_use] pub fn new_zeroed_slice_in(len: usize, alloc: A) -> Box<[mem::MaybeUninit], A> { - unsafe { RawVec::with_capacity_zeroed_in(len, alloc).into_box(len) } + unsafe { RawVec::with_capacity_zeroed_in(len, alloc, T::LAYOUT).into_box(len) } } } @@ -1556,7 +1556,7 @@ impl BoxFromSlice for Box<[T]> { #[inline] fn from_slice(slice: &[T]) -> Self { let len = slice.len(); - let buf = RawVec::with_capacity(len); + let buf = RawVec::with_capacity(len, T::LAYOUT); unsafe { ptr::copy_nonoverlapping(slice.as_ptr(), buf.ptr(), len); buf.into_box(slice.len()).assume_init() diff --git a/library/alloc/src/collections/vec_deque/mod.rs b/library/alloc/src/collections/vec_deque/mod.rs index a07f250d7d88c..9baed577b8fd1 100644 --- a/library/alloc/src/collections/vec_deque/mod.rs +++ b/library/alloc/src/collections/vec_deque/mod.rs @@ -11,6 +11,7 @@ use core::cmp::{self, Ordering}; use core::fmt; use core::hash::{Hash, Hasher}; use core::iter::{repeat_n, repeat_with, ByRefSized}; +use core::marker::PhantomData; use core::mem::{ManuallyDrop, SizedTypeProperties}; use core::ops::{Index, IndexMut, Range, RangeBounds}; use core::ptr; @@ -102,7 +103,8 @@ pub struct VecDeque< // if `len == 0`, the exact value of `head` is unimportant. // if `T` is zero-Sized, then `self.len <= usize::MAX`, otherwise `self.len <= isize::MAX as usize`. len: usize, - buf: RawVec, + buf: RawVec, + _marker: PhantomData, } #[stable(feature = "rust1", since = "1.0.0")] @@ -126,11 +128,24 @@ impl Clone for VecDeque { #[stable(feature = "rust1", since = "1.0.0")] unsafe impl<#[may_dangle] T, A: Allocator> Drop for VecDeque { fn drop(&mut self) { + use core::alloc::Layout; + + struct Guard<'a, A: Allocator> { + buf: &'a mut RawVec, + layout: Layout, + } + + impl Drop for Guard<'_, A> { + fn drop(&mut self) { + self.buf.drop(self.layout); + } + } + /// Runs the destructor for all items in the slice when it gets dropped (normally or /// during unwinding). - struct Dropper<'a, T>(&'a mut [T]); + struct Dropper(*mut [T]); - impl<'a, T> Drop for Dropper<'a, T> { + impl Drop for Dropper { fn drop(&mut self) { unsafe { ptr::drop_in_place(self.0); @@ -139,12 +154,16 @@ unsafe impl<#[may_dangle] T, A: Allocator> Drop for VecDeque { } let (front, back) = self.as_mut_slices(); + let front = front as *mut [T]; + let back = back as *mut [T]; + + let _guard = Guard { buf: &mut self.buf, layout: T::LAYOUT }; + unsafe { let _back_dropper = Dropper(back); // use drop for [T] ptr::drop_in_place(front); } - // RawVec handles deallocation } } @@ -559,7 +578,7 @@ impl VecDeque { #[must_use] pub const fn new() -> VecDeque { // FIXME: This should just be `VecDeque::new_in(Global)` once that hits stable. - VecDeque { head: 0, len: 0, buf: RawVec::NEW } + VecDeque { head: 0, len: 0, buf: RawVec::new::(), _marker: PhantomData } } /// Creates an empty deque with space for at least `capacity` elements. @@ -599,7 +618,12 @@ impl VecDeque { #[inline] #[unstable(feature = "try_with_capacity", issue = "91913")] pub fn try_with_capacity(capacity: usize) -> Result, TryReserveError> { - Ok(VecDeque { head: 0, len: 0, buf: RawVec::try_with_capacity_in(capacity, Global)? }) + Ok(VecDeque { + head: 0, + len: 0, + buf: RawVec::try_with_capacity_in(capacity, Global, T::LAYOUT)?, + _marker: PhantomData, + }) } } @@ -616,7 +640,12 @@ impl VecDeque { #[inline] #[unstable(feature = "allocator_api", issue = "32838")] pub const fn new_in(alloc: A) -> VecDeque { - VecDeque { head: 0, len: 0, buf: RawVec::new_in(alloc) } + VecDeque { + head: 0, + len: 0, + buf: RawVec::new_in(alloc, core::mem::align_of::()), + _marker: PhantomData, + } } /// Creates an empty deque with space for at least `capacity` elements. @@ -630,7 +659,12 @@ impl VecDeque { /// ``` #[unstable(feature = "allocator_api", issue = "32838")] pub fn with_capacity_in(capacity: usize, alloc: A) -> VecDeque { - VecDeque { head: 0, len: 0, buf: RawVec::with_capacity_in(capacity, alloc) } + VecDeque { + head: 0, + len: 0, + buf: RawVec::with_capacity_in(capacity, alloc, T::LAYOUT), + _marker: PhantomData, + } } /// Creates a `VecDeque` from a raw allocation, when the initialized @@ -661,6 +695,7 @@ impl VecDeque { head: initialized.start, len: initialized.end.unchecked_sub(initialized.start), buf: RawVec::from_raw_parts_in(ptr, capacity, alloc), + _marker: PhantomData, } } } @@ -767,7 +802,7 @@ impl VecDeque { #[inline] #[stable(feature = "rust1", since = "1.0.0")] pub fn capacity(&self) -> usize { - if T::IS_ZST { usize::MAX } else { self.buf.capacity() } + self.buf.capacity(core::mem::size_of::()) } /// Reserves the minimum capacity for at least `additional` more elements to be inserted in the @@ -798,7 +833,7 @@ impl VecDeque { let old_cap = self.capacity(); if new_cap > old_cap { - self.buf.reserve_exact(self.len, additional); + self.buf.reserve_exact(self.len, additional, T::LAYOUT); unsafe { self.handle_capacity_increase(old_cap); } @@ -829,7 +864,7 @@ impl VecDeque { if new_cap > old_cap { // we don't need to reserve_exact(), as the size doesn't have // to be a power of 2. - self.buf.reserve(self.len, additional); + self.buf.reserve(self.len, additional, T::LAYOUT); unsafe { self.handle_capacity_increase(old_cap); } @@ -880,7 +915,7 @@ impl VecDeque { let old_cap = self.capacity(); if new_cap > old_cap { - self.buf.try_reserve_exact(self.len, additional)?; + self.buf.try_reserve_exact(self.len, additional, T::LAYOUT)?; unsafe { self.handle_capacity_increase(old_cap); } @@ -928,7 +963,7 @@ impl VecDeque { let old_cap = self.capacity(); if new_cap > old_cap { - self.buf.try_reserve(self.len, additional)?; + self.buf.try_reserve(self.len, additional, T::LAYOUT)?; unsafe { self.handle_capacity_increase(old_cap); } @@ -1070,7 +1105,7 @@ impl VecDeque { let guard = Guard { deque: self, old_head, target_cap }; - guard.deque.buf.shrink_to_fit(target_cap); + guard.deque.buf.shrink_to_fit(target_cap, T::LAYOUT); // Don't drop the guard if we didn't unwind. mem::forget(guard); @@ -2175,7 +2210,7 @@ impl VecDeque { // buffer without it being full emerge debug_assert!(self.is_full()); let old_cap = self.capacity(); - self.buf.grow_one(); + self.buf.grow_one(T::LAYOUT); unsafe { self.handle_capacity_increase(old_cap); } @@ -2980,7 +3015,12 @@ impl From> for VecDeque { #[inline] fn from(other: Vec) -> Self { let (ptr, len, cap, alloc) = other.into_raw_parts_with_alloc(); - Self { head: 0, len, buf: unsafe { RawVec::from_raw_parts_in(ptr, cap, alloc) } } + Self { + head: 0, + len, + buf: unsafe { RawVec::from_raw_parts_in(ptr, cap, alloc) }, + _marker: PhantomData, + } } } @@ -3020,7 +3060,7 @@ impl From> for Vec { unsafe { let other = ManuallyDrop::new(other); - let buf = other.buf.ptr(); + let buf: *mut T = other.buf.ptr(); let len = other.len(); let cap = other.capacity(); let alloc = ptr::read(other.allocator()); diff --git a/library/alloc/src/raw_vec.rs b/library/alloc/src/raw_vec.rs index 7b7dae5a057f0..f4b6c3c63fc1a 100644 --- a/library/alloc/src/raw_vec.rs +++ b/library/alloc/src/raw_vec.rs @@ -1,9 +1,8 @@ #![unstable(feature = "raw_vec_internals", reason = "unstable const warnings", issue = "none")] -use core::alloc::LayoutError; use core::cmp; use core::hint; -use core::mem::{self, ManuallyDrop, MaybeUninit, SizedTypeProperties}; +use core::mem::{ManuallyDrop, MaybeUninit, SizedTypeProperties}; use core::ptr::{self, NonNull, Unique}; #[cfg(not(no_global_oom_handling))] @@ -66,8 +65,8 @@ impl Cap { /// `usize::MAX`. This means that you need to be careful when round-tripping this type with a /// `Box<[T]>`, since `capacity()` won't yield the length. #[allow(missing_debug_implementations)] -pub(crate) struct RawVec { - ptr: Unique, +pub(crate) struct RawVec { + ptr: Unique, /// Never used for ZSTs; it's `capacity()`'s responsibility to return usize::MAX in that case. /// /// # Safety @@ -77,22 +76,16 @@ pub(crate) struct RawVec { alloc: A, } -impl RawVec { - /// HACK(Centril): This exists because stable `const fn` can only call stable `const fn`, so - /// they cannot call `Self::new()`. - /// - /// If you change `RawVec::new` or dependencies, please take care to not introduce anything - /// that would truly const-call something unstable. - pub const NEW: Self = Self::new(); - +impl RawVec { /// Creates the biggest possible `RawVec` (on the system heap) /// without allocating. If `T` has positive size, then this makes a /// `RawVec` with capacity `0`. If `T` is zero-sized, then it makes a /// `RawVec` with capacity `usize::MAX`. Useful for implementing /// delayed allocation. #[must_use] - pub const fn new() -> Self { - Self::new_in(Global) + #[rustc_const_stable(feature = "raw_vec_internals_const", since = "1.81")] + pub const fn new() -> Self { + Self::new_in(Global, core::mem::align_of::()) } /// Creates a `RawVec` (on the system heap) with exactly the @@ -113,8 +106,8 @@ impl RawVec { #[cfg(not(any(no_global_oom_handling, test)))] #[must_use] #[inline] - pub fn with_capacity(capacity: usize) -> Self { - match Self::try_allocate_in(capacity, AllocInit::Uninitialized, Global) { + pub fn with_capacity(capacity: usize, elem_layout: Layout) -> Self { + match Self::try_allocate_in(capacity, AllocInit::Uninitialized, Global, elem_layout) { Ok(res) => res, Err(err) => handle_error(err), } @@ -124,38 +117,43 @@ impl RawVec { #[cfg(not(any(no_global_oom_handling, test)))] #[must_use] #[inline] - pub fn with_capacity_zeroed(capacity: usize) -> Self { - Self::with_capacity_zeroed_in(capacity, Global) + pub fn with_capacity_zeroed(capacity: usize, elem_layout: Layout) -> Self { + Self::with_capacity_zeroed_in(capacity, Global, elem_layout) } } -impl RawVec { - // Tiny Vecs are dumb. Skip to: - // - 8 if the element size is 1, because any heap allocators is likely - // to round up a request of less than 8 bytes to at least 8 bytes. - // - 4 if elements are moderate-sized (<= 1 KiB). - // - 1 otherwise, to avoid wasting too much space for very short Vecs. - pub(crate) const MIN_NON_ZERO_CAP: usize = if mem::size_of::() == 1 { +// Tiny Vecs are dumb. Skip to: +// - 8 if the element size is 1, because any heap allocators is likely +// to round up a request of less than 8 bytes to at least 8 bytes. +// - 4 if elements are moderate-sized (<= 1 KiB). +// - 1 otherwise, to avoid wasting too much space for very short Vecs. +pub const fn min_non_zero_cap(size: usize) -> usize { + if size == 1 { 8 - } else if mem::size_of::() <= 1024 { + } else if size <= 1024 { 4 } else { 1 - }; + } +} +impl RawVec { /// Like `new`, but parameterized over the choice of allocator for /// the returned `RawVec`. - pub const fn new_in(alloc: A) -> Self { + #[inline] + #[rustc_const_stable(feature = "raw_vec_internals_const", since = "1.81")] + pub const fn new_in(alloc: A, align: usize) -> Self { + let ptr = unsafe { core::mem::transmute(align) }; // `cap: 0` means "unallocated". zero-sized types are ignored. - Self { ptr: Unique::dangling(), cap: Cap::ZERO, alloc } + Self { ptr, cap: Cap::ZERO, alloc } } /// Like `with_capacity`, but parameterized over the choice of /// allocator for the returned `RawVec`. #[cfg(not(no_global_oom_handling))] #[inline] - pub fn with_capacity_in(capacity: usize, alloc: A) -> Self { - match Self::try_allocate_in(capacity, AllocInit::Uninitialized, alloc) { + pub fn with_capacity_in(capacity: usize, alloc: A, elem_layout: Layout) -> Self { + match Self::try_allocate_in(capacity, AllocInit::Uninitialized, alloc, elem_layout) { Ok(res) => res, Err(err) => handle_error(err), } @@ -164,16 +162,20 @@ impl RawVec { /// Like `try_with_capacity`, but parameterized over the choice of /// allocator for the returned `RawVec`. #[inline] - pub fn try_with_capacity_in(capacity: usize, alloc: A) -> Result { - Self::try_allocate_in(capacity, AllocInit::Uninitialized, alloc) + pub fn try_with_capacity_in( + capacity: usize, + alloc: A, + elem_layout: Layout, + ) -> Result { + Self::try_allocate_in(capacity, AllocInit::Uninitialized, alloc, elem_layout) } /// Like `with_capacity_zeroed`, but parameterized over the choice /// of allocator for the returned `RawVec`. #[cfg(not(no_global_oom_handling))] #[inline] - pub fn with_capacity_zeroed_in(capacity: usize, alloc: A) -> Self { - match Self::try_allocate_in(capacity, AllocInit::Zeroed, alloc) { + pub fn with_capacity_zeroed_in(capacity: usize, alloc: A, elem_layout: Layout) -> Self { + match Self::try_allocate_in(capacity, AllocInit::Zeroed, alloc, elem_layout) { Ok(res) => res, Err(err) => handle_error(err), } @@ -191,10 +193,10 @@ impl RawVec { /// /// Note, that the requested capacity and `self.capacity()` could differ, as /// an allocator could overallocate and return a greater memory block than requested. - pub unsafe fn into_box(self, len: usize) -> Box<[MaybeUninit], A> { + pub unsafe fn into_box(self, len: usize) -> Box<[MaybeUninit], A> { // Sanity-check one half of the safety requirement (we cannot check the other half). debug_assert!( - len <= self.capacity(), + len <= self.capacity(core::mem::size_of::()), "`len` must be smaller than or equal to `self.capacity()`" ); @@ -209,15 +211,15 @@ impl RawVec { capacity: usize, init: AllocInit, alloc: A, + elem_layout: Layout, ) -> Result { // Don't allocate here because `Drop` will not deallocate when `capacity` is 0. - - if T::IS_ZST || capacity == 0 { - Ok(Self::new_in(alloc)) + if elem_layout.size() == 0 || capacity == 0 { + Ok(Self::new_in(alloc, elem_layout.align())) } else { // We avoid `unwrap_or_else` here because it bloats the amount of // LLVM IR generated. - let layout = match Layout::array::(capacity) { + let layout = match layout_array(capacity, elem_layout) { Ok(layout) => layout, Err(_) => return Err(CapacityOverflow.into()), }; @@ -254,9 +256,9 @@ impl RawVec { /// If the `ptr` and `capacity` come from a `RawVec` created via `alloc`, then this is /// guaranteed. #[inline] - pub unsafe fn from_raw_parts_in(ptr: *mut T, capacity: usize, alloc: A) -> Self { + pub unsafe fn from_raw_parts_in(ptr: *mut T, capacity: usize, alloc: A) -> Self { let cap = if T::IS_ZST { Cap::ZERO } else { unsafe { Cap(capacity) } }; - Self { ptr: unsafe { Unique::new_unchecked(ptr) }, cap, alloc } + Self { ptr: unsafe { Unique::new_unchecked(ptr.cast()) }, cap, alloc } } /// A convenience method for hoisting the non-null precondition out of [`RawVec::from_raw_parts_in`]. @@ -265,51 +267,51 @@ impl RawVec { /// /// See [`RawVec::from_raw_parts_in`]. #[inline] - pub(crate) unsafe fn from_nonnull_in(ptr: NonNull, capacity: usize, alloc: A) -> Self { + pub unsafe fn from_nonnull_in(ptr: NonNull, capacity: usize, alloc: A) -> Self { let cap = if T::IS_ZST { Cap::ZERO } else { unsafe { Cap(capacity) } }; - Self { ptr: Unique::from(ptr), cap, alloc } + Self { ptr: Unique::from(ptr.cast()), cap, alloc } } /// Gets a raw pointer to the start of the allocation. Note that this is /// `Unique::dangling()` if `capacity == 0` or `T` is zero-sized. In the former case, you must /// be careful. #[inline] - pub fn ptr(&self) -> *mut T { - self.ptr.as_ptr() + pub fn ptr(&self) -> *mut T { + unsafe { core::mem::transmute(self.ptr) } } #[inline] - pub fn non_null(&self) -> NonNull { - NonNull::from(self.ptr) + pub fn non_null(&self) -> NonNull { + unsafe { core::mem::transmute(self.ptr) } } /// Gets the capacity of the allocation. /// /// This will always be `usize::MAX` if `T` is zero-sized. - #[inline(always)] - pub fn capacity(&self) -> usize { - if T::IS_ZST { usize::MAX } else { self.cap.0 } + #[inline] + pub fn capacity(&self, elem_size: usize) -> usize { + if elem_size == 0 { usize::MAX } else { self.cap.0 } } /// Returns a shared reference to the allocator backing this `RawVec`. + #[inline] pub fn allocator(&self) -> &A { &self.alloc } - fn current_memory(&self) -> Option<(NonNull, Layout)> { - if T::IS_ZST || self.cap.0 == 0 { + #[inline] + fn current_memory(&self, elem_layout: Layout) -> Option<(NonNull, Layout)> { + if elem_layout.size() == 0 || self.cap.0 == 0 { None } else { // We could use Layout::array here which ensures the absence of isize and usize overflows // and could hypothetically handle differences between stride and size, but this memory // has already been allocated so we know it can't overflow and currently Rust does not // support such types. So we can do better by skipping some checks and avoid an unwrap. - const { assert!(mem::size_of::() % mem::align_of::() == 0) }; unsafe { - let align = mem::align_of::(); - let size = mem::size_of::().unchecked_mul(self.cap.0); - let layout = Layout::from_size_align_unchecked(size, align); - Some((self.ptr.cast().into(), layout)) + let alloc_size = elem_layout.size().unchecked_mul(self.cap.0); + let layout = Layout::from_size_align_unchecked(alloc_size, elem_layout.align()); + Some((self.ptr.into(), layout)) } } } @@ -335,24 +337,25 @@ impl RawVec { /// Aborts on OOM. #[cfg(not(no_global_oom_handling))] #[inline] - pub fn reserve(&mut self, len: usize, additional: usize) { + pub fn reserve(&mut self, len: usize, additional: usize, elem_layout: Layout) { // Callers expect this function to be very cheap when there is already sufficient capacity. // Therefore, we move all the resizing and error-handling logic from grow_amortized and // handle_reserve behind a call, while making sure that this function is likely to be // inlined as just a comparison and a call if the comparison fails. #[cold] - fn do_reserve_and_handle( - slf: &mut RawVec, + fn do_reserve_and_handle( + slf: &mut RawVec, len: usize, additional: usize, + elem_layout: Layout, ) { - if let Err(err) = slf.grow_amortized(len, additional) { + if let Err(err) = slf.grow_amortized(len, additional, elem_layout) { handle_error(err); } } - if self.needs_to_grow(len, additional) { - do_reserve_and_handle(self, len, additional); + if self.needs_to_grow(len, additional, elem_layout) { + do_reserve_and_handle(self, len, additional, elem_layout); } } @@ -360,20 +363,25 @@ impl RawVec { /// caller to ensure `len == self.capacity()`. #[cfg(not(no_global_oom_handling))] #[inline(never)] - pub fn grow_one(&mut self) { - if let Err(err) = self.grow_amortized(self.cap.0, 1) { + pub fn grow_one(&mut self, elem_layout: Layout) { + if let Err(err) = self.grow_amortized(self.cap.0, 1, elem_layout) { handle_error(err); } } /// The same as `reserve`, but returns on errors instead of panicking or aborting. - pub fn try_reserve(&mut self, len: usize, additional: usize) -> Result<(), TryReserveError> { - if self.needs_to_grow(len, additional) { - self.grow_amortized(len, additional)?; + pub fn try_reserve( + &mut self, + len: usize, + additional: usize, + elem_layout: Layout, + ) -> Result<(), TryReserveError> { + if self.needs_to_grow(len, additional, elem_layout) { + self.grow_amortized(len, additional, elem_layout)?; } unsafe { // Inform the optimizer that the reservation has succeeded or wasn't needed - hint::assert_unchecked(!self.needs_to_grow(len, additional)); + hint::assert_unchecked(!self.needs_to_grow(len, additional, elem_layout)); } Ok(()) } @@ -396,8 +404,8 @@ impl RawVec { /// /// Aborts on OOM. #[cfg(not(no_global_oom_handling))] - pub fn reserve_exact(&mut self, len: usize, additional: usize) { - if let Err(err) = self.try_reserve_exact(len, additional) { + pub fn reserve_exact(&mut self, len: usize, additional: usize, elem_layout: Layout) { + if let Err(err) = self.try_reserve_exact(len, additional, elem_layout) { handle_error(err); } } @@ -407,13 +415,14 @@ impl RawVec { &mut self, len: usize, additional: usize, + elem_layout: Layout, ) -> Result<(), TryReserveError> { - if self.needs_to_grow(len, additional) { - self.grow_exact(len, additional)?; + if self.needs_to_grow(len, additional, elem_layout) { + self.grow_exact(len, additional, elem_layout)?; } unsafe { // Inform the optimizer that the reservation has succeeded or wasn't needed - hint::assert_unchecked(!self.needs_to_grow(len, additional)); + hint::assert_unchecked(!self.needs_to_grow(len, additional, elem_layout)); } Ok(()) } @@ -430,23 +439,23 @@ impl RawVec { /// Aborts on OOM. #[cfg(not(no_global_oom_handling))] #[inline] - pub fn shrink_to_fit(&mut self, cap: usize) { - if let Err(err) = self.shrink(cap) { + pub fn shrink_to_fit(&mut self, cap: usize, elem_layout: Layout) { + if let Err(err) = self.shrink(cap, elem_layout) { handle_error(err); } } -} -impl RawVec { /// Returns if the buffer needs to grow to fulfill the needed extra capacity. /// Mainly used to make inlining reserve-calls possible without inlining `grow`. - fn needs_to_grow(&self, len: usize, additional: usize) -> bool { - additional > self.capacity().wrapping_sub(len) + #[inline] + fn needs_to_grow(&self, len: usize, additional: usize, elem_layout: Layout) -> bool { + additional > self.capacity(elem_layout.size()).wrapping_sub(len) } /// # Safety: /// /// `cap` must not exceed `isize::MAX`. + #[inline] unsafe fn set_ptr_and_cap(&mut self, ptr: NonNull<[u8]>, cap: usize) { // Allocators currently return a `NonNull<[u8]>` whose length matches // the size requested. If that ever changes, the capacity here should @@ -455,18 +464,16 @@ impl RawVec { self.cap = unsafe { Cap(cap) }; } - // This method is usually instantiated many times. So we want it to be as - // small as possible, to improve compile times. But we also want as much of - // its contents to be statically computable as possible, to make the - // generated code run faster. Therefore, this method is carefully written - // so that all of the code that depends on `T` is within it, while as much - // of the code that doesn't depend on `T` as possible is in functions that - // are non-generic over `T`. - fn grow_amortized(&mut self, len: usize, additional: usize) -> Result<(), TryReserveError> { + fn grow_amortized( + &mut self, + len: usize, + additional: usize, + elem_layout: Layout, + ) -> Result<(), TryReserveError> { // This is ensured by the calling contexts. debug_assert!(additional > 0); - if T::IS_ZST { + if elem_layout.size() == 0 { // Since we return a capacity of `usize::MAX` when `elem_size` is // 0, getting to here necessarily means the `RawVec` is overfull. return Err(CapacityOverflow.into()); @@ -478,32 +485,32 @@ impl RawVec { // This guarantees exponential growth. The doubling cannot overflow // because `cap <= isize::MAX` and the type of `cap` is `usize`. let cap = cmp::max(self.cap.0 * 2, required_cap); - let cap = cmp::max(Self::MIN_NON_ZERO_CAP, cap); + let cap = cmp::max(min_non_zero_cap(elem_layout.size()), cap); - let new_layout = Layout::array::(cap); + let new_layout = layout_array(cap, elem_layout)?; - // `finish_grow` is non-generic over `T`. - let ptr = finish_grow(new_layout, self.current_memory(), &mut self.alloc)?; + let ptr = finish_grow(new_layout, self.current_memory(elem_layout), &mut self.alloc)?; // SAFETY: finish_grow would have resulted in a capacity overflow if we tried to allocate more than isize::MAX items unsafe { self.set_ptr_and_cap(ptr, cap) }; Ok(()) } - // The constraints on this method are much the same as those on - // `grow_amortized`, but this method is usually instantiated less often so - // it's less critical. - fn grow_exact(&mut self, len: usize, additional: usize) -> Result<(), TryReserveError> { - if T::IS_ZST { + fn grow_exact( + &mut self, + len: usize, + additional: usize, + elem_layout: Layout, + ) -> Result<(), TryReserveError> { + if elem_layout.size() == 0 { // Since we return a capacity of `usize::MAX` when the type size is // 0, getting to here necessarily means the `RawVec` is overfull. return Err(CapacityOverflow.into()); } let cap = len.checked_add(additional).ok_or(CapacityOverflow)?; - let new_layout = Layout::array::(cap); + let new_layout = layout_array(cap, elem_layout)?; - // `finish_grow` is non-generic over `T`. - let ptr = finish_grow(new_layout, self.current_memory(), &mut self.alloc)?; + let ptr = finish_grow(new_layout, self.current_memory(elem_layout), &mut self.alloc)?; // SAFETY: finish_grow would have resulted in a capacity overflow if we tried to allocate more than isize::MAX items unsafe { self.set_ptr_and_cap(ptr, cap); @@ -513,10 +520,10 @@ impl RawVec { #[cfg(not(no_global_oom_handling))] #[inline] - fn shrink(&mut self, cap: usize) -> Result<(), TryReserveError> { - assert!(cap <= self.capacity(), "Tried to shrink to a larger capacity"); + fn shrink(&mut self, cap: usize, elem_layout: Layout) -> Result<(), TryReserveError> { + assert!(cap <= self.capacity(elem_layout.size()), "Tried to shrink to a larger capacity"); // SAFETY: Just checked this isn't trying to grow - unsafe { self.shrink_unchecked(cap) } + unsafe { self.shrink_unchecked(cap, elem_layout) } } /// `shrink`, but without the capacity check. @@ -530,23 +537,27 @@ impl RawVec { /// # Safety /// `cap <= self.capacity()` #[cfg(not(no_global_oom_handling))] - unsafe fn shrink_unchecked(&mut self, cap: usize) -> Result<(), TryReserveError> { - let (ptr, layout) = if let Some(mem) = self.current_memory() { mem } else { return Ok(()) }; - // See current_memory() why this assert is here - const { assert!(mem::size_of::() % mem::align_of::() == 0) }; + unsafe fn shrink_unchecked( + &mut self, + cap: usize, + elem_layout: Layout, + ) -> Result<(), TryReserveError> { + let (ptr, layout) = + if let Some(mem) = self.current_memory(elem_layout) { mem } else { return Ok(()) }; // If shrinking to 0, deallocate the buffer. We don't reach this point // for the T::IS_ZST case since current_memory() will have returned // None. if cap == 0 { unsafe { self.alloc.deallocate(ptr, layout) }; - self.ptr = Unique::dangling(); + self.ptr = + unsafe { Unique::new_unchecked(ptr::without_provenance_mut(elem_layout.align())) }; self.cap = Cap::ZERO; } else { let ptr = unsafe { - // `Layout::array` cannot overflow here because it would have + // Layout cannot overflow here because it would have // overflowed earlier when capacity was larger. - let new_size = mem::size_of::().unchecked_mul(cap); + let new_size = elem_layout.size().unchecked_mul(cap); let new_layout = Layout::from_size_align_unchecked(new_size, layout.align()); self.alloc .shrink(ptr, layout, new_layout) @@ -559,24 +570,23 @@ impl RawVec { } Ok(()) } + + pub fn drop(&mut self, elem_layout: Layout) { + if let Some((ptr, layout)) = self.current_memory(elem_layout) { + unsafe { self.alloc.deallocate(ptr, layout) } + } + } } -// This function is outside `RawVec` to minimize compile times. See the comment -// above `RawVec::grow_amortized` for details. (The `A` parameter isn't -// significant, because the number of different `A` types seen in practice is -// much smaller than the number of `T` types.) #[inline(never)] fn finish_grow( - new_layout: Result, + new_layout: Layout, current_memory: Option<(NonNull, Layout)>, alloc: &mut A, ) -> Result, TryReserveError> where A: Allocator, { - // Check for the error here to minimize the size of `RawVec::grow_*`. - let new_layout = new_layout.map_err(|_| CapacityOverflow)?; - alloc_guard(new_layout.size())?; let memory = if let Some((ptr, old_layout)) = current_memory { @@ -593,15 +603,6 @@ where memory.map_err(|_| AllocError { layout: new_layout, non_exhaustive: () }.into()) } -unsafe impl<#[may_dangle] T, A: Allocator> Drop for RawVec { - /// Frees the memory owned by the `RawVec` *without* trying to drop its contents. - fn drop(&mut self) { - if let Some((ptr, layout)) = self.current_memory() { - unsafe { self.alloc.deallocate(ptr, layout) } - } - } -} - // Central function for reserve error handling. #[cfg(not(no_global_oom_handling))] #[cold] @@ -628,3 +629,8 @@ fn alloc_guard(alloc_size: usize) -> Result<(), TryReserveError> { Ok(()) } } + +#[inline] +fn layout_array(cap: usize, elem_layout: Layout) -> Result { + elem_layout.repeat(cap).map(|(layout, _pad)| layout).map_err(|_| CapacityOverflow.into()) +} diff --git a/library/alloc/src/raw_vec/tests.rs b/library/alloc/src/raw_vec/tests.rs index 4194be530612d..8852fc6cdfcf0 100644 --- a/library/alloc/src/raw_vec/tests.rs +++ b/library/alloc/src/raw_vec/tests.rs @@ -41,133 +41,129 @@ fn allocator_param() { } let a = BoundedAlloc { fuel: Cell::new(500) }; - let mut v: RawVec = RawVec::with_capacity_in(50, a); + let mut v = RawVec::with_capacity_in(50, a, u8::LAYOUT); assert_eq!(v.alloc.fuel.get(), 450); - v.reserve(50, 150); // (causes a realloc, thus using 50 + 150 = 200 units of fuel) + v.reserve(50, 150, u8::LAYOUT); // (causes a realloc, thus using 50 + 150 = 200 units of fuel) assert_eq!(v.alloc.fuel.get(), 250); + v.drop(u8::LAYOUT); } #[test] fn reserve_does_not_overallocate() { { - let mut v: RawVec = RawVec::new(); + let mut v = RawVec::new::(); // First, `reserve` allocates like `reserve_exact`. - v.reserve(0, 9); - assert_eq!(9, v.capacity()); + v.reserve(0, 9, u32::LAYOUT); + assert_eq!(9, v.capacity(u32::LAYOUT.size())); + v.drop(u32::LAYOUT); } { - let mut v: RawVec = RawVec::new(); - v.reserve(0, 7); - assert_eq!(7, v.capacity()); + let mut v = RawVec::new::(); + v.reserve(0, 7, u32::LAYOUT); + assert_eq!(7, v.capacity(u32::LAYOUT.size())); // 97 is more than double of 7, so `reserve` should work // like `reserve_exact`. - v.reserve(7, 90); - assert_eq!(97, v.capacity()); + v.reserve(7, 90, u32::LAYOUT); + assert_eq!(97, v.capacity(u32::LAYOUT.size())); + v.drop(u32::LAYOUT); } { - let mut v: RawVec = RawVec::new(); - v.reserve(0, 12); - assert_eq!(12, v.capacity()); - v.reserve(12, 3); + let mut v = RawVec::new::(); + v.reserve(0, 12, u32::LAYOUT); + assert_eq!(12, v.capacity(u32::LAYOUT.size())); + v.reserve(12, 3, u32::LAYOUT); // 3 is less than half of 12, so `reserve` must grow // exponentially. At the time of writing this test grow // factor is 2, so new capacity is 24, however, grow factor // of 1.5 is OK too. Hence `>= 18` in assert. - assert!(v.capacity() >= 12 + 12 / 2); + assert!(v.capacity(u32::LAYOUT.size()) >= 12 + 12 / 2); + v.drop(u32::LAYOUT); } } struct ZST; // A `RawVec` holding zero-sized elements should always look like this. -fn zst_sanity(v: &RawVec) { - assert_eq!(v.capacity(), usize::MAX); - assert_eq!(v.ptr(), core::ptr::Unique::::dangling().as_ptr()); - assert_eq!(v.current_memory(), None); +fn zst_sanity(v: &RawVec) { + assert_eq!(v.capacity(ZST::LAYOUT.size()), usize::MAX); + assert_eq!(v.ptr::(), core::ptr::Unique::::dangling().as_ptr()); + assert_eq!(v.current_memory(ZST::LAYOUT), None); } #[test] fn zst() { let cap_err = Err(crate::collections::TryReserveErrorKind::CapacityOverflow.into()); - assert_eq!(std::mem::size_of::(), 0); + assert_eq!(size_of::(), 0); // All these different ways of creating the RawVec produce the same thing. - let v: RawVec = RawVec::new(); + let v = RawVec::new::(); zst_sanity(&v); - let v: RawVec = RawVec::with_capacity_in(100, Global); + let v = RawVec::with_capacity_in(100, Global, ZST::LAYOUT); zst_sanity(&v); - let v: RawVec = RawVec::with_capacity_in(100, Global); + let v = RawVec::with_capacity_in(100, Global, ZST::LAYOUT); zst_sanity(&v); - let v: RawVec = RawVec::try_allocate_in(0, AllocInit::Uninitialized, Global).unwrap(); + let v = RawVec::try_allocate_in(0, AllocInit::Uninitialized, Global, ZST::LAYOUT).unwrap(); zst_sanity(&v); - let v: RawVec = RawVec::try_allocate_in(100, AllocInit::Uninitialized, Global).unwrap(); + let v = RawVec::try_allocate_in(100, AllocInit::Uninitialized, Global, ZST::LAYOUT).unwrap(); zst_sanity(&v); - let mut v: RawVec = - RawVec::try_allocate_in(usize::MAX, AllocInit::Uninitialized, Global).unwrap(); + let mut v = + RawVec::try_allocate_in(usize::MAX, AllocInit::Uninitialized, Global, ZST::LAYOUT).unwrap(); zst_sanity(&v); // Check all these operations work as expected with zero-sized elements. - assert!(!v.needs_to_grow(100, usize::MAX - 100)); - assert!(v.needs_to_grow(101, usize::MAX - 100)); + assert!(!v.needs_to_grow(100, usize::MAX - 100, ZST::LAYOUT)); + assert!(v.needs_to_grow(101, usize::MAX - 100, ZST::LAYOUT)); zst_sanity(&v); - v.reserve(100, usize::MAX - 100); + v.reserve(100, usize::MAX - 100, ZST::LAYOUT); //v.reserve(101, usize::MAX - 100); // panics, in `zst_reserve_panic` below zst_sanity(&v); - v.reserve_exact(100, usize::MAX - 100); + v.reserve_exact(100, usize::MAX - 100, ZST::LAYOUT); //v.reserve_exact(101, usize::MAX - 100); // panics, in `zst_reserve_exact_panic` below zst_sanity(&v); - assert_eq!(v.try_reserve(100, usize::MAX - 100), Ok(())); - assert_eq!(v.try_reserve(101, usize::MAX - 100), cap_err); + assert_eq!(v.try_reserve(100, usize::MAX - 100, ZST::LAYOUT), Ok(())); + assert_eq!(v.try_reserve(101, usize::MAX - 100, ZST::LAYOUT), cap_err); zst_sanity(&v); - assert_eq!(v.try_reserve_exact(100, usize::MAX - 100), Ok(())); - assert_eq!(v.try_reserve_exact(101, usize::MAX - 100), cap_err); + assert_eq!(v.try_reserve_exact(100, usize::MAX - 100, ZST::LAYOUT), Ok(())); + assert_eq!(v.try_reserve_exact(101, usize::MAX - 100, ZST::LAYOUT), cap_err); zst_sanity(&v); - assert_eq!(v.grow_amortized(100, usize::MAX - 100), cap_err); - assert_eq!(v.grow_amortized(101, usize::MAX - 100), cap_err); + assert_eq!(v.grow_amortized(100, usize::MAX - 100, ZST::LAYOUT), cap_err); + assert_eq!(v.grow_amortized(101, usize::MAX - 100, ZST::LAYOUT), cap_err); zst_sanity(&v); - assert_eq!(v.grow_exact(100, usize::MAX - 100), cap_err); - assert_eq!(v.grow_exact(101, usize::MAX - 100), cap_err); + assert_eq!(v.grow_exact(100, usize::MAX - 100, ZST::LAYOUT), cap_err); + assert_eq!(v.grow_exact(101, usize::MAX - 100, ZST::LAYOUT), cap_err); zst_sanity(&v); } #[test] #[should_panic(expected = "capacity overflow")] fn zst_reserve_panic() { - let mut v: RawVec = RawVec::new(); + let mut v: RawVec = RawVec::new::(); zst_sanity(&v); - v.reserve(101, usize::MAX - 100); + v.reserve(101, usize::MAX - 100, ZST::LAYOUT); } #[test] #[should_panic(expected = "capacity overflow")] fn zst_reserve_exact_panic() { - let mut v: RawVec = RawVec::new(); + let mut v: RawVec = RawVec::new::(); zst_sanity(&v); - v.reserve_exact(101, usize::MAX - 100); -} - -#[test] -fn niches() { - let baseline = size_of::>(); - assert_eq!(size_of::>>(), baseline); - assert_eq!(size_of::>>>(), baseline); - assert_eq!(size_of::>>>>(), baseline); + v.reserve_exact(101, usize::MAX - 100, ZST::LAYOUT); } diff --git a/library/alloc/src/vec/in_place_drop.rs b/library/alloc/src/vec/in_place_drop.rs index 4050c250130bb..4d62d0949fd6a 100644 --- a/library/alloc/src/vec/in_place_drop.rs +++ b/library/alloc/src/vec/in_place_drop.rs @@ -1,4 +1,5 @@ use core::marker::PhantomData; +use core::mem::SizedTypeProperties; use core::ptr::NonNull; use core::ptr::{self, drop_in_place}; use core::slice::{self}; @@ -42,9 +43,9 @@ impl Drop for InPlaceDstDataSrcBufDrop { #[inline] fn drop(&mut self) { unsafe { - let _drop_allocation = - RawVec::::from_nonnull_in(self.ptr.cast::(), self.src_cap, Global); + let mut buf = RawVec::from_nonnull_in(self.ptr.cast::(), self.src_cap, Global); drop_in_place(core::ptr::slice_from_raw_parts_mut::(self.ptr.as_ptr(), self.len)); + buf.drop(Src::LAYOUT); }; } } diff --git a/library/alloc/src/vec/into_iter.rs b/library/alloc/src/vec/into_iter.rs index 10f62e4bb62d8..dfd1d8898f857 100644 --- a/library/alloc/src/vec/into_iter.rs +++ b/library/alloc/src/vec/into_iter.rs @@ -141,7 +141,7 @@ impl IntoIter { // struct and then overwriting &mut self. // this creates less assembly self.cap = 0; - self.buf = RawVec::NEW.non_null(); + self.buf = RawVec::new::().non_null(); self.ptr = self.buf; self.end = self.buf.as_ptr(); @@ -491,8 +491,8 @@ unsafe impl<#[may_dangle] T, A: Allocator> Drop for IntoIter { unsafe { // `IntoIter::alloc` is not used anymore after this and will be dropped by RawVec let alloc = ManuallyDrop::take(&mut self.0.alloc); - // RawVec handles deallocation - let _ = RawVec::from_nonnull_in(self.0.buf, self.0.cap, alloc); + let mut buf = RawVec::from_nonnull_in(self.0.buf, self.0.cap, alloc); + buf.drop(T::LAYOUT); } } } diff --git a/library/alloc/src/vec/mod.rs b/library/alloc/src/vec/mod.rs index 729d5dd4fe4d2..c649578b02a7b 100644 --- a/library/alloc/src/vec/mod.rs +++ b/library/alloc/src/vec/mod.rs @@ -396,8 +396,9 @@ mod spec_extend; #[cfg_attr(not(test), rustc_diagnostic_item = "Vec")] #[rustc_insignificant_dtor] pub struct Vec { - buf: RawVec, + buf: RawVec, len: usize, + _marker: PhantomData, } //////////////////////////////////////////////////////////////////////////////// @@ -420,7 +421,7 @@ impl Vec { #[stable(feature = "rust1", since = "1.0.0")] #[must_use] pub const fn new() -> Self { - Vec { buf: RawVec::NEW, len: 0 } + Vec { buf: RawVec::new::(), len: 0, _marker: PhantomData } } /// Constructs a new, empty `Vec` with at least the specified capacity. @@ -634,7 +635,7 @@ impl Vec { #[inline] #[unstable(feature = "allocator_api", issue = "32838")] pub const fn new_in(alloc: A) -> Self { - Vec { buf: RawVec::new_in(alloc), len: 0 } + Vec { buf: RawVec::new_in(alloc, core::mem::align_of::()), len: 0, _marker: PhantomData } } /// Constructs a new, empty `Vec` with at least the specified capacity @@ -696,7 +697,11 @@ impl Vec { #[inline] #[unstable(feature = "allocator_api", issue = "32838")] pub fn with_capacity_in(capacity: usize, alloc: A) -> Self { - Vec { buf: RawVec::with_capacity_in(capacity, alloc), len: 0 } + Vec { + buf: RawVec::with_capacity_in(capacity, alloc, T::LAYOUT), + len: 0, + _marker: PhantomData, + } } /// Constructs a new, empty `Vec` with at least the specified capacity @@ -714,7 +719,11 @@ impl Vec { #[unstable(feature = "allocator_api", issue = "32838")] // #[unstable(feature = "try_with_capacity", issue = "91913")] pub fn try_with_capacity_in(capacity: usize, alloc: A) -> Result { - Ok(Vec { buf: RawVec::try_with_capacity_in(capacity, alloc)?, len: 0 }) + Ok(Vec { + buf: RawVec::try_with_capacity_in(capacity, alloc, T::LAYOUT)?, + len: 0, + _marker: PhantomData, + }) } /// Creates a `Vec` directly from a pointer, a length, a capacity, @@ -828,7 +837,13 @@ impl Vec { #[inline] #[unstable(feature = "allocator_api", issue = "32838")] pub unsafe fn from_raw_parts_in(ptr: *mut T, length: usize, capacity: usize, alloc: A) -> Self { - unsafe { Vec { buf: RawVec::from_raw_parts_in(ptr, capacity, alloc), len: length } } + unsafe { + Vec { + buf: RawVec::from_raw_parts_in(ptr, capacity, alloc), + len: length, + _marker: PhantomData, + } + } } /// A convenience method for hoisting the non-null precondition out of [`Vec::from_raw_parts_in`]. @@ -844,7 +859,13 @@ impl Vec { capacity: usize, alloc: A, ) -> Self { - unsafe { Vec { buf: RawVec::from_nonnull_in(ptr, capacity, alloc), len: length } } + unsafe { + Vec { + buf: RawVec::from_nonnull_in(ptr, capacity, alloc), + len: length, + _marker: PhantomData, + } + } } /// Decomposes a `Vec` into its raw components: `(pointer, length, capacity)`. @@ -946,7 +967,7 @@ impl Vec { #[inline] #[stable(feature = "rust1", since = "1.0.0")] pub fn capacity(&self) -> usize { - self.buf.capacity() + self.buf.capacity(core::mem::size_of::()) } /// Reserves capacity for at least `additional` more elements to be inserted @@ -969,7 +990,7 @@ impl Vec { #[cfg(not(no_global_oom_handling))] #[stable(feature = "rust1", since = "1.0.0")] pub fn reserve(&mut self, additional: usize) { - self.buf.reserve(self.len, additional); + self.buf.reserve(self.len, additional, T::LAYOUT); } /// Reserves the minimum capacity for at least `additional` more elements to @@ -999,7 +1020,7 @@ impl Vec { #[cfg(not(no_global_oom_handling))] #[stable(feature = "rust1", since = "1.0.0")] pub fn reserve_exact(&mut self, additional: usize) { - self.buf.reserve_exact(self.len, additional); + self.buf.reserve_exact(self.len, additional, T::LAYOUT); } /// Tries to reserve capacity for at least `additional` more elements to be inserted @@ -1036,7 +1057,7 @@ impl Vec { /// ``` #[stable(feature = "try_reserve", since = "1.57.0")] pub fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError> { - self.buf.try_reserve(self.len, additional) + self.buf.try_reserve(self.len, additional, T::LAYOUT) } /// Tries to reserve the minimum capacity for at least `additional` @@ -1079,7 +1100,7 @@ impl Vec { /// ``` #[stable(feature = "try_reserve", since = "1.57.0")] pub fn try_reserve_exact(&mut self, additional: usize) -> Result<(), TryReserveError> { - self.buf.try_reserve_exact(self.len, additional) + self.buf.try_reserve_exact(self.len, additional, T::LAYOUT) } /// Shrinks the capacity of the vector as much as possible. @@ -1107,7 +1128,7 @@ impl Vec { // they are equal, so we can avoid the panic case in `RawVec::shrink_to_fit` // by only calling it with a greater capacity. if self.capacity() > self.len { - self.buf.shrink_to_fit(self.len); + self.buf.shrink_to_fit(self.len, T::LAYOUT); } } @@ -1133,7 +1154,7 @@ impl Vec { #[stable(feature = "shrink_to", since = "1.56.0")] pub fn shrink_to(&mut self, min_capacity: usize) { if self.capacity() > min_capacity { - self.buf.shrink_to_fit(cmp::max(self.len, min_capacity)); + self.buf.shrink_to_fit(cmp::max(self.len, min_capacity), T::LAYOUT); } } @@ -1577,8 +1598,8 @@ impl Vec { } // space for the new element - if len == self.buf.capacity() { - self.buf.grow_one(); + if len == self.capacity() { + self.buf.grow_one(T::LAYOUT); } unsafe { @@ -1999,8 +2020,8 @@ impl Vec { let len = self.len; // This will panic or abort if we would allocate > isize::MAX bytes // or if the length increment would overflow for zero-sized types. - if len == self.buf.capacity() { - self.buf.grow_one(); + if len == self.capacity() { + self.buf.grow_one(T::LAYOUT); } unsafe { let end = self.as_mut_ptr().add(len); @@ -2047,7 +2068,7 @@ impl Vec { #[inline] #[unstable(feature = "vec_push_within_capacity", issue = "100486")] pub fn push_within_capacity(&mut self, value: T) -> Result<(), T> { - if self.len == self.buf.capacity() { + if self.len == self.capacity() { return Err(value); } unsafe { @@ -2445,7 +2466,7 @@ impl Vec { unsafe { slice::from_raw_parts_mut( self.as_mut_ptr().add(self.len) as *mut MaybeUninit, - self.buf.capacity() - self.len, + self.capacity() - self.len, ) } } @@ -2519,11 +2540,11 @@ impl Vec { let ptr = self.as_mut_ptr(); // SAFETY: // - `ptr` is guaranteed to be valid for `self.len` elements - // - but the allocation extends out to `self.buf.capacity()` elements, possibly + // - but the allocation extends out to `self.capacity()` elements, possibly // uninitialized let spare_ptr = unsafe { ptr.add(self.len) }; let spare_ptr = spare_ptr.cast::>(); - let spare_len = self.buf.capacity() - self.len; + let spare_len = self.capacity() - self.len; // SAFETY: // - `ptr` is guaranteed to be valid for `self.len` elements @@ -3005,13 +3026,13 @@ impl IntoIterator for Vec { let me = ManuallyDrop::new(self); let alloc = ManuallyDrop::new(ptr::read(me.allocator())); let buf = me.buf.non_null(); - let begin = buf.as_ptr(); + let begin: *mut T = buf.as_ptr(); let end = if T::IS_ZST { begin.wrapping_byte_add(me.len()) } else { begin.add(me.len()) as *const T }; - let cap = me.buf.capacity(); + let cap = me.capacity(); IntoIter { buf, phantom: PhantomData, cap, alloc, ptr: buf, end } } } @@ -3307,7 +3328,7 @@ unsafe impl<#[may_dangle] T, A: Allocator> Drop for Vec { // could avoid questions of validity in certain cases ptr::drop_in_place(ptr::slice_from_raw_parts_mut(self.as_mut_ptr(), self.len)) } - // RawVec handles deallocation + self.buf.drop(T::LAYOUT); } } diff --git a/library/alloc/src/vec/spec_from_elem.rs b/library/alloc/src/vec/spec_from_elem.rs index 01a6db14474bb..6858bbee7e612 100644 --- a/library/alloc/src/vec/spec_from_elem.rs +++ b/library/alloc/src/vec/spec_from_elem.rs @@ -1,3 +1,5 @@ +use core::marker::PhantomData; +use core::mem::SizedTypeProperties; use core::ptr; use crate::alloc::Allocator; @@ -22,7 +24,11 @@ impl SpecFromElem for T { #[inline] default fn from_elem(elem: T, n: usize, alloc: A) -> Vec { if elem.is_zero() { - return Vec { buf: RawVec::with_capacity_zeroed_in(n, alloc), len: n }; + return Vec { + buf: RawVec::with_capacity_zeroed_in(n, alloc, T::LAYOUT), + len: n, + _marker: PhantomData, + }; } let mut v = Vec::with_capacity_in(n, alloc); v.extend_with(n, elem); @@ -34,7 +40,11 @@ impl SpecFromElem for i8 { #[inline] fn from_elem(elem: i8, n: usize, alloc: A) -> Vec { if elem == 0 { - return Vec { buf: RawVec::with_capacity_zeroed_in(n, alloc), len: n }; + return Vec { + buf: RawVec::with_capacity_zeroed_in(n, alloc, i8::LAYOUT), + len: n, + _marker: PhantomData, + }; } let mut v = Vec::with_capacity_in(n, alloc); unsafe { @@ -49,7 +59,11 @@ impl SpecFromElem for u8 { #[inline] fn from_elem(elem: u8, n: usize, alloc: A) -> Vec { if elem == 0 { - return Vec { buf: RawVec::with_capacity_zeroed_in(n, alloc), len: n }; + return Vec { + buf: RawVec::with_capacity_zeroed_in(n, alloc, u8::LAYOUT), + len: n, + _marker: PhantomData, + }; } let mut v = Vec::with_capacity_in(n, alloc); unsafe { diff --git a/library/alloc/src/vec/spec_from_iter_nested.rs b/library/alloc/src/vec/spec_from_iter_nested.rs index f915ebb86e5a5..2af839f38610c 100644 --- a/library/alloc/src/vec/spec_from_iter_nested.rs +++ b/library/alloc/src/vec/spec_from_iter_nested.rs @@ -2,7 +2,7 @@ use core::cmp; use core::iter::TrustedLen; use core::ptr; -use crate::raw_vec::RawVec; +use crate::raw_vec::min_non_zero_cap; use super::{SpecExtend, Vec}; @@ -28,7 +28,7 @@ where Some(element) => { let (lower, _) = iterator.size_hint(); let initial_capacity = - cmp::max(RawVec::::MIN_NON_ZERO_CAP, lower.saturating_add(1)); + cmp::max(min_non_zero_cap(core::mem::size_of::()), lower.saturating_add(1)); let mut vector = Vec::with_capacity(initial_capacity); unsafe { // SAFETY: We requested capacity at least 1 diff --git a/library/alloc/src/vec/splice.rs b/library/alloc/src/vec/splice.rs index 852fdcc3f5ce7..1bcd34a3a9f18 100644 --- a/library/alloc/src/vec/splice.rs +++ b/library/alloc/src/vec/splice.rs @@ -1,4 +1,5 @@ use crate::alloc::{Allocator, Global}; +use core::mem::SizedTypeProperties; use core::ptr::{self}; use core::slice::{self}; @@ -126,7 +127,7 @@ impl Drain<'_, T, A> { unsafe fn move_tail(&mut self, additional: usize) { let vec = unsafe { self.vec.as_mut() }; let len = self.tail_start + self.tail_len; - vec.buf.reserve(len, additional); + vec.buf.reserve(len, additional, T::LAYOUT); let new_tail_start = self.tail_start + additional; unsafe { diff --git a/library/core/src/mem/mod.rs b/library/core/src/mem/mod.rs index dd4b6e823434e..306ff889d4b02 100644 --- a/library/core/src/mem/mod.rs +++ b/library/core/src/mem/mod.rs @@ -5,6 +5,7 @@ #![stable(feature = "rust1", since = "1.0.0")] +use crate::alloc::Layout; use crate::clone; use crate::cmp; use crate::fmt; @@ -1235,6 +1236,10 @@ pub trait SizedTypeProperties: Sized { #[doc(hidden)] #[unstable(feature = "sized_type_properties", issue = "none")] const IS_ZST: bool = size_of::() == 0; + + #[doc(hidden)] + #[unstable(feature = "sized_type_properties", issue = "none")] + const LAYOUT: Layout = Layout::new::(); } #[doc(hidden)] #[unstable(feature = "sized_type_properties", issue = "none")] diff --git a/src/etc/gdb_providers.py b/src/etc/gdb_providers.py index 227695cdadd58..a7ae2cd02f852 100644 --- a/src/etc/gdb_providers.py +++ b/src/etc/gdb_providers.py @@ -96,6 +96,7 @@ def to_string(self): def display_hint(): return "string" + def _enumerate_array_elements(element_ptrs): for (i, element_ptr) in enumerate(element_ptrs): key = "[{}]".format(i) @@ -112,6 +113,7 @@ def _enumerate_array_elements(element_ptrs): yield key, element + class StdSliceProvider(printer_base): def __init__(self, valobj): self._valobj = valobj @@ -130,11 +132,14 @@ def children(self): def display_hint(): return "array" + class StdVecProvider(printer_base): def __init__(self, valobj): self._valobj = valobj self._length = int(valobj["len"]) self._data_ptr = unwrap_unique_or_non_null(valobj["buf"]["ptr"]) + ptr_ty = gdb.Type.pointer(valobj.type.template_argument(0)) + self._data_ptr = self._data_ptr.reinterpret_cast(ptr_ty) def to_string(self): return "Vec(size={})".format(self._length) @@ -160,6 +165,8 @@ def __init__(self, valobj): cap = cap[ZERO_FIELD] self._cap = int(cap) self._data_ptr = unwrap_unique_or_non_null(valobj["buf"]["ptr"]) + ptr_ty = gdb.Type.pointer(valobj.type.template_argument(0)) + self._data_ptr = self._data_ptr.reinterpret_cast(ptr_ty) def to_string(self): return "VecDeque(size={})".format(self._size) diff --git a/tests/debuginfo/strings-and-strs.rs b/tests/debuginfo/strings-and-strs.rs index 4a6adc2fc5331..de2fb68d92f8b 100644 --- a/tests/debuginfo/strings-and-strs.rs +++ b/tests/debuginfo/strings-and-strs.rs @@ -7,7 +7,7 @@ // gdb-command:run // gdb-command:print plain_string -// gdbr-check:$1 = alloc::string::String {vec: alloc::vec::Vec {buf: alloc::raw_vec::RawVec {ptr: core::ptr::unique::Unique {pointer: core::ptr::non_null::NonNull {pointer: 0x[...]}, _marker: core::marker::PhantomData}, cap: alloc::raw_vec::Cap (5), alloc: alloc::alloc::Global}, len: 5}} +// gdbr-check:$1 = alloc::string::String {vec: alloc::vec::Vec {buf: alloc::raw_vec::RawVec {ptr: core::ptr::unique::Unique {pointer: core::ptr::non_null::NonNull {pointer: 0x[...]}, _marker: core::marker::PhantomData}, cap: alloc::raw_vec::Cap (5), alloc: alloc::alloc::Global}, len: 5, _marker: core::marker::PhantomData}} // gdb-command:print plain_str // gdbr-check:$2 = "Hello" diff --git a/tests/mir-opt/inline/inline_shims.drop.Inline.panic-abort.diff b/tests/mir-opt/inline/inline_shims.drop.Inline.panic-abort.diff index 2a36ccaab110a..9170d74596803 100644 --- a/tests/mir-opt/inline/inline_shims.drop.Inline.panic-abort.diff +++ b/tests/mir-opt/inline/inline_shims.drop.Inline.panic-abort.diff @@ -25,7 +25,7 @@ + StorageLive(_6); + StorageLive(_7); + _6 = &mut (*_4); -+ _7 = as Drop>::drop(move _6) -> [return: bb2, unwind unreachable]; ++ _7 = as Drop>::drop(move _6) -> [return: bb1, unwind unreachable]; } bb1: { @@ -39,22 +39,18 @@ + StorageLive(_8); + StorageLive(_9); + _8 = discriminant((*_5)); -+ switchInt(move _8) -> [0: bb3, otherwise: bb4]; ++ switchInt(move _8) -> [0: bb2, otherwise: bb3]; } bb2: { -+ drop(((*_4).0: alloc::raw_vec::RawVec)) -> [return: bb1, unwind unreachable]; -+ } -+ -+ bb3: { + StorageDead(_9); + StorageDead(_8); StorageDead(_5); return; + } + -+ bb4: { -+ drop((((*_5) as Some).0: B)) -> [return: bb3, unwind unreachable]; ++ bb3: { ++ drop((((*_5) as Some).0: B)) -> [return: bb2, unwind unreachable]; } } diff --git a/tests/mir-opt/inline/inline_shims.drop.Inline.panic-unwind.diff b/tests/mir-opt/inline/inline_shims.drop.Inline.panic-unwind.diff index e11561076e641..8548475216e29 100644 --- a/tests/mir-opt/inline/inline_shims.drop.Inline.panic-unwind.diff +++ b/tests/mir-opt/inline/inline_shims.drop.Inline.panic-unwind.diff @@ -8,33 +8,43 @@ let _3: (); let mut _4: *mut std::vec::Vec; let mut _5: *mut std::option::Option; -+ scope 1 (inlined std::ptr::drop_in_place::> - shim(Some(Option))) { -+ let mut _6: isize; -+ let mut _7: isize; ++ scope 1 (inlined std::ptr::drop_in_place::> - shim(Some(Vec))) { ++ let mut _6: &mut std::vec::Vec; ++ let mut _7: (); ++ } ++ scope 2 (inlined std::ptr::drop_in_place::> - shim(Some(Option))) { ++ let mut _8: isize; ++ let mut _9: isize; + } bb0: { StorageLive(_3); StorageLive(_4); _4 = _1; - _3 = std::ptr::drop_in_place::>(move _4) -> [return: bb1, unwind continue]; +- _3 = std::ptr::drop_in_place::>(move _4) -> [return: bb1, unwind continue]; ++ StorageLive(_6); ++ StorageLive(_7); ++ _6 = &mut (*_4); ++ _7 = as Drop>::drop(move _6) -> [return: bb1, unwind continue]; } bb1: { ++ StorageDead(_7); ++ StorageDead(_6); StorageDead(_4); StorageDead(_3); StorageLive(_5); _5 = _2; - _0 = std::ptr::drop_in_place::>(move _5) -> [return: bb2, unwind continue]; -+ StorageLive(_6); -+ StorageLive(_7); -+ _6 = discriminant((*_5)); -+ switchInt(move _6) -> [0: bb2, otherwise: bb3]; ++ StorageLive(_8); ++ StorageLive(_9); ++ _8 = discriminant((*_5)); ++ switchInt(move _8) -> [0: bb2, otherwise: bb3]; } bb2: { -+ StorageDead(_7); -+ StorageDead(_6); ++ StorageDead(_9); ++ StorageDead(_8); StorageDead(_5); return; + } diff --git a/tests/mir-opt/pre-codegen/vec_deref.vec_deref_to_slice.PreCodegen.after.panic-abort.mir b/tests/mir-opt/pre-codegen/vec_deref.vec_deref_to_slice.PreCodegen.after.panic-abort.mir index 1c9ed25d7f2b2..00618802db485 100644 --- a/tests/mir-opt/pre-codegen/vec_deref.vec_deref_to_slice.PreCodegen.after.panic-abort.mir +++ b/tests/mir-opt/pre-codegen/vec_deref.vec_deref_to_slice.PreCodegen.after.panic-abort.mir @@ -5,41 +5,35 @@ fn vec_deref_to_slice(_1: &Vec) -> &[u8] { let mut _0: &[u8]; scope 1 (inlined as Deref>::deref) { debug self => _1; - let mut _4: *const u8; - let mut _5: usize; + let mut _5: *const u8; + let mut _6: usize; scope 2 (inlined Vec::::as_ptr) { debug self => _1; - let mut _2: &alloc::raw_vec::RawVec; - scope 3 (inlined alloc::raw_vec::RawVec::::ptr) { + let mut _2: &alloc::raw_vec::RawVec; + let mut _4: *mut u8; + scope 3 (inlined alloc::raw_vec::RawVec::ptr::) { debug self => _2; - let mut _3: std::ptr::NonNull; - scope 4 (inlined Unique::::as_ptr) { - debug ((self: Unique).0: std::ptr::NonNull) => _3; - debug ((self: Unique).1: std::marker::PhantomData) => const PhantomData::; - scope 5 (inlined NonNull::::as_ptr) { - debug self => _3; - } - } + let mut _3: std::ptr::Unique; } } - scope 6 (inlined std::slice::from_raw_parts::<'_, u8>) { - debug data => _4; - debug len => _5; - let _6: *const [u8]; - scope 7 (inlined core::ub_checks::check_language_ub) { - scope 8 (inlined core::ub_checks::check_language_ub::runtime) { + scope 4 (inlined std::slice::from_raw_parts::<'_, u8>) { + debug data => _5; + debug len => _6; + let _7: *const [u8]; + scope 5 (inlined core::ub_checks::check_language_ub) { + scope 6 (inlined core::ub_checks::check_language_ub::runtime) { } } - scope 9 (inlined std::mem::size_of::) { + scope 7 (inlined std::mem::size_of::) { } - scope 10 (inlined align_of::) { + scope 8 (inlined align_of::) { } - scope 11 (inlined slice_from_raw_parts::) { - debug data => _4; - debug len => _5; - scope 12 (inlined std::ptr::from_raw_parts::<[u8], u8>) { - debug data_pointer => _4; - debug metadata => _5; + scope 9 (inlined slice_from_raw_parts::) { + debug data => _5; + debug len => _6; + scope 10 (inlined std::ptr::from_raw_parts::<[u8], u8>) { + debug data_pointer => _5; + debug metadata => _6; } } } @@ -47,19 +41,22 @@ fn vec_deref_to_slice(_1: &Vec) -> &[u8] { bb0: { StorageLive(_4); + StorageLive(_5); StorageLive(_2); - _2 = &((*_1).0: alloc::raw_vec::RawVec); + _2 = &((*_1).0: alloc::raw_vec::RawVec); StorageLive(_3); - _3 = ((((*_1).0: alloc::raw_vec::RawVec).0: std::ptr::Unique).0: std::ptr::NonNull); - _4 = (_3.0: *const u8); + _3 = (((*_1).0: alloc::raw_vec::RawVec).0: std::ptr::Unique); + _4 = move _3 as *mut u8 (Transmute); StorageDead(_3); + _5 = _4 as *const u8 (PtrToPtr); StorageDead(_2); - StorageLive(_5); - _5 = ((*_1).1: usize); - _6 = *const [u8] from (_4, _5); + StorageLive(_6); + _6 = ((*_1).1: usize); + _7 = *const [u8] from (_5, _6); + StorageDead(_6); StorageDead(_5); StorageDead(_4); - _0 = &(*_6); + _0 = &(*_7); return; } } diff --git a/tests/mir-opt/pre-codegen/vec_deref.vec_deref_to_slice.PreCodegen.after.panic-unwind.mir b/tests/mir-opt/pre-codegen/vec_deref.vec_deref_to_slice.PreCodegen.after.panic-unwind.mir index 1c9ed25d7f2b2..00618802db485 100644 --- a/tests/mir-opt/pre-codegen/vec_deref.vec_deref_to_slice.PreCodegen.after.panic-unwind.mir +++ b/tests/mir-opt/pre-codegen/vec_deref.vec_deref_to_slice.PreCodegen.after.panic-unwind.mir @@ -5,41 +5,35 @@ fn vec_deref_to_slice(_1: &Vec) -> &[u8] { let mut _0: &[u8]; scope 1 (inlined as Deref>::deref) { debug self => _1; - let mut _4: *const u8; - let mut _5: usize; + let mut _5: *const u8; + let mut _6: usize; scope 2 (inlined Vec::::as_ptr) { debug self => _1; - let mut _2: &alloc::raw_vec::RawVec; - scope 3 (inlined alloc::raw_vec::RawVec::::ptr) { + let mut _2: &alloc::raw_vec::RawVec; + let mut _4: *mut u8; + scope 3 (inlined alloc::raw_vec::RawVec::ptr::) { debug self => _2; - let mut _3: std::ptr::NonNull; - scope 4 (inlined Unique::::as_ptr) { - debug ((self: Unique).0: std::ptr::NonNull) => _3; - debug ((self: Unique).1: std::marker::PhantomData) => const PhantomData::; - scope 5 (inlined NonNull::::as_ptr) { - debug self => _3; - } - } + let mut _3: std::ptr::Unique; } } - scope 6 (inlined std::slice::from_raw_parts::<'_, u8>) { - debug data => _4; - debug len => _5; - let _6: *const [u8]; - scope 7 (inlined core::ub_checks::check_language_ub) { - scope 8 (inlined core::ub_checks::check_language_ub::runtime) { + scope 4 (inlined std::slice::from_raw_parts::<'_, u8>) { + debug data => _5; + debug len => _6; + let _7: *const [u8]; + scope 5 (inlined core::ub_checks::check_language_ub) { + scope 6 (inlined core::ub_checks::check_language_ub::runtime) { } } - scope 9 (inlined std::mem::size_of::) { + scope 7 (inlined std::mem::size_of::) { } - scope 10 (inlined align_of::) { + scope 8 (inlined align_of::) { } - scope 11 (inlined slice_from_raw_parts::) { - debug data => _4; - debug len => _5; - scope 12 (inlined std::ptr::from_raw_parts::<[u8], u8>) { - debug data_pointer => _4; - debug metadata => _5; + scope 9 (inlined slice_from_raw_parts::) { + debug data => _5; + debug len => _6; + scope 10 (inlined std::ptr::from_raw_parts::<[u8], u8>) { + debug data_pointer => _5; + debug metadata => _6; } } } @@ -47,19 +41,22 @@ fn vec_deref_to_slice(_1: &Vec) -> &[u8] { bb0: { StorageLive(_4); + StorageLive(_5); StorageLive(_2); - _2 = &((*_1).0: alloc::raw_vec::RawVec); + _2 = &((*_1).0: alloc::raw_vec::RawVec); StorageLive(_3); - _3 = ((((*_1).0: alloc::raw_vec::RawVec).0: std::ptr::Unique).0: std::ptr::NonNull); - _4 = (_3.0: *const u8); + _3 = (((*_1).0: alloc::raw_vec::RawVec).0: std::ptr::Unique); + _4 = move _3 as *mut u8 (Transmute); StorageDead(_3); + _5 = _4 as *const u8 (PtrToPtr); StorageDead(_2); - StorageLive(_5); - _5 = ((*_1).1: usize); - _6 = *const [u8] from (_4, _5); + StorageLive(_6); + _6 = ((*_1).1: usize); + _7 = *const [u8] from (_5, _6); + StorageDead(_6); StorageDead(_5); StorageDead(_4); - _0 = &(*_6); + _0 = &(*_7); return; } } diff --git a/tests/mir-opt/unusual_item_types.core.ptr-drop_in_place.Vec_i32_.AddMovesForPackedDrops.before.mir b/tests/mir-opt/unusual_item_types.core.ptr-drop_in_place.Vec_i32_.AddMovesForPackedDrops.before.mir index b587941835515..2c6e241dd6227 100644 --- a/tests/mir-opt/unusual_item_types.core.ptr-drop_in_place.Vec_i32_.AddMovesForPackedDrops.before.mir +++ b/tests/mir-opt/unusual_item_types.core.ptr-drop_in_place.Vec_i32_.AddMovesForPackedDrops.before.mir @@ -6,7 +6,7 @@ fn std::ptr::drop_in_place(_1: *mut Vec) -> () { let mut _3: (); bb0: { - goto -> bb6; + goto -> bb4; } bb1: { @@ -21,16 +21,8 @@ fn std::ptr::drop_in_place(_1: *mut Vec) -> () { goto -> bb1; } - bb4 (cleanup): { - drop(((*_1).0: alloc::raw_vec::RawVec)) -> [return: bb2, unwind terminate(cleanup)]; - } - - bb5: { - drop(((*_1).0: alloc::raw_vec::RawVec)) -> [return: bb3, unwind: bb2]; - } - - bb6: { + bb4: { _2 = &mut (*_1); - _3 = as Drop>::drop(move _2) -> [return: bb5, unwind: bb4]; + _3 = as Drop>::drop(move _2) -> [return: bb3, unwind: bb2]; } } From 8f9e05eaeb5a78d8c46e674f93849b593c26a966 Mon Sep 17 00:00:00 2001 From: Ben Kimock Date: Thu, 11 Jul 2024 19:41:48 -0400 Subject: [PATCH 2/5] Paper over the clippy ICE --- .../clippy/tests/ui/borrow_interior_mutable_const/others.rs | 3 +-- .../tests/ui/borrow_interior_mutable_const/others.stderr | 6 +++--- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/tools/clippy/tests/ui/borrow_interior_mutable_const/others.rs b/src/tools/clippy/tests/ui/borrow_interior_mutable_const/others.rs index 0ea93dd84625c..452d1b198133e 100644 --- a/src/tools/clippy/tests/ui/borrow_interior_mutable_const/others.rs +++ b/src/tools/clippy/tests/ui/borrow_interior_mutable_const/others.rs @@ -10,7 +10,7 @@ use std::sync::Once; const ATOMIC: AtomicUsize = AtomicUsize::new(5); const CELL: Cell = Cell::new(6); -const ATOMIC_TUPLE: ([AtomicUsize; 1], Vec, u8) = ([ATOMIC], Vec::new(), 7); +const ATOMIC_TUPLE: ([AtomicUsize; 1], Option>, u8) = ([ATOMIC], None, 7); const INTEGER: u8 = 8; const STRING: String = String::new(); const STR: &str = "012345"; @@ -74,7 +74,6 @@ fn main() { let _ = &(&&&&ATOMIC_TUPLE).0; //~ ERROR: interior mutability let _ = &ATOMIC_TUPLE.0[0]; //~ ERROR: interior mutability let _ = ATOMIC_TUPLE.0[0].load(Ordering::SeqCst); //~ ERROR: interior mutability - let _ = &*ATOMIC_TUPLE.1; let _ = &ATOMIC_TUPLE.2; let _ = (&&&&ATOMIC_TUPLE).0; let _ = (&&&&ATOMIC_TUPLE).2; diff --git a/src/tools/clippy/tests/ui/borrow_interior_mutable_const/others.stderr b/src/tools/clippy/tests/ui/borrow_interior_mutable_const/others.stderr index 33c774667f942..9a9028c864986 100644 --- a/src/tools/clippy/tests/ui/borrow_interior_mutable_const/others.stderr +++ b/src/tools/clippy/tests/ui/borrow_interior_mutable_const/others.stderr @@ -92,7 +92,7 @@ LL | let _ = ATOMIC_TUPLE.0[0].load(Ordering::SeqCst); = help: assign this const to a local or static variable, and use the variable here error: a `const` item with interior mutability should not be borrowed - --> tests/ui/borrow_interior_mutable_const/others.rs:82:13 + --> tests/ui/borrow_interior_mutable_const/others.rs:81:13 | LL | let _ = ATOMIC_TUPLE.0[0]; | ^^^^^^^^^^^^ @@ -100,7 +100,7 @@ LL | let _ = ATOMIC_TUPLE.0[0]; = help: assign this const to a local or static variable, and use the variable here error: a `const` item with interior mutability should not be borrowed - --> tests/ui/borrow_interior_mutable_const/others.rs:87:5 + --> tests/ui/borrow_interior_mutable_const/others.rs:86:5 | LL | CELL.set(2); | ^^^^ @@ -108,7 +108,7 @@ LL | CELL.set(2); = help: assign this const to a local or static variable, and use the variable here error: a `const` item with interior mutability should not be borrowed - --> tests/ui/borrow_interior_mutable_const/others.rs:88:16 + --> tests/ui/borrow_interior_mutable_const/others.rs:87:16 | LL | assert_eq!(CELL.get(), 6); | ^^^^ From fa45b5bd14a4560613a4c342db44f56cfc2e8690 Mon Sep 17 00:00:00 2001 From: Ben Kimock Date: Sun, 14 Jul 2024 16:56:21 -0400 Subject: [PATCH 3/5] Move RawVec logic to RawVecInner --- library/alloc/src/boxed.rs | 14 +- .../alloc/src/collections/vec_deque/mod.rs | 74 +--- library/alloc/src/raw_vec.rs | 405 ++++++++++++------ library/alloc/src/raw_vec/tests.rs | 103 +++-- library/alloc/src/vec/in_place_drop.rs | 5 +- library/alloc/src/vec/into_iter.rs | 6 +- library/alloc/src/vec/mod.rs | 71 ++- library/alloc/src/vec/spec_from_elem.rs | 20 +- .../alloc/src/vec/spec_from_iter_nested.rs | 4 +- library/alloc/src/vec/splice.rs | 3 +- src/etc/gdb_providers.py | 10 +- tests/debuginfo/strings-and-strs.rs | 2 +- .../inline_shims.drop.Inline.panic-abort.diff | 12 +- ...inline_shims.drop.Inline.panic-unwind.diff | 30 +- ..._to_slice.PreCodegen.after.panic-abort.mir | 67 +-- ...to_slice.PreCodegen.after.panic-unwind.mir | 67 +-- ...Vec_i32_.AddMovesForPackedDrops.before.mir | 14 +- 17 files changed, 487 insertions(+), 420 deletions(-) diff --git a/library/alloc/src/boxed.rs b/library/alloc/src/boxed.rs index 84c1172ce3698..f299aa0124dbe 100644 --- a/library/alloc/src/boxed.rs +++ b/library/alloc/src/boxed.rs @@ -675,7 +675,7 @@ impl Box<[T]> { #[unstable(feature = "new_uninit", issue = "63291")] #[must_use] pub fn new_uninit_slice(len: usize) -> Box<[mem::MaybeUninit]> { - unsafe { RawVec::with_capacity(len, T::LAYOUT).into_box(len) } + unsafe { RawVec::with_capacity(len).into_box(len) } } /// Constructs a new boxed slice with uninitialized contents, with the memory @@ -700,7 +700,7 @@ impl Box<[T]> { #[unstable(feature = "new_uninit", issue = "63291")] #[must_use] pub fn new_zeroed_slice(len: usize) -> Box<[mem::MaybeUninit]> { - unsafe { RawVec::with_capacity_zeroed(len, T::LAYOUT).into_box(len) } + unsafe { RawVec::with_capacity_zeroed(len).into_box(len) } } /// Constructs a new boxed slice with uninitialized contents. Returns an error if @@ -727,7 +727,7 @@ impl Box<[T]> { #[inline] pub fn try_new_uninit_slice(len: usize) -> Result]>, AllocError> { let ptr = if T::IS_ZST || len == 0 { - NonNull::::dangling() + NonNull::dangling() } else { let layout = match Layout::array::>(len) { Ok(l) => l, @@ -761,7 +761,7 @@ impl Box<[T]> { #[inline] pub fn try_new_zeroed_slice(len: usize) -> Result]>, AllocError> { let ptr = if T::IS_ZST || len == 0 { - NonNull::::dangling() + NonNull::dangling() } else { let layout = match Layout::array::>(len) { Ok(l) => l, @@ -801,7 +801,7 @@ impl Box<[T], A> { // #[unstable(feature = "new_uninit", issue = "63291")] #[must_use] pub fn new_uninit_slice_in(len: usize, alloc: A) -> Box<[mem::MaybeUninit], A> { - unsafe { RawVec::with_capacity_in(len, alloc, T::LAYOUT).into_box(len) } + unsafe { RawVec::with_capacity_in(len, alloc).into_box(len) } } /// Constructs a new boxed slice with uninitialized contents in the provided allocator, @@ -829,7 +829,7 @@ impl Box<[T], A> { // #[unstable(feature = "new_uninit", issue = "63291")] #[must_use] pub fn new_zeroed_slice_in(len: usize, alloc: A) -> Box<[mem::MaybeUninit], A> { - unsafe { RawVec::with_capacity_zeroed_in(len, alloc, T::LAYOUT).into_box(len) } + unsafe { RawVec::with_capacity_zeroed_in(len, alloc).into_box(len) } } } @@ -1556,7 +1556,7 @@ impl BoxFromSlice for Box<[T]> { #[inline] fn from_slice(slice: &[T]) -> Self { let len = slice.len(); - let buf = RawVec::with_capacity(len, T::LAYOUT); + let buf = RawVec::with_capacity(len); unsafe { ptr::copy_nonoverlapping(slice.as_ptr(), buf.ptr(), len); buf.into_box(slice.len()).assume_init() diff --git a/library/alloc/src/collections/vec_deque/mod.rs b/library/alloc/src/collections/vec_deque/mod.rs index 9baed577b8fd1..a07f250d7d88c 100644 --- a/library/alloc/src/collections/vec_deque/mod.rs +++ b/library/alloc/src/collections/vec_deque/mod.rs @@ -11,7 +11,6 @@ use core::cmp::{self, Ordering}; use core::fmt; use core::hash::{Hash, Hasher}; use core::iter::{repeat_n, repeat_with, ByRefSized}; -use core::marker::PhantomData; use core::mem::{ManuallyDrop, SizedTypeProperties}; use core::ops::{Index, IndexMut, Range, RangeBounds}; use core::ptr; @@ -103,8 +102,7 @@ pub struct VecDeque< // if `len == 0`, the exact value of `head` is unimportant. // if `T` is zero-Sized, then `self.len <= usize::MAX`, otherwise `self.len <= isize::MAX as usize`. len: usize, - buf: RawVec, - _marker: PhantomData, + buf: RawVec, } #[stable(feature = "rust1", since = "1.0.0")] @@ -128,24 +126,11 @@ impl Clone for VecDeque { #[stable(feature = "rust1", since = "1.0.0")] unsafe impl<#[may_dangle] T, A: Allocator> Drop for VecDeque { fn drop(&mut self) { - use core::alloc::Layout; - - struct Guard<'a, A: Allocator> { - buf: &'a mut RawVec, - layout: Layout, - } - - impl Drop for Guard<'_, A> { - fn drop(&mut self) { - self.buf.drop(self.layout); - } - } - /// Runs the destructor for all items in the slice when it gets dropped (normally or /// during unwinding). - struct Dropper(*mut [T]); + struct Dropper<'a, T>(&'a mut [T]); - impl Drop for Dropper { + impl<'a, T> Drop for Dropper<'a, T> { fn drop(&mut self) { unsafe { ptr::drop_in_place(self.0); @@ -154,16 +139,12 @@ unsafe impl<#[may_dangle] T, A: Allocator> Drop for VecDeque { } let (front, back) = self.as_mut_slices(); - let front = front as *mut [T]; - let back = back as *mut [T]; - - let _guard = Guard { buf: &mut self.buf, layout: T::LAYOUT }; - unsafe { let _back_dropper = Dropper(back); // use drop for [T] ptr::drop_in_place(front); } + // RawVec handles deallocation } } @@ -578,7 +559,7 @@ impl VecDeque { #[must_use] pub const fn new() -> VecDeque { // FIXME: This should just be `VecDeque::new_in(Global)` once that hits stable. - VecDeque { head: 0, len: 0, buf: RawVec::new::(), _marker: PhantomData } + VecDeque { head: 0, len: 0, buf: RawVec::NEW } } /// Creates an empty deque with space for at least `capacity` elements. @@ -618,12 +599,7 @@ impl VecDeque { #[inline] #[unstable(feature = "try_with_capacity", issue = "91913")] pub fn try_with_capacity(capacity: usize) -> Result, TryReserveError> { - Ok(VecDeque { - head: 0, - len: 0, - buf: RawVec::try_with_capacity_in(capacity, Global, T::LAYOUT)?, - _marker: PhantomData, - }) + Ok(VecDeque { head: 0, len: 0, buf: RawVec::try_with_capacity_in(capacity, Global)? }) } } @@ -640,12 +616,7 @@ impl VecDeque { #[inline] #[unstable(feature = "allocator_api", issue = "32838")] pub const fn new_in(alloc: A) -> VecDeque { - VecDeque { - head: 0, - len: 0, - buf: RawVec::new_in(alloc, core::mem::align_of::()), - _marker: PhantomData, - } + VecDeque { head: 0, len: 0, buf: RawVec::new_in(alloc) } } /// Creates an empty deque with space for at least `capacity` elements. @@ -659,12 +630,7 @@ impl VecDeque { /// ``` #[unstable(feature = "allocator_api", issue = "32838")] pub fn with_capacity_in(capacity: usize, alloc: A) -> VecDeque { - VecDeque { - head: 0, - len: 0, - buf: RawVec::with_capacity_in(capacity, alloc, T::LAYOUT), - _marker: PhantomData, - } + VecDeque { head: 0, len: 0, buf: RawVec::with_capacity_in(capacity, alloc) } } /// Creates a `VecDeque` from a raw allocation, when the initialized @@ -695,7 +661,6 @@ impl VecDeque { head: initialized.start, len: initialized.end.unchecked_sub(initialized.start), buf: RawVec::from_raw_parts_in(ptr, capacity, alloc), - _marker: PhantomData, } } } @@ -802,7 +767,7 @@ impl VecDeque { #[inline] #[stable(feature = "rust1", since = "1.0.0")] pub fn capacity(&self) -> usize { - self.buf.capacity(core::mem::size_of::()) + if T::IS_ZST { usize::MAX } else { self.buf.capacity() } } /// Reserves the minimum capacity for at least `additional` more elements to be inserted in the @@ -833,7 +798,7 @@ impl VecDeque { let old_cap = self.capacity(); if new_cap > old_cap { - self.buf.reserve_exact(self.len, additional, T::LAYOUT); + self.buf.reserve_exact(self.len, additional); unsafe { self.handle_capacity_increase(old_cap); } @@ -864,7 +829,7 @@ impl VecDeque { if new_cap > old_cap { // we don't need to reserve_exact(), as the size doesn't have // to be a power of 2. - self.buf.reserve(self.len, additional, T::LAYOUT); + self.buf.reserve(self.len, additional); unsafe { self.handle_capacity_increase(old_cap); } @@ -915,7 +880,7 @@ impl VecDeque { let old_cap = self.capacity(); if new_cap > old_cap { - self.buf.try_reserve_exact(self.len, additional, T::LAYOUT)?; + self.buf.try_reserve_exact(self.len, additional)?; unsafe { self.handle_capacity_increase(old_cap); } @@ -963,7 +928,7 @@ impl VecDeque { let old_cap = self.capacity(); if new_cap > old_cap { - self.buf.try_reserve(self.len, additional, T::LAYOUT)?; + self.buf.try_reserve(self.len, additional)?; unsafe { self.handle_capacity_increase(old_cap); } @@ -1105,7 +1070,7 @@ impl VecDeque { let guard = Guard { deque: self, old_head, target_cap }; - guard.deque.buf.shrink_to_fit(target_cap, T::LAYOUT); + guard.deque.buf.shrink_to_fit(target_cap); // Don't drop the guard if we didn't unwind. mem::forget(guard); @@ -2210,7 +2175,7 @@ impl VecDeque { // buffer without it being full emerge debug_assert!(self.is_full()); let old_cap = self.capacity(); - self.buf.grow_one(T::LAYOUT); + self.buf.grow_one(); unsafe { self.handle_capacity_increase(old_cap); } @@ -3015,12 +2980,7 @@ impl From> for VecDeque { #[inline] fn from(other: Vec) -> Self { let (ptr, len, cap, alloc) = other.into_raw_parts_with_alloc(); - Self { - head: 0, - len, - buf: unsafe { RawVec::from_raw_parts_in(ptr, cap, alloc) }, - _marker: PhantomData, - } + Self { head: 0, len, buf: unsafe { RawVec::from_raw_parts_in(ptr, cap, alloc) } } } } @@ -3060,7 +3020,7 @@ impl From> for Vec { unsafe { let other = ManuallyDrop::new(other); - let buf: *mut T = other.buf.ptr(); + let buf = other.buf.ptr(); let len = other.len(); let cap = other.capacity(); let alloc = ptr::read(other.allocator()); diff --git a/library/alloc/src/raw_vec.rs b/library/alloc/src/raw_vec.rs index f4b6c3c63fc1a..3d35c8347354e 100644 --- a/library/alloc/src/raw_vec.rs +++ b/library/alloc/src/raw_vec.rs @@ -2,6 +2,7 @@ use core::cmp; use core::hint; +use core::marker::PhantomData; use core::mem::{ManuallyDrop, MaybeUninit, SizedTypeProperties}; use core::ptr::{self, NonNull, Unique}; @@ -65,7 +66,13 @@ impl Cap { /// `usize::MAX`. This means that you need to be careful when round-tripping this type with a /// `Box<[T]>`, since `capacity()` won't yield the length. #[allow(missing_debug_implementations)] -pub(crate) struct RawVec { +pub(crate) struct RawVec { + inner: RawVecInner, + _marker: PhantomData, +} + +#[allow(missing_debug_implementations)] +struct RawVecInner { ptr: Unique, /// Never used for ZSTs; it's `capacity()`'s responsibility to return usize::MAX in that case. /// @@ -76,7 +83,14 @@ pub(crate) struct RawVec { alloc: A, } -impl RawVec { +impl RawVec { + /// HACK(Centril): This exists because stable `const fn` can only call stable `const fn`, so + /// they cannot call `Self::new()`. + /// + /// If you change `RawVec::new` or dependencies, please take care to not introduce anything + /// that would truly const-call something unstable. + pub const NEW: Self = Self::new(); + /// Creates the biggest possible `RawVec` (on the system heap) /// without allocating. If `T` has positive size, then this makes a /// `RawVec` with capacity `0`. If `T` is zero-sized, then it makes a @@ -84,8 +98,8 @@ impl RawVec { /// delayed allocation. #[must_use] #[rustc_const_stable(feature = "raw_vec_internals_const", since = "1.81")] - pub const fn new() -> Self { - Self::new_in(Global, core::mem::align_of::()) + pub const fn new() -> Self { + Self { inner: RawVecInner::new::(), _marker: PhantomData } } /// Creates a `RawVec` (on the system heap) with exactly the @@ -106,19 +120,37 @@ impl RawVec { #[cfg(not(any(no_global_oom_handling, test)))] #[must_use] #[inline] - pub fn with_capacity(capacity: usize, elem_layout: Layout) -> Self { - match Self::try_allocate_in(capacity, AllocInit::Uninitialized, Global, elem_layout) { - Ok(res) => res, - Err(err) => handle_error(err), - } + pub fn with_capacity(capacity: usize) -> Self { + Self { inner: RawVecInner::with_capacity(capacity, T::LAYOUT), _marker: PhantomData } } /// Like `with_capacity`, but guarantees the buffer is zeroed. #[cfg(not(any(no_global_oom_handling, test)))] #[must_use] #[inline] - pub fn with_capacity_zeroed(capacity: usize, elem_layout: Layout) -> Self { - Self::with_capacity_zeroed_in(capacity, Global, elem_layout) + pub fn with_capacity_zeroed(capacity: usize) -> Self { + Self { + inner: RawVecInner::with_capacity_zeroed_in(capacity, Global, T::LAYOUT), + _marker: PhantomData, + } + } +} + +impl RawVecInner { + #[must_use] + #[rustc_const_stable(feature = "raw_vec_internals_const", since = "1.81")] + const fn new() -> Self { + Self::new_in(Global, core::mem::align_of::()) + } + + #[cfg(not(any(no_global_oom_handling, test)))] + #[must_use] + #[inline] + fn with_capacity(capacity: usize, elem_layout: Layout) -> Self { + match Self::try_allocate_in(capacity, AllocInit::Uninitialized, Global, elem_layout) { + Ok(res) => res, + Err(err) => handle_error(err), + } } } @@ -127,7 +159,7 @@ impl RawVec { // to round up a request of less than 8 bytes to at least 8 bytes. // - 4 if elements are moderate-sized (<= 1 KiB). // - 1 otherwise, to avoid wasting too much space for very short Vecs. -pub const fn min_non_zero_cap(size: usize) -> usize { +const fn min_non_zero_cap(size: usize) -> usize { if size == 1 { 8 } else if size <= 1024 { @@ -137,47 +169,47 @@ pub const fn min_non_zero_cap(size: usize) -> usize { } } -impl RawVec { +impl RawVec { + #[cfg(not(no_global_oom_handling))] + pub(crate) const MIN_NON_ZERO_CAP: usize = min_non_zero_cap(size_of::()); + /// Like `new`, but parameterized over the choice of allocator for /// the returned `RawVec`. #[inline] #[rustc_const_stable(feature = "raw_vec_internals_const", since = "1.81")] - pub const fn new_in(alloc: A, align: usize) -> Self { - let ptr = unsafe { core::mem::transmute(align) }; - // `cap: 0` means "unallocated". zero-sized types are ignored. - Self { ptr, cap: Cap::ZERO, alloc } + pub const fn new_in(alloc: A) -> Self { + Self { inner: RawVecInner::new_in(alloc, align_of::()), _marker: PhantomData } } /// Like `with_capacity`, but parameterized over the choice of /// allocator for the returned `RawVec`. #[cfg(not(no_global_oom_handling))] #[inline] - pub fn with_capacity_in(capacity: usize, alloc: A, elem_layout: Layout) -> Self { - match Self::try_allocate_in(capacity, AllocInit::Uninitialized, alloc, elem_layout) { - Ok(res) => res, - Err(err) => handle_error(err), + pub fn with_capacity_in(capacity: usize, alloc: A) -> Self { + Self { + inner: RawVecInner::with_capacity_in(capacity, alloc, T::LAYOUT), + _marker: PhantomData, } } /// Like `try_with_capacity`, but parameterized over the choice of /// allocator for the returned `RawVec`. #[inline] - pub fn try_with_capacity_in( - capacity: usize, - alloc: A, - elem_layout: Layout, - ) -> Result { - Self::try_allocate_in(capacity, AllocInit::Uninitialized, alloc, elem_layout) + pub fn try_with_capacity_in(capacity: usize, alloc: A) -> Result { + Ok(Self { + inner: RawVecInner::try_with_capacity_in(capacity, alloc, T::LAYOUT)?, + _marker: PhantomData, + }) } /// Like `with_capacity_zeroed`, but parameterized over the choice /// of allocator for the returned `RawVec`. #[cfg(not(no_global_oom_handling))] #[inline] - pub fn with_capacity_zeroed_in(capacity: usize, alloc: A, elem_layout: Layout) -> Self { - match Self::try_allocate_in(capacity, AllocInit::Zeroed, alloc, elem_layout) { - Ok(res) => res, - Err(err) => handle_error(err), + pub fn with_capacity_zeroed_in(capacity: usize, alloc: A) -> Self { + Self { + inner: RawVecInner::with_capacity_zeroed_in(capacity, alloc, T::LAYOUT), + _marker: PhantomData, } } @@ -193,17 +225,207 @@ impl RawVec { /// /// Note, that the requested capacity and `self.capacity()` could differ, as /// an allocator could overallocate and return a greater memory block than requested. - pub unsafe fn into_box(self, len: usize) -> Box<[MaybeUninit], A> { + pub unsafe fn into_box(self, len: usize) -> Box<[MaybeUninit], A> { // Sanity-check one half of the safety requirement (we cannot check the other half). debug_assert!( - len <= self.capacity(core::mem::size_of::()), + len <= self.capacity(), "`len` must be smaller than or equal to `self.capacity()`" ); let me = ManuallyDrop::new(self); unsafe { let slice = ptr::slice_from_raw_parts_mut(me.ptr() as *mut MaybeUninit, len); - Box::from_raw_in(slice, ptr::read(&me.alloc)) + Box::from_raw_in(slice, ptr::read(&me.inner.alloc)) + } + } + + /// Reconstitutes a `RawVec` from a pointer, capacity, and allocator. + /// + /// # Safety + /// + /// The `ptr` must be allocated (via the given allocator `alloc`), and with the given + /// `capacity`. + /// The `capacity` cannot exceed `isize::MAX` for sized types. (only a concern on 32-bit + /// systems). For ZSTs capacity is ignored. + /// If the `ptr` and `capacity` come from a `RawVec` created via `alloc`, then this is + /// guaranteed. + #[inline] + pub unsafe fn from_raw_parts_in(ptr: *mut T, capacity: usize, alloc: A) -> Self { + // SAFETY: Precondition passed to the caller + unsafe { + Self { + inner: RawVecInner::from_raw_parts_in(ptr, capacity, alloc), + _marker: PhantomData, + } + } + } + + /// A convenience method for hoisting the non-null precondition out of [`RawVec::from_raw_parts_in`]. + /// + /// # Safety + /// + /// See [`RawVec::from_raw_parts_in`]. + #[inline] + pub unsafe fn from_nonnull_in(ptr: NonNull, capacity: usize, alloc: A) -> Self { + // SAFETY: Precondition passed to the caller + unsafe { + Self { inner: RawVecInner::from_nonnull_in(ptr, capacity, alloc), _marker: PhantomData } + } + } + + /// Gets a raw pointer to the start of the allocation. Note that this is + /// `Unique::dangling()` if `capacity == 0` or `T` is zero-sized. In the former case, you must + /// be careful. + #[inline] + pub fn ptr(&self) -> *mut T { + self.inner.ptr() + } + + #[inline] + pub fn non_null(&self) -> NonNull { + self.inner.non_null() + } + + /// Gets the capacity of the allocation. + /// + /// This will always be `usize::MAX` if `T` is zero-sized. + #[inline] + pub fn capacity(&self) -> usize { + self.inner.capacity(size_of::()) + } + + /// Returns a shared reference to the allocator backing this `RawVec`. + #[inline] + pub fn allocator(&self) -> &A { + self.inner.allocator() + } + + /// Ensures that the buffer contains at least enough space to hold `len + + /// additional` elements. If it doesn't already have enough capacity, will + /// reallocate enough space plus comfortable slack space to get amortized + /// *O*(1) behavior. Will limit this behavior if it would needlessly cause + /// itself to panic. + /// + /// If `len` exceeds `self.capacity()`, this may fail to actually allocate + /// the requested space. This is not really unsafe, but the unsafe + /// code *you* write that relies on the behavior of this function may break. + /// + /// This is ideal for implementing a bulk-push operation like `extend`. + /// + /// # Panics + /// + /// Panics if the new capacity exceeds `isize::MAX` _bytes_. + /// + /// # Aborts + /// + /// Aborts on OOM. + #[cfg(not(no_global_oom_handling))] + #[inline] + pub fn reserve(&mut self, len: usize, additional: usize) { + self.inner.reserve(len, additional, T::LAYOUT) + } + + /// A specialized version of `self.reserve(len, 1)` which requires the + /// caller to ensure `len == self.capacity()`. + #[cfg(not(no_global_oom_handling))] + #[inline(never)] + pub fn grow_one(&mut self) { + self.inner.grow_one(T::LAYOUT) + } + + /// The same as `reserve`, but returns on errors instead of panicking or aborting. + pub fn try_reserve(&mut self, len: usize, additional: usize) -> Result<(), TryReserveError> { + self.inner.try_reserve(len, additional, T::LAYOUT) + } + + /// Ensures that the buffer contains at least enough space to hold `len + + /// additional` elements. If it doesn't already, will reallocate the + /// minimum possible amount of memory necessary. Generally this will be + /// exactly the amount of memory necessary, but in principle the allocator + /// is free to give back more than we asked for. + /// + /// If `len` exceeds `self.capacity()`, this may fail to actually allocate + /// the requested space. This is not really unsafe, but the unsafe code + /// *you* write that relies on the behavior of this function may break. + /// + /// # Panics + /// + /// Panics if the new capacity exceeds `isize::MAX` _bytes_. + /// + /// # Aborts + /// + /// Aborts on OOM. + #[cfg(not(no_global_oom_handling))] + pub fn reserve_exact(&mut self, len: usize, additional: usize) { + self.inner.reserve_exact(len, additional, T::LAYOUT) + } + + /// The same as `reserve_exact`, but returns on errors instead of panicking or aborting. + pub fn try_reserve_exact( + &mut self, + len: usize, + additional: usize, + ) -> Result<(), TryReserveError> { + self.inner.try_reserve_exact(len, additional, T::LAYOUT) + } + + /// Shrinks the buffer down to the specified capacity. If the given amount + /// is 0, actually completely deallocates. + /// + /// # Panics + /// + /// Panics if the given amount is *larger* than the current capacity. + /// + /// # Aborts + /// + /// Aborts on OOM. + #[cfg(not(no_global_oom_handling))] + #[inline] + pub fn shrink_to_fit(&mut self, cap: usize) { + self.inner.shrink_to_fit(cap, T::LAYOUT) + } +} + +unsafe impl<#[may_dangle] T, A: Allocator> Drop for RawVec { + /// Frees the memory owned by the `RawVec` *without* trying to drop its contents. + fn drop(&mut self) { + self.inner.drop(T::LAYOUT) + } +} + +impl RawVecInner { + #[inline] + #[rustc_const_stable(feature = "raw_vec_internals_const", since = "1.81")] + const fn new_in(alloc: A, align: usize) -> Self { + let ptr = unsafe { core::mem::transmute(align) }; + // `cap: 0` means "unallocated". zero-sized types are ignored. + Self { ptr, cap: Cap::ZERO, alloc } + } + + #[cfg(not(no_global_oom_handling))] + #[inline] + fn with_capacity_in(capacity: usize, alloc: A, elem_layout: Layout) -> Self { + match Self::try_allocate_in(capacity, AllocInit::Uninitialized, alloc, elem_layout) { + Ok(res) => res, + Err(err) => handle_error(err), + } + } + + #[inline] + fn try_with_capacity_in( + capacity: usize, + alloc: A, + elem_layout: Layout, + ) -> Result { + Self::try_allocate_in(capacity, AllocInit::Uninitialized, alloc, elem_layout) + } + + #[cfg(not(no_global_oom_handling))] + #[inline] + fn with_capacity_zeroed_in(capacity: usize, alloc: A, elem_layout: Layout) -> Self { + match Self::try_allocate_in(capacity, AllocInit::Zeroed, alloc, elem_layout) { + Ok(res) => res, + Err(err) => handle_error(err), } } @@ -245,57 +467,35 @@ impl RawVec { } } - /// Reconstitutes a `RawVec` from a pointer, capacity, and allocator. - /// - /// # Safety - /// - /// The `ptr` must be allocated (via the given allocator `alloc`), and with the given - /// `capacity`. - /// The `capacity` cannot exceed `isize::MAX` for sized types. (only a concern on 32-bit - /// systems). For ZSTs capacity is ignored. - /// If the `ptr` and `capacity` come from a `RawVec` created via `alloc`, then this is - /// guaranteed. #[inline] - pub unsafe fn from_raw_parts_in(ptr: *mut T, capacity: usize, alloc: A) -> Self { + unsafe fn from_raw_parts_in(ptr: *mut T, capacity: usize, alloc: A) -> Self { let cap = if T::IS_ZST { Cap::ZERO } else { unsafe { Cap(capacity) } }; Self { ptr: unsafe { Unique::new_unchecked(ptr.cast()) }, cap, alloc } } - /// A convenience method for hoisting the non-null precondition out of [`RawVec::from_raw_parts_in`]. - /// - /// # Safety - /// - /// See [`RawVec::from_raw_parts_in`]. #[inline] - pub unsafe fn from_nonnull_in(ptr: NonNull, capacity: usize, alloc: A) -> Self { + unsafe fn from_nonnull_in(ptr: NonNull, capacity: usize, alloc: A) -> Self { let cap = if T::IS_ZST { Cap::ZERO } else { unsafe { Cap(capacity) } }; Self { ptr: Unique::from(ptr.cast()), cap, alloc } } - /// Gets a raw pointer to the start of the allocation. Note that this is - /// `Unique::dangling()` if `capacity == 0` or `T` is zero-sized. In the former case, you must - /// be careful. #[inline] - pub fn ptr(&self) -> *mut T { + fn ptr(&self) -> *mut T { unsafe { core::mem::transmute(self.ptr) } } #[inline] - pub fn non_null(&self) -> NonNull { + fn non_null(&self) -> NonNull { unsafe { core::mem::transmute(self.ptr) } } - /// Gets the capacity of the allocation. - /// - /// This will always be `usize::MAX` if `T` is zero-sized. #[inline] - pub fn capacity(&self, elem_size: usize) -> usize { + fn capacity(&self, elem_size: usize) -> usize { if elem_size == 0 { usize::MAX } else { self.cap.0 } } - /// Returns a shared reference to the allocator backing this `RawVec`. #[inline] - pub fn allocator(&self) -> &A { + fn allocator(&self) -> &A { &self.alloc } @@ -316,35 +516,16 @@ impl RawVec { } } - /// Ensures that the buffer contains at least enough space to hold `len + - /// additional` elements. If it doesn't already have enough capacity, will - /// reallocate enough space plus comfortable slack space to get amortized - /// *O*(1) behavior. Will limit this behavior if it would needlessly cause - /// itself to panic. - /// - /// If `len` exceeds `self.capacity()`, this may fail to actually allocate - /// the requested space. This is not really unsafe, but the unsafe - /// code *you* write that relies on the behavior of this function may break. - /// - /// This is ideal for implementing a bulk-push operation like `extend`. - /// - /// # Panics - /// - /// Panics if the new capacity exceeds `isize::MAX` _bytes_. - /// - /// # Aborts - /// - /// Aborts on OOM. #[cfg(not(no_global_oom_handling))] #[inline] - pub fn reserve(&mut self, len: usize, additional: usize, elem_layout: Layout) { + fn reserve(&mut self, len: usize, additional: usize, elem_layout: Layout) { // Callers expect this function to be very cheap when there is already sufficient capacity. // Therefore, we move all the resizing and error-handling logic from grow_amortized and // handle_reserve behind a call, while making sure that this function is likely to be // inlined as just a comparison and a call if the comparison fails. #[cold] fn do_reserve_and_handle( - slf: &mut RawVec, + slf: &mut RawVecInner, len: usize, additional: usize, elem_layout: Layout, @@ -359,18 +540,15 @@ impl RawVec { } } - /// A specialized version of `self.reserve(len, 1)` which requires the - /// caller to ensure `len == self.capacity()`. #[cfg(not(no_global_oom_handling))] #[inline(never)] - pub fn grow_one(&mut self, elem_layout: Layout) { + fn grow_one(&mut self, elem_layout: Layout) { if let Err(err) = self.grow_amortized(self.cap.0, 1, elem_layout) { handle_error(err); } } - /// The same as `reserve`, but returns on errors instead of panicking or aborting. - pub fn try_reserve( + fn try_reserve( &mut self, len: usize, additional: usize, @@ -386,32 +564,14 @@ impl RawVec { Ok(()) } - /// Ensures that the buffer contains at least enough space to hold `len + - /// additional` elements. If it doesn't already, will reallocate the - /// minimum possible amount of memory necessary. Generally this will be - /// exactly the amount of memory necessary, but in principle the allocator - /// is free to give back more than we asked for. - /// - /// If `len` exceeds `self.capacity()`, this may fail to actually allocate - /// the requested space. This is not really unsafe, but the unsafe code - /// *you* write that relies on the behavior of this function may break. - /// - /// # Panics - /// - /// Panics if the new capacity exceeds `isize::MAX` _bytes_. - /// - /// # Aborts - /// - /// Aborts on OOM. #[cfg(not(no_global_oom_handling))] - pub fn reserve_exact(&mut self, len: usize, additional: usize, elem_layout: Layout) { + fn reserve_exact(&mut self, len: usize, additional: usize, elem_layout: Layout) { if let Err(err) = self.try_reserve_exact(len, additional, elem_layout) { handle_error(err); } } - /// The same as `reserve_exact`, but returns on errors instead of panicking or aborting. - pub fn try_reserve_exact( + fn try_reserve_exact( &mut self, len: usize, additional: usize, @@ -427,34 +587,19 @@ impl RawVec { Ok(()) } - /// Shrinks the buffer down to the specified capacity. If the given amount - /// is 0, actually completely deallocates. - /// - /// # Panics - /// - /// Panics if the given amount is *larger* than the current capacity. - /// - /// # Aborts - /// - /// Aborts on OOM. #[cfg(not(no_global_oom_handling))] #[inline] - pub fn shrink_to_fit(&mut self, cap: usize, elem_layout: Layout) { + fn shrink_to_fit(&mut self, cap: usize, elem_layout: Layout) { if let Err(err) = self.shrink(cap, elem_layout) { handle_error(err); } } - /// Returns if the buffer needs to grow to fulfill the needed extra capacity. - /// Mainly used to make inlining reserve-calls possible without inlining `grow`. #[inline] fn needs_to_grow(&self, len: usize, additional: usize, elem_layout: Layout) -> bool { additional > self.capacity(elem_layout.size()).wrapping_sub(len) } - /// # Safety: - /// - /// `cap` must not exceed `isize::MAX`. #[inline] unsafe fn set_ptr_and_cap(&mut self, ptr: NonNull<[u8]>, cap: usize) { // Allocators currently return a `NonNull<[u8]>` whose length matches @@ -526,16 +671,6 @@ impl RawVec { unsafe { self.shrink_unchecked(cap, elem_layout) } } - /// `shrink`, but without the capacity check. - /// - /// This is split out so that `shrink` can inline the check, since it - /// optimizes out in things like `shrink_to_fit`, without needing to - /// also inline all this code, as doing that ends up failing the - /// `vec-shrink-panic` codegen test when `shrink_to_fit` ends up being too - /// big for LLVM to be willing to inline. - /// - /// # Safety - /// `cap <= self.capacity()` #[cfg(not(no_global_oom_handling))] unsafe fn shrink_unchecked( &mut self, @@ -571,7 +706,7 @@ impl RawVec { Ok(()) } - pub fn drop(&mut self, elem_layout: Layout) { + fn drop(&mut self, elem_layout: Layout) { if let Some((ptr, layout)) = self.current_memory(elem_layout) { unsafe { self.alloc.deallocate(ptr, layout) } } diff --git a/library/alloc/src/raw_vec/tests.rs b/library/alloc/src/raw_vec/tests.rs index 8852fc6cdfcf0..a345b0ba61e3e 100644 --- a/library/alloc/src/raw_vec/tests.rs +++ b/library/alloc/src/raw_vec/tests.rs @@ -41,129 +41,122 @@ fn allocator_param() { } let a = BoundedAlloc { fuel: Cell::new(500) }; - let mut v = RawVec::with_capacity_in(50, a, u8::LAYOUT); - assert_eq!(v.alloc.fuel.get(), 450); - v.reserve(50, 150, u8::LAYOUT); // (causes a realloc, thus using 50 + 150 = 200 units of fuel) - assert_eq!(v.alloc.fuel.get(), 250); - v.drop(u8::LAYOUT); + let mut v: RawVec = RawVec::with_capacity_in(50, a); + assert_eq!(v.inner.alloc.fuel.get(), 450); + v.reserve(50, 150); // (causes a realloc, thus using 50 + 150 = 200 units of fuel) + assert_eq!(v.inner.alloc.fuel.get(), 250); } #[test] fn reserve_does_not_overallocate() { { - let mut v = RawVec::new::(); + let mut v: RawVec = RawVec::new(); // First, `reserve` allocates like `reserve_exact`. - v.reserve(0, 9, u32::LAYOUT); - assert_eq!(9, v.capacity(u32::LAYOUT.size())); - v.drop(u32::LAYOUT); + v.reserve(0, 9); + assert_eq!(9, v.capacity()); } { - let mut v = RawVec::new::(); - v.reserve(0, 7, u32::LAYOUT); - assert_eq!(7, v.capacity(u32::LAYOUT.size())); + let mut v: RawVec = RawVec::new(); + v.reserve(0, 7); + assert_eq!(7, v.capacity()); // 97 is more than double of 7, so `reserve` should work // like `reserve_exact`. - v.reserve(7, 90, u32::LAYOUT); - assert_eq!(97, v.capacity(u32::LAYOUT.size())); - v.drop(u32::LAYOUT); + v.reserve(7, 90); + assert_eq!(97, v.capacity()); } { - let mut v = RawVec::new::(); - v.reserve(0, 12, u32::LAYOUT); - assert_eq!(12, v.capacity(u32::LAYOUT.size())); - v.reserve(12, 3, u32::LAYOUT); + let mut v: RawVec = RawVec::new(); + v.reserve(0, 12); + assert_eq!(12, v.capacity()); + v.reserve(12, 3); // 3 is less than half of 12, so `reserve` must grow // exponentially. At the time of writing this test grow // factor is 2, so new capacity is 24, however, grow factor // of 1.5 is OK too. Hence `>= 18` in assert. - assert!(v.capacity(u32::LAYOUT.size()) >= 12 + 12 / 2); - v.drop(u32::LAYOUT); + assert!(v.capacity() >= 12 + 12 / 2); } } struct ZST; // A `RawVec` holding zero-sized elements should always look like this. -fn zst_sanity(v: &RawVec) { - assert_eq!(v.capacity(ZST::LAYOUT.size()), usize::MAX); - assert_eq!(v.ptr::(), core::ptr::Unique::::dangling().as_ptr()); - assert_eq!(v.current_memory(ZST::LAYOUT), None); +fn zst_sanity(v: &RawVec) { + assert_eq!(v.capacity(), usize::MAX); + assert_eq!(v.ptr(), core::ptr::Unique::::dangling().as_ptr()); + assert_eq!(v.inner.current_memory(T::LAYOUT), None); } #[test] fn zst() { let cap_err = Err(crate::collections::TryReserveErrorKind::CapacityOverflow.into()); - assert_eq!(size_of::(), 0); + assert_eq!(std::mem::size_of::(), 0); // All these different ways of creating the RawVec produce the same thing. - let v = RawVec::new::(); + let v: RawVec = RawVec::new(); zst_sanity(&v); - let v = RawVec::with_capacity_in(100, Global, ZST::LAYOUT); + let v: RawVec = RawVec::with_capacity_in(100, Global); zst_sanity(&v); - let v = RawVec::with_capacity_in(100, Global, ZST::LAYOUT); + let v: RawVec = RawVec::with_capacity_in(100, Global); zst_sanity(&v); - let v = RawVec::try_allocate_in(0, AllocInit::Uninitialized, Global, ZST::LAYOUT).unwrap(); - zst_sanity(&v); - - let v = RawVec::try_allocate_in(100, AllocInit::Uninitialized, Global, ZST::LAYOUT).unwrap(); - zst_sanity(&v); - - let mut v = - RawVec::try_allocate_in(usize::MAX, AllocInit::Uninitialized, Global, ZST::LAYOUT).unwrap(); + let mut v: RawVec = RawVec::with_capacity_in(usize::MAX, Global); zst_sanity(&v); // Check all these operations work as expected with zero-sized elements. - assert!(!v.needs_to_grow(100, usize::MAX - 100, ZST::LAYOUT)); - assert!(v.needs_to_grow(101, usize::MAX - 100, ZST::LAYOUT)); - zst_sanity(&v); - - v.reserve(100, usize::MAX - 100, ZST::LAYOUT); + v.reserve(100, usize::MAX - 100); //v.reserve(101, usize::MAX - 100); // panics, in `zst_reserve_panic` below zst_sanity(&v); - v.reserve_exact(100, usize::MAX - 100, ZST::LAYOUT); + v.reserve_exact(100, usize::MAX - 100); //v.reserve_exact(101, usize::MAX - 100); // panics, in `zst_reserve_exact_panic` below zst_sanity(&v); - assert_eq!(v.try_reserve(100, usize::MAX - 100, ZST::LAYOUT), Ok(())); - assert_eq!(v.try_reserve(101, usize::MAX - 100, ZST::LAYOUT), cap_err); + assert_eq!(v.try_reserve(100, usize::MAX - 100), Ok(())); + assert_eq!(v.try_reserve(101, usize::MAX - 100), cap_err); zst_sanity(&v); - assert_eq!(v.try_reserve_exact(100, usize::MAX - 100, ZST::LAYOUT), Ok(())); - assert_eq!(v.try_reserve_exact(101, usize::MAX - 100, ZST::LAYOUT), cap_err); + assert_eq!(v.try_reserve_exact(100, usize::MAX - 100), Ok(())); + assert_eq!(v.try_reserve_exact(101, usize::MAX - 100), cap_err); zst_sanity(&v); - assert_eq!(v.grow_amortized(100, usize::MAX - 100, ZST::LAYOUT), cap_err); - assert_eq!(v.grow_amortized(101, usize::MAX - 100, ZST::LAYOUT), cap_err); + assert_eq!(v.inner.grow_amortized(100, usize::MAX - 100, ZST::LAYOUT), cap_err); + assert_eq!(v.inner.grow_amortized(101, usize::MAX - 100, ZST::LAYOUT), cap_err); zst_sanity(&v); - assert_eq!(v.grow_exact(100, usize::MAX - 100, ZST::LAYOUT), cap_err); - assert_eq!(v.grow_exact(101, usize::MAX - 100, ZST::LAYOUT), cap_err); + assert_eq!(v.inner.grow_exact(100, usize::MAX - 100, ZST::LAYOUT), cap_err); + assert_eq!(v.inner.grow_exact(101, usize::MAX - 100, ZST::LAYOUT), cap_err); zst_sanity(&v); } #[test] #[should_panic(expected = "capacity overflow")] fn zst_reserve_panic() { - let mut v: RawVec = RawVec::new::(); + let mut v: RawVec = RawVec::new(); zst_sanity(&v); - v.reserve(101, usize::MAX - 100, ZST::LAYOUT); + v.reserve(101, usize::MAX - 100); } #[test] #[should_panic(expected = "capacity overflow")] fn zst_reserve_exact_panic() { - let mut v: RawVec = RawVec::new::(); + let mut v: RawVec = RawVec::new(); zst_sanity(&v); - v.reserve_exact(101, usize::MAX - 100, ZST::LAYOUT); + v.reserve_exact(101, usize::MAX - 100); +} + +#[test] +fn niches() { + let baseline = size_of::>(); + assert_eq!(size_of::>>(), baseline); + assert_eq!(size_of::>>>(), baseline); + assert_eq!(size_of::>>>>(), baseline); } diff --git a/library/alloc/src/vec/in_place_drop.rs b/library/alloc/src/vec/in_place_drop.rs index 4d62d0949fd6a..4050c250130bb 100644 --- a/library/alloc/src/vec/in_place_drop.rs +++ b/library/alloc/src/vec/in_place_drop.rs @@ -1,5 +1,4 @@ use core::marker::PhantomData; -use core::mem::SizedTypeProperties; use core::ptr::NonNull; use core::ptr::{self, drop_in_place}; use core::slice::{self}; @@ -43,9 +42,9 @@ impl Drop for InPlaceDstDataSrcBufDrop { #[inline] fn drop(&mut self) { unsafe { - let mut buf = RawVec::from_nonnull_in(self.ptr.cast::(), self.src_cap, Global); + let _drop_allocation = + RawVec::::from_nonnull_in(self.ptr.cast::(), self.src_cap, Global); drop_in_place(core::ptr::slice_from_raw_parts_mut::(self.ptr.as_ptr(), self.len)); - buf.drop(Src::LAYOUT); }; } } diff --git a/library/alloc/src/vec/into_iter.rs b/library/alloc/src/vec/into_iter.rs index dfd1d8898f857..10f62e4bb62d8 100644 --- a/library/alloc/src/vec/into_iter.rs +++ b/library/alloc/src/vec/into_iter.rs @@ -141,7 +141,7 @@ impl IntoIter { // struct and then overwriting &mut self. // this creates less assembly self.cap = 0; - self.buf = RawVec::new::().non_null(); + self.buf = RawVec::NEW.non_null(); self.ptr = self.buf; self.end = self.buf.as_ptr(); @@ -491,8 +491,8 @@ unsafe impl<#[may_dangle] T, A: Allocator> Drop for IntoIter { unsafe { // `IntoIter::alloc` is not used anymore after this and will be dropped by RawVec let alloc = ManuallyDrop::take(&mut self.0.alloc); - let mut buf = RawVec::from_nonnull_in(self.0.buf, self.0.cap, alloc); - buf.drop(T::LAYOUT); + // RawVec handles deallocation + let _ = RawVec::from_nonnull_in(self.0.buf, self.0.cap, alloc); } } } diff --git a/library/alloc/src/vec/mod.rs b/library/alloc/src/vec/mod.rs index c649578b02a7b..729d5dd4fe4d2 100644 --- a/library/alloc/src/vec/mod.rs +++ b/library/alloc/src/vec/mod.rs @@ -396,9 +396,8 @@ mod spec_extend; #[cfg_attr(not(test), rustc_diagnostic_item = "Vec")] #[rustc_insignificant_dtor] pub struct Vec { - buf: RawVec, + buf: RawVec, len: usize, - _marker: PhantomData, } //////////////////////////////////////////////////////////////////////////////// @@ -421,7 +420,7 @@ impl Vec { #[stable(feature = "rust1", since = "1.0.0")] #[must_use] pub const fn new() -> Self { - Vec { buf: RawVec::new::(), len: 0, _marker: PhantomData } + Vec { buf: RawVec::NEW, len: 0 } } /// Constructs a new, empty `Vec` with at least the specified capacity. @@ -635,7 +634,7 @@ impl Vec { #[inline] #[unstable(feature = "allocator_api", issue = "32838")] pub const fn new_in(alloc: A) -> Self { - Vec { buf: RawVec::new_in(alloc, core::mem::align_of::()), len: 0, _marker: PhantomData } + Vec { buf: RawVec::new_in(alloc), len: 0 } } /// Constructs a new, empty `Vec` with at least the specified capacity @@ -697,11 +696,7 @@ impl Vec { #[inline] #[unstable(feature = "allocator_api", issue = "32838")] pub fn with_capacity_in(capacity: usize, alloc: A) -> Self { - Vec { - buf: RawVec::with_capacity_in(capacity, alloc, T::LAYOUT), - len: 0, - _marker: PhantomData, - } + Vec { buf: RawVec::with_capacity_in(capacity, alloc), len: 0 } } /// Constructs a new, empty `Vec` with at least the specified capacity @@ -719,11 +714,7 @@ impl Vec { #[unstable(feature = "allocator_api", issue = "32838")] // #[unstable(feature = "try_with_capacity", issue = "91913")] pub fn try_with_capacity_in(capacity: usize, alloc: A) -> Result { - Ok(Vec { - buf: RawVec::try_with_capacity_in(capacity, alloc, T::LAYOUT)?, - len: 0, - _marker: PhantomData, - }) + Ok(Vec { buf: RawVec::try_with_capacity_in(capacity, alloc)?, len: 0 }) } /// Creates a `Vec` directly from a pointer, a length, a capacity, @@ -837,13 +828,7 @@ impl Vec { #[inline] #[unstable(feature = "allocator_api", issue = "32838")] pub unsafe fn from_raw_parts_in(ptr: *mut T, length: usize, capacity: usize, alloc: A) -> Self { - unsafe { - Vec { - buf: RawVec::from_raw_parts_in(ptr, capacity, alloc), - len: length, - _marker: PhantomData, - } - } + unsafe { Vec { buf: RawVec::from_raw_parts_in(ptr, capacity, alloc), len: length } } } /// A convenience method for hoisting the non-null precondition out of [`Vec::from_raw_parts_in`]. @@ -859,13 +844,7 @@ impl Vec { capacity: usize, alloc: A, ) -> Self { - unsafe { - Vec { - buf: RawVec::from_nonnull_in(ptr, capacity, alloc), - len: length, - _marker: PhantomData, - } - } + unsafe { Vec { buf: RawVec::from_nonnull_in(ptr, capacity, alloc), len: length } } } /// Decomposes a `Vec` into its raw components: `(pointer, length, capacity)`. @@ -967,7 +946,7 @@ impl Vec { #[inline] #[stable(feature = "rust1", since = "1.0.0")] pub fn capacity(&self) -> usize { - self.buf.capacity(core::mem::size_of::()) + self.buf.capacity() } /// Reserves capacity for at least `additional` more elements to be inserted @@ -990,7 +969,7 @@ impl Vec { #[cfg(not(no_global_oom_handling))] #[stable(feature = "rust1", since = "1.0.0")] pub fn reserve(&mut self, additional: usize) { - self.buf.reserve(self.len, additional, T::LAYOUT); + self.buf.reserve(self.len, additional); } /// Reserves the minimum capacity for at least `additional` more elements to @@ -1020,7 +999,7 @@ impl Vec { #[cfg(not(no_global_oom_handling))] #[stable(feature = "rust1", since = "1.0.0")] pub fn reserve_exact(&mut self, additional: usize) { - self.buf.reserve_exact(self.len, additional, T::LAYOUT); + self.buf.reserve_exact(self.len, additional); } /// Tries to reserve capacity for at least `additional` more elements to be inserted @@ -1057,7 +1036,7 @@ impl Vec { /// ``` #[stable(feature = "try_reserve", since = "1.57.0")] pub fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError> { - self.buf.try_reserve(self.len, additional, T::LAYOUT) + self.buf.try_reserve(self.len, additional) } /// Tries to reserve the minimum capacity for at least `additional` @@ -1100,7 +1079,7 @@ impl Vec { /// ``` #[stable(feature = "try_reserve", since = "1.57.0")] pub fn try_reserve_exact(&mut self, additional: usize) -> Result<(), TryReserveError> { - self.buf.try_reserve_exact(self.len, additional, T::LAYOUT) + self.buf.try_reserve_exact(self.len, additional) } /// Shrinks the capacity of the vector as much as possible. @@ -1128,7 +1107,7 @@ impl Vec { // they are equal, so we can avoid the panic case in `RawVec::shrink_to_fit` // by only calling it with a greater capacity. if self.capacity() > self.len { - self.buf.shrink_to_fit(self.len, T::LAYOUT); + self.buf.shrink_to_fit(self.len); } } @@ -1154,7 +1133,7 @@ impl Vec { #[stable(feature = "shrink_to", since = "1.56.0")] pub fn shrink_to(&mut self, min_capacity: usize) { if self.capacity() > min_capacity { - self.buf.shrink_to_fit(cmp::max(self.len, min_capacity), T::LAYOUT); + self.buf.shrink_to_fit(cmp::max(self.len, min_capacity)); } } @@ -1598,8 +1577,8 @@ impl Vec { } // space for the new element - if len == self.capacity() { - self.buf.grow_one(T::LAYOUT); + if len == self.buf.capacity() { + self.buf.grow_one(); } unsafe { @@ -2020,8 +1999,8 @@ impl Vec { let len = self.len; // This will panic or abort if we would allocate > isize::MAX bytes // or if the length increment would overflow for zero-sized types. - if len == self.capacity() { - self.buf.grow_one(T::LAYOUT); + if len == self.buf.capacity() { + self.buf.grow_one(); } unsafe { let end = self.as_mut_ptr().add(len); @@ -2068,7 +2047,7 @@ impl Vec { #[inline] #[unstable(feature = "vec_push_within_capacity", issue = "100486")] pub fn push_within_capacity(&mut self, value: T) -> Result<(), T> { - if self.len == self.capacity() { + if self.len == self.buf.capacity() { return Err(value); } unsafe { @@ -2466,7 +2445,7 @@ impl Vec { unsafe { slice::from_raw_parts_mut( self.as_mut_ptr().add(self.len) as *mut MaybeUninit, - self.capacity() - self.len, + self.buf.capacity() - self.len, ) } } @@ -2540,11 +2519,11 @@ impl Vec { let ptr = self.as_mut_ptr(); // SAFETY: // - `ptr` is guaranteed to be valid for `self.len` elements - // - but the allocation extends out to `self.capacity()` elements, possibly + // - but the allocation extends out to `self.buf.capacity()` elements, possibly // uninitialized let spare_ptr = unsafe { ptr.add(self.len) }; let spare_ptr = spare_ptr.cast::>(); - let spare_len = self.capacity() - self.len; + let spare_len = self.buf.capacity() - self.len; // SAFETY: // - `ptr` is guaranteed to be valid for `self.len` elements @@ -3026,13 +3005,13 @@ impl IntoIterator for Vec { let me = ManuallyDrop::new(self); let alloc = ManuallyDrop::new(ptr::read(me.allocator())); let buf = me.buf.non_null(); - let begin: *mut T = buf.as_ptr(); + let begin = buf.as_ptr(); let end = if T::IS_ZST { begin.wrapping_byte_add(me.len()) } else { begin.add(me.len()) as *const T }; - let cap = me.capacity(); + let cap = me.buf.capacity(); IntoIter { buf, phantom: PhantomData, cap, alloc, ptr: buf, end } } } @@ -3328,7 +3307,7 @@ unsafe impl<#[may_dangle] T, A: Allocator> Drop for Vec { // could avoid questions of validity in certain cases ptr::drop_in_place(ptr::slice_from_raw_parts_mut(self.as_mut_ptr(), self.len)) } - self.buf.drop(T::LAYOUT); + // RawVec handles deallocation } } diff --git a/library/alloc/src/vec/spec_from_elem.rs b/library/alloc/src/vec/spec_from_elem.rs index 6858bbee7e612..01a6db14474bb 100644 --- a/library/alloc/src/vec/spec_from_elem.rs +++ b/library/alloc/src/vec/spec_from_elem.rs @@ -1,5 +1,3 @@ -use core::marker::PhantomData; -use core::mem::SizedTypeProperties; use core::ptr; use crate::alloc::Allocator; @@ -24,11 +22,7 @@ impl SpecFromElem for T { #[inline] default fn from_elem(elem: T, n: usize, alloc: A) -> Vec { if elem.is_zero() { - return Vec { - buf: RawVec::with_capacity_zeroed_in(n, alloc, T::LAYOUT), - len: n, - _marker: PhantomData, - }; + return Vec { buf: RawVec::with_capacity_zeroed_in(n, alloc), len: n }; } let mut v = Vec::with_capacity_in(n, alloc); v.extend_with(n, elem); @@ -40,11 +34,7 @@ impl SpecFromElem for i8 { #[inline] fn from_elem(elem: i8, n: usize, alloc: A) -> Vec { if elem == 0 { - return Vec { - buf: RawVec::with_capacity_zeroed_in(n, alloc, i8::LAYOUT), - len: n, - _marker: PhantomData, - }; + return Vec { buf: RawVec::with_capacity_zeroed_in(n, alloc), len: n }; } let mut v = Vec::with_capacity_in(n, alloc); unsafe { @@ -59,11 +49,7 @@ impl SpecFromElem for u8 { #[inline] fn from_elem(elem: u8, n: usize, alloc: A) -> Vec { if elem == 0 { - return Vec { - buf: RawVec::with_capacity_zeroed_in(n, alloc, u8::LAYOUT), - len: n, - _marker: PhantomData, - }; + return Vec { buf: RawVec::with_capacity_zeroed_in(n, alloc), len: n }; } let mut v = Vec::with_capacity_in(n, alloc); unsafe { diff --git a/library/alloc/src/vec/spec_from_iter_nested.rs b/library/alloc/src/vec/spec_from_iter_nested.rs index 2af839f38610c..f915ebb86e5a5 100644 --- a/library/alloc/src/vec/spec_from_iter_nested.rs +++ b/library/alloc/src/vec/spec_from_iter_nested.rs @@ -2,7 +2,7 @@ use core::cmp; use core::iter::TrustedLen; use core::ptr; -use crate::raw_vec::min_non_zero_cap; +use crate::raw_vec::RawVec; use super::{SpecExtend, Vec}; @@ -28,7 +28,7 @@ where Some(element) => { let (lower, _) = iterator.size_hint(); let initial_capacity = - cmp::max(min_non_zero_cap(core::mem::size_of::()), lower.saturating_add(1)); + cmp::max(RawVec::::MIN_NON_ZERO_CAP, lower.saturating_add(1)); let mut vector = Vec::with_capacity(initial_capacity); unsafe { // SAFETY: We requested capacity at least 1 diff --git a/library/alloc/src/vec/splice.rs b/library/alloc/src/vec/splice.rs index 1bcd34a3a9f18..852fdcc3f5ce7 100644 --- a/library/alloc/src/vec/splice.rs +++ b/library/alloc/src/vec/splice.rs @@ -1,5 +1,4 @@ use crate::alloc::{Allocator, Global}; -use core::mem::SizedTypeProperties; use core::ptr::{self}; use core::slice::{self}; @@ -127,7 +126,7 @@ impl Drain<'_, T, A> { unsafe fn move_tail(&mut self, additional: usize) { let vec = unsafe { self.vec.as_mut() }; let len = self.tail_start + self.tail_len; - vec.buf.reserve(len, additional, T::LAYOUT); + vec.buf.reserve(len, additional); let new_tail_start = self.tail_start + additional; unsafe { diff --git a/src/etc/gdb_providers.py b/src/etc/gdb_providers.py index a7ae2cd02f852..e8f9dee07d3e9 100644 --- a/src/etc/gdb_providers.py +++ b/src/etc/gdb_providers.py @@ -56,7 +56,7 @@ def __init__(self, valobj): self._valobj = valobj vec = valobj["vec"] self._length = int(vec["len"]) - self._data_ptr = unwrap_unique_or_non_null(vec["buf"]["ptr"]) + self._data_ptr = unwrap_unique_or_non_null(vec["buf"]["inner"]["ptr"]) def to_string(self): return self._data_ptr.lazy_string(encoding="utf-8", length=self._length) @@ -74,7 +74,7 @@ def __init__(self, valobj): vec = buf[ZERO_FIELD] if is_windows else buf self._length = int(vec["len"]) - self._data_ptr = unwrap_unique_or_non_null(vec["buf"]["ptr"]) + self._data_ptr = unwrap_unique_or_non_null(vec["buf"]["inner"]["ptr"]) def to_string(self): return self._data_ptr.lazy_string(encoding="utf-8", length=self._length) @@ -137,7 +137,7 @@ class StdVecProvider(printer_base): def __init__(self, valobj): self._valobj = valobj self._length = int(valobj["len"]) - self._data_ptr = unwrap_unique_or_non_null(valobj["buf"]["ptr"]) + self._data_ptr = unwrap_unique_or_non_null(valobj["buf"]["inner"]["ptr"]) ptr_ty = gdb.Type.pointer(valobj.type.template_argument(0)) self._data_ptr = self._data_ptr.reinterpret_cast(ptr_ty) @@ -160,11 +160,11 @@ def __init__(self, valobj): self._head = int(valobj["head"]) self._size = int(valobj["len"]) # BACKCOMPAT: rust 1.75 - cap = valobj["buf"]["cap"] + cap = valobj["buf"]["inner"]["cap"] if cap.type.code != gdb.TYPE_CODE_INT: cap = cap[ZERO_FIELD] self._cap = int(cap) - self._data_ptr = unwrap_unique_or_non_null(valobj["buf"]["ptr"]) + self._data_ptr = unwrap_unique_or_non_null(valobj["buf"]["inner"]["ptr"]) ptr_ty = gdb.Type.pointer(valobj.type.template_argument(0)) self._data_ptr = self._data_ptr.reinterpret_cast(ptr_ty) diff --git a/tests/debuginfo/strings-and-strs.rs b/tests/debuginfo/strings-and-strs.rs index de2fb68d92f8b..2d29ac12bd8a0 100644 --- a/tests/debuginfo/strings-and-strs.rs +++ b/tests/debuginfo/strings-and-strs.rs @@ -7,7 +7,7 @@ // gdb-command:run // gdb-command:print plain_string -// gdbr-check:$1 = alloc::string::String {vec: alloc::vec::Vec {buf: alloc::raw_vec::RawVec {ptr: core::ptr::unique::Unique {pointer: core::ptr::non_null::NonNull {pointer: 0x[...]}, _marker: core::marker::PhantomData}, cap: alloc::raw_vec::Cap (5), alloc: alloc::alloc::Global}, len: 5, _marker: core::marker::PhantomData}} +// gdbr-check:$1 = alloc::string::String {vec: alloc::vec::Vec {buf: alloc::raw_vec::RawVec {inner: alloc::raw_vec::RawVecInner {ptr: core::ptr::unique::Unique {pointer: core::ptr::non_null::NonNull {pointer: 0x[...]}, _marker: core::marker::PhantomData}, cap: alloc::raw_vec::Cap (5), alloc: alloc::alloc::Global}, _marker: core::marker::PhantomData}, len: 5}} // gdb-command:print plain_str // gdbr-check:$2 = "Hello" diff --git a/tests/mir-opt/inline/inline_shims.drop.Inline.panic-abort.diff b/tests/mir-opt/inline/inline_shims.drop.Inline.panic-abort.diff index 9170d74596803..2a36ccaab110a 100644 --- a/tests/mir-opt/inline/inline_shims.drop.Inline.panic-abort.diff +++ b/tests/mir-opt/inline/inline_shims.drop.Inline.panic-abort.diff @@ -25,7 +25,7 @@ + StorageLive(_6); + StorageLive(_7); + _6 = &mut (*_4); -+ _7 = as Drop>::drop(move _6) -> [return: bb1, unwind unreachable]; ++ _7 = as Drop>::drop(move _6) -> [return: bb2, unwind unreachable]; } bb1: { @@ -39,18 +39,22 @@ + StorageLive(_8); + StorageLive(_9); + _8 = discriminant((*_5)); -+ switchInt(move _8) -> [0: bb2, otherwise: bb3]; ++ switchInt(move _8) -> [0: bb3, otherwise: bb4]; } bb2: { ++ drop(((*_4).0: alloc::raw_vec::RawVec)) -> [return: bb1, unwind unreachable]; ++ } ++ ++ bb3: { + StorageDead(_9); + StorageDead(_8); StorageDead(_5); return; + } + -+ bb3: { -+ drop((((*_5) as Some).0: B)) -> [return: bb2, unwind unreachable]; ++ bb4: { ++ drop((((*_5) as Some).0: B)) -> [return: bb3, unwind unreachable]; } } diff --git a/tests/mir-opt/inline/inline_shims.drop.Inline.panic-unwind.diff b/tests/mir-opt/inline/inline_shims.drop.Inline.panic-unwind.diff index 8548475216e29..e11561076e641 100644 --- a/tests/mir-opt/inline/inline_shims.drop.Inline.panic-unwind.diff +++ b/tests/mir-opt/inline/inline_shims.drop.Inline.panic-unwind.diff @@ -8,43 +8,33 @@ let _3: (); let mut _4: *mut std::vec::Vec; let mut _5: *mut std::option::Option; -+ scope 1 (inlined std::ptr::drop_in_place::> - shim(Some(Vec))) { -+ let mut _6: &mut std::vec::Vec; -+ let mut _7: (); -+ } -+ scope 2 (inlined std::ptr::drop_in_place::> - shim(Some(Option))) { -+ let mut _8: isize; -+ let mut _9: isize; ++ scope 1 (inlined std::ptr::drop_in_place::> - shim(Some(Option))) { ++ let mut _6: isize; ++ let mut _7: isize; + } bb0: { StorageLive(_3); StorageLive(_4); _4 = _1; -- _3 = std::ptr::drop_in_place::>(move _4) -> [return: bb1, unwind continue]; -+ StorageLive(_6); -+ StorageLive(_7); -+ _6 = &mut (*_4); -+ _7 = as Drop>::drop(move _6) -> [return: bb1, unwind continue]; + _3 = std::ptr::drop_in_place::>(move _4) -> [return: bb1, unwind continue]; } bb1: { -+ StorageDead(_7); -+ StorageDead(_6); StorageDead(_4); StorageDead(_3); StorageLive(_5); _5 = _2; - _0 = std::ptr::drop_in_place::>(move _5) -> [return: bb2, unwind continue]; -+ StorageLive(_8); -+ StorageLive(_9); -+ _8 = discriminant((*_5)); -+ switchInt(move _8) -> [0: bb2, otherwise: bb3]; ++ StorageLive(_6); ++ StorageLive(_7); ++ _6 = discriminant((*_5)); ++ switchInt(move _6) -> [0: bb2, otherwise: bb3]; } bb2: { -+ StorageDead(_9); -+ StorageDead(_8); ++ StorageDead(_7); ++ StorageDead(_6); StorageDead(_5); return; + } diff --git a/tests/mir-opt/pre-codegen/vec_deref.vec_deref_to_slice.PreCodegen.after.panic-abort.mir b/tests/mir-opt/pre-codegen/vec_deref.vec_deref_to_slice.PreCodegen.after.panic-abort.mir index 00618802db485..2824a964fa132 100644 --- a/tests/mir-opt/pre-codegen/vec_deref.vec_deref_to_slice.PreCodegen.after.panic-abort.mir +++ b/tests/mir-opt/pre-codegen/vec_deref.vec_deref_to_slice.PreCodegen.after.panic-abort.mir @@ -5,58 +5,65 @@ fn vec_deref_to_slice(_1: &Vec) -> &[u8] { let mut _0: &[u8]; scope 1 (inlined as Deref>::deref) { debug self => _1; - let mut _5: *const u8; - let mut _6: usize; + let mut _6: *const u8; + let mut _7: usize; scope 2 (inlined Vec::::as_ptr) { debug self => _1; - let mut _2: &alloc::raw_vec::RawVec; - let mut _4: *mut u8; - scope 3 (inlined alloc::raw_vec::RawVec::ptr::) { + let mut _2: &alloc::raw_vec::RawVec; + let mut _5: *mut u8; + scope 3 (inlined alloc::raw_vec::RawVec::::ptr) { debug self => _2; - let mut _3: std::ptr::Unique; + let mut _3: &alloc::raw_vec::RawVecInner; + scope 4 (inlined alloc::raw_vec::RawVecInner::ptr::) { + debug self => _3; + let mut _4: std::ptr::Unique; + } } } - scope 4 (inlined std::slice::from_raw_parts::<'_, u8>) { - debug data => _5; - debug len => _6; - let _7: *const [u8]; - scope 5 (inlined core::ub_checks::check_language_ub) { - scope 6 (inlined core::ub_checks::check_language_ub::runtime) { + scope 5 (inlined std::slice::from_raw_parts::<'_, u8>) { + debug data => _6; + debug len => _7; + let _8: *const [u8]; + scope 6 (inlined core::ub_checks::check_language_ub) { + scope 7 (inlined core::ub_checks::check_language_ub::runtime) { } } - scope 7 (inlined std::mem::size_of::) { + scope 8 (inlined std::mem::size_of::) { } - scope 8 (inlined align_of::) { + scope 9 (inlined align_of::) { } - scope 9 (inlined slice_from_raw_parts::) { - debug data => _5; - debug len => _6; - scope 10 (inlined std::ptr::from_raw_parts::<[u8], u8>) { - debug data_pointer => _5; - debug metadata => _6; + scope 10 (inlined slice_from_raw_parts::) { + debug data => _6; + debug len => _7; + scope 11 (inlined std::ptr::from_raw_parts::<[u8], u8>) { + debug data_pointer => _6; + debug metadata => _7; } } } } bb0: { - StorageLive(_4); StorageLive(_5); + StorageLive(_6); StorageLive(_2); - _2 = &((*_1).0: alloc::raw_vec::RawVec); + _2 = &((*_1).0: alloc::raw_vec::RawVec); StorageLive(_3); - _3 = (((*_1).0: alloc::raw_vec::RawVec).0: std::ptr::Unique); - _4 = move _3 as *mut u8 (Transmute); + _3 = &(((*_1).0: alloc::raw_vec::RawVec).0: alloc::raw_vec::RawVecInner); + StorageLive(_4); + _4 = ((((*_1).0: alloc::raw_vec::RawVec).0: alloc::raw_vec::RawVecInner).0: std::ptr::Unique); + _5 = move _4 as *mut u8 (Transmute); + StorageDead(_4); StorageDead(_3); - _5 = _4 as *const u8 (PtrToPtr); + _6 = _5 as *const u8 (PtrToPtr); StorageDead(_2); - StorageLive(_6); - _6 = ((*_1).1: usize); - _7 = *const [u8] from (_5, _6); + StorageLive(_7); + _7 = ((*_1).1: usize); + _8 = *const [u8] from (_6, _7); + StorageDead(_7); StorageDead(_6); StorageDead(_5); - StorageDead(_4); - _0 = &(*_7); + _0 = &(*_8); return; } } diff --git a/tests/mir-opt/pre-codegen/vec_deref.vec_deref_to_slice.PreCodegen.after.panic-unwind.mir b/tests/mir-opt/pre-codegen/vec_deref.vec_deref_to_slice.PreCodegen.after.panic-unwind.mir index 00618802db485..2824a964fa132 100644 --- a/tests/mir-opt/pre-codegen/vec_deref.vec_deref_to_slice.PreCodegen.after.panic-unwind.mir +++ b/tests/mir-opt/pre-codegen/vec_deref.vec_deref_to_slice.PreCodegen.after.panic-unwind.mir @@ -5,58 +5,65 @@ fn vec_deref_to_slice(_1: &Vec) -> &[u8] { let mut _0: &[u8]; scope 1 (inlined as Deref>::deref) { debug self => _1; - let mut _5: *const u8; - let mut _6: usize; + let mut _6: *const u8; + let mut _7: usize; scope 2 (inlined Vec::::as_ptr) { debug self => _1; - let mut _2: &alloc::raw_vec::RawVec; - let mut _4: *mut u8; - scope 3 (inlined alloc::raw_vec::RawVec::ptr::) { + let mut _2: &alloc::raw_vec::RawVec; + let mut _5: *mut u8; + scope 3 (inlined alloc::raw_vec::RawVec::::ptr) { debug self => _2; - let mut _3: std::ptr::Unique; + let mut _3: &alloc::raw_vec::RawVecInner; + scope 4 (inlined alloc::raw_vec::RawVecInner::ptr::) { + debug self => _3; + let mut _4: std::ptr::Unique; + } } } - scope 4 (inlined std::slice::from_raw_parts::<'_, u8>) { - debug data => _5; - debug len => _6; - let _7: *const [u8]; - scope 5 (inlined core::ub_checks::check_language_ub) { - scope 6 (inlined core::ub_checks::check_language_ub::runtime) { + scope 5 (inlined std::slice::from_raw_parts::<'_, u8>) { + debug data => _6; + debug len => _7; + let _8: *const [u8]; + scope 6 (inlined core::ub_checks::check_language_ub) { + scope 7 (inlined core::ub_checks::check_language_ub::runtime) { } } - scope 7 (inlined std::mem::size_of::) { + scope 8 (inlined std::mem::size_of::) { } - scope 8 (inlined align_of::) { + scope 9 (inlined align_of::) { } - scope 9 (inlined slice_from_raw_parts::) { - debug data => _5; - debug len => _6; - scope 10 (inlined std::ptr::from_raw_parts::<[u8], u8>) { - debug data_pointer => _5; - debug metadata => _6; + scope 10 (inlined slice_from_raw_parts::) { + debug data => _6; + debug len => _7; + scope 11 (inlined std::ptr::from_raw_parts::<[u8], u8>) { + debug data_pointer => _6; + debug metadata => _7; } } } } bb0: { - StorageLive(_4); StorageLive(_5); + StorageLive(_6); StorageLive(_2); - _2 = &((*_1).0: alloc::raw_vec::RawVec); + _2 = &((*_1).0: alloc::raw_vec::RawVec); StorageLive(_3); - _3 = (((*_1).0: alloc::raw_vec::RawVec).0: std::ptr::Unique); - _4 = move _3 as *mut u8 (Transmute); + _3 = &(((*_1).0: alloc::raw_vec::RawVec).0: alloc::raw_vec::RawVecInner); + StorageLive(_4); + _4 = ((((*_1).0: alloc::raw_vec::RawVec).0: alloc::raw_vec::RawVecInner).0: std::ptr::Unique); + _5 = move _4 as *mut u8 (Transmute); + StorageDead(_4); StorageDead(_3); - _5 = _4 as *const u8 (PtrToPtr); + _6 = _5 as *const u8 (PtrToPtr); StorageDead(_2); - StorageLive(_6); - _6 = ((*_1).1: usize); - _7 = *const [u8] from (_5, _6); + StorageLive(_7); + _7 = ((*_1).1: usize); + _8 = *const [u8] from (_6, _7); + StorageDead(_7); StorageDead(_6); StorageDead(_5); - StorageDead(_4); - _0 = &(*_7); + _0 = &(*_8); return; } } diff --git a/tests/mir-opt/unusual_item_types.core.ptr-drop_in_place.Vec_i32_.AddMovesForPackedDrops.before.mir b/tests/mir-opt/unusual_item_types.core.ptr-drop_in_place.Vec_i32_.AddMovesForPackedDrops.before.mir index 2c6e241dd6227..b587941835515 100644 --- a/tests/mir-opt/unusual_item_types.core.ptr-drop_in_place.Vec_i32_.AddMovesForPackedDrops.before.mir +++ b/tests/mir-opt/unusual_item_types.core.ptr-drop_in_place.Vec_i32_.AddMovesForPackedDrops.before.mir @@ -6,7 +6,7 @@ fn std::ptr::drop_in_place(_1: *mut Vec) -> () { let mut _3: (); bb0: { - goto -> bb4; + goto -> bb6; } bb1: { @@ -21,8 +21,16 @@ fn std::ptr::drop_in_place(_1: *mut Vec) -> () { goto -> bb1; } - bb4: { + bb4 (cleanup): { + drop(((*_1).0: alloc::raw_vec::RawVec)) -> [return: bb2, unwind terminate(cleanup)]; + } + + bb5: { + drop(((*_1).0: alloc::raw_vec::RawVec)) -> [return: bb3, unwind: bb2]; + } + + bb6: { _2 = &mut (*_1); - _3 = as Drop>::drop(move _2) -> [return: bb3, unwind: bb2]; + _3 = as Drop>::drop(move _2) -> [return: bb5, unwind: bb4]; } } From 597314bc1490bdddf5e795ee7eb1781b8ae86354 Mon Sep 17 00:00:00 2001 From: Ben Kimock Date: Mon, 15 Jul 2024 00:25:42 -0400 Subject: [PATCH 4/5] Remove unnecessary inline(never) and remove a use of ? in RawVec --- library/alloc/src/raw_vec.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/library/alloc/src/raw_vec.rs b/library/alloc/src/raw_vec.rs index 3d35c8347354e..403a9abe028e6 100644 --- a/library/alloc/src/raw_vec.rs +++ b/library/alloc/src/raw_vec.rs @@ -196,10 +196,10 @@ impl RawVec { /// allocator for the returned `RawVec`. #[inline] pub fn try_with_capacity_in(capacity: usize, alloc: A) -> Result { - Ok(Self { - inner: RawVecInner::try_with_capacity_in(capacity, alloc, T::LAYOUT)?, - _marker: PhantomData, - }) + match RawVecInner::try_with_capacity_in(capacity, alloc, T::LAYOUT) { + Ok(inner) => Ok(Self { inner, _marker: PhantomData }), + Err(e) => Err(e), + } } /// Like `with_capacity_zeroed`, but parameterized over the choice @@ -541,7 +541,7 @@ impl RawVecInner { } #[cfg(not(no_global_oom_handling))] - #[inline(never)] + #[inline] fn grow_one(&mut self, elem_layout: Layout) { if let Err(err) = self.grow_amortized(self.cap.0, 1, elem_layout) { handle_error(err); From d6bb817f05c7386c4146d4b06ab689d90df326fd Mon Sep 17 00:00:00 2001 From: Ben Kimock Date: Mon, 15 Jul 2024 19:13:58 -0400 Subject: [PATCH 5/5] Flatten RawVec::capacity --- library/alloc/src/raw_vec.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/alloc/src/raw_vec.rs b/library/alloc/src/raw_vec.rs index 403a9abe028e6..59c0cda73eda2 100644 --- a/library/alloc/src/raw_vec.rs +++ b/library/alloc/src/raw_vec.rs @@ -291,7 +291,7 @@ impl RawVec { /// This will always be `usize::MAX` if `T` is zero-sized. #[inline] pub fn capacity(&self) -> usize { - self.inner.capacity(size_of::()) + if T::IS_ZST { usize::MAX } else { self.inner.cap.0 } } /// Returns a shared reference to the allocator backing this `RawVec`.