| 1 |
// PR c++/121961 |
| 2 |
// { dg-additional-options "-fsanitize=address" } |
| 3 |
// { dg-do run { target c++20 } } |
| 4 |
|
| 5 |
#include <coroutine> |
| 6 |
|
| 7 |
struct Result |
| 8 |
{ |
| 9 |
int Value; |
| 10 |
}; |
| 11 |
|
| 12 |
template<typename... Args> |
| 13 |
struct std::coroutine_traits<Result, Args...> |
| 14 |
{ |
| 15 |
struct promise_type; |
| 16 |
|
| 17 |
struct Wrapper |
| 18 |
{ |
| 19 |
std::coroutine_handle<promise_type> Handle_; |
| 20 |
bool Own_ = false; |
| 21 |
|
| 22 |
~Wrapper () |
| 23 |
{ |
| 24 |
if (Own_) |
| 25 |
Handle_.destroy (); |
| 26 |
} |
| 27 |
|
| 28 |
operator Result () |
| 29 |
{ |
| 30 |
Own_ = true; |
| 31 |
return { Handle_.promise ().Value_ }; |
| 32 |
} |
| 33 |
}; |
| 34 |
|
| 35 |
struct promise_type |
| 36 |
{ |
| 37 |
int Value_ = 0; |
| 38 |
|
| 39 |
std::suspend_never initial_suspend () noexcept { return {}; } |
| 40 |
std::suspend_always final_suspend () noexcept { return {}; } |
| 41 |
void unhandled_exception () {} |
| 42 |
|
| 43 |
void return_value (int v) { Value_ = v; } |
| 44 |
|
| 45 |
Wrapper get_return_object () |
| 46 |
{ |
| 47 |
return { std::coroutine_handle<promise_type>::from_promise (*this) }; |
| 48 |
} |
| 49 |
}; |
| 50 |
}; |
| 51 |
|
| 52 |
struct Awaitable |
| 53 |
{ |
| 54 |
int V_; |
| 55 |
bool await_ready () const noexcept { return true; } |
| 56 |
void await_suspend (std::coroutine_handle<>) const noexcept {} |
| 57 |
int await_resume () const noexcept { return V_; } |
| 58 |
}; |
| 59 |
|
| 60 |
Result simple () |
| 61 |
{ |
| 62 |
auto a = co_await Awaitable { 3 }; |
| 63 |
auto b = co_await Awaitable { 5 }; |
| 64 |
co_return a * b; |
| 65 |
} |
| 66 |
|
| 67 |
int main () |
| 68 |
{ |
| 69 |
auto r = simple (); |
| 70 |
return r.Value == 15 ? 0 : 1; |
| 71 |
} |