Add invariant to VecDeque::pop_* that len < cap if pop successful
Similar to #114370 for VecDeque instead of Vec. It now uses `core::hint::assert_unchecked`.
This commit is contained in:
parent
7a495cc13d
commit
7a2678de7d
@ -1614,7 +1614,10 @@ pub fn pop_front(&mut self) -> Option<T> {
|
||||
let old_head = self.head;
|
||||
self.head = self.to_physical_idx(1);
|
||||
self.len -= 1;
|
||||
Some(unsafe { self.buffer_read(old_head) })
|
||||
unsafe {
|
||||
core::hint::assert_unchecked(self.len < self.capacity());
|
||||
Some(self.buffer_read(old_head))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -1638,7 +1641,10 @@ pub fn pop_back(&mut self) -> Option<T> {
|
||||
None
|
||||
} else {
|
||||
self.len -= 1;
|
||||
Some(unsafe { self.buffer_read(self.to_physical_idx(self.len)) })
|
||||
unsafe {
|
||||
core::hint::assert_unchecked(self.len < self.capacity());
|
||||
Some(self.buffer_read(self.to_physical_idx(self.len)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
67
tests/codegen/vecdeque_pop_push.rs
Normal file
67
tests/codegen/vecdeque_pop_push.rs
Normal file
@ -0,0 +1,67 @@
|
||||
//@ compile-flags: -O
|
||||
|
||||
#![crate_type = "lib"]
|
||||
|
||||
use std::collections::VecDeque;
|
||||
|
||||
#[no_mangle]
|
||||
// CHECK-LABEL: @noop_back(
|
||||
pub fn noop_back(v: &mut VecDeque<u8>) {
|
||||
// CHECK-NOT: grow
|
||||
// CHECK: tail call void @llvm.assume
|
||||
// CHECK-NOT: grow
|
||||
// CHECK: ret
|
||||
if let Some(x) = v.pop_back() {
|
||||
v.push_back(x);
|
||||
}
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
// CHECK-LABEL: @noop_front(
|
||||
pub fn noop_front(v: &mut VecDeque<u8>) {
|
||||
// CHECK-NOT: grow
|
||||
// CHECK: tail call void @llvm.assume
|
||||
// CHECK-NOT: grow
|
||||
// CHECK: ret
|
||||
if let Some(x) = v.pop_front() {
|
||||
v.push_front(x);
|
||||
}
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
// CHECK-LABEL: @move_byte_front_to_back(
|
||||
pub fn move_byte_front_to_back(v: &mut VecDeque<u8>) {
|
||||
// CHECK-NOT: grow
|
||||
// CHECK: tail call void @llvm.assume
|
||||
// CHECK-NOT: grow
|
||||
// CHECK: ret
|
||||
if let Some(x) = v.pop_front() {
|
||||
v.push_back(x);
|
||||
}
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
// CHECK-LABEL: @move_byte_back_to_front(
|
||||
pub fn move_byte_back_to_front(v: &mut VecDeque<u8>) {
|
||||
// CHECK-NOT: grow
|
||||
// CHECK: tail call void @llvm.assume
|
||||
// CHECK-NOT: grow
|
||||
// CHECK: ret
|
||||
if let Some(x) = v.pop_back() {
|
||||
v.push_front(x);
|
||||
}
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
// CHECK-LABEL: @push_back_byte(
|
||||
pub fn push_back_byte(v: &mut VecDeque<u8>) {
|
||||
// CHECK: call {{.*}}grow
|
||||
v.push_back(3);
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
// CHECK-LABEL: @push_front_byte(
|
||||
pub fn push_front_byte(v: &mut VecDeque<u8>) {
|
||||
// CHECK: call {{.*}}grow
|
||||
v.push_front(3);
|
||||
}
|
Loading…
Reference in New Issue
Block a user