Debian Codegraph @

gcc/testsuite/g++.dg/coroutines/torture/co-ret-04-GRO-nontriv.C

raw
1 // { dg-do run }
2 // { dg-skip-if "requires hosted libstdc++ for cstdlib abort" { ! hostedlib } }
3
4 // GRO differs from eventual return type and has non-trivial dtor.
5
6 #include "../coro.h"
7
8 struct coro1 {
9 struct promise_type;
10 using handle_type = coro::coroutine_handle<coro1::promise_type>;
11 handle_type handle;
12
13 struct nontriv {
14 handle_type handle;
15 nontriv () : handle(0) {PRINT("nontriv nul ctor");}
16 nontriv (handle_type _handle)
17 : handle(_handle) {
18 PRINT("Created nontriv object from handle");
19 }
20 ~nontriv () {
21 PRINT("Destroyed nontriv");
22 }
23 };
24
25 coro1 () : handle(0) {}
26 coro1 (handle_type _handle)
27 : handle(_handle) {
28 PRINT("Created coro1 object from handle");
29 }
30 coro1 (nontriv _nt)
31 : handle(_nt.handle) {
32 PRINT("Created coro1 object from nontriv");
33 }
34 coro1 (const coro1 &) = delete; // no copying
35 coro1 (coro1 &&s) : handle(s.handle) {
36 s.handle = nullptr;
37 PRINT("coro1 mv ctor ");
38 }
39 coro1 &operator = (coro1 &&s) {
40 handle = s.handle;
41 s.handle = nullptr;
42 PRINT("coro1 op= ");
43 return *this;
44 }
45 ~coro1() {
46 PRINT("Destroyed coro1");
47 if ( handle )
48 handle.destroy();
49 }
50
51 struct suspend_never_prt {
52 bool await_ready() const noexcept { return true; }
53 void await_suspend(handle_type) const noexcept { PRINT ("susp-never-susp");}
54 void await_resume() const noexcept { PRINT ("susp-never-resume");}
55 ~suspend_never_prt() {};
56 };
57
58 struct suspend_always_prt {
59 bool await_ready() const noexcept { return false; }
60 void await_suspend(handle_type) const noexcept { PRINT ("susp-always-susp");}
61 void await_resume() const noexcept { PRINT ("susp-always-resume");}
62 };
63
64 struct promise_type {
65 promise_type() { PRINT ("Created Promise"); }
66 ~promise_type() { PRINT ("Destroyed Promise"); }
67
68 auto get_return_object () {
69 PRINT ("get_return_object: handle from promise");
70 return nontriv(handle_type::from_promise (*this));
71 }
72 auto initial_suspend () {
73 PRINT ("get initial_suspend (always)");
74 return suspend_always_prt{};
75 }
76 auto final_suspend () noexcept {
77 PRINT ("get final_suspend (always)");
78 return suspend_always_prt{};
79 }
80 void return_void () {
81 PRINT ("return_void ()");
82 }
83 void unhandled_exception() { PRINT ("** unhandled exception"); }
84 };
85 };
86
87 struct coro1
88 f () noexcept
89 {
90 PRINT ("coro1: about to return");
91 co_return;
92 }
93
94 int main ()
95 {
96 PRINT ("main: create coro1");
97 struct coro1 x = f ();
98 PRINT ("main: got coro1 - resuming");
99 if (x.handle.done())
100 abort();
101 x.handle.resume();
102 PRINT ("main: after resume");
103 if (!x.handle.done())
104 {
105 PRINT ("main: apparently not done...");
106 abort ();
107 }
108 PRINT ("main: returning");
109 return 0;
110 }