Skip to content

Commit 7b64edc

Browse files
committed
Add #[track_caller] attributes to functions that may panic
1 parent 31c9862 commit 7b64edc

File tree

7 files changed

+26
-0
lines changed

7 files changed

+26
-0
lines changed

src/map.rs

+7
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,7 @@ impl<K, V, S> IndexMap<K, V, S> {
299299
///
300300
/// ***Panics*** if the starting point is greater than the end point or if
301301
/// the end point is greater than the length of the map.
302+
#[track_caller]
302303
pub fn drain<R>(&mut self, range: R) -> Drain<'_, K, V>
303304
where
304305
R: RangeBounds<usize>,
@@ -313,6 +314,7 @@ impl<K, V, S> IndexMap<K, V, S> {
313314
/// the elements `[0, at)` with its previous capacity unchanged.
314315
///
315316
/// ***Panics*** if `at > len`.
317+
#[track_caller]
316318
pub fn split_off(&mut self, at: usize) -> Self
317319
where
318320
S: Clone,
@@ -493,6 +495,7 @@ where
493495
/// assert_eq!(map.get_index_of(&'+'), Some(27));
494496
/// assert_eq!(map.len(), 28);
495497
/// ```
498+
#[track_caller]
496499
pub fn insert_before(&mut self, mut index: usize, key: K, value: V) -> (usize, Option<V>) {
497500
assert!(index <= self.len(), "index out of bounds");
498501
match self.entry(key) {
@@ -571,6 +574,7 @@ where
571574
/// // This is an invalid index for moving an existing key!
572575
/// map.shift_insert(map.len(), 'a', ());
573576
/// ```
577+
#[track_caller]
574578
pub fn shift_insert(&mut self, index: usize, key: K, value: V) -> Option<V> {
575579
let len = self.len();
576580
match self.entry(key) {
@@ -627,6 +631,7 @@ where
627631
/// assert!(map.into_iter().eq([(0, '_'), (1, 'A'), (5, 'E'), (3, 'C'), (2, 'B'), (4, 'D')]));
628632
/// assert_eq!(removed, &[(2, 'b'), (3, 'c')]);
629633
/// ```
634+
#[track_caller]
630635
pub fn splice<R, I>(&mut self, range: R, replace_with: I) -> Splice<'_, I::IntoIter, K, V, S>
631636
where
632637
R: RangeBounds<usize>,
@@ -1278,6 +1283,7 @@ impl<K, V, S> IndexMap<K, V, S> {
12781283
/// ***Panics*** if `from` or `to` are out of bounds.
12791284
///
12801285
/// Computes in **O(n)** time (average).
1286+
#[track_caller]
12811287
pub fn move_index(&mut self, from: usize, to: usize) {
12821288
self.core.move_index(from, to)
12831289
}
@@ -1287,6 +1293,7 @@ impl<K, V, S> IndexMap<K, V, S> {
12871293
/// ***Panics*** if `a` or `b` are out of bounds.
12881294
///
12891295
/// Computes in **O(1)** time (average).
1296+
#[track_caller]
12901297
pub fn swap_indices(&mut self, a: usize, b: usize) {
12911298
self.core.swap_indices(a, b)
12921299
}

src/map/core.rs

+7
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ impl<K, V> IndexMapCore<K, V> {
183183
}
184184
}
185185

186+
#[track_caller]
186187
pub(crate) fn drain<R>(&mut self, range: R) -> vec::Drain<'_, Bucket<K, V>>
187188
where
188189
R: RangeBounds<usize>,
@@ -205,6 +206,7 @@ impl<K, V> IndexMapCore<K, V> {
205206
self.entries.par_drain(range)
206207
}
207208

209+
#[track_caller]
208210
pub(crate) fn split_off(&mut self, at: usize) -> Self {
209211
assert!(at <= self.entries.len());
210212
self.erase_indices(at, self.entries.len());
@@ -215,6 +217,7 @@ impl<K, V> IndexMapCore<K, V> {
215217
Self { indices, entries }
216218
}
217219

220+
#[track_caller]
218221
pub(crate) fn split_splice<R>(&mut self, range: R) -> (Self, vec::IntoIter<Bucket<K, V>>)
219222
where
220223
R: RangeBounds<usize>,
@@ -403,11 +406,13 @@ impl<K, V> IndexMapCore<K, V> {
403406
}
404407

405408
#[inline]
409+
#[track_caller]
406410
pub(super) fn move_index(&mut self, from: usize, to: usize) {
407411
self.borrow_mut().move_index(from, to);
408412
}
409413

410414
#[inline]
415+
#[track_caller]
411416
pub(crate) fn swap_indices(&mut self, a: usize, b: usize) {
412417
self.borrow_mut().swap_indices(a, b);
413418
}
@@ -670,6 +675,7 @@ impl<'a, K, V> RefMut<'a, K, V> {
670675
}
671676
}
672677

678+
#[track_caller]
673679
fn move_index(&mut self, from: usize, to: usize) {
674680
let from_hash = self.entries[from].hash;
675681
let _ = self.entries[to]; // explicit bounds check
@@ -691,6 +697,7 @@ impl<'a, K, V> RefMut<'a, K, V> {
691697
}
692698
}
693699

700+
#[track_caller]
694701
fn swap_indices(&mut self, a: usize, b: usize) {
695702
// If they're equal and in-bounds, there's nothing to do.
696703
if a == b && a < self.entries.len() {

src/map/core/entry.rs

+2
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,7 @@ impl<'a, K, V> OccupiedEntry<'a, K, V> {
294294
/// ***Panics*** if `to` is out of bounds.
295295
///
296296
/// Computes in **O(n)** time (average).
297+
#[track_caller]
297298
pub fn move_index(self, to: usize) {
298299
let index = self.index();
299300
self.into_ref_mut().move_index(index, to);
@@ -532,6 +533,7 @@ impl<'a, K, V> IndexedEntry<'a, K, V> {
532533
/// ***Panics*** if `to` is out of bounds.
533534
///
534535
/// Computes in **O(n)** time (average).
536+
#[track_caller]
535537
pub fn move_index(mut self, to: usize) {
536538
self.map.move_index(self.index, to);
537539
}

src/map/iter.rs

+1
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,7 @@ where
667667
K: Hash + Eq,
668668
S: BuildHasher,
669669
{
670+
#[track_caller]
670671
pub(super) fn new<R>(map: &'a mut IndexMap<K, V, S>, range: R, replace_with: I) -> Self
671672
where
672673
R: RangeBounds<usize>,

src/set.rs

+7
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ impl<T, S> IndexSet<T, S> {
250250
///
251251
/// ***Panics*** if the starting point is greater than the end point or if
252252
/// the end point is greater than the length of the set.
253+
#[track_caller]
253254
pub fn drain<R>(&mut self, range: R) -> Drain<'_, T>
254255
where
255256
R: RangeBounds<usize>,
@@ -264,6 +265,7 @@ impl<T, S> IndexSet<T, S> {
264265
/// the elements `[0, at)` with its previous capacity unchanged.
265266
///
266267
/// ***Panics*** if `at > len`.
268+
#[track_caller]
267269
pub fn split_off(&mut self, at: usize) -> Self
268270
where
269271
S: Clone,
@@ -426,6 +428,7 @@ where
426428
/// assert_eq!(set.get_index_of(&'+'), Some(27));
427429
/// assert_eq!(set.len(), 28);
428430
/// ```
431+
#[track_caller]
429432
pub fn insert_before(&mut self, index: usize, value: T) -> (usize, bool) {
430433
let (index, existing) = self.map.insert_before(index, value, ());
431434
(index, existing.is_none())
@@ -483,6 +486,7 @@ where
483486
/// // This is an invalid index for moving an existing value!
484487
/// set.shift_insert(set.len(), 'a');
485488
/// ```
489+
#[track_caller]
486490
pub fn shift_insert(&mut self, index: usize, value: T) -> bool {
487491
self.map.shift_insert(index, value, ()).is_none()
488492
}
@@ -584,6 +588,7 @@ where
584588
/// assert!(set.into_iter().eq([0, 1, 5, 3, 2, 4]));
585589
/// assert_eq!(removed, &[2, 3]);
586590
/// ```
591+
#[track_caller]
587592
pub fn splice<R, I>(&mut self, range: R, replace_with: I) -> Splice<'_, I::IntoIter, T, S>
588593
where
589594
R: RangeBounds<usize>,
@@ -1050,6 +1055,7 @@ impl<T, S> IndexSet<T, S> {
10501055
/// ***Panics*** if `from` or `to` are out of bounds.
10511056
///
10521057
/// Computes in **O(n)** time (average).
1058+
#[track_caller]
10531059
pub fn move_index(&mut self, from: usize, to: usize) {
10541060
self.map.move_index(from, to)
10551061
}
@@ -1059,6 +1065,7 @@ impl<T, S> IndexSet<T, S> {
10591065
/// ***Panics*** if `a` or `b` are out of bounds.
10601066
///
10611067
/// Computes in **O(1)** time (average).
1068+
#[track_caller]
10621069
pub fn swap_indices(&mut self, a: usize, b: usize) {
10631070
self.map.swap_indices(a, b)
10641071
}

src/set/iter.rs

+1
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,7 @@ where
542542
T: Hash + Eq,
543543
S: BuildHasher,
544544
{
545+
#[track_caller]
545546
pub(super) fn new<R>(set: &'a mut IndexSet<T, S>, range: R, replace_with: I) -> Self
546547
where
547548
R: RangeBounds<usize>,

src/util.rs

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ pub(crate) fn third<A, B, C>(t: (A, B, C)) -> C {
44
t.2
55
}
66

7+
#[track_caller]
78
pub(crate) fn simplify_range<R>(range: R, len: usize) -> Range<usize>
89
where
910
R: RangeBounds<usize>,

0 commit comments

Comments
 (0)