Skip to content

Commit 600d7b9

Browse files
committed
use .get().unwrap() in [T]::get_unchecked
1 parent cc705b8 commit 600d7b9

File tree

5 files changed

+35
-27
lines changed

5 files changed

+35
-27
lines changed

library/core/src/slice/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -662,7 +662,7 @@ impl<T> [T] {
662662
// SAFETY: the caller must uphold most of the safety requirements for `get_unchecked`;
663663
// the slice is dereferenceable because `self` is a safe reference.
664664
// The returned pointer is safe because impls of `SliceIndex` have to guarantee that it is.
665-
unsafe { &*index.get_unchecked(self) }
665+
unsafe { &*index.get(self).unwrap_unchecked() }
666666
}
667667

668668
/// Returns a mutable reference to an element or subslice, without doing

src/tools/miri/tests/fail/stacked_borrows/zst_slice.stderr

+10-19
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,17 @@
1-
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
2-
--> RUSTLIB/core/src/slice/mod.rs:LL:CC
1+
error: Undefined Behavior: entering unreachable code
2+
##[error] --> /checkout/library/core/src/slice/mod.rs:665:20
33
|
4-
LL | unsafe { &*index.get_unchecked(self) }
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
6-
| |
7-
| trying to retag from <TAG> for SharedReadOnly permission at ALLOC[0x4], but that tag does not exist in the borrow stack for this location
8-
| this error occurs as part of retag at ALLOC[0x4..0x8]
9-
|
10-
= help: this indicates a potential bug in the program: it performed an invalid operation, but the Stacked Borrows rules it violated are still experimental
11-
= help: see https://git.1-hub.cnrust-lang/unsafe-code-guidelines/blob/master/wip/stacked-borrows.md for further information
12-
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
13-
--> $DIR/zst_slice.rs:LL:CC
14-
|
15-
LL | assert_eq!(*s.get_unchecked(1), 2);
16-
| ^^^^^^^^^^^^^^^^^^
17-
= note: BACKTRACE (of the first span):
18-
= note: inside `core::slice::<impl [i32]>::get_unchecked::<usize>` at RUSTLIB/core/src/slice/mod.rs:LL:CC
4+
LL | unsafe { &*index.get(self).unwrap_unchecked() }
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ entering unreachable code
6+
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
7+
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
8+
= note: BACKTRACE:
9+
= note: inside `core::slice::<impl [i32]>::get_unchecked::<usize>` at /checkout/library/core/src/slice/mod.rs:665:20: 665:54
10+
= note: inside `core::slice::<impl [i32]>::get_unchecked::<usize>` at /checkout/library/core/src/slice/mod.rs:665:20: 665:54
1911
note: inside `main`
20-
--> $DIR/zst_slice.rs:LL:CC
12+
--> tests/fail/stacked_borrows/zst_slice.rs:9:21
2113
|
2214
LL | assert_eq!(*s.get_unchecked(1), 2);
23-
| ^^^^^^^^^^^^^^^^^^
2415

2516
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
2617

Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//@compile-flags: -Zmiri-disable-stacked-borrows
22
fn main() {
33
let v: Vec<u8> = Vec::with_capacity(10);
4-
let undef = unsafe { *v.get_unchecked(5) }; //~ ERROR: uninitialized
4+
let undef = unsafe { *v.get_unchecked(5) }; //~ ERROR: unreachable
55
let x = undef + 1;
66
panic!("this should never print: {}", x);
77
}

src/tools/miri/tests/fail/uninit_byte_read.stderr

+10-6
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
1-
error: Undefined Behavior: using uninitialized data, but this operation requires initialized memory
2-
--> $DIR/uninit_byte_read.rs:LL:CC
3-
|
4-
LL | let undef = unsafe { *v.get_unchecked(5) };
5-
| ^^^^^^^^^^^^^^^^^^^ using uninitialized data, but this operation requires initialized memory
1+
error: Undefined Behavior: entering unreachable code
2+
##[error] --> /checkout/library/core/src/slice/mod.rs:665:20
63
|
4+
LL | unsafe { &*index.get(self).unwrap_unchecked() }
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ entering unreachable code
76
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
87
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
98
= note: BACKTRACE:
10-
= note: inside `main` at $DIR/uninit_byte_read.rs:LL:CC
9+
= note: inside `core::slice::<impl [u8]>::get_unchecked::<usize>` at /checkout/library/core/src/slice/mod.rs:665:20: 665:54
10+
= note: inside `core::slice::<impl [u8]>::get_unchecked::<usize>` at /checkout/library/core/src/slice/mod.rs:665:20: 665:54
11+
note: inside `main`
12+
--> tests/fail/uninit_byte_read.rs:4:27
13+
|
14+
LL | let undef = unsafe { *v.get_unchecked(5) };
1115

1216
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
1317

tests/codegen/issues/issue-116878.rs

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// no-system-llvm
2+
// compile-flags: -O
3+
// ignore-debug: the debug assertions get in the way
4+
#![crate_type = "lib"]
5+
6+
/// Make sure no bounds checks are emitted after a `get_unchecked`.
7+
// CHECK-LABEL: @unchecked_slice_no_bounds_check
8+
#[no_mangle]
9+
pub unsafe fn unchecked_slice_no_bounds_check(s: &[u8]) -> u8 {
10+
let a = *s.get_unchecked(1);
11+
// CHECK-NOT: panic_bounds_check
12+
a + s[0]
13+
}

0 commit comments

Comments
 (0)