Skip to content

Commit 6dee5f1

Browse files
committed
Auto merge of #70573 - IgorPerikov:issue#70524_detailed_panic_messages, r=LukasKalbertodt
Detailed panic messages for Vec functions pass indexes to insert, remove, drain, and split_off panic messages closes #70524
2 parents 4015890 + 9fc77c0 commit 6dee5f1

File tree

1 file changed

+55
-6
lines changed

1 file changed

+55
-6
lines changed

src/liballoc/vec.rs

+55-6
Original file line numberDiff line numberDiff line change
@@ -964,8 +964,16 @@ impl<T> Vec<T> {
964964
#[inline]
965965
#[stable(feature = "rust1", since = "1.0.0")]
966966
pub fn swap_remove(&mut self, index: usize) -> T {
967+
#[cold]
968+
#[inline(never)]
969+
fn assert_failed(index: usize, len: usize) -> ! {
970+
panic!("swap_remove index (is {}) should be < len (is {})", index, len);
971+
}
972+
967973
let len = self.len();
968-
assert!(index < len);
974+
if !(index < len) {
975+
assert_failed(index, len);
976+
}
969977
unsafe {
970978
// We replace self[index] with the last element. Note that if the
971979
// bounds check above succeeds there must be a last element (which
@@ -995,8 +1003,16 @@ impl<T> Vec<T> {
9951003
/// ```
9961004
#[stable(feature = "rust1", since = "1.0.0")]
9971005
pub fn insert(&mut self, index: usize, element: T) {
1006+
#[cold]
1007+
#[inline(never)]
1008+
fn assert_failed(index: usize, len: usize) -> ! {
1009+
panic!("insertion index (is {}) should be <= len (is {})", index, len);
1010+
}
1011+
9981012
let len = self.len();
999-
assert!(index <= len);
1013+
if !(index <= len) {
1014+
assert_failed(index, len);
1015+
}
10001016

10011017
// space for the new element
10021018
if len == self.buf.capacity() {
@@ -1035,8 +1051,16 @@ impl<T> Vec<T> {
10351051
/// ```
10361052
#[stable(feature = "rust1", since = "1.0.0")]
10371053
pub fn remove(&mut self, index: usize) -> T {
1054+
#[cold]
1055+
#[inline(never)]
1056+
fn assert_failed(index: usize, len: usize) -> ! {
1057+
panic!("removal index (is {}) should be < len (is {})", index, len);
1058+
}
1059+
10381060
let len = self.len();
1039-
assert!(index < len);
1061+
if !(index < len) {
1062+
assert_failed(index, len);
1063+
}
10401064
unsafe {
10411065
// infallible
10421066
let ret;
@@ -1294,8 +1318,25 @@ impl<T> Vec<T> {
12941318
Excluded(&n) => n,
12951319
Unbounded => len,
12961320
};
1297-
assert!(start <= end);
1298-
assert!(end <= len);
1321+
1322+
#[cold]
1323+
#[inline(never)]
1324+
fn start_assert_failed(start: usize, end: usize) -> ! {
1325+
panic!("start drain index (is {}) should be <= end drain index (is {})", start, end);
1326+
}
1327+
1328+
#[cold]
1329+
#[inline(never)]
1330+
fn end_assert_failed(end: usize, len: usize) -> ! {
1331+
panic!("end drain index (is {}) should be <= len (is {})", end, len);
1332+
}
1333+
1334+
if !(start <= end) {
1335+
start_assert_failed(start, end);
1336+
}
1337+
if !(end <= len) {
1338+
end_assert_failed(end, len);
1339+
}
12991340

13001341
unsafe {
13011342
// set self.vec length's to start, to be safe in case Drain is leaked
@@ -1385,7 +1426,15 @@ impl<T> Vec<T> {
13851426
#[must_use = "use `.truncate()` if you don't need the other half"]
13861427
#[stable(feature = "split_off", since = "1.4.0")]
13871428
pub fn split_off(&mut self, at: usize) -> Self {
1388-
assert!(at <= self.len(), "`at` out of bounds");
1429+
#[cold]
1430+
#[inline(never)]
1431+
fn assert_failed(at: usize, len: usize) -> ! {
1432+
panic!("`at` split index (is {}) should be <= len (is {})", at, len);
1433+
}
1434+
1435+
if !(at <= self.len()) {
1436+
assert_failed(at, self.len());
1437+
}
13891438

13901439
let other_len = self.len - at;
13911440
let mut other = Vec::with_capacity(other_len);

0 commit comments

Comments
 (0)