Skip to content
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

Remove unreachable panics from VecDeque::{front/back}[_mut] #80834

Merged
merged 1 commit into from
Jan 15, 2021
Merged
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
9 changes: 4 additions & 5 deletions library/alloc/src/collections/vec_deque/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1292,7 +1292,7 @@ impl<T> VecDeque<T> {
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn front(&self) -> Option<&T> {
if !self.is_empty() { Some(&self[0]) } else { None }
self.get(0)
}

/// Provides a mutable reference to the front element, or `None` if the
Expand All @@ -1316,7 +1316,7 @@ impl<T> VecDeque<T> {
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn front_mut(&mut self) -> Option<&mut T> {
if !self.is_empty() { Some(&mut self[0]) } else { None }
self.get_mut(0)
}

/// Provides a reference to the back element, or `None` if the `VecDeque` is
Expand All @@ -1336,7 +1336,7 @@ impl<T> VecDeque<T> {
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn back(&self) -> Option<&T> {
if !self.is_empty() { Some(&self[self.len() - 1]) } else { None }
self.get(self.len().wrapping_sub(1))
}

/// Provides a mutable reference to the back element, or `None` if the
Expand All @@ -1360,8 +1360,7 @@ impl<T> VecDeque<T> {
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn back_mut(&mut self) -> Option<&mut T> {
let len = self.len();
if !self.is_empty() { Some(&mut self[len - 1]) } else { None }
self.get_mut(self.len().wrapping_sub(1))
}

/// Removes the first element and returns it, or `None` if the `VecDeque` is
Expand Down
19 changes: 19 additions & 0 deletions src/test/codegen/vecdeque_no_panic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// This test checks that `VecDeque::front[_mut]()` and `VecDeque::back[_mut]()` can't panic.

// compile-flags: -O
// ignore-debug: the debug assertions get in the way

#![crate_type = "lib"]

use std::collections::VecDeque;

// CHECK-LABEL: @dont_panic
#[no_mangle]
pub fn dont_panic(v: &mut VecDeque<usize>) {
// CHECK-NOT: expect
// CHECK-NOT: panic
v.front();
v.front_mut();
v.back();
v.back_mut();
}