Skip to content

Commit 341eaf5

Browse files
Add more tests for generator resume arguments
1 parent 392e595 commit 341eaf5

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//! Tests that panics inside a generator will correctly drop the initial resume argument.
2+
3+
// run-pass
4+
5+
#![feature(generators, generator_trait)]
6+
7+
use std::ops::Generator;
8+
use std::panic::{catch_unwind, AssertUnwindSafe};
9+
use std::pin::Pin;
10+
use std::sync::atomic::{AtomicUsize, Ordering};
11+
12+
static DROP: AtomicUsize = AtomicUsize::new(0);
13+
14+
struct Dropper {}
15+
16+
impl Drop for Dropper {
17+
fn drop(&mut self) {
18+
DROP.fetch_add(1, Ordering::SeqCst);
19+
}
20+
}
21+
22+
fn main() {
23+
let mut gen = |_arg| {
24+
if true {
25+
panic!();
26+
}
27+
yield ();
28+
};
29+
let mut gen = Pin::new(&mut gen);
30+
31+
assert_eq!(DROP.load(Ordering::Acquire), 0);
32+
let res = catch_unwind(AssertUnwindSafe(|| gen.as_mut().resume(Dropper {})));
33+
assert!(res.is_err());
34+
assert_eq!(DROP.load(Ordering::Acquire), 1);
35+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//! Tests that we cannot produce a generator that accepts a resume argument
2+
//! with any lifetime and then stores it across a `yield`.
3+
4+
#![feature(generators, generator_trait)]
5+
6+
use std::ops::Generator;
7+
8+
fn test(a: impl for<'a> Generator<&'a mut bool>) {}
9+
10+
fn main() {
11+
let gen = |arg: &mut bool| {
12+
yield ();
13+
*arg = true;
14+
};
15+
test(gen);
16+
//~^ ERROR type mismatch in function arguments
17+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error[E0631]: type mismatch in function arguments
2+
--> $DIR/resume-arg-late-bound.rs:15:10
3+
|
4+
LL | fn test(a: impl for<'a> Generator<&'a mut bool>) {}
5+
| ---- ------------------------------- required by this bound in `test`
6+
...
7+
LL | test(gen);
8+
| ^^^
9+
| |
10+
| expected signature of `for<'a> fn(&'a mut bool) -> _`
11+
| found signature of `fn(&mut bool) -> _`
12+
13+
error: aborting due to previous error
14+
15+
For more information about this error, try `rustc --explain E0631`.

0 commit comments

Comments
 (0)