forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharray-index-is-temporary.rs
42 lines (39 loc) · 975 Bytes
/
array-index-is-temporary.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// Retagging (from Stacked Borrows) relies on the array index being a fresh
// temporary, so that side-effects cannot change it.
// Test that this is indeed the case.
unsafe fn foo(z: *mut usize) -> u32 {
*z = 2;
99
}
fn main() {
let mut x = [42, 43, 44];
let mut y = 1;
let z: *mut usize = &mut y;
x[y] = unsafe { foo(z) };
}
// END RUST SOURCE
// START rustc.main.EraseRegions.after.mir
// bb0: {
// ...
// _5 = &mut _2;
// _4 = &mut (*_5);
// _3 = move _4 as *mut usize (Misc);
// ...
// _7 = _3;
// _6 = const foo(move _7) -> bb1;
// }
//
// bb1: {
// ...
// _8 = _2;
// _9 = Len(_1);
// _10 = Lt(_8, _9);
// assert(move _10, "index out of bounds: the len is move _9 but the index is _8") -> bb2;
// }
//
// bb2: {
// _1[_8] = move _6;
// ...
// return;
// }
// END rustc.main.EraseRegions.after.mir