Add more tests for generator resume arguments
This commit is contained in:
parent
392e59500a
commit
341eaf5f55
3 changed files with 67 additions and 0 deletions
35
src/test/ui/generator/panic-drops-resume.rs
Normal file
35
src/test/ui/generator/panic-drops-resume.rs
Normal file
|
@ -0,0 +1,35 @@
|
|||
//! Tests that panics inside a generator will correctly drop the initial resume argument.
|
||||
|
||||
// run-pass
|
||||
|
||||
#![feature(generators, generator_trait)]
|
||||
|
||||
use std::ops::Generator;
|
||||
use std::panic::{catch_unwind, AssertUnwindSafe};
|
||||
use std::pin::Pin;
|
||||
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||
|
||||
static DROP: AtomicUsize = AtomicUsize::new(0);
|
||||
|
||||
struct Dropper {}
|
||||
|
||||
impl Drop for Dropper {
|
||||
fn drop(&mut self) {
|
||||
DROP.fetch_add(1, Ordering::SeqCst);
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut gen = |_arg| {
|
||||
if true {
|
||||
panic!();
|
||||
}
|
||||
yield ();
|
||||
};
|
||||
let mut gen = Pin::new(&mut gen);
|
||||
|
||||
assert_eq!(DROP.load(Ordering::Acquire), 0);
|
||||
let res = catch_unwind(AssertUnwindSafe(|| gen.as_mut().resume(Dropper {})));
|
||||
assert!(res.is_err());
|
||||
assert_eq!(DROP.load(Ordering::Acquire), 1);
|
||||
}
|
17
src/test/ui/generator/resume-arg-late-bound.rs
Normal file
17
src/test/ui/generator/resume-arg-late-bound.rs
Normal file
|
@ -0,0 +1,17 @@
|
|||
//! Tests that we cannot produce a generator that accepts a resume argument
|
||||
//! with any lifetime and then stores it across a `yield`.
|
||||
|
||||
#![feature(generators, generator_trait)]
|
||||
|
||||
use std::ops::Generator;
|
||||
|
||||
fn test(a: impl for<'a> Generator<&'a mut bool>) {}
|
||||
|
||||
fn main() {
|
||||
let gen = |arg: &mut bool| {
|
||||
yield ();
|
||||
*arg = true;
|
||||
};
|
||||
test(gen);
|
||||
//~^ ERROR type mismatch in function arguments
|
||||
}
|
15
src/test/ui/generator/resume-arg-late-bound.stderr
Normal file
15
src/test/ui/generator/resume-arg-late-bound.stderr
Normal file
|
@ -0,0 +1,15 @@
|
|||
error[E0631]: type mismatch in function arguments
|
||||
--> $DIR/resume-arg-late-bound.rs:15:10
|
||||
|
|
||||
LL | fn test(a: impl for<'a> Generator<&'a mut bool>) {}
|
||||
| ---- ------------------------------- required by this bound in `test`
|
||||
...
|
||||
LL | test(gen);
|
||||
| ^^^
|
||||
| |
|
||||
| expected signature of `for<'a> fn(&'a mut bool) -> _`
|
||||
| found signature of `fn(&mut bool) -> _`
|
||||
|
||||
error: aborting due to previous error
|
||||
|
||||
For more information about this error, try `rustc --explain E0631`.
|
Loading…
Add table
Reference in a new issue