about summary refs log tree commit diff
path: root/src/test
diff options
context:
space:
mode:
authorTomasz Miąsko <tomasz.miasko@gmail.com>2019-11-29 00:00:00 +0000
committerTomasz Miąsko <tomasz.miasko@gmail.com>2019-11-29 20:13:54 +0100
commit45c4e11e43e177056d87b15ea4bddbf03a469036 (patch)
tree4d9d412ecbcebcaff25595e90042df5ba6ca23da /src/test
parentc4375c9dfdd7f31de909f6e9384bac1bf37b44da (diff)
downloadrust-45c4e11e43e177056d87b15ea4bddbf03a469036.tar.gz
rust-45c4e11e43e177056d87b15ea4bddbf03a469036.zip
SimplifyArmIdentity only for locals with the same type
Co-Authored-By: Mazdak Farrokhzad <twingoow@gmail.com>
Diffstat (limited to 'src/test')
-rw-r--r--src/test/mir-opt/simplify-arm-identity.rs75
-rw-r--r--src/test/ui/issues/issue-66851.rs20
2 files changed, 95 insertions, 0 deletions
diff --git a/src/test/mir-opt/simplify-arm-identity.rs b/src/test/mir-opt/simplify-arm-identity.rs
new file mode 100644
index 00000000000..a8fa64255fb
--- /dev/null
+++ b/src/test/mir-opt/simplify-arm-identity.rs
@@ -0,0 +1,75 @@
+// Checks that `SimplifyArmIdentity` is not applied if enums have incompatible layouts.
+// Regression test for issue #66856.
+//
+// compile-flags: -Zmir-opt-level=2
+
+enum Src {
+    Foo(u8),
+    Bar,
+}
+
+enum Dst {
+    Foo(u8),
+}
+
+fn main() {
+    let e: Src = Src::Foo(0);
+    let _: Dst = match e {
+        Src::Foo(x) => Dst::Foo(x),
+        Src::Bar => Dst::Foo(0),
+    };
+}
+
+// END RUST SOURCE
+// START rustc.main.SimplifyArmIdentity.before.mir
+// fn main() -> () {
+//     ...
+//     bb0: {
+//         StorageLive(_1);
+//         ((_1 as Foo).0: u8) = const 0u8;
+//         discriminant(_1) = 0;
+//         StorageLive(_2);
+//         _3 = discriminant(_1);
+//         switchInt(move _3) -> [0isize: bb3, 1isize: bb1, otherwise: bb2];
+//     }
+//     bb1: {
+//         ((_2 as Foo).0: u8) = const 0u8;
+//         discriminant(_2) = 0;
+//         goto -> bb4;
+//     }
+//     ...
+//     bb3: {
+//         _4 = ((_1 as Foo).0: u8);
+//         ((_2 as Foo).0: u8) = move _4;
+//         discriminant(_2) = 0;
+//         goto -> bb4;
+//     }
+//     ...
+// }
+// END rustc.main.SimplifyArmIdentity.before.mir
+// START rustc.main.SimplifyArmIdentity.after.mir
+// fn main() -> () {
+//     ...
+//     bb0: {
+//         StorageLive(_1);
+//         ((_1 as Foo).0: u8) = const 0u8;
+//         discriminant(_1) = 0;
+//         StorageLive(_2);
+//         _3 = discriminant(_1);
+//         switchInt(move _3) -> [0isize: bb3, 1isize: bb1, otherwise: bb2];
+//     }
+//     bb1: {
+//         ((_2 as Foo).0: u8) = const 0u8;
+//         discriminant(_2) = 0;
+//         goto -> bb4;
+//     }
+//     ...
+//     bb3: {
+//         _4 = ((_1 as Foo).0: u8);
+//         ((_2 as Foo).0: u8) = move _4;
+//         discriminant(_2) = 0;
+//         goto -> bb4;
+//     }
+//     ...
+// }
+// END rustc.main.SimplifyArmIdentity.after.mir
diff --git a/src/test/ui/issues/issue-66851.rs b/src/test/ui/issues/issue-66851.rs
new file mode 100644
index 00000000000..72d62a30a33
--- /dev/null
+++ b/src/test/ui/issues/issue-66851.rs
@@ -0,0 +1,20 @@
+// This used to mis-compile because the mir-opt `SimplifyArmIdentity`
+// did not check that the types matched up in the `Ok(r)` branch.
+//
+// run-pass
+// compile-flags: -Zmir-opt-level=2
+
+#[derive(Debug, PartialEq, Eq)]
+enum SpecialsRes { Res(u64) }
+
+fn e103() -> SpecialsRes {
+    if let Ok(r) = "1".parse() {
+        SpecialsRes::Res(r)
+    } else {
+        SpecialsRes::Res(42)
+    }
+}
+
+fn main() {
+    assert_eq!(e103(), SpecialsRes::Res(1));
+}