Skip to content

Commit b64e911

Browse files
committed
Add test.
1 parent cba1407 commit b64e911

File tree

3 files changed

+168
-0
lines changed

3 files changed

+168
-0
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
- // MIR for `mut_raw_then_mut_shr` before ReferencePropagation
2+
+ // MIR for `mut_raw_then_mut_shr` after ReferencePropagation
3+
4+
fn mut_raw_then_mut_shr() -> (i32, i32) {
5+
let mut _0: (i32, i32); // return place in scope 0 at $DIR/reference_prop.rs:+0:30: +0:40
6+
let mut _1: i32; // in scope 0 at $DIR/reference_prop.rs:+1:9: +1:14
7+
let mut _4: *mut i32; // in scope 0 at $DIR/reference_prop.rs:+3:16: +3:36
8+
let mut _5: &mut i32; // in scope 0 at $DIR/reference_prop.rs:+3:16: +3:26
9+
let _8: (); // in scope 0 at $DIR/reference_prop.rs:+7:5: +7:26
10+
let mut _9: i32; // in scope 0 at $DIR/reference_prop.rs:+8:6: +8:7
11+
let mut _10: i32; // in scope 0 at $DIR/reference_prop.rs:+8:9: +8:10
12+
scope 1 {
13+
debug x => _1; // in scope 1 at $DIR/reference_prop.rs:+1:9: +1:14
14+
let _2: &mut i32; // in scope 1 at $DIR/reference_prop.rs:+2:9: +2:13
15+
scope 2 {
16+
debug xref => _2; // in scope 2 at $DIR/reference_prop.rs:+2:9: +2:13
17+
let _3: *mut i32; // in scope 2 at $DIR/reference_prop.rs:+3:9: +3:13
18+
scope 3 {
19+
debug xraw => _3; // in scope 3 at $DIR/reference_prop.rs:+3:9: +3:13
20+
let _6: &i32; // in scope 3 at $DIR/reference_prop.rs:+4:9: +4:13
21+
scope 4 {
22+
debug xshr => _6; // in scope 4 at $DIR/reference_prop.rs:+4:9: +4:13
23+
let _7: i32; // in scope 4 at $DIR/reference_prop.rs:+6:9: +6:10
24+
scope 5 {
25+
debug a => _7; // in scope 5 at $DIR/reference_prop.rs:+6:9: +6:10
26+
scope 6 {
27+
}
28+
}
29+
}
30+
}
31+
}
32+
}
33+
34+
bb0: {
35+
StorageLive(_1); // scope 0 at $DIR/reference_prop.rs:+1:9: +1:14
36+
_1 = const 2_i32; // scope 0 at $DIR/reference_prop.rs:+1:17: +1:18
37+
- StorageLive(_2); // scope 1 at $DIR/reference_prop.rs:+2:9: +2:13
38+
_2 = &mut _1; // scope 1 at $DIR/reference_prop.rs:+2:16: +2:22
39+
StorageLive(_3); // scope 2 at $DIR/reference_prop.rs:+3:9: +3:13
40+
StorageLive(_4); // scope 2 at $DIR/reference_prop.rs:+3:16: +3:36
41+
- StorageLive(_5); // scope 2 at $DIR/reference_prop.rs:+3:16: +3:26
42+
- _5 = &mut (*_2); // scope 2 at $DIR/reference_prop.rs:+3:16: +3:26
43+
- _4 = &raw mut (*_5); // scope 2 at $DIR/reference_prop.rs:+3:16: +3:26
44+
+ _5 = &mut _1; // scope 2 at $DIR/reference_prop.rs:+3:16: +3:26
45+
+ _4 = &raw mut (*_2); // scope 2 at $DIR/reference_prop.rs:+3:16: +3:26
46+
_3 = _4; // scope 2 at $DIR/reference_prop.rs:+3:16: +3:36
47+
- StorageDead(_5); // scope 2 at $DIR/reference_prop.rs:+3:36: +3:37
48+
StorageDead(_4); // scope 2 at $DIR/reference_prop.rs:+3:36: +3:37
49+
StorageLive(_6); // scope 3 at $DIR/reference_prop.rs:+4:9: +4:13
50+
- _6 = &(*_2); // scope 3 at $DIR/reference_prop.rs:+4:16: +4:22
51+
+ _6 = &_1; // scope 3 at $DIR/reference_prop.rs:+4:16: +4:22
52+
StorageLive(_7); // scope 4 at $DIR/reference_prop.rs:+6:9: +6:10
53+
- _7 = (*_6); // scope 4 at $DIR/reference_prop.rs:+6:13: +6:18
54+
- StorageLive(_8); // scope 5 at $DIR/reference_prop.rs:+7:5: +7:26
55+
- (*_3) = const 4_i32; // scope 6 at $DIR/reference_prop.rs:+7:14: +7:23
56+
- _8 = const (); // scope 6 at $DIR/reference_prop.rs:+7:5: +7:26
57+
- StorageDead(_8); // scope 5 at $DIR/reference_prop.rs:+7:25: +7:26
58+
+ _7 = (*_2); // scope 4 at $DIR/reference_prop.rs:+6:13: +6:18
59+
+ (*_5) = const 4_i32; // scope 6 at $DIR/reference_prop.rs:+7:14: +7:23
60+
StorageLive(_9); // scope 5 at $DIR/reference_prop.rs:+8:6: +8:7
61+
_9 = _7; // scope 5 at $DIR/reference_prop.rs:+8:6: +8:7
62+
StorageLive(_10); // scope 5 at $DIR/reference_prop.rs:+8:9: +8:10
63+
_10 = _1; // scope 5 at $DIR/reference_prop.rs:+8:9: +8:10
64+
_0 = (move _9, move _10); // scope 5 at $DIR/reference_prop.rs:+8:5: +8:11
65+
StorageDead(_10); // scope 5 at $DIR/reference_prop.rs:+8:10: +8:11
66+
StorageDead(_9); // scope 5 at $DIR/reference_prop.rs:+8:10: +8:11
67+
StorageDead(_7); // scope 4 at $DIR/reference_prop.rs:+9:1: +9:2
68+
StorageDead(_6); // scope 3 at $DIR/reference_prop.rs:+9:1: +9:2
69+
StorageDead(_3); // scope 2 at $DIR/reference_prop.rs:+9:1: +9:2
70+
- StorageDead(_2); // scope 1 at $DIR/reference_prop.rs:+9:1: +9:2
71+
StorageDead(_1); // scope 0 at $DIR/reference_prop.rs:+9:1: +9:2
72+
return; // scope 0 at $DIR/reference_prop.rs:+9:2: +9:2
73+
}
74+
}
75+

