Skip to content

Commit

Permalink
Auto merge of #70573 - IgorPerikov:issue#70524_detailed_panic_message…
Browse files Browse the repository at this point in the history
…s, r=LukasKalbertodt

Detailed panic messages for Vec functions

pass indexes to insert, remove, drain, and split_off panic messages

closes #70524
  • Loading branch information
bors committed Apr 6, 2020
2 parents 4015890 + 9fc77c0 commit 6dee5f1
Showing 1 changed file with 55 additions and 6 deletions.
61 changes: 55 additions & 6 deletions src/liballoc/vec.rs
Expand Up @@ -964,8 +964,16 @@ impl<T> Vec<T> {
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn swap_remove(&mut self, index: usize) -> T {
#[cold]
#[inline(never)]
fn assert_failed(index: usize, len: usize) -> ! {
panic!("swap_remove index (is {}) should be < len (is {})", index, len);
}

let len = self.len();
assert!(index < len);
if !(index < len) {
assert_failed(index, len);
}
unsafe {
// We replace self[index] with the last element. Note that if the
// bounds check above succeeds there must be a last element (which
Expand Down Expand Up @@ -995,8 +1003,16 @@ impl<T> Vec<T> {
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn insert(&mut self, index: usize, element: T) {
#[cold]
#[inline(never)]
fn assert_failed(index: usize, len: usize) -> ! {
panic!("insertion index (is {}) should be <= len (is {})", index, len);
}

let len = self.len();
assert!(index <= len);
if !(index <= len) {
assert_failed(index, len);
}

// space for the new element
if len == self.buf.capacity() {
Expand Down Expand Up @@ -1035,8 +1051,16 @@ impl<T> Vec<T> {
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn remove(&mut self, index: usize) -> T {
#[cold]
#[inline(never)]
fn assert_failed(index: usize, len: usize) -> ! {
panic!("removal index (is {}) should be < len (is {})", index, len);
}

let len = self.len();
assert!(index < len);
if !(index < len) {
assert_failed(index, len);
}
unsafe {
// infallible
let ret;
Expand Down Expand Up @@ -1294,8 +1318,25 @@ impl<T> Vec<T> {
Excluded(&n) => n,
Unbounded => len,
};
assert!(start <= end);
assert!(end <= len);

#[cold]
#[inline(never)]
fn start_assert_failed(start: usize, end: usize) -> ! {
panic!("start drain index (is {}) should be <= end drain index (is {})", start, end);
}

#[cold]
#[inline(never)]
fn end_assert_failed(end: usize, len: usize) -> ! {
panic!("end drain index (is {}) should be <= len (is {})", end, len);
}

if !(start <= end) {
start_assert_failed(start, end);
}
if !(end <= len) {
end_assert_failed(end, len);
}

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

if !(at <= self.len()) {
assert_failed(at, self.len());
}

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

0 comments on commit 6dee5f1

Please sign in to comment.