diff --git a/library/core/src/slice/mod.rs b/library/core/src/slice/mod.rs index 45080eda2ce2f..1495460bcded0 100644 --- a/library/core/src/slice/mod.rs +++ b/library/core/src/slice/mod.rs @@ -662,7 +662,7 @@ impl [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 diff --git a/src/tools/miri/tests/fail/stacked_borrows/zst_slice.rs b/src/tools/miri/tests/fail/stacked_borrows/zst_slice.rs index fd51fa6468a04..96b266f4af51b 100644 --- a/src/tools/miri/tests/fail/stacked_borrows/zst_slice.rs +++ b/src/tools/miri/tests/fail/stacked_borrows/zst_slice.rs @@ -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 { diff --git a/src/tools/miri/tests/fail/stacked_borrows/zst_slice.stderr b/src/tools/miri/tests/fail/stacked_borrows/zst_slice.stderr index 950abc4cbcf92..88b4c02e93907 100644 --- a/src/tools/miri/tests/fail/stacked_borrows/zst_slice.stderr +++ b/src/tools/miri/tests/fail/stacked_borrows/zst_slice.stderr @@ -1,20 +1,12 @@ -error: Undefined Behavior: trying to retag from 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 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: 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::::get_unchecked::` at RUSTLIB/core/src/slice/mod.rs:LL:CC note: inside `main` --> $DIR/zst_slice.rs:LL:CC diff --git a/src/tools/miri/tests/fail/uninit_byte_read.rs b/src/tools/miri/tests/fail/uninit_byte_read.rs index f1dace0cff9b1..9bc2b4338b0db 100644 --- a/src/tools/miri/tests/fail/uninit_byte_read.rs +++ b/src/tools/miri/tests/fail/uninit_byte_read.rs @@ -1,7 +1,7 @@ //@compile-flags: -Zmiri-disable-stacked-borrows fn main() { let v: Vec = 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); } diff --git a/src/tools/miri/tests/fail/uninit_byte_read.stderr b/src/tools/miri/tests/fail/uninit_byte_read.stderr index 9f7638888d643..13bd5749a2417 100644 --- a/src/tools/miri/tests/fail/uninit_byte_read.stderr +++ b/src/tools/miri/tests/fail/uninit_byte_read.stderr @@ -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 diff --git a/tests/codegen/issues/issue-116878.rs b/tests/codegen/issues/issue-116878.rs new file mode 100644 index 0000000000000..d5f679459f731 --- /dev/null +++ b/tests/codegen/issues/issue-116878.rs @@ -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] +}