]> git.lizzy.rs Git - rust.git/blob - src/test/mir-opt/early_otherwise_branch_soundness.rs
Rollup merge of #99864 - klensy:bootstrap-art-dupe, r=jyn514
[rust.git] / src / test / mir-opt / early_otherwise_branch_soundness.rs
1 // unit-test: EarlyOtherwiseBranch
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 }