diff --git a/library/alloc/src/vec/mod.rs b/library/alloc/src/vec/mod.rs index 1b24c63755051..ce135a9e8d3c2 100644 --- a/library/alloc/src/vec/mod.rs +++ b/library/alloc/src/vec/mod.rs @@ -1903,6 +1903,7 @@ impl Vec { } 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 0000000000000..714c2043dd213 --- /dev/null +++ b/tests/codegen/vec_pop_push_noop.rs @@ -0,0 +1,25 @@ +// compile-flags: -O + +#![crate_type = "lib"] + +#[no_mangle] +pub fn noop(v: &mut Vec) { + // CHECK: start: + // CHECK-NEXT: getelementptr + // CHECK-NEXT: load + // CHECK-NEXT: icmp eq + // CHECK-NEXT: br + + // CHECK: add + // CHECK-NEXT: getelementptr + // CHECK-NEXT: load + // CHECK-NEXT: icmp + // CHECK-NEXT: tail call + // CHECK-NEXT: br label %bb3 + + // CHECK: bb3: + // CHECK-NEXT: ret void + if let Some(x) = v.pop() { + v.push(x) + } +}