|
| 1 | +// min-llvm-version: 11.0.0 |
| 2 | +// compile-flags: -O |
| 3 | +// ignore-debug: the debug assertions get in the way |
| 4 | +#![crate_type = "lib"] |
| 5 | + |
| 6 | +// Make sure no bounds checks are emitted when slicing or indexing |
| 7 | +// with an index from `position()` or `rposition()`. |
| 8 | + |
| 9 | +// CHECK-LABEL: @position_slice_to_no_bounds_check |
| 10 | +#[no_mangle] |
| 11 | +pub fn position_slice_to_no_bounds_check(s: &[u8]) -> &[u8] { |
| 12 | + // CHECK-NOT: panic |
| 13 | + // CHECK-NOT: slice_index_len_fail |
| 14 | + if let Some(idx) = s.iter().position(|b| *b == b'\\') { |
| 15 | + &s[..idx] |
| 16 | + } else { |
| 17 | + s |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +// CHECK-LABEL: @position_slice_from_no_bounds_check |
| 22 | +#[no_mangle] |
| 23 | +pub fn position_slice_from_no_bounds_check(s: &[u8]) -> &[u8] { |
| 24 | + // CHECK-NOT: panic |
| 25 | + // CHECK-NOT: slice_index_len_fail |
| 26 | + if let Some(idx) = s.iter().position(|b| *b == b'\\') { |
| 27 | + &s[idx..] |
| 28 | + } else { |
| 29 | + s |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +// CHECK-LABEL: @position_index_no_bounds_check |
| 34 | +#[no_mangle] |
| 35 | +pub fn position_index_no_bounds_check(s: &[u8]) -> u8 { |
| 36 | + // CHECK-NOT: panic |
| 37 | + // CHECK-NOT: slice_index_len_fail |
| 38 | + if let Some(idx) = s.iter().position(|b| *b == b'\\') { |
| 39 | + s[idx] |
| 40 | + } else { |
| 41 | + 42 |
| 42 | + } |
| 43 | +} |
| 44 | +// CHECK-LABEL: @rposition_slice_to_no_bounds_check |
| 45 | +#[no_mangle] |
| 46 | +pub fn rposition_slice_to_no_bounds_check(s: &[u8]) -> &[u8] { |
| 47 | + // CHECK-NOT: panic |
| 48 | + // CHECK-NOT: slice_index_len_fail |
| 49 | + if let Some(idx) = s.iter().rposition(|b| *b == b'\\') { |
| 50 | + &s[..idx] |
| 51 | + } else { |
| 52 | + s |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +// CHECK-LABEL: @rposition_slice_from_no_bounds_check |
| 57 | +#[no_mangle] |
| 58 | +pub fn rposition_slice_from_no_bounds_check(s: &[u8]) -> &[u8] { |
| 59 | + // CHECK-NOT: panic |
| 60 | + // CHECK-NOT: slice_index_len_fail |
| 61 | + if let Some(idx) = s.iter().rposition(|b| *b == b'\\') { |
| 62 | + &s[idx..] |
| 63 | + } else { |
| 64 | + s |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +// CHECK-LABEL: @rposition_index_no_bounds_check |
| 69 | +#[no_mangle] |
| 70 | +pub fn rposition_index_no_bounds_check(s: &[u8]) -> u8 { |
| 71 | + // CHECK-NOT: panic |
| 72 | + // CHECK-NOT: slice_index_len_fail |
| 73 | + if let Some(idx) = s.iter().rposition(|b| *b == b'\\') { |
| 74 | + s[idx] |
| 75 | + } else { |
| 76 | + 42 |
| 77 | + } |
| 78 | +} |
0 commit comments