Debian Codegraph @

gcc/testsuite/g++.dg/coroutines/torture/exceptions-test-01-n4849-a.C

raw
1 // { dg-do run }
2 // { dg-skip-if "requires hosted libstdc++ for cstdlib abort" { ! hostedlib } }
3
4 // Test exceptions in the initial await expression, per n4849.
5
6 #include "../coro.h"
7 #include <exception>
8
9 int gX = 0;
10
11 struct coro1 {
12 struct promise_type;
13 using handle_type = coro::coroutine_handle<coro1::promise_type>;
14 handle_type handle;
15
16 coro1 () : handle(0) {}
17 coro1 (handle_type _handle)
18 : handle(_handle) {
19 PRINT("Created coro1 object from handle");
20 }
21 coro1 (const coro1 &) = delete; // no copying
22 coro1 (coro1 &&s) : handle(s.handle) {
23 s.handle = nullptr;
24 PRINT("coro1 mv ctor ");
25 }
26 coro1 &operator = (coro1 &&s) {
27 handle = s.handle;
28 s.handle = nullptr;
29 PRINT("coro1 op= ");
30 return *this;
31 }
32 ~coro1() {
33 PRINT("Destroyed coro1");
34 if ( handle )
35 handle.destroy();
36 }
37
38 struct suspend_never_prt {
39 bool await_ready() const noexcept { return true; }
40 void await_suspend(handle_type) const noexcept { PRINT ("susp-never-susp"); }
41 void await_resume() const noexcept { PRINT ("susp-never-resume");}
42 };
43
44 struct suspend_always_prt {
45 bool await_ready() const noexcept { return false; }
46 void await_suspend(handle_type) const noexcept { PRINT ("susp-always-susp"); }
47 void await_resume() const noexcept { PRINT ("susp-always-resume"); }
48 ~suspend_always_prt() { PRINT ("susp-always-DTOR"); }
49 };
50
51 /* Constructing this with:
52 * a value of '1' will cause the initial suspend await_suspend()
53 call to throw.
54 * a value of '2' will cause the await resume to throw. */
55 struct suspend_always_susp_throws_prt {
56 int thrower;
57 suspend_always_susp_throws_prt (int _t) : thrower(_t) {}
58 bool await_ready() const noexcept { return false; }
59
60 void await_suspend(handle_type) const
61 { PRINT ("suspend_always_susp_throws_prt:await_suspend");
62 if (thrower == 1)
63 throw (42);
64 }
65 void await_resume() const
66 { PRINT ("suspend_always_susp_throws_prt:await_resume");
67 if (thrower == 2)
68 throw (6174);
69 }
70 ~suspend_always_susp_throws_prt() { PRINT ("suspend_always_susp_throws_prt-DTOR"); }
71 };
72
73 struct promise_type {
74 int throw_control = 0;
75 int value;
76 promise_type(int k) : throw_control(k), value(-373)
77 { PRINTF ("Created Promise with %d\n", k);}
78
79 ~promise_type() { PRINT ("Destroyed Promise"); }
80
81 auto get_return_object () {
82 PRINT ("get_return_object: handle from promise");
83 return handle_type::from_promise (*this);
84 }
85 // This provides the tests for what catches exceptions thrown at
86 // different points in the initial await expression.
87 auto initial_suspend () {
88 PRINT ("get initial_suspend (always)");
89 return suspend_always_susp_throws_prt(throw_control);
90 }
91 auto final_suspend () noexcept {
92 PRINT ("get final_suspend (always)");
93 return suspend_always_prt{};
94 }
95 void return_value (int v) {
96 PRINTF ("return_value () %d\n",v);
97 value = v;
98 }
99 auto yield_value (int v) {
100 PRINTF ("yield_value () %d and suspend always\n",v);
101 value = v;
102 return suspend_always_prt{};
103 }
104 int get_value (void) { return value; }
105
106 void unhandled_exception() {
107 PRINT ("unhandled_exception: caught one!");
108 gX = -11;
109 // returning from here should end up in final_suspend.
110 }
111 };
112 };
113
114 // This doesn't have to do much - we only need to exercise the initial
115 // await expression.
116
117 struct coro1
118 n4849_ia_thrower (int k)
119 {
120 PRINT ("f: about to return 22");
121 co_return 22;
122 }
123
124 int main ()
125 {
126 {
127 /* Case 0 - nothing should throw. */
128 struct coro1 x0;
129 try {
130 x0 = n4849_ia_thrower (0);
131 } catch (...) {
132 PRINT ("main: case 0 ctor threw?");
133 abort ();
134 }
135 /* Resume the initial suspend expression. */
136 PRINT ("main: got coro, resuming..");
137 x0.handle.resume();
138 int y = x0.handle.promise().get_value();
139 if ( y != 22 )
140 {
141 PRINT ("main: case 0 got the wrong answer.");
142 abort ();
143 }
144 if (!x0.handle.done())
145 {
146 PRINT ("main: case 0 not done.");
147 abort ();
148 }
149 if (gX != 0)
150 {
151 PRINT ("main: case 0 body threw?");
152 abort ();
153 }
154 }
155
156 {
157 /* Case 1 - initial suspend should throw and thus be caught by the
158 ramp's caller. */
159 struct coro1 x1;
160 try {
161 x1 = n4849_ia_thrower (1);
162 } catch (int message) {
163 PRINTF ("main: caught an int %d\n", message);
164 if (message != 42)
165 {
166 PRINT ("main: unexpected value?");
167 abort ();
168 }
169 } catch (...) {
170 PRINT ("main: case 1 ctor threw something else?");
171 abort ();
172 }
173 if (gX != 0)
174 {
175 PRINT ("main: case 0 body threw (how?)");
176 abort ();
177 }
178 }
179
180 {
181 /* Case 2 - the await_resume from the initial await expression throws
182 this should be caught by the regular function body wrapper. */
183 struct coro1 x2;
184 try {
185 x2 = n4849_ia_thrower (2);
186 } catch (...) {
187 PRINT ("main: case 2 ctor threw?");
188 abort ();
189 }
190 // We now resume - and expect the await_resume to throw which should
191 // be caught by unhandled_exception().
192 PRINT ("main: got coro, resuming..");
193 x2.handle.resume();
194 int y = x2.handle.promise().get_value();
195 if ( y != -373 )
196 {
197 PRINT ("main: case 2 got the wrong answer.");
198 abort ();
199 }
200 if (!x2.handle.done())
201 {
202 PRINT ("main: case 2 not done.");
203 abort ();
204 }
205 if (gX != -11)
206 {
207 PRINT ("main: n4849_is_thrower await_resume exception not caught");
208 abort ();
209 }
210 }
211 PRINT ("main: returning");
212 return 0;
213 }