diff options
| author | Yuki Okushi <huyuumi.dev@gmail.com> | 2021-01-15 18:26:11 +0900 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-01-15 18:26:11 +0900 |
| commit | 1b8fd02daab85e129f0dda9c92ea11aaa14b7485 (patch) | |
| tree | 430e8c9eb2d45f681155b91b75cea49a793f9b8f | |
| parent | 0dedc6c0546c709087ebad1dc1f9d85b183b2f09 (diff) | |
| parent | 744f885e2a505136203e3e0eef51f72dbe78b511 (diff) | |
| download | rust-1b8fd02daab85e129f0dda9c92ea11aaa14b7485.tar.gz rust-1b8fd02daab85e129f0dda9c92ea11aaa14b7485.zip | |
Rollup merge of #80834 - bugadani:vecdeque, r=oli-obk
Remove unreachable panics from VecDeque::{front/back}[_mut]
`VecDeque`'s `front`, `front_mut`, `back` and `back_mut` methods are implemented in terms of the index operator, which causes these functions to contain [unreachable panic calls](https://rust.godbolt.org/z/MTnq1o).
This PR reimplements these methods in terms of `get[_mut]` instead.
| -rw-r--r-- | library/alloc/src/collections/vec_deque/mod.rs | 9 | ||||
| -rw-r--r-- | src/test/codegen/vecdeque_no_panic.rs | 19 |
2 files changed, 23 insertions, 5 deletions
diff --git a/library/alloc/src/collections/vec_deque/mod.rs b/library/alloc/src/collections/vec_deque/mod.rs index f8fad6de1a3..5b61e8911a5 100644 --- a/library/alloc/src/collections/vec_deque/mod.rs +++ b/library/alloc/src/collections/vec_deque/mod.rs @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/src/test/codegen/vecdeque_no_panic.rs b/src/test/codegen/vecdeque_no_panic.rs new file mode 100644 index 00000000000..cbf420bada9 --- /dev/null +++ b/src/test/codegen/vecdeque_no_panic.rs @@ -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(); +} |
