Debian Codegraph @

gcc/testsuite/g++.dg/coroutines/pr99576_1.C

raw
1 // { dg-do run }
2 // { dg-skip-if "requires hosted libstdc++ for iostream" { ! hostedlib } }
3 /*
4 Test that instances created in capture clauses within co_await statements do not get
5 'promoted'. This would lead to their members destructors getting called more
6 than once.
7
8 Correct output should look like:
9 START TASK
10 Foo() 0x4f9320
11 IN LAMBDA
12 ~Foo() 0x4f9320
13 TASK RETURN
14 */
15 #include <coroutine>
16 #include <exception>
17 #include <iostream>
18 #include <utility>
19
20 static unsigned int struct_Foo_destructor_counter = 0;
21 static bool lambda_was_executed = false;
22
23 class Task {
24 public:
25 struct promise_type {
26 struct final_awaitable {
27 bool await_ready() noexcept { return false; }
28 auto await_suspend(std::coroutine_handle<promise_type> coro) noexcept {
29 return coro.promise().continuation;
30 }
31 void await_resume() noexcept {}
32 };
33 Task get_return_object() {
34 return Task(std::coroutine_handle<promise_type>::from_promise(*this));
35 }
36 std::suspend_always initial_suspend() { return {}; }
37 final_awaitable final_suspend() noexcept { return {}; }
38 void unhandled_exception() { std::terminate(); }
39 void return_void() {}
40
41 std::coroutine_handle<void> continuation = std::noop_coroutine();
42 };
43
44 Task(Task const&) = delete;
45 Task(Task&& other) noexcept
46 : handle_(std::exchange(other.handle_, nullptr)) {}
47 Task& operator=(Task const&) = delete;
48 Task& operator=(Task&& other) noexcept {
49 handle_ = std::exchange(other.handle_, nullptr);
50 return *this;
51 }
52 ~Task() {
53 if (handle_) {
54 handle_.destroy();
55 }
56 }
57
58 bool await_ready() const { return false; }
59 auto await_suspend(std::coroutine_handle<void> continuation) {
60 handle_.promise().continuation = continuation;
61 return handle_;
62 }
63 void await_resume() {}
64
65 private:
66 explicit Task(std::coroutine_handle<promise_type> handle) : handle_(handle) {}
67
68 std::coroutine_handle<promise_type> handle_;
69 };
70
71 struct RunTask {
72 struct promise_type {
73 RunTask get_return_object() { return {}; }
74 std::suspend_never initial_suspend() { return {}; }
75 std::suspend_never final_suspend() noexcept { return {}; }
76 void return_void() {}
77 void unhandled_exception() { std::terminate(); }
78 };
79 };
80
81 struct Foo {
82 Foo() {
83 std::cout << "Foo() " << (void *)this << std::endl;
84 }
85
86 ~Foo() {
87 std::cout << "~Foo() " << (void *)this << std::endl;
88 struct_Foo_destructor_counter++;
89
90 if (struct_Foo_destructor_counter > 1 || !lambda_was_executed) {
91 std::cout << "The destructor of Foo was called more than once or too early!\n";
92 __builtin_abort();
93 }
94 }
95
96 Foo(Foo&&) = delete;
97 Foo(Foo const&) = delete;
98 Foo& operator=(Foo&&) = delete;
99 Foo& operator=(Foo const&) = delete;
100 };
101
102 Task DoAsync() {
103 std::cout << "START TASK\n";
104 co_await [foo = Foo{}]() -> Task { // foo is constructed inplace, no copy/move is performed.
105 // foo itself must not get 'promoted'.
106 std::cout << "IN LAMBDA\n";
107 lambda_was_executed = true;
108 co_return;
109 }();
110 // After the co_await statement the temporary lambda and foo
111 // must now have been destroyed
112 if (struct_Foo_destructor_counter == 0){
113 std::cout << "foo was not destroyed after the co_await statement!\n";
114 __builtin_abort();
115 }
116 std::cout << "TASK RETURN\n";
117 co_return;
118 }
119
120 RunTask Main() { co_await DoAsync(); }
121
122 int main() {
123 Main();
124 return 0;
125 }