summaryrefslogtreecommitdiff
path: root/tests/codegen/issues/issue-73396-bounds-check-after-position.rs
blob: 2d7797887912e004a1aad16cd64f74fe6b40786b (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// compile-flags: -O
// ignore-debug: the debug assertions get in the way
#![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_start_index_len_fail
    // CHECK-NOT: slice_end_index_len_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_start_index_len_fail
    // CHECK-NOT: slice_end_index_len_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_start_index_len_fail
    // CHECK-NOT: slice_end_index_len_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_start_index_len_fail
    // CHECK-NOT: slice_end_index_len_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_start_index_len_fail
    // CHECK-NOT: slice_end_index_len_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_start_index_len_fail
    // CHECK-NOT: slice_end_index_len_fail
    // CHECK-NOT: panic_bounds_check
    // CHECK-NOT: unreachable
    if let Some(idx) = s.iter().rposition(|b| *b == b'\\') {
        s[idx]
    } else {
        42
    }
}