File: binary-search-index-no-bound-check.rs

package info (click to toggle)
rustc-web 1.78.0%2Bdfsg1-2~deb11u3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,245,360 kB
  • sloc: xml: 147,985; javascript: 18,022; sh: 11,083; python: 10,265; ansic: 6,172; cpp: 5,023; asm: 4,390; makefile: 4,269
file content (39 lines) | stat: -rw-r--r-- 1,185 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
//@ compile-flags: -O
#![crate_type = "lib"]

// Make sure no bounds checks are emitted when slicing or indexing
// with an index from `binary_search`.

// CHECK-LABEL: @binary_search_index_no_bounds_check
#[no_mangle]
pub fn binary_search_index_no_bounds_check(s: &[u8]) -> u8 {
    // CHECK-NOT: panic
    // CHECK-NOT: slice_start_index_len_fail
    // CHECK-NOT: slice_end_index_len_fail
    // CHECK-NOT: panic_bounds_check
    if let Ok(idx) = s.binary_search(&b'\\') {
        s[idx]
    } else {
        42
    }
}

// Similarly, check that `partition_point` is known to return a valid fencepost.

// CHECK-LABEL: @unknown_split
#[no_mangle]
pub fn unknown_split(x: &[i32], i: usize) -> (&[i32], &[i32]) {
    // This just makes sure that the subsequent function is looking for the
    // absence of something that might actually be there.

    // CHECK: call core::panicking::panic
    x.split_at(i)
}

// CHECK-LABEL: @partition_point_split_no_bounds_check
#[no_mangle]
pub fn partition_point_split_no_bounds_check(x: &[i32], needle: i32) -> (&[i32], &[i32]) {
    // CHECK-NOT: call core::panicking::panic
    let i = x.partition_point(|p| p < &needle);
    x.split_at(i)
}