Debian Codegraph @

gcc/testsuite/g++.dg/coroutines/torture/co-ret-08-template-cast-ret.C

raw
1 // { dg-do run }
2 // { dg-skip-if "requires hosted libstdc++ for cstdlib abort" { ! hostedlib } }
3
4 // Test templated co-return.
5
6 #include "../coro.h"
7
8 struct suspend_never_prt {
9 bool await_ready() const noexcept { return true; }
10 void await_suspend(coro::coroutine_handle<>) const noexcept
11 { PRINT ("susp-never-susp"); }
12 void await_resume() const noexcept { PRINT ("susp-never-resume");}
13 };
14
15 /* NOTE: this has a DTOR to test that pathway. */
16 struct suspend_always_prt {
17 bool await_ready() const noexcept { return false; }
18 void await_suspend(coro::coroutine_handle<>) const noexcept
19 { PRINT ("susp-always-susp"); }
20 void await_resume() const noexcept { PRINT ("susp-always-resume"); }
21 ~suspend_always_prt() { PRINT ("susp-always-DTOR"); }
22 };
23
24 template <typename T>
25 struct coro1 {
26 struct promise_type;
27 using handle_type = coro::coroutine_handle<coro1::promise_type>;
28 handle_type handle;
29 coro1 () : handle(0) {}
30 coro1 (handle_type _handle)
31 : handle(_handle) {
32 PRINT("Created coro1 object from handle");
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 promise_type {
52 T value;
53 promise_type() { PRINT ("Created Promise"); }
54 ~promise_type() { PRINT ("Destroyed Promise"); }
55
56 auto get_return_object () {
57 PRINT ("get_return_object: handle from promise");
58 return handle_type::from_promise (*this);
59 }
60 suspend_always_prt initial_suspend () const {
61 PRINT ("get initial_suspend (always)");
62 return suspend_always_prt{};
63 }
64 suspend_always_prt final_suspend () const noexcept {
65 PRINT ("get final_suspend (always)");
66 return suspend_always_prt{};
67 }
68 void return_value (T v) {
69 PRINTF ("return_value () %d\n",v);
70 value = v;
71 }
72 T get_value (void) { return value; }
73 void unhandled_exception() { PRINT ("** unhandled exception"); }
74 };
75 };
76
77 template <typename T>
78 coro1<T> f () noexcept
79 {
80 PRINT ("coro1: about to return");
81 co_return (T)42;
82 }
83
84 // The test will only really for int, but that's OK here.
85 int main ()
86 {
87 PRINT ("main: create coro1");
88 auto x = f<int>();
89 PRINT ("main: got coro1 - resuming");
90 if (x.handle.done())
91 abort();
92 x.handle.resume();
93 PRINT ("main: after resume");
94 int y = x.handle.promise().get_value();
95 if ( y != 42 )
96 abort ();
97 if (!x.handle.done())
98 {
99 PRINT ("main: apparently not done...");
100 abort ();
101 //x.handle.resume();
102 }
103 PRINT ("main: returning");
104 return 0;
105 }