Debian Codegraph @

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

raw
1 // { dg-do run }
2
3 // With the changes to deal with CWG2563 (and PR119916) we now use the
4 // referenced promise in the return expression. It is quite reasonable
5 // for a body implementation to complete before control is returned to
6 // the ramp - so we need to ensure that the promise lifetime is extended
7 // in that case, tested here.
8
9 #include <coroutine>
10
11 #ifdef OUTPUT
12 #include <iostream>
13 #endif
14
15 struct Promise;
16
17 int promise_life = 0;
18
19 struct Handle : std::coroutine_handle<Promise> {
20
21 Handle(Promise &p) : std::coroutine_handle<Promise>(Handle::from_promise(p)) {
22 #ifdef OUTPUT
23 std::cout << "Handle(Promise &) " << promise_life << std::endl;
24 #endif
25 if (promise_life <= 0)
26 __builtin_abort ();
27 }
28
29 Handle(Promise &&p) : std::coroutine_handle<Promise>(Handle::from_promise(p)) {
30 #ifdef OUTPUT
31 std::cout << "Handle(Promise &&) " << promise_life << std::endl;
32 #endif
33 if (promise_life <= 0)
34 __builtin_abort ();
35 }
36
37 using promise_type = Promise;
38 };
39
40 struct Promise {
41 Promise() {
42 #ifdef OUTPUT
43 std::cout << "Promise()" << std::endl;
44 #endif
45 promise_life++;
46 }
47
48 Promise(Promise& p){
49 #ifdef OUTPUT
50 std::cout << "Promise(Promise&)" << std::endl;
51 #endif
52 promise_life++;
53 }
54
55 ~Promise() {
56 #ifdef OUTPUT
57 std::cout << "~Promise()" << std::endl;
58 #endif
59 if (promise_life <= 0)
60 __builtin_abort ();
61 promise_life--;
62 }
63
64 Promise& get_return_object() noexcept {
65 #ifdef OUTPUT
66 std::cout << "get_return_object() " << promise_life << std::endl;
67 #endif
68 if (promise_life <= 0)
69 __builtin_abort ();
70 return *this;
71 }
72
73 std::suspend_never initial_suspend() const noexcept {
74 #ifdef OUTPUT
75 std::cout << "initial_suspend()" << std::endl;
76 #endif
77 return {};
78 }
79 std::suspend_never final_suspend() const noexcept {
80 #ifdef OUTPUT
81 std::cout << "final_suspend()" << std::endl;
82 #endif
83 return {};
84 }
85 void return_void() const noexcept {
86 if (!promise_life)
87 __builtin_abort ();
88 #ifdef OUTPUT
89 std::cout << "return_void()" << std::endl;
90 #endif
91 }
92 void unhandled_exception() const noexcept {}
93 };
94
95 Handle Coro() {
96
97 #ifdef OUTPUT
98 std::cout << "Coro()" << std::endl;
99 #endif
100 co_return;
101 }
102
103 int main() {
104
105 Coro();
106 #ifdef OUTPUT
107 std::cout << "done Coro()" << std::endl;
108 #endif
109 if (promise_life)
110 __builtin_abort ();
111 return 0;
112 }