forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of rust-lang#108763 - scottmcm:indexing-nuw-lengths, r=cuv…
…iper Use `nuw` when calculating slice lengths from `Range`s An `assume` would definitely not be worth it, but since the flag is almost free we might as well tell LLVM this, especially on `_unchecked` calls where there's no obvious way for it to deduce it. (Today neither safe nor unsafe indexing gets it: <https://rust.godbolt.org/z/G1jYT548s>)
- Loading branch information
Showing
4 changed files
with
68 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// compile-flags: -O | ||
// only-64bit (because the LLVM type of i64 for usize shows up) | ||
// ignore-debug: the debug assertions get in the way | ||
|
||
#![crate_type = "lib"] | ||
|
||
use std::ops::Range; | ||
|
||
// CHECK-LABEL: @index_by_range( | ||
#[no_mangle] | ||
pub fn index_by_range(x: &[u16], r: Range<usize>) -> &[u16] { | ||
// CHECK: sub nuw i64 | ||
&x[r] | ||
} | ||
|
||
// CHECK-LABEL: @get_unchecked_by_range( | ||
#[no_mangle] | ||
pub unsafe fn get_unchecked_by_range(x: &[u16], r: Range<usize>) -> &[u16] { | ||
// CHECK: sub nuw i64 | ||
x.get_unchecked(r) | ||
} | ||
|
||
// CHECK-LABEL: @index_mut_by_range( | ||
#[no_mangle] | ||
pub fn index_mut_by_range(x: &mut [i32], r: Range<usize>) -> &mut [i32] { | ||
// CHECK: sub nuw i64 | ||
&mut x[r] | ||
} | ||
|
||
// CHECK-LABEL: @get_unchecked_mut_by_range( | ||
#[no_mangle] | ||
pub unsafe fn get_unchecked_mut_by_range(x: &mut [i32], r: Range<usize>) -> &mut [i32] { | ||
// CHECK: sub nuw i64 | ||
x.get_unchecked_mut(r) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#![feature(const_slice_index)] | ||
|
||
const A: [(); 5] = [(), (), (), (), ()]; | ||
|
||
// Since the indexing is on a ZST, the addresses are all fine, | ||
// but we should still catch the bad range. | ||
const B: &[()] = unsafe { A.get_unchecked(3..1) }; | ||
|
||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
error[E0080]: evaluation of constant value failed | ||
--> $SRC_DIR/core/src/slice/index.rs:LL:COL | ||
| | ||
= note: overflow executing `unchecked_sub` | ||
| | ||
note: inside `<std::ops::Range<usize> as SliceIndex<[()]>>::get_unchecked` | ||
--> $SRC_DIR/core/src/slice/index.rs:LL:COL | ||
note: inside `core::slice::<impl [()]>::get_unchecked::<std::ops::Range<usize>>` | ||
--> $SRC_DIR/core/src/slice/mod.rs:LL:COL | ||
note: inside `B` | ||
--> $DIR/ub-slice-get-unchecked.rs:7:27 | ||
| | ||
LL | const B: &[()] = unsafe { A.get_unchecked(3..1) }; | ||
| ^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0080`. |