]> git.lizzy.rs Git - rust.git/blob - tests/codegen/binary-search-index-no-bound-check.rs
Rollup merge of #107731 - RalfJung:interpret-discriminant, r=cjgillot
[rust.git] / tests / codegen / binary-search-index-no-bound-check.rs
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]
10 pub 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 }
19
20 // Similarly, check that `partition_point` is known to return a valid fencepost.
21
22 // CHECK-LABEL: @unknown_split
23 #[no_mangle]
24 pub 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]
34 pub 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 }