]> git.lizzy.rs Git - rust.git/blob - tests/codegen/issue-73396-bounds-check-after-position.rs
Auto merge of #107778 - weihanglo:update-cargo, r=weihanglo
[rust.git] / tests / codegen / issue-73396-bounds-check-after-position.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 `position()` or `rposition()`.
7
8 // CHECK-LABEL: @position_slice_to_no_bounds_check
9 #[no_mangle]
10 pub fn position_slice_to_no_bounds_check(s: &[u8]) -> &[u8] {
11     // CHECK-NOT: panic
12     // CHECK-NOT: slice_index_len_fail
13     if let Some(idx) = s.iter().position(|b| *b == b'\\') {
14         &s[..idx]
15     } else {
16         s
17     }
18 }
19
20 // CHECK-LABEL: @position_slice_from_no_bounds_check
21 #[no_mangle]
22 pub fn position_slice_from_no_bounds_check(s: &[u8]) -> &[u8] {
23     // CHECK-NOT: panic
24     // CHECK-NOT: slice_index_len_fail
25     if let Some(idx) = s.iter().position(|b| *b == b'\\') {
26         &s[idx..]
27     } else {
28         s
29     }
30 }
31
32 // CHECK-LABEL: @position_index_no_bounds_check
33 #[no_mangle]
34 pub fn position_index_no_bounds_check(s: &[u8]) -> u8 {
35     // CHECK-NOT: panic
36     // CHECK-NOT: slice_index_len_fail
37     if let Some(idx) = s.iter().position(|b| *b == b'\\') {
38         s[idx]
39     } else {
40         42
41     }
42 }
43 // CHECK-LABEL: @rposition_slice_to_no_bounds_check
44 #[no_mangle]
45 pub fn rposition_slice_to_no_bounds_check(s: &[u8]) -> &[u8] {
46     // CHECK-NOT: panic
47     // CHECK-NOT: slice_index_len_fail
48     if let Some(idx) = s.iter().rposition(|b| *b == b'\\') {
49         &s[..idx]
50     } else {
51         s
52     }
53 }
54
55 // CHECK-LABEL: @rposition_slice_from_no_bounds_check
56 #[no_mangle]
57 pub fn rposition_slice_from_no_bounds_check(s: &[u8]) -> &[u8] {
58     // CHECK-NOT: panic
59     // CHECK-NOT: slice_index_len_fail
60     if let Some(idx) = s.iter().rposition(|b| *b == b'\\') {
61         &s[idx..]
62     } else {
63         s
64     }
65 }
66
67 // CHECK-LABEL: @rposition_index_no_bounds_check
68 #[no_mangle]
69 pub fn rposition_index_no_bounds_check(s: &[u8]) -> u8 {
70     // CHECK-NOT: panic
71     // CHECK-NOT: slice_index_len_fail
72     if let Some(idx) = s.iter().rposition(|b| *b == b'\\') {
73         s[idx]
74     } else {
75         42
76     }
77 }