Skip to content

Commit

Permalink
Avoid emptiness check in PeekMut::pop
Browse files Browse the repository at this point in the history
  • Loading branch information
EFanZh committed Oct 3, 2024
1 parent 9c7013c commit 9995f03
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
5 changes: 4 additions & 1 deletion library/alloc/src/collections/binary_heap/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,10 @@ impl<'a, T: Ord, A: Allocator> PeekMut<'a, T, A> {
// the caller could've mutated the element. It is removed from the
// heap on the next line and pop() is not sensitive to its value.
}
this.heap.pop().unwrap()

// SAFETY: Have a `PeekMut` element proves that the associated binary heap being non-empty,
// so the `pop` operation will not fail.
unsafe { this.heap.pop().unwrap_unchecked() }
}
}

Expand Down
12 changes: 12 additions & 0 deletions tests/codegen/binary-heap-peek-mut-pop-no-panic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//@ compile-flags: -O
#![crate_type = "lib"]

use std::collections::binary_heap::PeekMut;

// CHECK-LABEL: @peek_mut_pop
#[no_mangle]
pub fn peek_mut_pop(peek_mut: PeekMut<u32>) -> u32 {
// CHECK-NOT: panic
// CHECK-NOT: unwrap_failed
PeekMut::pop(peek_mut)
}

0 comments on commit 9995f03

Please sign in to comment.