Debian Codegraph @

gcc/testsuite/g++.dg/coroutines/torture/pr113773.C

raw
1 // { dg-do run }
2 #include <coroutine>
3 #ifdef OUTPUT
4 #include <iostream>
5 #endif
6
7 struct result {
8 operator int() {
9 throw 42;
10 }
11 };
12
13 static int p_dtor_count = 0;
14 class promise {
15 public:
16 result get_return_object() { return {}; }
17 std::suspend_never initial_suspend() {
18 #ifdef OUTPUT
19 std::cout << "initial suspend" << std::endl;
20 #endif
21 return {};
22 }
23 void unhandled_exception() {
24 #ifdef OUTPUT
25 std::cout << "unhandled exception" << std::endl;
26 #endif
27 }
28 std::suspend_never final_suspend() noexcept {
29 #ifdef OUTPUT
30 std::cout << "final suspend" << std::endl;
31 #endif
32 return {};
33 }
34 void return_void() {}
35 ~promise() {
36 p_dtor_count++;
37 #ifdef OUTPUT
38 std::cout << "~promise()" << std::endl;
39 #endif
40 }
41 };
42
43 template <class... Args>
44 struct std::coroutine_traits<int, Args...> {
45 using promise_type = promise;
46 };
47
48 int f() {
49 co_return;
50 }
51
52 int main() {
53 try {
54 f();
55 }
56 catch (int i) {
57 if (i != 42)
58 __builtin_abort ();
59 #ifdef OUTPUT
60 std::cout << "caught 42" << std::endl;
61 #endif
62 }
63 if (p_dtor_count != 1)
64 __builtin_abort ();
65 return 0;
66 }