Auto merge of #114370 - krtab:pop_assume_cap, r=scottmcm
Add invariant to Vec::pop that len < cap if pop successful Fixes: https://github.com/rust-lang/rust/issues/114334
This commit is contained in:
commit
49691b1f70
@ -1956,6 +1956,7 @@ pub fn pop(&mut self) -> Option<T> {
|
|||||||
} else {
|
} else {
|
||||||
unsafe {
|
unsafe {
|
||||||
self.len -= 1;
|
self.len -= 1;
|
||||||
|
core::intrinsics::assume(self.len < self.capacity());
|
||||||
Some(ptr::read(self.as_ptr().add(self.len())))
|
Some(ptr::read(self.as_ptr().add(self.len())))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
24
tests/codegen/vec_pop_push_noop.rs
Normal file
24
tests/codegen/vec_pop_push_noop.rs
Normal file
@ -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…
Reference in New Issue
Block a user