]>
git.proxmox.com Git - rustc.git/blob - src/test/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.
4 fn modify_before_assert_slice_slice(x
: &[&[i32]]) -> i32 {
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`.
11 fn modify_before_assert_array_slice(x
: &[&[i32]; 3]) -> i32 {
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`.
18 fn modify_before_assert_slice_array(x
: &[&[i32; 3]]) -> i32 {
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`.
25 fn modify_before_assert_array_array(x
: &[&[i32; 3]; 3]) -> i32 {
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`.
32 fn modify_after_assert_slice_slice(x
: &[&[i32]]) -> i32 {
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
39 fn modify_after_assert_array_slice(x
: &[&[i32]; 1]) -> i32 {
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
46 fn modify_after_assert_slice_array(x
: &[&[i32; 3]]) -> i32 {
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
53 fn modify_after_assert_array_array(x
: &[&[i32; 3]; 1]) -> i32 {
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
60 fn modify_after_assert_slice_slice_array(x
: &[&[[i32; 1]]]) -> i32 {
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
67 fn modify_after_assert_slice_slice_slice(x
: &[&[&[i32]]]) -> i32 {
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
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]]]),