about summary refs log tree commit diff
path: root/src/test/mir-opt/array-index-is-temporary.rs
blob: 096f98bade25ae255c4fc6c7db4c6a2dc57e6005 (plain)
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
// 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: {
//         ...
//         _4 = &mut _2;
//         _3 = &raw mut (*_4);
//         ...
//         _6 = _3;
//         _5 = const foo(move _6) -> bb1;
//     }
//
//     bb1: {
//         ...
//         _7 = _2;
//         _8 = Len(_1);
//         _9 = Lt(_7, _8);
//         assert(move _9, "index out of bounds: the len is move _8 but the index is _7") -> bb2;
//     }
//
//     bb2: {
//         _1[_7] = move _5;
//         ...
//         return;
//     }
// END rustc.main.EraseRegions.after.mir