]> git.lizzy.rs Git - rust.git/blob - tests/ui/generator/drop-and-replace.rs
Rollup merge of #106714 - Ezrashaw:remove-e0490, r=davidtwco
[rust.git] / tests / ui / generator / drop-and-replace.rs
1 // run-pass
2 // Regression test for incorrect DropAndReplace behavior introduced in #60840
3 // and fixed in #61373. When combined with the optimization implemented in
4 // #60187, this produced incorrect code for generators when a saved local was
5 // re-assigned.
6
7 #![feature(generators, generator_trait)]
8
9 use std::ops::{Generator, GeneratorState};
10 use std::pin::Pin;
11
12 #[derive(Debug, PartialEq)]
13 struct Foo(i32);
14
15 impl Drop for Foo {
16     fn drop(&mut self) { }
17 }
18
19 fn main() {
20     let mut a = || {
21         let mut x = Foo(4);
22         yield;
23         assert_eq!(x.0, 4);
24
25         // At one point this tricked our dataflow analysis into thinking `x` was
26         // StorageDead after the assignment.
27         x = Foo(5);
28         assert_eq!(x.0, 5);
29
30         {
31             let y = Foo(6);
32             yield;
33             assert_eq!(y.0, 6);
34         }
35
36         assert_eq!(x.0, 5);
37     };
38
39     loop {
40         match Pin::new(&mut a).resume(()) {
41             GeneratorState::Complete(()) => break,
42             _ => (),
43         }
44     }
45 }