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

Add invariant to VecDeque::pop_* that len < cap if pop successful #123089

Merged
merged 1 commit into from
Apr 8, 2024
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
10 changes: 8 additions & 2 deletions library/alloc/src/collections/vec_deque/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1614,7 +1614,10 @@ impl<T, A: Allocator> VecDeque<T, A> {
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))
}
}
}

Expand All @@ -1638,7 +1641,10 @@ impl<T, A: Allocator> VecDeque<T, A> {
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)))
}
}
}

Expand Down
67 changes: 67 additions & 0 deletions tests/codegen/vecdeque_pop_push.rs
Original file line number Diff line number Diff line change
@@ -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>) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for including those tests btw, having the test to make sure the above tests are correct is super important!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I merely mimicked the Vec version of this PR.
The 1st iteration was too much Vec-oriented.

// 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