Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit f9b6f93

Browse files
committedOct 19, 2023
Auto merge of #116915 - bend-n:unwet, r=<try>
Use `.get().unwrap()` in `[T]::get_unchecked` Fixes #116878
2 parents 89432aa + a70548d commit f9b6f93

File tree

6 files changed

+24
-19
lines changed

6 files changed

+24
-19
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.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//@compile-flags: -Zmiri-strict-provenance
2-
//@error-in-other-file: /retag .* tag does not exist in the borrow stack/
2+
//@error-in-other-file: unreachable
33

44
fn main() {
55
unsafe {

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

+6-14
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,12 @@
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
1+
error: Undefined Behavior: entering unreachable code
22
--> RUSTLIB/core/src/slice/mod.rs:LL:CC
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]
4+
LL | unsafe { &*index.get(self).unwrap_unchecked() }
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ entering unreachable code
96
|
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://github.com/rust-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):
7+
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
8+
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
9+
= note: BACKTRACE:
1810
= note: inside `core::slice::<impl [i32]>::get_unchecked::<usize>` at RUSTLIB/core/src/slice/mod.rs:LL:CC
1911
note: inside `main`
2012
--> $DIR/zst_slice.rs:LL:CC
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.as_ptr().add(5) }; //~ ERROR: uninitialized
55
let x = undef + 1;
66
panic!("this should never print: {}", x);
77
}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error: Undefined Behavior: using uninitialized data, but this operation requires initialized memory
22
--> $DIR/uninit_byte_read.rs:LL:CC
33
|
4-
LL | let undef = unsafe { *v.get_unchecked(5) };
5-
| ^^^^^^^^^^^^^^^^^^^ using uninitialized data, but this operation requires initialized memory
4+
LL | let undef = unsafe { *v.as_ptr().add(5) };
5+
| ^^^^^^^^^^^^^^^^^^ using uninitialized data, but this operation requires initialized memory
66
|
77
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
88
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information

‎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)
Failed to load comments.