[docs] Add LICENSE.txt to the root of the mono-repo
[llvm-project.git] / clang / test / SemaCXX / coroutine-final-suspend-noexcept.cpp
blob35c00b84ea39843c6e5e32a3786d90c4e35dc088
1 // This file contains references to sections of the Coroutines TS, which can be
2 // found at http://wg21.link/coroutines.
4 // RUN: %clang_cc1 -std=c++20 -verify %s -fcxx-exceptions -fexceptions -Wunused-result
6 namespace std {
8 template <class Ret, typename... T>
9 struct coroutine_traits { using promise_type = typename Ret::promise_type; };
11 template <class Promise = void>
12 struct coroutine_handle {
13 static coroutine_handle from_address(void *);
14 void *address() const noexcept;
16 template <>
17 struct coroutine_handle<void> {
18 template <class PromiseType>
19 coroutine_handle(coroutine_handle<PromiseType>);
20 void *address() const noexcept;
23 struct suspend_always {
24 bool await_ready() { return false; } // expected-note 2 {{must be declared with 'noexcept'}}
25 void await_suspend(coroutine_handle<>) {} // expected-note 2 {{must be declared with 'noexcept'}}
26 void await_resume() {} // expected-note 2 {{must be declared with 'noexcept'}}
27 ~suspend_always() noexcept(false); // expected-note 2 {{must be declared with 'noexcept'}}
30 } // namespace std
32 using namespace std;
34 struct A {
35 bool await_ready();
36 void await_resume();
37 template <typename F>
38 void await_suspend(F);
41 struct coro_t {
42 struct promise_type {
43 coro_t get_return_object();
44 suspend_always initial_suspend();
45 suspend_always final_suspend(); // expected-note 2 {{must be declared with 'noexcept'}}
46 void return_void();
47 static void unhandled_exception();
51 coro_t f(int n) { // expected-error {{the expression 'co_await __promise.final_suspend()' is required to be non-throwing}}
52 A a{};
53 co_await a;
56 template <typename T>
57 coro_t f_dep(T n) { // expected-error {{the expression 'co_await __promise.final_suspend()' is required to be non-throwing}}
58 A a{};
59 co_await a;
62 void foo() {
63 f_dep<int>(5); // expected-note {{in instantiation of function template specialization 'f_dep<int>' requested here}}
66 struct PositiveFinalSuspend {
67 bool await_ready() noexcept;
68 coroutine_handle<> await_suspend(coroutine_handle<>) noexcept;
69 void await_resume() noexcept;
72 struct correct_coro {
73 struct promise_type {
74 correct_coro get_return_object();
75 suspend_always initial_suspend();
76 PositiveFinalSuspend final_suspend() noexcept;
77 void return_void();
78 static void unhandled_exception();
82 correct_coro f2(int n) {
83 co_return;
86 struct NegativeFinalSuspend {
87 bool await_ready() noexcept;
88 coroutine_handle<> await_suspend(coroutine_handle<>) noexcept;
89 void await_resume() noexcept;
90 ~NegativeFinalSuspend() noexcept(false); // expected-note {{must be declared with 'noexcept'}}
93 struct incorrect_coro {
94 struct promise_type {
95 incorrect_coro get_return_object();
96 suspend_always initial_suspend();
97 NegativeFinalSuspend final_suspend() noexcept;
98 void return_void();
99 static void unhandled_exception();
103 incorrect_coro f3(int n) { // expected-error {{the expression 'co_await __promise.final_suspend()' is required to be non-throwing}}
104 co_return;