Skip to content

Commit 5451c6d

Browse files
committed
Add #[track_caller] to allocating methods of Vec & VecDeque
1 parent f6236f6 commit 5451c6d

12 files changed

+102
-4
lines changed

Diff for: library/alloc/src/collections/vec_deque/mod.rs

+25
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ pub struct VecDeque<
107107

108108
#[stable(feature = "rust1", since = "1.0.0")]
109109
impl<T: Clone, A: Allocator + Clone> Clone for VecDeque<T, A> {
110+
#[track_caller]
110111
fn clone(&self) -> Self {
111112
let mut deq = Self::with_capacity_in(self.len(), self.allocator().clone());
112113
deq.extend(self.iter().cloned());
@@ -117,6 +118,7 @@ impl<T: Clone, A: Allocator + Clone> Clone for VecDeque<T, A> {
117118
///
118119
/// This method is preferred over simply assigning `source.clone()` to `self`,
119120
/// as it avoids reallocation if possible.
121+
#[track_caller]
120122
fn clone_from(&mut self, source: &Self) {
121123
self.clear();
122124
self.extend(source.iter().cloned());
@@ -480,6 +482,7 @@ impl<T, A: Allocator> VecDeque<T, A> {
480482
/// Frobs the head and tail sections around to handle the fact that we
481483
/// just reallocated. Unsafe because it trusts old_capacity.
482484
#[inline]
485+
#[track_caller]
483486
unsafe fn handle_capacity_increase(&mut self, old_capacity: usize) {
484487
let new_capacity = self.capacity();
485488
debug_assert!(new_capacity >= old_capacity);
@@ -560,6 +563,7 @@ impl<T> VecDeque<T> {
560563
#[inline]
561564
#[stable(feature = "rust1", since = "1.0.0")]
562565
#[must_use]
566+
#[track_caller]
563567
pub fn with_capacity(capacity: usize) -> VecDeque<T> {
564568
Self::with_capacity_in(capacity, Global)
565569
}
@@ -615,6 +619,7 @@ impl<T, A: Allocator> VecDeque<T, A> {
615619
/// let deque: VecDeque<u32> = VecDeque::with_capacity(10);
616620
/// ```
617621
#[unstable(feature = "allocator_api", issue = "32838")]
622+
#[track_caller]
618623
pub fn with_capacity_in(capacity: usize, alloc: A) -> VecDeque<T, A> {
619624
VecDeque { head: 0, len: 0, buf: RawVec::with_capacity_in(capacity, alloc) }
620625
}
@@ -779,6 +784,7 @@ impl<T, A: Allocator> VecDeque<T, A> {
779784
///
780785
/// [`reserve`]: VecDeque::reserve
781786
#[stable(feature = "rust1", since = "1.0.0")]
787+
#[track_caller]
782788
pub fn reserve_exact(&mut self, additional: usize) {
783789
let new_cap = self.len.checked_add(additional).expect("capacity overflow");
784790
let old_cap = self.capacity();
@@ -808,6 +814,7 @@ impl<T, A: Allocator> VecDeque<T, A> {
808814
/// assert!(buf.capacity() >= 11);
809815
/// ```
810816
#[stable(feature = "rust1", since = "1.0.0")]
817+
#[track_caller]
811818
pub fn reserve(&mut self, additional: usize) {
812819
let new_cap = self.len.checked_add(additional).expect("capacity overflow");
813820
let old_cap = self.capacity();
@@ -939,6 +946,7 @@ impl<T, A: Allocator> VecDeque<T, A> {
939946
/// assert!(buf.capacity() >= 4);
940947
/// ```
941948
#[stable(feature = "deque_extras_15", since = "1.5.0")]
949+
#[track_caller]
942950
pub fn shrink_to_fit(&mut self) {
943951
self.shrink_to(0);
944952
}
@@ -964,6 +972,7 @@ impl<T, A: Allocator> VecDeque<T, A> {
964972
/// assert!(buf.capacity() >= 4);
965973
/// ```
966974
#[stable(feature = "shrink_to", since = "1.56.0")]
975+
#[track_caller]
967976
pub fn shrink_to(&mut self, min_capacity: usize) {
968977
let target_cap = min_capacity.max(self.len);
969978

@@ -1729,6 +1738,7 @@ impl<T, A: Allocator> VecDeque<T, A> {
17291738
/// assert_eq!(d.front(), Some(&2));
17301739
/// ```
17311740
#[stable(feature = "rust1", since = "1.0.0")]
1741+
#[track_caller]
17321742
pub fn push_front(&mut self, value: T) {
17331743
if self.is_full() {
17341744
self.grow();
@@ -1756,6 +1766,7 @@ impl<T, A: Allocator> VecDeque<T, A> {
17561766
/// ```
17571767
#[stable(feature = "rust1", since = "1.0.0")]
17581768
#[rustc_confusables("push", "put", "append")]
1769+
#[track_caller]
17591770
pub fn push_back(&mut self, value: T) {
17601771
if self.is_full() {
17611772
self.grow();
@@ -1865,6 +1876,7 @@ impl<T, A: Allocator> VecDeque<T, A> {
18651876
/// assert_eq!(vec_deque, &['a', 'd', 'b', 'c']);
18661877
/// ```
18671878
#[stable(feature = "deque_extras_15", since = "1.5.0")]
1879+
#[track_caller]
18681880
pub fn insert(&mut self, index: usize, value: T) {
18691881
assert!(index <= self.len(), "index out of bounds");
18701882
if self.is_full() {
@@ -1968,6 +1980,7 @@ impl<T, A: Allocator> VecDeque<T, A> {
19681980
#[inline]
19691981
#[must_use = "use `.truncate()` if you don't need the other half"]
19701982
#[stable(feature = "split_off", since = "1.4.0")]
1983+
#[track_caller]
19711984
pub fn split_off(&mut self, at: usize) -> Self
19721985
where
19731986
A: Clone,
@@ -2034,6 +2047,7 @@ impl<T, A: Allocator> VecDeque<T, A> {
20342047
/// ```
20352048
#[inline]
20362049
#[stable(feature = "append", since = "1.4.0")]
2050+
#[track_caller]
20372051
pub fn append(&mut self, other: &mut Self) {
20382052
if T::IS_ZST {
20392053
self.len = self.len.checked_add(other.len).expect("capacity overflow");
@@ -2156,6 +2170,7 @@ impl<T, A: Allocator> VecDeque<T, A> {
21562170
// be called in cold paths.
21572171
// This may panic or abort
21582172
#[inline(never)]
2173+
#[track_caller]
21592174
fn grow(&mut self) {
21602175
// Extend or possibly remove this assertion when valid use-cases for growing the
21612176
// buffer without it being full emerge
@@ -2194,6 +2209,7 @@ impl<T, A: Allocator> VecDeque<T, A> {
21942209
/// assert_eq!(buf, [5, 10, 101, 102, 103]);
21952210
/// ```
21962211
#[stable(feature = "vec_resize_with", since = "1.33.0")]
2212+
#[track_caller]
21972213
pub fn resize_with(&mut self, new_len: usize, generator: impl FnMut() -> T) {
21982214
let len = self.len;
21992215

@@ -2740,6 +2756,7 @@ impl<T: Clone, A: Allocator> VecDeque<T, A> {
27402756
/// assert_eq!(buf, [5, 10, 20, 20, 20]);
27412757
/// ```
27422758
#[stable(feature = "deque_extras", since = "1.16.0")]
2759+
#[track_caller]
27432760
pub fn resize(&mut self, new_len: usize, value: T) {
27442761
if new_len > self.len() {
27452762
let extra = new_len - self.len();
@@ -2859,6 +2876,7 @@ impl<T, A: Allocator> IndexMut<usize> for VecDeque<T, A> {
28592876

28602877
#[stable(feature = "rust1", since = "1.0.0")]
28612878
impl<T> FromIterator<T> for VecDeque<T> {
2879+
#[track_caller]
28622880
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> VecDeque<T> {
28632881
SpecFromIter::spec_from_iter(iter.into_iter())
28642882
}
@@ -2898,33 +2916,39 @@ impl<'a, T, A: Allocator> IntoIterator for &'a mut VecDeque<T, A> {
28982916

28992917
#[stable(feature = "rust1", since = "1.0.0")]
29002918
impl<T, A: Allocator> Extend<T> for VecDeque<T, A> {
2919+
#[track_caller]
29012920
fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) {
29022921
<Self as SpecExtend<T, I::IntoIter>>::spec_extend(self, iter.into_iter());
29032922
}
29042923

29052924
#[inline]
2925+
#[track_caller]
29062926
fn extend_one(&mut self, elem: T) {
29072927
self.push_back(elem);
29082928
}
29092929

29102930
#[inline]
2931+
#[track_caller]
29112932
fn extend_reserve(&mut self, additional: usize) {
29122933
self.reserve(additional);
29132934
}
29142935
}
29152936

29162937
#[stable(feature = "extend_ref", since = "1.2.0")]
29172938
impl<'a, T: 'a + Copy, A: Allocator> Extend<&'a T> for VecDeque<T, A> {
2939+
#[track_caller]
29182940
fn extend<I: IntoIterator<Item = &'a T>>(&mut self, iter: I) {
29192941
self.spec_extend(iter.into_iter());
29202942
}
29212943

29222944
#[inline]
2945+
#[track_caller]
29232946
fn extend_one(&mut self, &elem: &'a T) {
29242947
self.push_back(elem);
29252948
}
29262949

29272950
#[inline]
2951+
#[track_caller]
29282952
fn extend_reserve(&mut self, additional: usize) {
29292953
self.reserve(additional);
29302954
}
@@ -3014,6 +3038,7 @@ impl<T, const N: usize> From<[T; N]> for VecDeque<T> {
30143038
/// let deq2: VecDeque<_> = [1, 2, 3, 4].into();
30153039
/// assert_eq!(deq1, deq2);
30163040
/// ```
3041+
#[track_caller]
30173042
fn from(arr: [T; N]) -> Self {
30183043
let mut deq = VecDeque::with_capacity(N);
30193044
let arr = ManuallyDrop::new(arr);

Diff for: library/alloc/src/collections/vec_deque/spec_extend.rs

+6
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@ use super::VecDeque;
77

88
// Specialization trait used for VecDeque::extend
99
pub(super) trait SpecExtend<T, I> {
10+
#[track_caller]
1011
fn spec_extend(&mut self, iter: I);
1112
}
1213

1314
impl<T, I, A: Allocator> SpecExtend<T, I> for VecDeque<T, A>
1415
where
1516
I: Iterator<Item = T>,
1617
{
18+
#[track_caller]
1719
default fn spec_extend(&mut self, mut iter: I) {
1820
// This function should be the moral equivalent of:
1921
//
@@ -53,6 +55,7 @@ impl<T, I, A: Allocator> SpecExtend<T, I> for VecDeque<T, A>
5355
where
5456
I: TrustedLen<Item = T>,
5557
{
58+
#[track_caller]
5659
default fn spec_extend(&mut self, iter: I) {
5760
// This is the case for a TrustedLen iterator.
5861
let (low, high) = iter.size_hint();
@@ -85,6 +88,7 @@ where
8588
}
8689

8790
impl<T, A: Allocator> SpecExtend<T, vec::IntoIter<T>> for VecDeque<T, A> {
91+
#[track_caller]
8892
fn spec_extend(&mut self, mut iterator: vec::IntoIter<T>) {
8993
let slice = iterator.as_slice();
9094
self.reserve(slice.len());
@@ -102,6 +106,7 @@ where
102106
I: Iterator<Item = &'a T>,
103107
T: Copy,
104108
{
109+
#[track_caller]
105110
default fn spec_extend(&mut self, iterator: I) {
106111
self.spec_extend(iterator.copied())
107112
}
@@ -111,6 +116,7 @@ impl<'a, T: 'a, A: Allocator> SpecExtend<&'a T, slice::Iter<'a, T>> for VecDeque
111116
where
112117
T: Copy,
113118
{
119+
#[track_caller]
114120
fn spec_extend(&mut self, iterator: slice::Iter<'a, T>) {
115121
let slice = iterator.as_slice();
116122
self.reserve(slice.len());

Diff for: library/alloc/src/collections/vec_deque/spec_from_iter.rs

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ impl<T, I> SpecFromIter<T, I> for VecDeque<T>
99
where
1010
I: Iterator<Item = T>,
1111
{
12+
#[track_caller]
1213
default fn spec_from_iter(iterator: I) -> Self {
1314
// Since converting is O(1) now, just re-use the `Vec` logic for
1415
// anything where we can't do something extra-special for `VecDeque`,

Diff for: library/alloc/src/raw_vec.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ mod tests;
2121
// only one location which panics rather than a bunch throughout the module.
2222
#[cfg(not(no_global_oom_handling))]
2323
#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never))]
24-
fn capacity_overflow() -> ! {
24+
#[track_caller]
25+
pub(crate) fn capacity_overflow() -> ! {
2526
panic!("capacity overflow");
2627
}
2728

@@ -113,6 +114,7 @@ impl<T> RawVec<T, Global> {
113114
#[cfg(not(any(no_global_oom_handling, test)))]
114115
#[must_use]
115116
#[inline]
117+
#[track_caller]
116118
pub fn with_capacity(capacity: usize) -> Self {
117119
match Self::try_allocate_in(capacity, AllocInit::Uninitialized, Global) {
118120
Ok(res) => res,
@@ -124,6 +126,7 @@ impl<T> RawVec<T, Global> {
124126
#[cfg(not(any(no_global_oom_handling, test)))]
125127
#[must_use]
126128
#[inline]
129+
#[track_caller]
127130
pub fn with_capacity_zeroed(capacity: usize) -> Self {
128131
Self::with_capacity_zeroed_in(capacity, Global)
129132
}
@@ -154,6 +157,7 @@ impl<T, A: Allocator> RawVec<T, A> {
154157
/// allocator for the returned `RawVec`.
155158
#[cfg(not(no_global_oom_handling))]
156159
#[inline]
160+
#[track_caller]
157161
pub fn with_capacity_in(capacity: usize, alloc: A) -> Self {
158162
match Self::try_allocate_in(capacity, AllocInit::Uninitialized, alloc) {
159163
Ok(res) => res,
@@ -172,6 +176,7 @@ impl<T, A: Allocator> RawVec<T, A> {
172176
/// of allocator for the returned `RawVec`.
173177
#[cfg(not(no_global_oom_handling))]
174178
#[inline]
179+
#[track_caller]
175180
pub fn with_capacity_zeroed_in(capacity: usize, alloc: A) -> Self {
176181
match Self::try_allocate_in(capacity, AllocInit::Zeroed, alloc) {
177182
Ok(res) => res,
@@ -335,6 +340,7 @@ impl<T, A: Allocator> RawVec<T, A> {
335340
/// Aborts on OOM.
336341
#[cfg(not(no_global_oom_handling))]
337342
#[inline]
343+
#[track_caller]
338344
pub fn reserve(&mut self, len: usize, additional: usize) {
339345
// Callers expect this function to be very cheap when there is already sufficient capacity.
340346
// Therefore, we move all the resizing and error-handling logic from grow_amortized and
@@ -360,6 +366,7 @@ impl<T, A: Allocator> RawVec<T, A> {
360366
/// caller to ensure `len == self.capacity()`.
361367
#[cfg(not(no_global_oom_handling))]
362368
#[inline(never)]
369+
#[track_caller]
363370
pub fn grow_one(&mut self) {
364371
if let Err(err) = self.grow_amortized(self.cap.0, 1) {
365372
handle_error(err);
@@ -396,6 +403,7 @@ impl<T, A: Allocator> RawVec<T, A> {
396403
///
397404
/// Aborts on OOM.
398405
#[cfg(not(no_global_oom_handling))]
406+
#[track_caller]
399407
pub fn reserve_exact(&mut self, len: usize, additional: usize) {
400408
if let Err(err) = self.try_reserve_exact(len, additional) {
401409
handle_error(err);
@@ -429,6 +437,7 @@ impl<T, A: Allocator> RawVec<T, A> {
429437
///
430438
/// Aborts on OOM.
431439
#[cfg(not(no_global_oom_handling))]
440+
#[track_caller]
432441
pub fn shrink_to_fit(&mut self, cap: usize) {
433442
if let Err(err) = self.shrink(cap) {
434443
handle_error(err);
@@ -510,7 +519,6 @@ impl<T, A: Allocator> RawVec<T, A> {
510519
Ok(())
511520
}
512521

513-
#[cfg(not(no_global_oom_handling))]
514522
fn shrink(&mut self, cap: usize) -> Result<(), TryReserveError> {
515523
assert!(cap <= self.capacity(), "Tried to shrink to a larger capacity");
516524

@@ -588,6 +596,7 @@ unsafe impl<#[may_dangle] T, A: Allocator> Drop for RawVec<T, A> {
588596
// Central function for reserve error handling.
589597
#[cfg(not(no_global_oom_handling))]
590598
#[cold]
599+
#[track_caller]
591600
fn handle_error(e: TryReserveError) -> ! {
592601
match e.kind() {
593602
CapacityOverflow => capacity_overflow(),

Diff for: library/alloc/src/vec/cow.rs

+1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ impl<'a, T> FromIterator<T> for Cow<'a, [T]>
5959
where
6060
T: Clone,
6161
{
62+
#[track_caller]
6263
fn from_iter<I: IntoIterator<Item = T>>(it: I) -> Cow<'a, [T]> {
6364
Cow::Owned(FromIterator::from_iter(it))
6465
}

Diff for: library/alloc/src/vec/in_place_collect.rs

+2
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ where
229229
I: Iterator<Item = T> + InPlaceCollect,
230230
<I as SourceIter>::Source: AsVecIntoIter,
231231
{
232+
#[track_caller]
232233
default fn from_iter(iterator: I) -> Self {
233234
// Select the implementation in const eval to avoid codegen of the dead branch to improve compile times.
234235
let fun: fn(I) -> Vec<T> = const {
@@ -246,6 +247,7 @@ where
246247
}
247248
}
248249

250+
#[track_caller]
249251
fn from_iter_in_place<I, T>(mut iterator: I) -> Vec<T>
250252
where
251253
I: Iterator<Item = T> + InPlaceCollect,

0 commit comments

Comments
 (0)