Skip to content

Commit

Permalink
Add FileCheck for early_otherwise_branch*.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
DianQK committed Feb 22, 2024
1 parent 889bb7a commit 4613221
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 5 deletions.
24 changes: 23 additions & 1 deletion tests/mir-opt/early_otherwise_branch.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
// skip-filecheck
// unit-test: EarlyOtherwiseBranch
// compile-flags: -Zmir-enable-passes=+UninhabitedEnumBranching

// We can't optimize it because y may be an invalid value.
// EMIT_MIR early_otherwise_branch.opt1.EarlyOtherwiseBranch.diff
fn opt1(x: Option<u32>, y: Option<u32>) -> u32 {
// CHECK-LABEL: fn opt1(
// CHECK: bb0: {
// CHECK: [[LOCAL1:_.*]] = discriminant({{.*}});
// CHECK-NOT: Ne
// CHECK-NOT: discriminant
// CHECK: switchInt(move [[LOCAL1]]) -> [
// CHECK-NEXT: }
match (x, y) {
(Some(a), Some(b)) => 0,
_ => 1,
Expand All @@ -13,6 +19,14 @@ fn opt1(x: Option<u32>, y: Option<u32>) -> u32 {

// EMIT_MIR early_otherwise_branch.opt2.EarlyOtherwiseBranch.diff
fn opt2(x: Option<u32>, y: Option<u32>) -> u32 {
// CHECK-LABEL: fn opt2(
// CHECK: let mut [[CMP_LOCAL:_.*]]: bool;
// CHECK: bb0: {
// CHECK: [[LOCAL1:_.*]] = discriminant({{.*}});
// CHECK: [[LOCAL2:_.*]] = discriminant({{.*}});
// CHECK: [[CMP_LOCAL]] = Ne([[LOCAL1]], move [[LOCAL2]]);
// CHECK: switchInt(move [[CMP_LOCAL]]) -> [
// CHECK-NEXT: }
match (x, y) {
(Some(a), Some(b)) => 0,
(None, None) => 2,
Expand All @@ -23,6 +37,14 @@ fn opt2(x: Option<u32>, y: Option<u32>) -> u32 {
// optimize despite different types
// EMIT_MIR early_otherwise_branch.opt3.EarlyOtherwiseBranch.diff
fn opt3(x: Option<u32>, y: Option<bool>) -> u32 {
// CHECK-LABEL: fn opt3(
// CHECK: let mut [[CMP_LOCAL:_.*]]: bool;
// CHECK: bb0: {
// CHECK: [[LOCAL1:_.*]] = discriminant({{.*}});
// CHECK: [[LOCAL2:_.*]] = discriminant({{.*}});
// CHECK: [[CMP_LOCAL]] = Ne([[LOCAL1]], move [[LOCAL2]]);
// CHECK: switchInt(move [[CMP_LOCAL]]) -> [
// CHECK-NEXT: }
match (x, y) {
(Some(a), Some(b)) => 0,
(None, None) => 2,
Expand Down
9 changes: 8 additions & 1 deletion tests/mir-opt/early_otherwise_branch_3_element_tuple.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
// skip-filecheck
// unit-test: EarlyOtherwiseBranch
// compile-flags: -Zmir-enable-passes=+UninhabitedEnumBranching

// EMIT_MIR early_otherwise_branch_3_element_tuple.opt1.EarlyOtherwiseBranch.diff
fn opt1(x: Option<u32>, y: Option<u32>, z: Option<u32>) -> u32 {
// CHECK-LABEL: fn opt1(
// CHECK: let mut [[CMP_LOCAL:_.*]]: bool;
// CHECK: bb0: {
// CHECK: [[LOCAL1:_.*]] = discriminant({{.*}});
// CHECK: [[LOCAL2:_.*]] = discriminant({{.*}});
// CHECK: [[CMP_LOCAL]] = Ne([[LOCAL1]], move [[LOCAL2]]);
// CHECK: switchInt(move [[CMP_LOCAL]]) -> [
// CHECK-NEXT: }
match (x, y, z) {
(Some(a), Some(b), Some(c)) => 0,
(None, None, None) => 0,
Expand Down
8 changes: 7 additions & 1 deletion tests/mir-opt/early_otherwise_branch_68867.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// skip-filecheck
// unit-test: EarlyOtherwiseBranch
// compile-flags: -Zmir-enable-passes=+UninhabitedEnumBranching

Expand All @@ -20,6 +19,13 @@ pub extern "C" fn try_sum(
x: &ViewportPercentageLength,
other: &ViewportPercentageLength,
) -> Result<ViewportPercentageLength, ()> {
// CHECK-LABEL: fn try_sum(
// CHECK: bb0: {
// CHECK: [[LOCAL1:_.*]] = discriminant({{.*}});
// CHECK-NOT: Ne
// CHECK-NOT: discriminant
// CHECK: switchInt(move [[LOCAL1]]) -> [
// CHECK-NEXT: }
use self::ViewportPercentageLength::*;
Ok(match (x, other) {
(&Vw(one), &Vw(other)) => Vw(one + other),
Expand Down
8 changes: 7 additions & 1 deletion tests/mir-opt/early_otherwise_branch_noopt.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// skip-filecheck
// unit-test: EarlyOtherwiseBranch
// compile-flags: -Zmir-enable-passes=+UninhabitedEnumBranching

Expand All @@ -7,6 +6,13 @@

// EMIT_MIR early_otherwise_branch_noopt.noopt1.EarlyOtherwiseBranch.diff
fn noopt1(x: Option<u32>, y: Option<u32>) -> u32 {
// CHECK-LABEL: fn noopt1(
// CHECK: bb0: {
// CHECK: [[LOCAL1:_.*]] = discriminant({{.*}});
// CHECK-NOT: Ne
// CHECK-NOT: discriminant
// CHECK: switchInt(move [[LOCAL1]]) -> [
// CHECK-NEXT: }
match (x, y) {
(Some(a), Some(b)) => 0,
(Some(a), None) => 1,
Expand Down
15 changes: 14 additions & 1 deletion tests/mir-opt/early_otherwise_branch_soundness.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// skip-filecheck
// unit-test: EarlyOtherwiseBranch
// compile-flags: -Zmir-enable-passes=+UninhabitedEnumBranching

Expand All @@ -12,12 +11,26 @@ enum E<'a> {

// EMIT_MIR early_otherwise_branch_soundness.no_downcast.EarlyOtherwiseBranch.diff
fn no_downcast(e: &E) -> u32 {
// CHECK-LABEL: fn no_downcast(
// CHECK: bb0: {
// CHECK: [[LOCAL1:_.*]] = discriminant({{.*}});
// CHECK-NOT: Ne
// CHECK-NOT: discriminant
// CHECK: switchInt(move [[LOCAL1]]) -> [
// CHECK-NEXT: }
if let E::Some(E::Some(_)) = e { 1 } else { 2 }
}

// SAFETY: if `a` is `Some`, `b` must point to a valid, initialized value
// EMIT_MIR early_otherwise_branch_soundness.no_deref_ptr.EarlyOtherwiseBranch.diff
unsafe fn no_deref_ptr(a: Option<i32>, b: *const Option<i32>) -> i32 {
// CHECK-LABEL: fn no_deref_ptr(
// CHECK: bb0: {
// CHECK: [[LOCAL1:_.*]] = discriminant({{.*}});
// CHECK-NOT: Ne
// CHECK-NOT: discriminant
// CHECK: switchInt(move [[LOCAL1]]) -> [
// CHECK-NEXT: }
match a {
// `*b` being correct depends on `a == Some(_)`
Some(_) => match *b {
Expand Down

0 comments on commit 4613221

Please sign in to comment.