Skip to content

Commit

Permalink
Add test for DropAndReplace bug
Browse files Browse the repository at this point in the history
  • Loading branch information
tmandry committed May 30, 2019
1 parent a09f605 commit e9ad4fe
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions src/test/run-pass/generator/drop-and-replace.rs
Original file line number Diff line number Diff line change
@@ -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,
_ => (),
}
}
}

0 comments on commit e9ad4fe

Please sign in to comment.