about summary refs log tree commit diff
path: root/src/test/run-pass
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/run-pass
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/run-pass')
-rw-r--r--src/test/run-pass/generator/drop-and-replace.rs44
1 files changed, 44 insertions, 0 deletions
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,
+            _ => (),
+        }
+    }
+}