Skip to content

Commit

Permalink
Improve panic messages
Browse files Browse the repository at this point in the history
  • Loading branch information
savannstm committed Jan 13, 2025
1 parent 7b64edc commit 2f55755
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 13 deletions.
37 changes: 30 additions & 7 deletions src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,13 @@ where
/// ```
#[track_caller]
pub fn insert_before(&mut self, mut index: usize, key: K, value: V) -> (usize, Option<V>) {
assert!(index <= self.len(), "index out of bounds");
let len = self.len();

assert!(
index <= len,
"index out of bounds: the len is {len} but the index is {index}. Expected index <= len"
);

match self.entry(key) {
Entry::Occupied(mut entry) => {
if index > entry.index() {
Expand Down Expand Up @@ -579,13 +585,21 @@ where
let len = self.len();
match self.entry(key) {
Entry::Occupied(mut entry) => {
assert!(index < len, "index out of bounds");
assert!(
index < len,
"index out of bounds: the len is {len} but the index is {index}"
);

let old = mem::replace(entry.get_mut(), value);
entry.move_index(index);
Some(old)
}
Entry::Vacant(entry) => {
assert!(index <= len, "index out of bounds");
assert!(
index <= len,
"index out of bounds: the len is {len} but the index is {index}. Expected index <= len"
);

entry.shift_insert(index, value);
None
}
Expand Down Expand Up @@ -1332,7 +1346,7 @@ where
///
/// ***Panics*** if `key` is not present in the map.
fn index(&self, key: &Q) -> &V {
self.get(key).expect("IndexMap: key not found")
self.get(key).expect("no entry found for key")
}
}

Expand Down Expand Up @@ -1374,7 +1388,7 @@ where
///
/// ***Panics*** if `key` is not present in the map.
fn index_mut(&mut self, key: &Q) -> &mut V {
self.get_mut(key).expect("IndexMap: key not found")
self.get_mut(key).expect("no entry found for key")
}
}

Expand Down Expand Up @@ -1418,7 +1432,12 @@ 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)
.expect("IndexMap: index out of bounds")
.unwrap_or_else(|| {
panic!(
"index out of bounds: the len is {len} but the index is {index}",
len = self.len()
);
})
.1
}
}
Expand Down Expand Up @@ -1457,8 +1476,12 @@ impl<K, V, S> IndexMut<usize> for IndexMap<K, V, S> {
///
/// ***Panics*** if `index` is out of bounds.
fn index_mut(&mut self, index: usize) -> &mut V {
let len: usize = self.len();

self.get_index_mut(index)
.expect("IndexMap: index out of bounds")
.unwrap_or_else(|| {
panic!("index out of bounds: the len is {len} but the index is {index}");
})
.1
}
}
Expand Down
7 changes: 6 additions & 1 deletion src/map/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,12 @@ impl<K, V> IndexMapCore<K, V> {

#[track_caller]
pub(crate) fn split_off(&mut self, at: usize) -> Self {
assert!(at <= self.entries.len());
let len = self.entries.len();
assert!(
at <= len,
"index out of bounds: the len is {len} but the index is {at}. Expected index <= len"
);

self.erase_indices(at, self.entries.len());
let entries = self.entries.split_off(at);

Expand Down
8 changes: 6 additions & 2 deletions src/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1106,8 +1106,12 @@ 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)
.expect("IndexSet: index out of bounds")
self.get_index(index).unwrap_or_else(|| {
panic!(
"index out of bounds: the len is {len} but the index is {index}",
len = self.len()
);
})
}
}

Expand Down
10 changes: 7 additions & 3 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,21 @@ where
Bound::Unbounded => 0,
Bound::Included(&i) if i <= len => i,
Bound::Excluded(&i) if i < len => i + 1,
bound => panic!("range start {:?} should be <= length {}", bound, len),
Bound::Included(i) | Bound::Excluded(i) => {
panic!("range start index {i} out of range for slice of length {len}")
}
};
let end = match range.end_bound() {
Bound::Unbounded => len,
Bound::Excluded(&i) if i <= len => i,
Bound::Included(&i) if i < len => i + 1,
bound => panic!("range end {:?} should be <= length {}", bound, len),
Bound::Included(i) | Bound::Excluded(i) => {
panic!("range end index {i} out of range for slice of length {len}")
}
};
if start > end {
panic!(
"range start {:?} should be <= range end {:?}",
"range start index {:?} should be <= range end index {:?}",
range.start_bound(),
range.end_bound()
);
Expand Down

0 comments on commit 2f55755

Please sign in to comment.