Skip to content

Commit

Permalink
Auto merge of rust-lang#116915 - bend-n:unwet, r=<try>
Browse files Browse the repository at this point in the history
Use `.get().unwrap()` in `[T]::get_unchecked`

Fixes rust-lang#116878
  • Loading branch information
bors committed Oct 19, 2023
2 parents 89432aa + a70548d commit f9b6f93
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 19 deletions.
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
2 changes: 1 addition & 1 deletion src/tools/miri/tests/fail/stacked_borrows/zst_slice.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//@compile-flags: -Zmiri-strict-provenance
//@error-in-other-file: /retag .* tag does not exist in the borrow stack/
//@error-in-other-file: unreachable

fn main() {
unsafe {
Expand Down
20 changes: 6 additions & 14 deletions src/tools/miri/tests/fail/stacked_borrows/zst_slice.stderr
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
error: Undefined Behavior: trying to retag from <TAG> for SharedReadOnly permission at ALLOC[0x4], but that tag does not exist in the borrow stack for this location
error: Undefined Behavior: entering unreachable code
--> RUSTLIB/core/src/slice/mod.rs:LL:CC
|
LL | unsafe { &*index.get_unchecked(self) }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
| |
| trying to retag from <TAG> for SharedReadOnly permission at ALLOC[0x4], but that tag does not exist in the borrow stack for this location
| this error occurs as part of retag at ALLOC[0x4..0x8]
LL | unsafe { &*index.get(self).unwrap_unchecked() }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ entering unreachable code
|
= help: this indicates a potential bug in the program: it performed an invalid operation, but the Stacked Borrows rules it violated are still experimental
= help: see https://github.com/rust-lang/unsafe-code-guidelines/blob/master/wip/stacked-borrows.md for further information
help: <TAG> would have been created here, but this is a zero-size retag ([0x0..0x0]) so the tag in question does not exist anywhere
--> $DIR/zst_slice.rs:LL:CC
|
LL | assert_eq!(*s.get_unchecked(1), 2);
| ^^^^^^^^^^^^^^^^^^
= note: BACKTRACE (of the first span):
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
= note: BACKTRACE:
= note: inside `core::slice::<impl [i32]>::get_unchecked::<usize>` at RUSTLIB/core/src/slice/mod.rs:LL:CC
note: inside `main`
--> $DIR/zst_slice.rs:LL:CC
Expand Down
2 changes: 1 addition & 1 deletion src/tools/miri/tests/fail/uninit_byte_read.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//@compile-flags: -Zmiri-disable-stacked-borrows
fn main() {
let v: Vec<u8> = Vec::with_capacity(10);
let undef = unsafe { *v.get_unchecked(5) }; //~ ERROR: uninitialized
let undef = unsafe { *v.as_ptr().add(5) }; //~ ERROR: uninitialized
let x = undef + 1;
panic!("this should never print: {}", x);
}
4 changes: 2 additions & 2 deletions src/tools/miri/tests/fail/uninit_byte_read.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error: Undefined Behavior: using uninitialized data, but this operation requires initialized memory
--> $DIR/uninit_byte_read.rs:LL:CC
|
LL | let undef = unsafe { *v.get_unchecked(5) };
| ^^^^^^^^^^^^^^^^^^^ using uninitialized data, but this operation requires initialized memory
LL | let undef = unsafe { *v.as_ptr().add(5) };
| ^^^^^^^^^^^^^^^^^^ using uninitialized data, but this operation requires initialized memory
|
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
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 f9b6f93

Please sign in to comment.