| 1 |
// PR120453 - reduced testcase amended to add a TaskBase move constructor |
| 2 |
// and a LazyTask destructor, to more closely match the original code. |
| 3 |
// { dg-additional-options "-w" } |
| 4 |
namespace std { |
| 5 |
template <typename> struct __coroutine_traits_impl; |
| 6 |
template <typename _Result> |
| 7 |
requires requires { typename _Result; } |
| 8 |
struct __coroutine_traits_impl<_Result>; |
| 9 |
template <typename _Result, typename> struct coroutine_traits : _Result {}; |
| 10 |
template <typename = void> struct coroutine_handle { |
| 11 |
static coroutine_handle from_address(void *); |
| 12 |
operator coroutine_handle<>(); |
| 13 |
void *address(); |
| 14 |
}; |
| 15 |
struct suspend_never { |
| 16 |
bool await_ready(); |
| 17 |
void await_suspend(coroutine_handle<>); |
| 18 |
void await_resume(); |
| 19 |
}; |
| 20 |
} // namespace std |
| 21 |
|
| 22 |
namespace QCoro { |
| 23 |
namespace detail { |
| 24 |
template <typename T> |
| 25 |
concept has_await_methods = requires(T t) { t; }; |
| 26 |
} // namespace detail |
| 27 |
|
| 28 |
template <typename T> |
| 29 |
concept Awaitable = detail::has_await_methods<T>; |
| 30 |
namespace detail { |
| 31 |
struct TaskFinalSuspend { |
| 32 |
bool await_ready() noexcept; |
| 33 |
template <typename Promise> |
| 34 |
void await_suspend(std::coroutine_handle<Promise>) noexcept; |
| 35 |
void await_resume() noexcept; |
| 36 |
}; |
| 37 |
struct TaskPromiseBase { |
| 38 |
std::suspend_never initial_suspend(); |
| 39 |
auto final_suspend() noexcept { return TaskFinalSuspend{}; } |
| 40 |
template <Awaitable T> auto &&await_transform(T &&); |
| 41 |
}; |
| 42 |
struct TaskPromise : TaskPromiseBase { |
| 43 |
void unhandled_exception(); |
| 44 |
}; |
| 45 |
template <typename> struct TaskAwaiterBase { |
| 46 |
bool await_ready(); |
| 47 |
void await_suspend(std::coroutine_handle<>); |
| 48 |
}; |
| 49 |
template <typename, template <typename> class, typename> struct TaskBase { |
| 50 |
TaskBase() = default; |
| 51 |
TaskBase(TaskBase &&) = default; |
| 52 |
void operator=(TaskBase &&); |
| 53 |
auto operator co_await() const; |
| 54 |
std::coroutine_handle<> mCoroutine; |
| 55 |
}; |
| 56 |
} // namespace detail |
| 57 |
template <typename> struct Task : detail::TaskBase<int, Task, int> {}; |
| 58 |
} // namespace QCoro |
| 59 |
|
| 60 |
namespace QCoro::detail { |
| 61 |
template <Awaitable T> auto &&TaskPromiseBase::await_transform(T &&awaitable) { |
| 62 |
return awaitable; |
| 63 |
} |
| 64 |
|
| 65 |
template <typename T, template <typename> class TaskImpl, typename PromiseType> |
| 66 |
auto TaskBase<T, TaskImpl, PromiseType>::operator co_await() const { |
| 67 |
class TaskAwaiter : public TaskAwaiterBase<PromiseType> { |
| 68 |
public: |
| 69 |
TaskAwaiter(std::coroutine_handle<>); |
| 70 |
auto await_resume() {} |
| 71 |
}; |
| 72 |
return TaskAwaiter{mCoroutine}; |
| 73 |
} |
| 74 |
|
| 75 |
} // namespace QCoro::detail |
| 76 |
|
| 77 |
namespace QCoro { |
| 78 |
template <typename T> class LazyTask ; |
| 79 |
namespace detail { |
| 80 |
struct LazyTaskPromise : TaskPromise { |
| 81 |
LazyTask<int> get_return_object(); |
| 82 |
}; |
| 83 |
} // namespace detail |
| 84 |
|
| 85 |
template <typename T> struct LazyTask : detail::TaskBase<int, LazyTask, int> { |
| 86 |
typedef detail::LazyTaskPromise promise_type; |
| 87 |
~LazyTask(); |
| 88 |
}; |
| 89 |
|
| 90 |
auto coro = [] -> LazyTask<int> |
| 91 |
{ |
| 92 |
co_await [] -> Task<int> { return {}; }(); |
| 93 |
}; |
| 94 |
|
| 95 |
} // namespace QCoro |