Change:
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

Discussion:
<!-- 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.