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