]> git.lizzy.rs Git - rust.git/blob - tests/codegen/issue-45964-bounds-check-slice-pos.rs
Rollup merge of #107190 - fmease:fix-81698, r=compiler-errors
[rust.git] / tests / codegen / issue-45964-bounds-check-slice-pos.rs
1 // This test case checks that slice::{r}position functions do not
2 // prevent optimizing away bounds checks
3
4 // compile-flags: -O
5 // ignore-debug: the debug assertions get in the way
6
7 #![crate_type="rlib"]
8
9 // CHECK-LABEL: @test
10 #[no_mangle]
11 pub fn test(y: &[u32], x: &u32, z: &u32) -> bool {
12     let result = match y.iter().position(|a| a == x) {
13         Some(p) => Ok(p),
14         None => Err(()),
15     };
16
17     if let Ok(p) = result {
18         // CHECK-NOT: panic
19         y[p] == *z
20     } else {
21         false
22     }
23 }
24
25 // CHECK-LABEL: @rtest
26 #[no_mangle]
27 pub fn rtest(y: &[u32], x: &u32, z: &u32) -> bool {
28     let result = match y.iter().rposition(|a| a == x) {
29         Some(p) => Ok(p),
30         None => Err(()),
31     };
32
33     if let Ok(p) = result {
34         // CHECK-NOT: panic
35         y[p] == *z
36     } else {
37         false
38     }
39 }