Debian Codegraph @

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

raw
1 // PR c++/121219
2 // { dg-do run }
3
4 #include <coroutine>
5 #ifdef OUTPUT
6 #include <iostream>
7 #endif
8 #include <stdexcept>
9
10 struct Task {
11 struct promise_type;
12 using handle_type = std::coroutine_handle<promise_type>;
13
14 struct promise_type {
15 Task* task_;
16 int result_;
17
18 static void* operator new(std::size_t size) noexcept {
19 void* p = ::operator new(size, std::nothrow);
20 #ifdef OUTPUT
21 std::cerr << "operator new (no arg) " << size << " -> " << p << std::endl;
22 #endif
23 return p;
24 }
25 static void operator delete(void* ptr) noexcept {
26 return ::operator delete(ptr, std::nothrow);
27 }
28 #if 1 // change to 0 to fix crash
29 static Task get_return_object_on_allocation_failure() noexcept {
30 #ifdef OUTPUT
31 std::cerr << "get_return_object_on_allocation_failure" << std::endl;
32 #endif
33 return Task(nullptr);
34 }
35 #endif
36
37 auto get_return_object() {
38 #ifdef OUTPUT
39 std::cerr << "get_return_object" << std::endl;
40 #endif
41 return Task{handle_type::from_promise(*this)};
42 }
43
44 auto initial_suspend() {
45 #ifdef OUTPUT
46 std::cerr << "initial_suspend" << std::endl;
47 #endif
48 return std::suspend_always{};
49 }
50
51 auto final_suspend() noexcept {
52 #ifdef OUTPUT
53 std::cerr << "final_suspend" << std::endl;
54 #endif
55 return std::suspend_never{}; // Coroutine auto-destructs
56 }
57
58 ~promise_type() {
59 if (task_) {
60 #ifdef OUTPUT
61 std::cerr << "promise_type destructor: Clearing Task handle" << std::endl;
62 #endif
63 task_->h_ = nullptr;
64 }
65 }
66
67 void unhandled_exception() {
68 #ifdef OUTPUT
69 std::cerr << "unhandled_exception" << std::endl;
70 #endif
71 std::terminate();
72 }
73
74 void return_value(int value) {
75 #ifdef OUTPUT
76 std::cerr << "return_value: " << value << std::endl;
77 #endif
78 result_ = value;
79 if (task_) {
80 task_->result_ = value;
81 task_->completed_ = true;
82 }
83 }
84 };
85
86 handle_type h_;
87 int result_;
88 bool completed_ = false;
89
90 Task(handle_type h) : h_(h) {
91 #ifdef OUTPUT
92 std::cerr << "Task constructor" << std::endl;
93 #endif
94 if (h_) {
95 h_.promise().task_ = this; // Link promise to Task
96 }
97 }
98
99 ~Task() {
100 #ifdef OUTPUT
101 std::cerr << "~Task destructor" << std::endl;
102 #endif
103 // Only destroy handle if still valid (coroutine not completed)
104 if (h_) {
105 #ifdef OUTPUT
106 std::cerr << "Destroying coroutine handle" << std::endl;
107 #endif
108 h_.destroy();
109 }
110 }
111
112 bool done() const {
113 return completed_ || !h_ || h_.done();
114 }
115
116 void resume() {
117 #ifdef OUTPUT
118 std::cerr << "Resuming task" << std::endl;
119 #endif
120 if (h_) h_.resume();
121 }
122
123 int result() const {
124 if (!done()) throw std::runtime_error("Result not available");
125 return result_;
126 }
127 };
128
129 Task my_coroutine() {
130 #ifdef OUTPUT
131 std::cerr << "Inside my_coroutine" << std::endl;
132 #endif
133 co_return 42;
134 }
135
136 int main() {
137 auto task = my_coroutine();
138 while (!task.done()) {
139 #ifdef OUTPUT
140 std::cerr << "Resuming task in main" << std::endl;
141 #endif
142 task.resume();
143 }
144 #ifdef OUTPUT
145 std::cerr << "Task completed in main, printing result" << std::endl;
146 #endif
147 if (task.result() != 42)
148 __builtin_abort ();
149 }