Skip to content

Commit 2f55755

Browse files
committed
Improve panic messages
1 parent 7b64edc commit 2f55755

File tree

4 files changed

+49
-13
lines changed

4 files changed

+49
-13
lines changed

src/map.rs

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,13 @@ where
497497
/// ```
498498
#[track_caller]
499499
pub fn insert_before(&mut self, mut index: usize, key: K, value: V) -> (usize, Option<V>) {
500-
assert!(index <= self.len(), "index out of bounds");
500+
let len = self.len();
501+
502+
assert!(
503+
index <= len,
504+
"index out of bounds: the len is {len} but the index is {index}. Expected index <= len"
505+
);
506+
501507
match self.entry(key) {
502508
Entry::Occupied(mut entry) => {
503509
if index > entry.index() {
@@ -579,13 +585,21 @@ where
579585
let len = self.len();
580586
match self.entry(key) {
581587
Entry::Occupied(mut entry) => {
582-
assert!(index < len, "index out of bounds");
588+
assert!(
589+
index < len,
590+
"index out of bounds: the len is {len} but the index is {index}"
591+
);
592+
583593
let old = mem::replace(entry.get_mut(), value);
584594
entry.move_index(index);
585595
Some(old)
586596
}
587597
Entry::Vacant(entry) => {
588-
assert!(index <= len, "index out of bounds");
598+
assert!(
599+
index <= len,
600+
"index out of bounds: the len is {len} but the index is {index}. Expected index <= len"
601+
);
602+
589603
entry.shift_insert(index, value);
590604
None
591605
}
@@ -1332,7 +1346,7 @@ where
13321346
///
13331347
/// ***Panics*** if `key` is not present in the map.
13341348
fn index(&self, key: &Q) -> &V {
1335-
self.get(key).expect("IndexMap: key not found")
1349+
self.get(key).expect("no entry found for key")
13361350
}
13371351
}
13381352

@@ -1374,7 +1388,7 @@ where
13741388
///
13751389
/// ***Panics*** if `key` is not present in the map.
13761390
fn index_mut(&mut self, key: &Q) -> &mut V {
1377-
self.get_mut(key).expect("IndexMap: key not found")
1391+
self.get_mut(key).expect("no entry found for key")
13781392
}
13791393
}
13801394

@@ -1418,7 +1432,12 @@ impl<K, V, S> Index<usize> for IndexMap<K, V, S> {
14181432
/// ***Panics*** if `index` is out of bounds.
14191433
fn index(&self, index: usize) -> &V {
14201434
self.get_index(index)
1421-
.expect("IndexMap: index out of bounds")
1435+
.unwrap_or_else(|| {
1436+
panic!(
1437+
"index out of bounds: the len is {len} but the index is {index}",
1438+
len = self.len()
1439+
);
1440+
})
14221441
.1
14231442
}
14241443
}
@@ -1457,8 +1476,12 @@ impl<K, V, S> IndexMut<usize> for IndexMap<K, V, S> {
14571476
///
14581477
/// ***Panics*** if `index` is out of bounds.
14591478
fn index_mut(&mut self, index: usize) -> &mut V {
1479+
let len: usize = self.len();
1480+
14601481
self.get_index_mut(index)
1461-
.expect("IndexMap: index out of bounds")
1482+
.unwrap_or_else(|| {
1483+
panic!("index out of bounds: the len is {len} but the index is {index}");
1484+
})
14621485
.1
14631486
}
14641487
}

src/map/core.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,12 @@ impl<K, V> IndexMapCore<K, V> {
208208

209209
#[track_caller]
210210
pub(crate) fn split_off(&mut self, at: usize) -> Self {
211-
assert!(at <= self.entries.len());
211+
let len = self.entries.len();
212+
assert!(
213+
at <= len,
214+
"index out of bounds: the len is {len} but the index is {at}. Expected index <= len"
215+
);
216+
212217
self.erase_indices(at, self.entries.len());
213218
let entries = self.entries.split_off(at);
214219

src/set.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1106,8 +1106,12 @@ impl<T, S> Index<usize> for IndexSet<T, S> {
11061106
///
11071107
/// ***Panics*** if `index` is out of bounds.
11081108
fn index(&self, index: usize) -> &T {
1109-
self.get_index(index)
1110-
.expect("IndexSet: index out of bounds")
1109+
self.get_index(index).unwrap_or_else(|| {
1110+
panic!(
1111+
"index out of bounds: the len is {len} but the index is {index}",
1112+
len = self.len()
1113+
);
1114+
})
11111115
}
11121116
}
11131117

src/util.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,21 @@ where
1313
Bound::Unbounded => 0,
1414
Bound::Included(&i) if i <= len => i,
1515
Bound::Excluded(&i) if i < len => i + 1,
16-
bound => panic!("range start {:?} should be <= length {}", bound, len),
16+
Bound::Included(i) | Bound::Excluded(i) => {
17+
panic!("range start index {i} out of range for slice of length {len}")
18+
}
1719
};
1820
let end = match range.end_bound() {
1921
Bound::Unbounded => len,
2022
Bound::Excluded(&i) if i <= len => i,
2123
Bound::Included(&i) if i < len => i + 1,
22-
bound => panic!("range end {:?} should be <= length {}", bound, len),
24+
Bound::Included(i) | Bound::Excluded(i) => {
25+
panic!("range end index {i} out of range for slice of length {len}")
26+
}
2327
};
2428
if start > end {
2529
panic!(
26-
"range start {:?} should be <= range end {:?}",
30+
"range start index {:?} should be <= range end index {:?}",
2731
range.start_bound(),
2832
range.end_bound()
2933
);

0 commit comments

Comments
 (0)