]> git.proxmox.com Git - rustc.git/blame - src/test/codegen/binary-search-index-no-bound-check.rs
New upstream version 1.66.0+dfsg1
[rustc.git] / src / test / codegen / binary-search-index-no-bound-check.rs
CommitLineData
cdc7bbd5
XL
1// compile-flags: -O
2// ignore-debug: the debug assertions get in the way
3#![crate_type = "lib"]
4
5// Make sure no bounds checks are emitted when slicing or indexing
6// with an index from `binary_search`.
7
8// CHECK-LABEL: @binary_search_index_no_bounds_check
9#[no_mangle]
10pub fn binary_search_index_no_bounds_check(s: &[u8]) -> u8 {
11 // CHECK-NOT: panic
12 // CHECK-NOT: slice_index_len_fail
13 if let Ok(idx) = s.binary_search(&b'\\') {
14 s[idx]
15 } else {
16 42
17 }
18}
2b03887a
FG
19
20// Similarly, check that `partition_point` is known to return a valid fencepost.
21
22// CHECK-LABEL: @unknown_split
23#[no_mangle]
24pub fn unknown_split(x: &[i32], i: usize) -> (&[i32], &[i32]) {
25 // This just makes sure that the subsequent function is looking for the
26 // absence of something that might actually be there.
27
28 // CHECK: call core::panicking::panic
29 x.split_at(i)
30}
31
32// CHECK-LABEL: @partition_point_split_no_bounds_check
33#[no_mangle]
34pub fn partition_point_split_no_bounds_check(x: &[i32], needle: i32) -> (&[i32], &[i32]) {
35 // CHECK-NOT: call core::panicking::panic
36 let i = x.partition_point(|p| p < &needle);
37 x.split_at(i)
38}