diff --git a/library/alloc/src/raw_vec.rs b/library/alloc/src/raw_vec.rs index 56f4ebe57f8af..5cae87c217482 100644 --- a/library/alloc/src/raw_vec.rs +++ b/library/alloc/src/raw_vec.rs @@ -138,6 +138,7 @@ impl RawVec { /// Like `with_capacity`, but parameterized over the choice of /// allocator for the returned `RawVec`. #[inline] + #[track_caller] pub fn with_capacity_in(capacity: usize, alloc: A) -> Self { Self::allocate_in(capacity, AllocInit::Uninitialized, alloc) } @@ -145,6 +146,7 @@ impl RawVec { /// Like `with_capacity_zeroed`, but parameterized over the choice /// of allocator for the returned `RawVec`. #[inline] + #[track_caller] pub fn with_capacity_zeroed_in(capacity: usize, alloc: A) -> Self { Self::allocate_in(capacity, AllocInit::Zeroed, alloc) } @@ -183,6 +185,7 @@ impl RawVec { } } + #[track_caller] fn allocate_in(capacity: usize, init: AllocInit, alloc: A) -> Self { if mem::size_of::() == 0 { Self::new_in(alloc) @@ -315,6 +318,7 @@ impl RawVec { /// # vector.push_all(&[1, 3, 5, 7, 9]); /// # } /// ``` + #[track_caller] pub fn reserve(&mut self, len: usize, additional: usize) { handle_reserve(self.try_reserve(len, additional)); } @@ -345,6 +349,7 @@ impl RawVec { /// # Aborts /// /// Aborts on OOM. + #[track_caller] pub fn reserve_exact(&mut self, len: usize, additional: usize) { handle_reserve(self.try_reserve_exact(len, additional)); } @@ -368,6 +373,7 @@ impl RawVec { /// # Aborts /// /// Aborts on OOM. + #[track_caller] pub fn shrink_to_fit(&mut self, amount: usize) { handle_reserve(self.shrink(amount)); } @@ -503,6 +509,7 @@ unsafe impl<#[may_dangle] T, A: Allocator> Drop for RawVec { // Central function for reserve error handling. #[inline] +#[track_caller] fn handle_reserve(result: Result<(), TryReserveError>) { match result { Err(CapacityOverflow) => capacity_overflow(), @@ -532,6 +539,7 @@ fn alloc_guard(alloc_size: usize) -> Result<(), TryReserveError> { // One central function responsible for reporting capacity overflows. This'll // ensure that the code generation related to these panics is minimal as there's // only one location which panics rather than a bunch throughout the module. +#[track_caller] fn capacity_overflow() -> ! { panic!("capacity overflow"); } diff --git a/library/alloc/src/vec/mod.rs b/library/alloc/src/vec/mod.rs index b6166617789a0..21b7b92ddb1a4 100644 --- a/library/alloc/src/vec/mod.rs +++ b/library/alloc/src/vec/mod.rs @@ -769,6 +769,7 @@ impl Vec { /// assert!(vec.capacity() >= 11); /// ``` #[doc(alias = "realloc")] + #[track_caller] #[stable(feature = "rust1", since = "1.0.0")] pub fn reserve(&mut self, additional: usize) { self.buf.reserve(self.len, additional); @@ -795,6 +796,7 @@ impl Vec { /// assert!(vec.capacity() >= 11); /// ``` #[doc(alias = "realloc")] + #[track_caller] #[stable(feature = "rust1", since = "1.0.0")] pub fn reserve_exact(&mut self, additional: usize) { self.buf.reserve_exact(self.len, additional); @@ -895,6 +897,7 @@ impl Vec { /// assert!(vec.capacity() >= 3); /// ``` #[doc(alias = "realloc")] + #[track_caller] #[stable(feature = "rust1", since = "1.0.0")] pub fn shrink_to_fit(&mut self) { // The capacity is never less than the length, and there's nothing to do when @@ -925,6 +928,7 @@ impl Vec { /// assert!(vec.capacity() >= 3); /// ``` #[doc(alias = "realloc")] + #[track_caller] #[unstable(feature = "shrink_to", reason = "new API", issue = "56431")] pub fn shrink_to(&mut self, min_capacity: usize) { if self.capacity() > min_capacity { @@ -956,6 +960,7 @@ impl Vec { /// let slice = vec.into_boxed_slice(); /// assert_eq!(slice.into_vec().capacity(), 3); /// ``` + #[track_caller] #[stable(feature = "rust1", since = "1.0.0")] pub fn into_boxed_slice(mut self) -> Box<[T], A> { unsafe { @@ -1539,6 +1544,7 @@ impl Vec { /// assert_eq!(vec, [1, 2, 3]); /// ``` #[inline] + #[track_caller] #[stable(feature = "rust1", since = "1.0.0")] pub fn push(&mut self, value: T) { // This will panic or abort if we would allocate > isize::MAX bytes @@ -1592,6 +1598,7 @@ impl Vec { /// assert_eq!(vec2, []); /// ``` #[inline] + #[track_caller] #[stable(feature = "append", since = "1.4.0")] pub fn append(&mut self, other: &mut Self) { unsafe { @@ -1602,6 +1609,7 @@ impl Vec { /// Appends elements to `Self` from other buffer. #[inline] + #[track_caller] unsafe fn append_elements(&mut self, other: *const [T]) { let count = unsafe { (*other).len() }; self.reserve(count); @@ -2009,6 +2017,7 @@ impl Vec { /// ``` /// /// [`extend`]: Vec::extend + #[track_caller] #[stable(feature = "vec_extend_from_slice", since = "1.6.0")] pub fn extend_from_slice(&mut self, other: &[T]) { self.spec_extend(other.iter()) @@ -2086,6 +2095,7 @@ impl T> ExtendWith for ExtendFunc { impl Vec { /// Extend the vector by `n` values, using the given generator. + #[track_caller] fn extend_with>(&mut self, n: usize, mut value: E) { self.reserve(n); @@ -2298,6 +2308,7 @@ impl, A: Allocator> IndexMut for Vec { #[stable(feature = "rust1", since = "1.0.0")] impl FromIterator for Vec { #[inline] + #[track_caller] fn from_iter>(iter: I) -> Vec { >::from_iter(iter.into_iter()) } @@ -2386,6 +2397,7 @@ impl Extend for Vec { impl Vec { // leaf method to which various SpecFrom/SpecExtend implementations delegate when // they have no further optimizations to apply + #[track_caller] fn extend_desugared>(&mut self, mut iterator: I) { // This is the case for a general iterator. // @@ -2519,16 +2531,19 @@ impl Vec { /// [`copy_from_slice`]: ../../std/primitive.slice.html#method.copy_from_slice #[stable(feature = "extend_ref", since = "1.2.0")] impl<'a, T: Copy + 'a, A: Allocator + 'a> Extend<&'a T> for Vec { + #[track_caller] fn extend>(&mut self, iter: I) { self.spec_extend(iter.into_iter()) } #[inline] + #[track_caller] fn extend_one(&mut self, &item: &'a T) { self.push(item); } #[inline] + #[track_caller] fn extend_reserve(&mut self, additional: usize) { self.reserve(additional); } diff --git a/library/alloc/src/vec/spec_extend.rs b/library/alloc/src/vec/spec_extend.rs index b6186a7ebaf73..cba7513999fa2 100644 --- a/library/alloc/src/vec/spec_extend.rs +++ b/library/alloc/src/vec/spec_extend.rs @@ -14,6 +14,7 @@ impl SpecExtend for Vec where I: Iterator, { + #[track_caller] default fn spec_extend(&mut self, iter: I) { self.extend_desugared(iter) } @@ -23,6 +24,7 @@ impl SpecExtend for Vec where I: TrustedLen, { + #[track_caller] default fn spec_extend(&mut self, iterator: I) { // This is the case for a TrustedLen iterator. let (low, high) = iterator.size_hint(); diff --git a/library/alloc/src/vec/spec_from_iter.rs b/library/alloc/src/vec/spec_from_iter.rs index bbfcc68daeff4..fa2a9a98c5552 100644 --- a/library/alloc/src/vec/spec_from_iter.rs +++ b/library/alloc/src/vec/spec_from_iter.rs @@ -38,6 +38,7 @@ where } impl SpecFromIter> for Vec { + #[track_caller] fn from_iter(iterator: IntoIter) -> Self { // A common case is passing a vector into a function which immediately // re-collects into a vector. We can short circuit this if the IntoIter @@ -71,6 +72,7 @@ where I: Iterator, T: Clone, { + #[track_caller] default fn from_iter(iterator: I) -> Self { SpecFromIter::from_iter(iterator.cloned()) }