Skip to content

Add #[track_caller] to all functions that are marked with ***Panics*** and can potentially panic #389

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1474,14 +1474,14 @@ impl<K, V, S> Index<usize> for IndexMap<K, V, S> {
///
/// ***Panics*** if `index` is out of bounds.
fn index(&self, index: usize) -> &V {
self.get_index(index)
.unwrap_or_else(|| {
panic!(
"index out of bounds: the len is {len} but the index is {index}",
len = self.len()
);
})
.1
if let Some((_, value)) = self.get_index(index) {
value
} else {
panic!(
"index out of bounds: the len is {len} but the index is {index}",
len = self.len()
);
}
}
}

Expand Down Expand Up @@ -1521,11 +1521,11 @@ impl<K, V, S> IndexMut<usize> for IndexMap<K, V, S> {
fn index_mut(&mut self, index: usize) -> &mut V {
let len: usize = self.len();

self.get_index_mut(index)
.unwrap_or_else(|| {
panic!("index out of bounds: the len is {len} but the index is {index}");
})
.1
if let Some((_, value)) = self.get_index_mut(index) {
value
} else {
panic!("index out of bounds: the len is {len} but the index is {index}");
}
}
}

Expand Down
7 changes: 0 additions & 7 deletions src/map/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,6 @@ impl<K, V> IndexMapCore<K, V> {
}
}

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

#[track_caller]
pub(crate) fn split_off(&mut self, at: usize) -> Self {
let len = self.entries.len();
assert!(
Expand All @@ -222,7 +220,6 @@ impl<K, V> IndexMapCore<K, V> {
Self { indices, entries }
}

#[track_caller]
pub(crate) fn split_splice<R>(&mut self, range: R) -> (Self, vec::IntoIter<Bucket<K, V>>)
where
R: RangeBounds<usize>,
Expand Down Expand Up @@ -411,13 +408,11 @@ impl<K, V> IndexMapCore<K, V> {
}

#[inline]
#[track_caller]
pub(super) fn move_index(&mut self, from: usize, to: usize) {
self.borrow_mut().move_index(from, to);
}

#[inline]
#[track_caller]
pub(crate) fn swap_indices(&mut self, a: usize, b: usize) {
self.borrow_mut().swap_indices(a, b);
}
Expand Down Expand Up @@ -684,7 +679,6 @@ impl<'a, K, V> RefMut<'a, K, V> {
}
}

#[track_caller]
fn move_index(&mut self, from: usize, to: usize) {
let from_hash = self.entries[from].hash;
let _ = self.entries[to]; // explicit bounds check
Expand All @@ -706,7 +700,6 @@ impl<'a, K, V> RefMut<'a, K, V> {
}
}

#[track_caller]
fn swap_indices(&mut self, a: usize, b: usize) {
// If they're equal and in-bounds, there's nothing to do.
if a == b && a < self.entries.len() {
Expand Down
2 changes: 0 additions & 2 deletions src/map/core/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,6 @@ impl<'a, K, V> OccupiedEntry<'a, K, V> {
/// ***Panics*** if `to` is out of bounds.
///
/// Computes in **O(n)** time (average).
#[track_caller]
pub fn move_index(self, to: usize) {
let index = self.index();
self.into_ref_mut().move_index(index, to);
Expand Down Expand Up @@ -533,7 +532,6 @@ impl<'a, K, V> IndexedEntry<'a, K, V> {
/// ***Panics*** if `to` is out of bounds.
///
/// Computes in **O(n)** time (average).
#[track_caller]
pub fn move_index(mut self, to: usize) {
self.map.move_index(self.index, to);
}
Expand Down
2 changes: 2 additions & 0 deletions src/map/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ impl<K, V> Slice<K, V> {
/// Divides one slice into two at an index.
///
/// ***Panics*** if `index > len`.
#[track_caller]
pub fn split_at(&self, index: usize) -> (&Self, &Self) {
let (first, second) = self.entries.split_at(index);
(Self::from_slice(first), Self::from_slice(second))
Expand All @@ -133,6 +134,7 @@ impl<K, V> Slice<K, V> {
/// Divides one mutable slice into two at an index.
///
/// ***Panics*** if `index > len`.
#[track_caller]
pub fn split_at_mut(&mut self, index: usize) -> (&mut Self, &mut Self) {
let (first, second) = self.entries.split_at_mut(index);
(Self::from_mut_slice(first), Self::from_mut_slice(second))
Expand Down
6 changes: 4 additions & 2 deletions src/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1106,12 +1106,14 @@ impl<T, S> Index<usize> for IndexSet<T, S> {
///
/// ***Panics*** if `index` is out of bounds.
fn index(&self, index: usize) -> &T {
self.get_index(index).unwrap_or_else(|| {
if let Some(value) = self.get_index(index) {
value
} else {
panic!(
"index out of bounds: the len is {len} but the index is {index}",
len = self.len()
);
})
}
}
}

Expand Down
1 change: 1 addition & 0 deletions src/set/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ impl<T> Slice<T> {
/// Divides one slice into two at an index.
///
/// ***Panics*** if `index > len`.
#[track_caller]
pub fn split_at(&self, index: usize) -> (&Self, &Self) {
let (first, second) = self.entries.split_at(index);
(Self::from_slice(first), Self::from_slice(second))
Expand Down
1 change: 0 additions & 1 deletion src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ pub(crate) fn third<A, B, C>(t: (A, B, C)) -> C {
t.2
}

#[track_caller]
pub(crate) fn simplify_range<R>(range: R, len: usize) -> Range<usize>
where
R: RangeBounds<usize>,
Expand Down