]> git.lizzy.rs Git - rust.git/blob - src/test/ui/generator/panic-drops-resume.rs
Auto merge of #98051 - davidtwco:split-dwarf-stabilization, r=wesleywiser
[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 // needs-unwind
5 // ignore-wasm       no unwind support
6 // ignore-emscripten no unwind support
7
8 #![feature(generators, generator_trait)]
9
10 use std::ops::Generator;
11 use std::panic::{catch_unwind, AssertUnwindSafe};
12 use std::pin::Pin;
13 use std::sync::atomic::{AtomicUsize, Ordering};
14
15 static DROP: AtomicUsize = AtomicUsize::new(0);
16
17 struct Dropper {}
18
19 impl Drop for Dropper {
20     fn drop(&mut self) {
21         DROP.fetch_add(1, Ordering::SeqCst);
22     }
23 }
24
25 fn main() {
26     let mut gen = |_arg| {
27         if true {
28             panic!();
29         }
30         yield ();
31     };
32     let mut gen = Pin::new(&mut gen);
33
34     assert_eq!(DROP.load(Ordering::Acquire), 0);
35     let res = catch_unwind(AssertUnwindSafe(|| gen.as_mut().resume(Dropper {})));
36     assert!(res.is_err());
37     assert_eq!(DROP.load(Ordering::Acquire), 1);
38 }