about summary refs log tree commit diff
path: root/src/test
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2019-06-06 06:36:12 +0000
committerbors <bors@rust-lang.org>2019-06-06 06:36:12 +0000
commitdaf1ed0e98e75c64c3b883fd845b37bfa42358de (patch)
treeb837eeea66f507e65fd05df9d7c269fbef684951 /src/test
parent740668dbd99dbf1726bbb0cca6cd0943ea2f7e27 (diff)
parent7718b14301b96c06c97d2c200508e0703b3de953 (diff)
downloadrust-daf1ed0e98e75c64c3b883fd845b37bfa42358de.tar.gz
rust-daf1ed0e98e75c64c3b883fd845b37bfa42358de.zip
Auto merge of #61373 - tmandry:emit-storagedead-along-unwind, r=eddyb
Emit StorageDead along unwind paths for generators

Completion of the work done in #60840. That PR made a change to implicitly consider a local `StorageDead` after Drop, but that was incorrect for DropAndReplace (see also #61060 which tried to fix this in a different way).

This finally enables the optimization implemented in #60187.

r? @eddyb
cc @Zoxc @cramertj @RalfJung
Diffstat (limited to 'src/test')
-rw-r--r--src/test/mir-opt/generator-storage-dead-unwind.rs106
-rw-r--r--src/test/run-pass/generator/drop-and-replace.rs44
2 files changed, 150 insertions, 0 deletions
diff --git a/src/test/mir-opt/generator-storage-dead-unwind.rs b/src/test/mir-opt/generator-storage-dead-unwind.rs
new file mode 100644
index 00000000000..7be17c4292a
--- /dev/null
+++ b/src/test/mir-opt/generator-storage-dead-unwind.rs
@@ -0,0 +1,106 @@
+// ignore-wasm32-bare compiled with panic=abort by default
+
+// Test that we generate StorageDead on unwind paths for generators.
+//
+// Basic block and local names can safely change, but the StorageDead statements
+// should not go away.
+
+#![feature(generators, generator_trait)]
+
+struct Foo(i32);
+
+impl Drop for Foo {
+    fn drop(&mut self) {}
+}
+
+struct Bar(i32);
+
+fn take<T>(_x: T) {}
+
+fn main() {
+    let _gen = || {
+        let a = Foo(5);
+        let b = Bar(6);
+        yield;
+        take(a);
+        take(b);
+    };
+}
+
+// END RUST SOURCE
+
+// START rustc.main-{{closure}}.StateTransform.before.mir
+// ...
+// let _2: Foo;
+// ...
+// let mut _7: Foo;
+// ...
+// let mut _9: Bar;
+// scope 1 {
+//     let _3: Bar;
+//     scope 2 {
+//     }
+// }
+// bb0: {
+//     StorageLive(_2);
+//     _2 = Foo(const 5i32,);
+//     StorageLive(_3);
+//     _3 = Bar(const 6i32,);
+//     ...
+//     _1 = suspend(move _5) -> [resume: bb2, drop: bb4];
+// }
+// bb1 (cleanup): {
+//     resume;
+// }
+// bb2: {
+//     ...
+//     StorageLive(_7);
+//     _7 = move _2;
+//     _6 = const take::<Foo>(move _7) -> [return: bb9, unwind: bb8];
+// }
+// bb3 (cleanup): {
+//     StorageDead(_2);
+//     drop(_1) -> bb1;
+// }
+// bb4: {
+//     ...
+//     StorageDead(_3);
+//     drop(_2) -> [return: bb5, unwind: bb3];
+// }
+// bb5: {
+//     StorageDead(_2);
+//     drop(_1) -> [return: bb6, unwind: bb1];
+// }
+// bb6: {
+//     generator_drop;
+// }
+// bb7 (cleanup): {
+//     StorageDead(_3);
+//     StorageDead(_2);
+//     drop(_1) -> bb1;
+// }
+// bb8 (cleanup): {
+//     StorageDead(_7);
+//     goto -> bb7;
+// }
+// bb9: {
+//     StorageDead(_7);
+//     StorageLive(_9);
+//     _9 = move _3;
+//     _8 = const take::<Bar>(move _9) -> [return: bb10, unwind: bb11];
+// }
+// bb10: {
+//     StorageDead(_9);
+//     ...
+//     StorageDead(_3);
+//     StorageDead(_2);
+//     drop(_1) -> [return: bb12, unwind: bb1];
+// }
+// bb11 (cleanup): {
+//     StorageDead(_9);
+//     goto -> bb7;
+// }
+// bb12: {
+//     return;
+// }
+// END rustc.main-{{closure}}.StateTransform.before.mir
diff --git a/src/test/run-pass/generator/drop-and-replace.rs b/src/test/run-pass/generator/drop-and-replace.rs
new file mode 100644
index 00000000000..042e1276db5
--- /dev/null
+++ b/src/test/run-pass/generator/drop-and-replace.rs
@@ -0,0 +1,44 @@
+// Regression test for incorrect DropAndReplace behavior introduced in #60840
+// and fixed in #61373. When combined with the optimization implemented in
+// #60187, this produced incorrect code for generators when a saved local was
+// re-assigned.
+
+#![feature(generators, generator_trait)]
+
+use std::ops::{Generator, GeneratorState};
+use std::pin::Pin;
+
+#[derive(Debug, PartialEq)]
+struct Foo(i32);
+
+impl Drop for Foo {
+    fn drop(&mut self) { }
+}
+
+fn main() {
+    let mut a = || {
+        let mut x = Foo(4);
+        yield;
+        assert_eq!(x.0, 4);
+
+        // At one point this tricked our dataflow analysis into thinking `x` was
+        // StorageDead after the assignment.
+        x = Foo(5);
+        assert_eq!(x.0, 5);
+
+        {
+            let y = Foo(6);
+            yield;
+            assert_eq!(y.0, 6);
+        }
+
+        assert_eq!(x.0, 5);
+    };
+
+    loop {
+        match Pin::new(&mut a).resume() {
+            GeneratorState::Complete(()) => break,
+            _ => (),
+        }
+    }
+}