tests/mir-opt/reference_prop.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,29 @@ fn maybe_dead(m: bool) {
433433
)
434434
}
435435

436+
fn mut_raw_then_mut_shr() -> (i32, i32) {
437+
let mut x = 2;
438+
let xref = &mut x;
439+
let xraw = &mut *xref as *mut _;
440+
let xshr = &*xref;
441+
// Verify that we completely replace with `x` in both cases.
442+
let a = *xshr;
443+
unsafe { *xraw = 4; }
444+
(a, x)
445+
}
446+
447+
fn unique_with_copies() {
448+
let y = {
449+
let mut a = 0;
450+
let x = &raw mut a;
451+
// `*y` is not replacable below, so we must not replace `*x`.
452+
unsafe { opaque(*x) };
453+
x
454+
};
455+
// But rewriting as `*x` is ok.
456+
unsafe { opaque(*y) };
457+
}
458+
436459
fn main() {
437460
let mut x = 5_usize;
438461
let mut y = 7_usize;
@@ -444,6 +467,8 @@ fn main() {
444467
multiple_storage();
445468
dominate_storage();
446469
maybe_dead(true);
470+
mut_raw_then_mut_shr();
471+
unique_with_copies();
447472
}
448473

449474
// EMIT_MIR reference_prop.reference_propagation.ReferencePropagation.diff
@@ -454,3 +479,5 @@ fn main() {
454479
// EMIT_MIR reference_prop.multiple_storage.ReferencePropagation.diff
455480
// EMIT_MIR reference_prop.dominate_storage.ReferencePropagation.diff
456481
// EMIT_MIR reference_prop.maybe_dead.ReferencePropagation.diff
482+
// EMIT_MIR reference_prop.mut_raw_then_mut_shr.ReferencePropagation.diff
483+
// EMIT_MIR reference_prop.unique_with_copies.ReferencePropagation.diff
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
- // MIR for `unique_with_copies` before ReferencePropagation
2+
+ // MIR for `unique_with_copies` after ReferencePropagation
3+
4+
fn unique_with_copies() -> () {
5+
let mut _0: (); // return place in scope 0 at $DIR/reference_prop.rs:+0:25: +0:25
6+
let _1: *mut i32; // in scope 0 at $DIR/reference_prop.rs:+1:9: +1:10
7+
let mut _2: i32; // in scope 0 at $DIR/reference_prop.rs:+2:13: +2:18
8+
let _4: (); // in scope 0 at $DIR/reference_prop.rs:+5:18: +5:28
9+
let mut _5: i32; // in scope 0 at $DIR/reference_prop.rs:+5:25: +5:27
10+
let _6: (); // in scope 0 at $DIR/reference_prop.rs:+9:14: +9:24
11+
let mut _7: i32; // in scope 0 at $DIR/reference_prop.rs:+9:21: +9:23
12+
scope 1 {
13+
debug y => _1; // in scope 1 at $DIR/reference_prop.rs:+1:9: +1:10
14+
scope 5 {
15+
}
16+
}
17+
scope 2 {
18+
debug a => _2; // in scope 2 at $DIR/reference_prop.rs:+2:13: +2:18
19+
let _3: *mut i32; // in scope 2 at $DIR/reference_prop.rs:+3:13: +3:14
20+
scope 3 {
21+
debug x => _3; // in scope 3 at $DIR/reference_prop.rs:+3:13: +3:14
22+
scope 4 {
23+
}
24+
}
25+
}
26+
27+
bb0: {
28+
StorageLive(_1); // scope 0 at $DIR/reference_prop.rs:+1:9: +1:10
29+
StorageLive(_2); // scope 0 at $DIR/reference_prop.rs:+2:13: +2:18
30+
_2 = const 0_i32; // scope 0 at $DIR/reference_prop.rs:+2:21: +2:22
31+
StorageLive(_3); // scope 2 at $DIR/reference_prop.rs:+3:13: +3:14
32+
_3 = &raw mut _2; // scope 2 at $DIR/reference_prop.rs:+3:17: +3:27
33+
StorageLive(_4); // scope 3 at $DIR/reference_prop.rs:+5:9: +5:30
34+
StorageLive(_5); // scope 4 at $DIR/reference_prop.rs:+5:25: +5:27
35+
- _5 = (*_3); // scope 4 at $DIR/reference_prop.rs:+5:25: +5:27
36+
+ _5 = _2; // scope 4 at $DIR/reference_prop.rs:+5:25: +5:27
37+
_4 = opaque::<i32>(move _5) -> bb1; // scope 4 at $DIR/reference_prop.rs:+5:18: +5:28
38+
// mir::Constant
39+
// + span: $DIR/reference_prop.rs:452:18: 452:24
40+
// + literal: Const { ty: fn(i32) {opaque::<i32>}, val: Value(<ZST>) }
41+
}
42+
43+
bb1: {
44+
StorageDead(_5); // scope 4 at $DIR/reference_prop.rs:+5:27: +5:28
45+
StorageDead(_4); // scope 3 at $DIR/reference_prop.rs:+5:30: +5:31
46+
_1 = _3; // scope 3 at $DIR/reference_prop.rs:+6:9: +6:10
47+
StorageDead(_3); // scope 2 at $DIR/reference_prop.rs:+7:5: +7:6
48+
StorageDead(_2); // scope 0 at $DIR/reference_prop.rs:+7:5: +7:6
49+
StorageLive(_6); // scope 1 at $DIR/reference_prop.rs:+9:5: +9:26
50+
StorageLive(_7); // scope 5 at $DIR/reference_prop.rs:+9:21: +9:23
51+
_7 = (*_1); // scope 5 at $DIR/reference_prop.rs:+9:21: +9:23
52+
_6 = opaque::<i32>(move _7) -> bb2; // scope 5 at $DIR/reference_prop.rs:+9:14: +9:24
53+
// mir::Constant
54+
// + span: $DIR/reference_prop.rs:456:14: 456:20
55+
// + literal: Const { ty: fn(i32) {opaque::<i32>}, val: Value(<ZST>) }
56+
}
57+
58+
bb2: {
59+
StorageDead(_7); // scope 5 at $DIR/reference_prop.rs:+9:23: +9:24
60+
StorageDead(_6); // scope 1 at $DIR/reference_prop.rs:+9:26: +9:27
61+
_0 = const (); // scope 0 at $DIR/reference_prop.rs:+0:25: +10:2
62+
StorageDead(_1); // scope 0 at $DIR/reference_prop.rs:+10:1: +10:2
63+
return; // scope 0 at $DIR/reference_prop.rs:+10:2: +10:2
64+
}
65+
}
66+

0 commit comments

Comments
 (0)