]> git.lizzy.rs Git - rust.git/blob - src/test/ui/generator/panic-drops-resume.rs
Rollup merge of #82259 - osa1:issue82156, r=petrochenkov
[rust.git] / src / test / ui / generator / panic-drops-resume.rs
1 //! Tests that panics inside a generator will correctly drop the initial resume argument.
2
3 // run-pass
4 // ignore-wasm       no unwind support
5 // ignore-emscripten no unwind support
6
7 #![feature(generators, generator_trait)]
8
9 use std::ops::Generator;
10 use std::panic::{catch_unwind, AssertUnwindSafe};
11 use std::pin::Pin;
12 use std::sync::atomic::{AtomicUsize, Ordering};
13
14 static DROP: AtomicUsize = AtomicUsize::new(0);
15
16 struct Dropper {}
17
18 impl Drop for Dropper {
19     fn drop(&mut self) {
20         DROP.fetch_add(1, Ordering::SeqCst);
21     }
22 }
23
24 fn main() {
25     let mut gen = |_arg| {
26         if true {
27             panic!();
28         }
29         yield ();
30     };
31     let mut gen = Pin::new(&mut gen);
32
33     assert_eq!(DROP.load(Ordering::Acquire), 0);
34     let res = catch_unwind(AssertUnwindSafe(|| gen.as_mut().resume(Dropper {})));
35     assert!(res.is_err());
36     assert_eq!(DROP.load(Ordering::Acquire), 1);
37 }