Skip to content

Commit c0c13b7

Browse files
committed
Improve test coverage of early_otherwise_branch mir opt
1 parent 709c94a commit c0c13b7

3 files changed

+105
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
- // MIR for `no_deref_ptr` before EarlyOtherwiseBranch
2+
+ // MIR for `no_deref_ptr` after EarlyOtherwiseBranch
3+
4+
fn no_deref_ptr(_1: Option<i32>, _2: *const Option<i32>) -> i32 {
5+
debug a => _1; // in scope 0 at $DIR/early_otherwise_branch_soundness.rs:18:24: 18:25
6+
debug b => _2; // in scope 0 at $DIR/early_otherwise_branch_soundness.rs:18:40: 18:41
7+
let mut _0: i32; // return place in scope 0 at $DIR/early_otherwise_branch_soundness.rs:18:66: 18:69
8+
let mut _3: isize; // in scope 0 at $DIR/early_otherwise_branch_soundness.rs:21:9: 21:16
9+
let mut _4: isize; // in scope 0 at $DIR/early_otherwise_branch_soundness.rs:22:13: 22:20
10+
let _5: i32; // in scope 0 at $DIR/early_otherwise_branch_soundness.rs:22:18: 22:19
11+
scope 1 {
12+
debug v => _5; // in scope 1 at $DIR/early_otherwise_branch_soundness.rs:22:18: 22:19
13+
}
14+
15+
bb0: {
16+
_3 = discriminant(_1); // scope 0 at $DIR/early_otherwise_branch_soundness.rs:19:11: 19:12
17+
switchInt(move _3) -> [1_isize: bb2, otherwise: bb1]; // scope 0 at $DIR/early_otherwise_branch_soundness.rs:19:5: 19:12
18+
}
19+
20+
bb1: {
21+
_0 = const 0_i32; // scope 0 at $DIR/early_otherwise_branch_soundness.rs:25:14: 25:15
22+
return; // scope 0 at $DIR/early_otherwise_branch_soundness.rs:25:14: 25:15
23+
}
24+
25+
bb2: {
26+
_4 = discriminant((*_2)); // scope 0 at $DIR/early_otherwise_branch_soundness.rs:21:26: 21:28
27+
switchInt(move _4) -> [1_isize: bb4, otherwise: bb3]; // scope 0 at $DIR/early_otherwise_branch_soundness.rs:21:20: 21:28
28+
}
29+
30+
bb3: {
31+
_0 = const 0_i32; // scope 0 at $DIR/early_otherwise_branch_soundness.rs:23:18: 23:19
32+
return; // scope 0 at $DIR/early_otherwise_branch_soundness.rs:23:18: 23:19
33+
}
34+
35+
bb4: {
36+
StorageLive(_5); // scope 0 at $DIR/early_otherwise_branch_soundness.rs:22:18: 22:19
37+
_5 = (((*_2) as Some).0: i32); // scope 0 at $DIR/early_otherwise_branch_soundness.rs:22:18: 22:19
38+
_0 = _5; // scope 1 at $DIR/early_otherwise_branch_soundness.rs:22:24: 22:25
39+
StorageDead(_5); // scope 0 at $DIR/early_otherwise_branch_soundness.rs:22:24: 22:25
40+
return; // scope 0 at $DIR/early_otherwise_branch_soundness.rs:22:24: 22:25
41+
}
42+
}
43+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
- // MIR for `no_downcast` before EarlyOtherwiseBranch
2+
+ // MIR for `no_downcast` after EarlyOtherwiseBranch
3+
4+
fn no_downcast(_1: &E) -> u32 {
5+
debug e => _1; // in scope 0 at $DIR/early_otherwise_branch_soundness.rs:12:16: 12:17
6+
let mut _0: u32; // return place in scope 0 at $DIR/early_otherwise_branch_soundness.rs:12:26: 12:29
7+
let mut _2: isize; // in scope 0 at $DIR/early_otherwise_branch_soundness.rs:13:20: 13:30
8+
let mut _3: isize; // in scope 0 at $DIR/early_otherwise_branch_soundness.rs:13:12: 13:31
9+
10+
bb0: {
11+
_3 = discriminant((*_1)); // scope 0 at $DIR/early_otherwise_branch_soundness.rs:13:12: 13:31
12+
switchInt(move _3) -> [1_isize: bb1, otherwise: bb3]; // scope 0 at $DIR/early_otherwise_branch_soundness.rs:13:12: 13:31
13+
}
14+
15+
bb1: {
16+
_2 = discriminant((*(((*_1) as Some).0: &E))); // scope 0 at $DIR/early_otherwise_branch_soundness.rs:13:12: 13:31
17+
switchInt(move _2) -> [1_isize: bb2, otherwise: bb3]; // scope 0 at $DIR/early_otherwise_branch_soundness.rs:13:12: 13:31
18+
}
19+
20+
bb2: {
21+
_0 = const 1_u32; // scope 0 at $DIR/early_otherwise_branch_soundness.rs:13:38: 13:39
22+
return; // scope 0 at $DIR/early_otherwise_branch_soundness.rs:13:5: 13:52
23+
}
24+
25+
bb3: {
26+
_0 = const 2_u32; // scope 0 at $DIR/early_otherwise_branch_soundness.rs:13:49: 13:50
27+
return; // scope 0 at $DIR/early_otherwise_branch_soundness.rs:13:5: 13:52
28+
}
29+
}
30+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// compile-flags: -Z mir-opt-level=4 -Zunsound-mir-opts
2+
3+
// Tests various cases that the `early_otherwise_branch` opt should *not* optimize
4+
5+
// From #78496
6+
enum E<'a> {
7+
Empty,
8+
Some(&'a E<'a>),
9+
}
10+
11+
// EMIT_MIR early_otherwise_branch_soundness.no_downcast.EarlyOtherwiseBranch.diff
12+
fn no_downcast(e: &E) -> u32 {
13+
if let E::Some(E::Some(_)) = e { 1 } else { 2 }
14+
}
15+
16+
// SAFETY: if `a` is `Some`, `b` must point to a valid, initialized value
17+
// EMIT_MIR early_otherwise_branch_soundness.no_deref_ptr.EarlyOtherwiseBranch.diff
18+
unsafe fn no_deref_ptr(a: Option<i32>, b: *const Option<i32>) -> i32 {
19+
match a {
20+
// `*b` being correct depends on `a == Some(_)`
21+
Some(_) => match *b {
22+
Some(v) => v,
23+
_ => 0,
24+
},
25+
_ => 0,
26+
}
27+
}
28+
29+
fn main() {
30+
no_downcast(&E::Empty);
31+
unsafe { no_deref_ptr(None, std::ptr::null()) };
32+
}

0 commit comments

Comments
 (0)