about summary refs log tree commit diff
path: root/tests/codegen-llvm/issues/issue-73396-bounds-check-after-position.rs
blob: 8a2200478aa080ba114daa972a2edc5b17dd13b1 (plain)
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//@ compile-flags: -Copt-level=3
#![crate_type = "lib"]

// Make sure no bounds checks are emitted when slicing or indexing
// with an index from `position()` or `rposition()`.

// CHECK-LABEL: @position_slice_to_no_bounds_check
#[no_mangle]
pub fn position_slice_to_no_bounds_check(s: &[u8]) -> &[u8] {
    // CHECK-NOT: panic
    // CHECK-NOT: slice_index_fail
    // CHECK-NOT: panic_bounds_check
    // CHECK-NOT: unreachable
    if let Some(idx) = s.iter().position(|b| *b == b'\\') { &s[..idx] } else { s }
}

// CHECK-LABEL: @position_slice_from_no_bounds_check
#[no_mangle]
pub fn position_slice_from_no_bounds_check(s: &[u8]) -> &[u8] {
    // CHECK-NOT: panic
    // CHECK-NOT: slice_index_fail
    // CHECK-NOT: panic_bounds_check
    // CHECK-NOT: unreachable
    if let Some(idx) = s.iter().position(|b| *b == b'\\') { &s[idx..] } else { s }
}

// CHECK-LABEL: @position_index_no_bounds_check
#[no_mangle]
pub fn position_index_no_bounds_check(s: &[u8]) -> u8 {
    // CHECK-NOT: panic
    // CHECK-NOT: slice_index_fail
    // CHECK-NOT: panic_bounds_check
    // CHECK-NOT: unreachable
    if let Some(idx) = s.iter().position(|b| *b == b'\\') { s[idx] } else { 42 }
}
// CHECK-LABEL: @rposition_slice_to_no_bounds_check
#[no_mangle]
pub fn rposition_slice_to_no_bounds_check(s: &[u8]) -> &[u8] {
    // CHECK-NOT: panic
    // CHECK-NOT: slice_index_fail
    // CHECK-NOT: panic_bounds_check
    // CHECK-NOT: unreachable
    if let Some(idx) = s.iter().rposition(|b| *b == b'\\') { &s[..idx] } else { s }
}

// CHECK-LABEL: @rposition_slice_from_no_bounds_check
#[no_mangle]
pub fn rposition_slice_from_no_bounds_check(s: &[u8]) -> &[u8] {
    // CHECK-NOT: panic
    // CHECK-NOT: slice_index_fail
    // CHECK-NOT: panic_bounds_check
    // CHECK-NOT: unreachable
    if let Some(idx) = s.iter().rposition(|b| *b == b'\\') { &s[idx..] } else { s }
}

// CHECK-LABEL: @rposition_index_no_bounds_check
#[no_mangle]
pub fn rposition_index_no_bounds_check(s: &[u8]) -> u8 {
    // CHECK-NOT: panic
    // CHECK-NOT: slice_index_fail
    // CHECK-NOT: panic_bounds_check
    // CHECK-NOT: unreachable
    if let Some(idx) = s.iter().rposition(|b| *b == b'\\') { s[idx] } else { 42 }
}