]> git.lizzy.rs Git - rust.git/blob - tests/ui/borrowck/slice-index-bounds-check-invalidation.rs
Rollup merge of #106726 - cmorin6:fix-comment-typos, r=Nilstrieb
[rust.git] / tests / ui / borrowck / slice-index-bounds-check-invalidation.rs
1 // Test that we error if a slice is modified after it has been bounds checked
2 // and before we actually index it.
3
4 fn modify_before_assert_slice_slice(x: &[&[i32]]) -> i32 {
5     let mut x = x;
6     let z: &[i32] = &[1, 2, 3];
7     let y: &[&[i32]] = &[z];
8     x[{ x = y; 0 }][2]              // OK we haven't checked any bounds before we index `x`.
9 }
10
11 fn modify_before_assert_array_slice(x: &[&[i32]; 3]) -> i32 {
12     let mut x = x;
13     let z: &[i32] = &[1, 2, 3];
14     let y: &[&[i32]; 3] = &[z, z, z];
15     x[{ x = y; 0 }][2]              // OK we haven't checked any bounds before we index `x`.
16 }
17
18 fn modify_before_assert_slice_array(x: &[&[i32; 3]]) -> i32 {
19     let mut x = x;
20     let z: &[i32; 3] = &[1, 2, 3];
21     let y: &[&[i32; 3]] = &[z];
22     x[{ x = y; 0 }][2]              // OK we haven't checked any bounds before we index `x`.
23 }
24
25 fn modify_before_assert_array_array(x: &[&[i32; 3]; 3]) -> i32 {
26     let mut x = x;
27     let z: &[i32; 3] = &[1, 2, 3];
28     let y: &[&[i32; 3]; 3] = &[z, z, z];
29     x[{ x = y; 0 }][2]              // OK we haven't checked any bounds before we index `x`.
30 }
31
32 fn modify_after_assert_slice_slice(x: &[&[i32]]) -> i32 {
33     let mut x = x;
34     let z: &[i32] = &[1, 2, 3];
35     let y: &[&[i32]] = &[&z];
36     x[1][{ x = y; 2}]               //~ ERROR cannot assign `x` in indexing expression
37 }
38
39 fn modify_after_assert_array_slice(x: &[&[i32]; 1]) -> i32 {
40     let mut x = x;
41     let z: &[i32] = &[1, 2, 3];
42     let y: &[&[i32]; 1] = &[&z];
43     x[0][{ x = y; 2}]               // OK cannot invalidate a fixed-size array bounds check
44 }
45
46 fn modify_after_assert_slice_array(x: &[&[i32; 3]]) -> i32 {
47     let mut x = x;
48     let z: &[i32; 3] = &[1, 2, 3];
49     let y: &[&[i32; 3]] = &[&z];
50     x[1][{ x = y; 2}]               //~ ERROR cannot assign `x` in indexing expression
51 }
52
53 fn modify_after_assert_array_array(x: &[&[i32; 3]; 1]) -> i32 {
54     let mut x = x;
55     let z: &[i32; 3] = &[1, 2, 3];
56     let y: &[&[i32; 3]; 1] = &[&z];
57     x[0][{ x = y; 2}]               // OK cannot invalidate a fixed-size array bounds check
58 }
59
60 fn modify_after_assert_slice_slice_array(x: &[&[[i32; 1]]]) -> i32 {
61     let mut x = x;
62     let z: &[[i32; 1]] = &[[1], [2], [3]];
63     let y: &[&[[i32; 1]]] = &[&z];
64     x[1][{ x = y; 2}][0]            //~ ERROR cannot assign `x` in indexing expression
65 }
66
67 fn modify_after_assert_slice_slice_slice(x: &[&[&[i32]]]) -> i32 {
68     let mut x = x;
69     let z: &[&[i32]] = &[&[1], &[2], &[3]];
70     let y: &[&[&[i32]]] = &[z];
71     x[1][{ x = y; 2}][0]            //~ ERROR cannot assign `x` in indexing expression
72 }
73
74
75 fn main() {
76     println!("{}", modify_after_assert_slice_array(&[&[4, 5, 6], &[9, 10, 11]]));
77     println!("{}", modify_after_assert_slice_slice(&[&[4, 5, 6], &[9, 10, 11]]));
78     println!("{}", modify_after_assert_slice_slice_array(&[&[[4], [5], [6]], &[[9], [10], [11]]]));
79     println!("{}", modify_after_assert_slice_slice_slice(
80         &[&[&[4], &[5], &[6]], &[&[9], &[10], &[11]]]),
81     );
82 }