Debian Codegraph @

gcc/testsuite/g++.dg/coroutines/torture/co-ret-09-bool-await-susp.C

raw
1 // { dg-do run }
2 // { dg-skip-if "requires hosted libstdc++ for cstdlib abort" { ! hostedlib } }
3
4 // test boolean return from await_suspend ().
5
6 #include "../coro.h"
7
8 int coro1_dtor_ran = 0;
9 int promise_dtor_ran = 0;
10
11 struct coro1 {
12 struct promise_type;
13 using handle_type = coro::coroutine_handle<coro1::promise_type>;
14 handle_type handle;
15 coro1 () : handle(0) {}
16 coro1 (handle_type _handle)
17 : handle(_handle) {
18 PRINT("Created coro1 object from handle");
19 }
20 coro1 (const coro1 &) = delete; // no copying
21 coro1 (coro1 &&s) : handle(s.handle) {
22 s.handle = nullptr;
23 PRINT("coro1 mv ctor ");
24 }
25 coro1 &operator = (coro1 &&s) {
26 handle = s.handle;
27 s.handle = nullptr;
28 PRINT("coro1 op= ");
29 return *this;
30 }
31
32 ~coro1() {
33 PRINT("Destroyed coro1");
34 coro1_dtor_ran++;
35 // The coro handle will point to an invalid frame by this stage,
36 // the coroutine will already have self-destroyed the frame and
37 // promise.
38 }
39
40 struct suspend_never_prt {
41 bool await_ready() const noexcept { return true; }
42 bool await_suspend(handle_type) const noexcept {
43 PRINT ("susp-never-susp"); // never executed.
44 return true; // ...
45 }
46 void await_resume() const noexcept {PRINT ("susp-never-resume");}
47 ~suspend_never_prt() {};
48 };
49
50 struct suspend_always_prt {
51 bool await_ready() const noexcept { return false; }
52 bool await_suspend(handle_type) const noexcept {
53 PRINT ("susp-always-susp, but we're going to continue.. ");
54 return false; // not going to suspend.
55 }
56 void await_resume() const noexcept { PRINT ("susp-always-resume");}
57 };
58
59 struct promise_type {
60 promise_type() { PRINT ("Created Promise"); }
61 ~promise_type() {
62 PRINT ("Destroyed Promise");
63 promise_dtor_ran++;
64 }
65
66 coro1 get_return_object () {
67 PRINT ("get_return_object: from handle from promise");
68 return coro1 (handle_type::from_promise (*this));
69 }
70 auto initial_suspend () {
71 PRINT ("get initial_suspend (always, but really never) ");
72 return suspend_always_prt{};
73 }
74 auto final_suspend () noexcept {
75 PRINT ("get final_suspend (always, but never) ");
76 return suspend_always_prt{};
77 }
78 void return_void () {
79 PRINT ("return_void ()");
80 }
81 void unhandled_exception() { PRINT ("** unhandled exception"); }
82 };
83 };
84
85 struct coro1
86 my_coro () noexcept
87 {
88 PRINT ("coro1: about to return");
89 co_return;
90 }
91
92 int main ()
93 {
94 { // scope so that we can examine the coro dtor marker.
95 PRINT ("main: creating coro");
96
97 // This should just run through to completion/destruction.
98 // In both the initial and final await expressions, the await_suspend()
99 // method will return 'false' and prevent the suspension.
100 struct coro1 x = my_coro ();
101
102 PRINT ("main: the coro frame should be already destroyed");
103 // We will take the running of the promise DTOR as evidence that the
104 // frame was destroyed as expected.
105 if (promise_dtor_ran != 1)
106 {
107 PRINT ("main: apparently we didn't destroy the frame");
108 abort ();
109 }
110 }
111 if (coro1_dtor_ran != 1 || promise_dtor_ran != 1)
112 {
113 PRINT ("main: bad DTOR counts");
114 abort ();
115 }
116 PRINT ("main: returning");
117 return 0;
118 }