Change:
<!-- Thank you for filing a bug report! Please provide a short summary of the bug, along with any information you feel relevant to replicating the bug. --I tried this code: Compile with on () I expected to see this happen: Both functions should compile to approximately the same thing, with no bounds check on the since the optimizer should be able to see that the slice is at least of length 2 due to the previous unchecked indexing. Instead, this happened: contains a bounds check (only does not contain a bounds check). rustc stable (1.73.0), nightly (2023-10-17), and 1.58.0 (when was stabilized) all have approximately the same codegen. : (no backtrace) label +C-optimization -C-bug
contains an assume intrinsic, doesn't. Might be worth adding it to the latter and see how that impacts perf, it's not guaranteed that it would actually improve things since we'd be emitting more IR and llvm would have to work harder. Commonly it's the case that if one uses one does so for all accesses on the slice, so this seems like an unusual pattern.

Discussion:
// cannot be longer than `isize::MAX`. They also guarantee that // `self` is in bounds of `slice` so `self` cannot overflow an `isize`, // so the call to `add` is safe. <del> unsafe { slice.as_ptr().add(self) } </del> <ins> unsafe { crate::intrinsics::assume(self < slice.len()); slice.as_ptr().add(self) } </ins> } #[inline]

//@compile-flags: -Zmiri-strict-provenance <del> //@error-in-other-file: /retag .* tag does not exist in the borrow stack/ </del> fn main() { unsafe { let a = [1, 2, 3]; let s = &a[0..0]; assert_eq!(s.len(), 0); <del> assert_eq!(*s.get_unchecked(1), 2); </del> <ins> assert_eq!(*s.as_ptr().add(1), 2); //~ ERROR: /retag .* tag does not exist in the borrow stack/ </ins> } }

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 <del> --> RUSTLIB/core/src/slice/mod.rs:LL:CC </del> <ins> --> $DIR/zst_slice.rs:LL:CC </ins> | <del> 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] </del> <ins> LL |         assert_eq!(*s.as_ptr().add(1), 2); |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |         | |         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] </ins> | = 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 | <del> LL |         assert_eq!(*s.get_unchecked(1), 2); |                     ^^^^^^^^^^^^^^^^^^ </del> <ins> LL |         assert_eq!(*s.as_ptr().add(1), 2); |                     ^^^^^^^^^^ </ins> = note: BACKTRACE (of the first span): <del> = 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 | LL |         assert_eq!(*s.get_unchecked(1), 2); |                     ^^^^^^^^^^^^^^^^^^ </del> <ins> = note: inside `main` at RUSTLIB/core/src/macros/mod.rs:LL:CC = note: this error originates in the macro `assert_eq` (in Nightly builds, run with -Z macro-backtrace for more info) </ins> note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace

//@compile-flags: -Zmiri-disable-stacked-borrows fn main() { let v: Vec<u8> = Vec::with_capacity(10); <del> let undef = unsafe { *v.get_unchecked(5) }; //~ ERROR: uninitialized </del> <ins> let undef = unsafe { *v.as_ptr().add(5) }; //~ ERROR: uninitialized </ins> let x = undef + 1; panic!("this should never print: {}", x); }

error: Undefined Behavior: using uninitialized data, but this operation requires initialized memory --> $DIR/uninit_byte_read.rs:LL:CC | <del> LL |     let undef = unsafe { *v.get_unchecked(5) }; |                          ^^^^^^^^^^^^^^^^^^^ using uninitialized data, but this operation requires initialized memory </del> <ins> LL |     let undef = unsafe { *v.as_ptr().add(5) }; |                          ^^^^^^^^^^^^^^^^^^ using uninitialized data, but this operation requires initialized memory </ins> | = 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

#[track_caller] fn check_all_outcomes<T: Eq + Hash + fmt::Display>(expected: HashSet<T>, generate: impl Fn() -> T) { let mut seen = HashSet::new(); <del> // Let's give it 8x as many tries as we are expecting values. let tries = expected.len() * 8; </del> <ins> // Let's give it sixteen times as many tries as we are expecting values. let tries = expected.len() * 16; </ins> for _ in 0..tries { let val = generate(); assert!(expected.contains(&val), "got an unexpected value: {val}");

<ins> // 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] } </ins>