Skip to content

Commit

Permalink
use .get().unwrap() in [T]::get_unchecked
Browse files Browse the repository at this point in the history
  • Loading branch information
bend-n committed Oct 18, 2023
1 parent cc705b8 commit 5c1254f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion library/core/src/slice/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,7 @@ impl<T> [T] {
// SAFETY: the caller must uphold most of the safety requirements for `get_unchecked`;
// the slice is dereferenceable because `self` is a safe reference.
// The returned pointer is safe because impls of `SliceIndex` have to guarantee that it is.
unsafe { &*index.get_unchecked(self) }
unsafe { &*index.get(self).unwrap_unchecked() }
}

/// Returns a mutable reference to an element or subslice, without doing
Expand Down
13 changes: 13 additions & 0 deletions tests/codegen/issues/issue-116878.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// no-system-llvm
// compile-flags: -O
// ignore-debug: the debug assertions get in the way
#![crate_type = "lib"]

/// Make sure no bounds checks are emitted after a `get_unchecked`.
// CHECK-LABEL: @unchecked_slice_no_bounds_check
#[no_mangle]
pub unsafe fn unchecked_slice_no_bounds_check(s: &[u8]) -> u8 {
let a = *s.get_unchecked(1);
// CHECK-NOT: panic_bounds_check
a + s[0]
}

0 comments on commit 5c1254f

Please sign in to comment.