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 Vec::pop that len < cap if pop successful #114370

Merged
merged 1 commit into from
Oct 16, 2023
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
1 change: 1 addition & 0 deletions library/alloc/src/vec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1956,6 +1956,7 @@ impl<T, A: Allocator> Vec<T, A> {
} else {
unsafe {
self.len -= 1;
core::intrinsics::assume(self.len < self.capacity());
Some(ptr::read(self.as_ptr().add(self.len())))
}
}
Expand Down
24 changes: 24 additions & 0 deletions tests/codegen/vec_pop_push_noop.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// compile-flags: -O

#![crate_type = "lib"]

#[no_mangle]
// CHECK-LABEL: @noop(
pub fn noop(v: &mut Vec<u8>) {
// CHECK-NOT: reserve_for_push
// CHECK-NOT: call
// CHECK: tail call void @llvm.assume
// CHECK-NOT: reserve_for_push
// CHECK-NOT: call
// CHECK: ret
if let Some(x) = v.pop() {
v.push(x)
}
}

#[no_mangle]
// CHECK-LABEL: @push_byte(
pub fn push_byte(v: &mut Vec<u8>) {
// CHECK: call {{.*}}reserve_for_push
v.push(3);
}
Loading