diff options
| author | bors <bors@rust-lang.org> | 2023-10-16 21:06:14 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2023-10-16 21:06:14 +0000 |
| commit | 49691b1f70d71dd7b8349c332b7f277ee527bf08 (patch) | |
| tree | 2976a977b73079219d28cbeaf2613bfb49a2dedb | |
| parent | a76ec181fba25f9fe64999ec2ae84bdc393560f2 (diff) | |
| parent | 0bcac8a7f25b994ddd3c2deda74dae04c677b816 (diff) | |
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
| -rw-r--r-- | library/alloc/src/vec/mod.rs | 1 | ||||
| -rw-r--r-- | tests/codegen/vec_pop_push_noop.rs | 24 |
2 files changed, 25 insertions, 0 deletions
diff --git a/library/alloc/src/vec/mod.rs b/library/alloc/src/vec/mod.rs index 35015238e6e..3b12c1bee0b 100644 --- a/library/alloc/src/vec/mod.rs +++ b/library/alloc/src/vec/mod.rs @@ -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()))) } } diff --git a/tests/codegen/vec_pop_push_noop.rs b/tests/codegen/vec_pop_push_noop.rs new file mode 100644 index 00000000000..8bc7b68a816 --- /dev/null +++ b/tests/codegen/vec_pop_push_noop.rs @@ -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); +} |
