| Brian Gesiak | c1b173a | 2018-06-23 18:01:02 +0000 | [diff] [blame] | 1 | // This file contains references to sections of the Coroutines TS, which can be |
| 2 | // found at http://wg21.link/coroutines. |
| 3 | |
| Eric Fiselier | 16269a8 | 2018-03-27 00:58:16 +0000 | [diff] [blame] | 4 | // RUN: %clang_cc1 -std=c++14 -fcoroutines-ts -verify %s -fcxx-exceptions -fexceptions -Wunused-result |
| Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 5 | |
| Eric Fiselier | a546528 | 2016-09-29 21:47:39 +0000 | [diff] [blame] | 6 | void no_coroutine_traits_bad_arg_await() { |
| Gor Nishanov | 3e048bb | 2016-10-04 00:31:16 +0000 | [diff] [blame] | 7 | co_await a; // expected-error {{include <experimental/coroutine>}} |
| Eric Fiselier | a546528 | 2016-09-29 21:47:39 +0000 | [diff] [blame] | 8 | // expected-error@-1 {{use of undeclared identifier 'a'}} |
| 9 | } |
| 10 | |
| 11 | void no_coroutine_traits_bad_arg_yield() { |
| Gor Nishanov | 3e048bb | 2016-10-04 00:31:16 +0000 | [diff] [blame] | 12 | co_yield a; // expected-error {{include <experimental/coroutine>}} |
| Eric Fiselier | a546528 | 2016-09-29 21:47:39 +0000 | [diff] [blame] | 13 | // expected-error@-1 {{use of undeclared identifier 'a'}} |
| 14 | } |
| 15 | |
| 16 | |
| 17 | void no_coroutine_traits_bad_arg_return() { |
| Gor Nishanov | 3e048bb | 2016-10-04 00:31:16 +0000 | [diff] [blame] | 18 | co_return a; // expected-error {{include <experimental/coroutine>}} |
| Eric Fiselier | a546528 | 2016-09-29 21:47:39 +0000 | [diff] [blame] | 19 | // expected-error@-1 {{use of undeclared identifier 'a'}} |
| 20 | } |
| 21 | |
| Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 22 | void no_coroutine_traits() { |
| Gor Nishanov | 6dcb0eb | 2017-03-09 03:09:43 +0000 | [diff] [blame] | 23 | co_await 4; // expected-error {{std::experimental::coroutine_traits type was not found; include <experimental/coroutine>}} |
| Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 24 | } |
| 25 | |
| 26 | namespace std { |
| Gor Nishanov | 3e048bb | 2016-10-04 00:31:16 +0000 | [diff] [blame] | 27 | namespace experimental { |
| Eric Fiselier | 166c6e6 | 2017-07-10 01:27:22 +0000 | [diff] [blame] | 28 | |
| 29 | template <class... Args> |
| 30 | struct void_t_imp { |
| 31 | using type = void; |
| 32 | }; |
| 33 | template <class... Args> |
| 34 | using void_t = typename void_t_imp<Args...>::type; |
| 35 | |
| 36 | template <class T, class = void> |
| 37 | struct traits_sfinae_base {}; |
| 38 | |
| 39 | template <class T> |
| 40 | struct traits_sfinae_base<T, void_t<typename T::promise_type>> { |
| 41 | using promise_type = typename T::promise_type; |
| 42 | }; |
| 43 | |
| 44 | template <class Ret, class... Args> |
| 45 | struct coroutine_traits : public traits_sfinae_base<Ret> {}; |
| Gor Nishanov | 6dcb0eb | 2017-03-09 03:09:43 +0000 | [diff] [blame] | 46 | }} // namespace std::experimental |
| Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 47 | |
| Richard Smith | 2af65c4 | 2015-11-24 02:34:39 +0000 | [diff] [blame] | 48 | template<typename Promise> struct coro {}; |
| Gor Nishanov | 3e048bb | 2016-10-04 00:31:16 +0000 | [diff] [blame] | 49 | template <typename Promise, typename... Ps> |
| 50 | struct std::experimental::coroutine_traits<coro<Promise>, Ps...> { |
| Richard Smith | 2af65c4 | 2015-11-24 02:34:39 +0000 | [diff] [blame] | 51 | using promise_type = Promise; |
| 52 | }; |
| 53 | |
| Gor Nishanov | 6dcb0eb | 2017-03-09 03:09:43 +0000 | [diff] [blame] | 54 | struct awaitable { |
| 55 | bool await_ready(); |
| 56 | template <typename F> void await_suspend(F); |
| 57 | void await_resume(); |
| 58 | } a; |
| 59 | |
| 60 | struct suspend_always { |
| 61 | bool await_ready() { return false; } |
| 62 | template <typename F> void await_suspend(F); |
| 63 | void await_resume() {} |
| 64 | }; |
| 65 | |
| 66 | struct suspend_never { |
| 67 | bool await_ready() { return true; } |
| 68 | template <typename F> void await_suspend(F); |
| 69 | void await_resume() {} |
| 70 | }; |
| 71 | |
| Gor Nishanov | db419a6 | 2017-09-05 19:31:52 +0000 | [diff] [blame] | 72 | struct auto_await_suspend { |
| 73 | bool await_ready(); |
| 74 | template <typename F> auto await_suspend(F) {} |
| 75 | void await_resume(); |
| 76 | }; |
| 77 | |
| Eric Fiselier | 166c6e6 | 2017-07-10 01:27:22 +0000 | [diff] [blame] | 78 | struct DummyVoidTag {}; |
| 79 | DummyVoidTag no_specialization() { // expected-error {{this function cannot be a coroutine: 'std::experimental::coroutine_traits<DummyVoidTag>' has no member named 'promise_type'}} |
| 80 | co_await a; |
| Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 81 | } |
| 82 | |
| Gor Nishanov | 3e048bb | 2016-10-04 00:31:16 +0000 | [diff] [blame] | 83 | template <typename... T> |
| 84 | struct std::experimental::coroutine_traits<int, T...> {}; |
| Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 85 | |
| Eric Fiselier | 89bf0e7 | 2017-03-06 22:52:28 +0000 | [diff] [blame] | 86 | int no_promise_type() { // expected-error {{this function cannot be a coroutine: 'std::experimental::coroutine_traits<int>' has no member named 'promise_type'}} |
| 87 | co_await a; |
| Richard Smith | 9b2f53e | 2015-11-19 02:36:35 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| Gor Nishanov | 3e048bb | 2016-10-04 00:31:16 +0000 | [diff] [blame] | 90 | template <> |
| 91 | struct std::experimental::coroutine_traits<double, double> { typedef int promise_type; }; |
| Eric Fiselier | 89bf0e7 | 2017-03-06 22:52:28 +0000 | [diff] [blame] | 92 | double bad_promise_type(double) { // expected-error {{this function cannot be a coroutine: 'experimental::coroutine_traits<double, double>::promise_type' (aka 'int') is not a class}} |
| 93 | co_await a; |
| Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 94 | } |
| 95 | |
| Gor Nishanov | 3e048bb | 2016-10-04 00:31:16 +0000 | [diff] [blame] | 96 | template <> |
| 97 | struct std::experimental::coroutine_traits<double, int> { |
| Richard Smith | 23da82c | 2015-11-20 22:40:06 +0000 | [diff] [blame] | 98 | struct promise_type {}; |
| 99 | }; |
| Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 100 | double bad_promise_type_2(int) { // expected-error {{no member named 'initial_suspend'}} |
| Gor Nishanov | 3e048bb | 2016-10-04 00:31:16 +0000 | [diff] [blame] | 101 | co_yield 0; // expected-error {{no member named 'yield_value' in 'std::experimental::coroutine_traits<double, int>::promise_type'}} |
| Richard Smith | 23da82c | 2015-11-20 22:40:06 +0000 | [diff] [blame] | 102 | } |
| 103 | |
| Eric Fiselier | 89bf0e7 | 2017-03-06 22:52:28 +0000 | [diff] [blame] | 104 | struct promise; // expected-note {{forward declaration}} |
| Eric Fiselier | 9813131 | 2016-10-06 21:23:38 +0000 | [diff] [blame] | 105 | struct promise_void; |
| 106 | struct void_tag {}; |
| Gor Nishanov | 3e048bb | 2016-10-04 00:31:16 +0000 | [diff] [blame] | 107 | template <typename... T> |
| 108 | struct std::experimental::coroutine_traits<void, T...> { using promise_type = promise; }; |
| Eric Fiselier | 9813131 | 2016-10-06 21:23:38 +0000 | [diff] [blame] | 109 | template <typename... T> |
| 110 | struct std::experimental::coroutine_traits<void, void_tag, T...> |
| 111 | { using promise_type = promise_void; }; |
| Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 112 | |
| Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 113 | // FIXME: This diagnostic is terrible. |
| Eric Fiselier | 89bf0e7 | 2017-03-06 22:52:28 +0000 | [diff] [blame] | 114 | void undefined_promise() { // expected-error {{this function cannot be a coroutine: 'experimental::coroutine_traits<void>::promise_type' (aka 'promise') is an incomplete type}} |
| Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 115 | co_await a; |
| 116 | } |
| 117 | |
| Richard Smith | ae3d147 | 2015-11-20 22:47:10 +0000 | [diff] [blame] | 118 | struct yielded_thing { const char *p; short a, b; }; |
| Richard Smith | 23da82c | 2015-11-20 22:40:06 +0000 | [diff] [blame] | 119 | |
| Richard Smith | d7bed4d | 2015-11-22 02:57:17 +0000 | [diff] [blame] | 120 | struct not_awaitable {}; |
| 121 | |
| Richard Smith | 23da82c | 2015-11-20 22:40:06 +0000 | [diff] [blame] | 122 | struct promise { |
| Richard Smith | 2af65c4 | 2015-11-24 02:34:39 +0000 | [diff] [blame] | 123 | void get_return_object(); |
| 124 | suspend_always initial_suspend(); |
| 125 | suspend_always final_suspend(); |
| Richard Smith | 71d403e | 2015-11-22 07:33:28 +0000 | [diff] [blame] | 126 | awaitable yield_value(int); // expected-note 2{{candidate}} |
| 127 | awaitable yield_value(yielded_thing); // expected-note 2{{candidate}} |
| 128 | not_awaitable yield_value(void()); // expected-note 2{{candidate}} |
| Richard Smith | 71d403e | 2015-11-22 07:33:28 +0000 | [diff] [blame] | 129 | void return_value(int); // expected-note 2{{here}} |
| Eric Fiselier | a9fdb34 | 2017-03-23 00:33:33 +0000 | [diff] [blame] | 130 | void unhandled_exception(); |
| Richard Smith | 23da82c | 2015-11-20 22:40:06 +0000 | [diff] [blame] | 131 | }; |
| 132 | |
| Eric Fiselier | 9813131 | 2016-10-06 21:23:38 +0000 | [diff] [blame] | 133 | struct promise_void { |
| 134 | void get_return_object(); |
| 135 | suspend_always initial_suspend(); |
| 136 | suspend_always final_suspend(); |
| Eric Fiselier | 9813131 | 2016-10-06 21:23:38 +0000 | [diff] [blame] | 137 | void return_void(); |
| Eric Fiselier | a9fdb34 | 2017-03-23 00:33:33 +0000 | [diff] [blame] | 138 | void unhandled_exception(); |
| Eric Fiselier | 9813131 | 2016-10-06 21:23:38 +0000 | [diff] [blame] | 139 | }; |
| 140 | |
| Gor Nishanov | 6dcb0eb | 2017-03-09 03:09:43 +0000 | [diff] [blame] | 141 | void no_coroutine_handle() { // expected-error {{std::experimental::coroutine_handle type was not found; include <experimental/coroutine> before defining a coroutine}} |
| 142 | //expected-note@-1 {{call to 'initial_suspend' implicitly required by the initial suspend point}} |
| 143 | co_return 5; //expected-note {{function is a coroutine due to use of 'co_return' here}} |
| 144 | } |
| 145 | |
| 146 | namespace std { |
| 147 | namespace experimental { |
| 148 | template <class PromiseType = void> |
| 149 | struct coroutine_handle { |
| 150 | static coroutine_handle from_address(void *); |
| 151 | }; |
| 152 | template <> |
| 153 | struct coroutine_handle<void> { |
| 154 | template <class PromiseType> |
| 155 | coroutine_handle(coroutine_handle<PromiseType>); |
| 156 | static coroutine_handle from_address(void *); |
| 157 | }; |
| 158 | }} // namespace std::experimental |
| 159 | |
| Richard Smith | 23da82c | 2015-11-20 22:40:06 +0000 | [diff] [blame] | 160 | void yield() { |
| 161 | co_yield 0; |
| Richard Smith | ae3d147 | 2015-11-20 22:47:10 +0000 | [diff] [blame] | 162 | co_yield {"foo", 1, 2}; |
| Erich Keane | 7130a93 | 2018-05-07 20:52:56 +0000 | [diff] [blame] | 163 | co_yield {1e100}; // expected-error {{cannot be narrowed}} expected-note {{explicit cast}} expected-warning {{implicit conversion}} expected-warning {{braces around scalar}} |
| Richard Smith | ae3d147 | 2015-11-20 22:47:10 +0000 | [diff] [blame] | 164 | co_yield {"foo", __LONG_LONG_MAX__}; // expected-error {{cannot be narrowed}} expected-note {{explicit cast}} expected-warning {{changes value}} |
| 165 | co_yield {"foo"}; |
| Richard Smith | 23da82c | 2015-11-20 22:40:06 +0000 | [diff] [blame] | 166 | co_yield "foo"; // expected-error {{no matching}} |
| Richard Smith | d7bed4d | 2015-11-22 02:57:17 +0000 | [diff] [blame] | 167 | co_yield 1.0; |
| 168 | co_yield yield; // expected-error {{no member named 'await_ready' in 'not_awaitable'}} |
| Richard Smith | 23da82c | 2015-11-20 22:40:06 +0000 | [diff] [blame] | 169 | } |
| Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 170 | |
| Gor Nishanov | db419a6 | 2017-09-05 19:31:52 +0000 | [diff] [blame] | 171 | void check_auto_await_suspend() { |
| 172 | co_await auto_await_suspend{}; // Should compile successfully. |
| 173 | } |
| 174 | |
| Richard Smith | 4ba6660 | 2015-11-22 07:05:16 +0000 | [diff] [blame] | 175 | void coreturn(int n) { |
| 176 | co_await a; |
| 177 | if (n == 0) |
| 178 | co_return 3; |
| 179 | if (n == 1) |
| Eric Fiselier | 9813131 | 2016-10-06 21:23:38 +0000 | [diff] [blame] | 180 | co_return {4}; // expected-warning {{braces around scalar initializer}} |
| Richard Smith | 4ba6660 | 2015-11-22 07:05:16 +0000 | [diff] [blame] | 181 | if (n == 2) |
| 182 | co_return "foo"; // expected-error {{cannot initialize a parameter of type 'int' with an lvalue of type 'const char [4]'}} |
| Eric Fiselier | 9813131 | 2016-10-06 21:23:38 +0000 | [diff] [blame] | 183 | co_return 42; |
| Richard Smith | 4ba6660 | 2015-11-22 07:05:16 +0000 | [diff] [blame] | 184 | } |
| 185 | |
| Eric Fiselier | 8d409e8 | 2017-03-09 05:01:31 +0000 | [diff] [blame] | 186 | template <class T> |
| 187 | void co_await_non_dependent_arg(T) { |
| 188 | co_await a; |
| 189 | } |
| 190 | template void co_await_non_dependent_arg(int); |
| 191 | |
| Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 192 | void mixed_yield() { |
| Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 193 | co_yield 0; // expected-note {{use of 'co_yield'}} |
| 194 | return; // expected-error {{not allowed in coroutine}} |
| Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 195 | } |
| 196 | |
| Eric Fiselier | cac0a59 | 2017-03-11 02:35:37 +0000 | [diff] [blame] | 197 | void mixed_yield_invalid() { |
| 198 | co_yield blah; // expected-error {{use of undeclared identifier}} |
| 199 | // expected-note@-1 {{function is a coroutine due to use of 'co_yield'}} |
| 200 | return; // expected-error {{return statement not allowed in coroutine}} |
| 201 | } |
| 202 | |
| 203 | template <class T> |
| 204 | void mixed_yield_template(T) { |
| 205 | co_yield blah; // expected-error {{use of undeclared identifier}} |
| 206 | // expected-note@-1 {{function is a coroutine due to use of 'co_yield'}} |
| 207 | return; // expected-error {{return statement not allowed in coroutine}} |
| 208 | } |
| 209 | |
| 210 | template <class T> |
| 211 | void mixed_yield_template2(T) { |
| 212 | co_yield 42; |
| 213 | // expected-note@-1 {{function is a coroutine due to use of 'co_yield'}} |
| 214 | return; // expected-error {{return statement not allowed in coroutine}} |
| 215 | } |
| 216 | |
| 217 | template <class T> |
| 218 | void mixed_yield_template3(T v) { |
| 219 | co_yield blah(v); |
| 220 | // expected-note@-1 {{function is a coroutine due to use of 'co_yield'}} |
| 221 | return; // expected-error {{return statement not allowed in coroutine}} |
| 222 | } |
| 223 | |
| Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 224 | void mixed_await() { |
| Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 225 | co_await a; // expected-note {{use of 'co_await'}} |
| 226 | return; // expected-error {{not allowed in coroutine}} |
| Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 227 | } |
| 228 | |
| Eric Fiselier | cac0a59 | 2017-03-11 02:35:37 +0000 | [diff] [blame] | 229 | void mixed_await_invalid() { |
| 230 | co_await 42; // expected-error {{'int' is not a structure or union}} |
| 231 | // expected-note@-1 {{function is a coroutine due to use of 'co_await'}} |
| 232 | return; // expected-error {{not allowed in coroutine}} |
| 233 | } |
| 234 | |
| 235 | template <class T> |
| 236 | void mixed_await_template(T) { |
| 237 | co_await 42; |
| 238 | // expected-note@-1 {{function is a coroutine due to use of 'co_await'}} |
| 239 | return; // expected-error {{not allowed in coroutine}} |
| 240 | } |
| 241 | |
| 242 | template <class T> |
| 243 | void mixed_await_template2(T v) { |
| 244 | co_await v; // expected-error {{'long' is not a structure or union}} |
| 245 | // expected-note@-1 {{function is a coroutine due to use of 'co_await'}} |
| 246 | return; // expected-error {{not allowed in coroutine}} |
| 247 | } |
| 248 | template void mixed_await_template2(long); // expected-note {{requested here}} |
| 249 | |
| Eric Fiselier | 9813131 | 2016-10-06 21:23:38 +0000 | [diff] [blame] | 250 | void only_coreturn(void_tag) { |
| Gor Nishanov | d97f6bf | 2017-01-10 00:08:31 +0000 | [diff] [blame] | 251 | co_return; // OK |
| Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 252 | } |
| 253 | |
| Eric Fiselier | 9813131 | 2016-10-06 21:23:38 +0000 | [diff] [blame] | 254 | void mixed_coreturn(void_tag, bool b) { |
| Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 255 | if (b) |
| Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 256 | co_return; // expected-note {{use of 'co_return'}} |
| Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 257 | else |
| Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 258 | return; // expected-error {{not allowed in coroutine}} |
| Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 259 | } |
| 260 | |
| Eric Fiselier | cac0a59 | 2017-03-11 02:35:37 +0000 | [diff] [blame] | 261 | void mixed_coreturn_invalid(bool b) { |
| 262 | if (b) |
| 263 | co_return; // expected-note {{use of 'co_return'}} |
| 264 | // expected-error@-1 {{no member named 'return_void' in 'promise'}} |
| 265 | else |
| 266 | return; // expected-error {{not allowed in coroutine}} |
| 267 | } |
| 268 | |
| 269 | template <class T> |
| 270 | void mixed_coreturn_template(void_tag, bool b, T v) { |
| 271 | if (b) |
| 272 | co_return v; // expected-note {{use of 'co_return'}} |
| 273 | // expected-error@-1 {{no member named 'return_value' in 'promise_void'}} |
| 274 | else |
| 275 | return; // expected-error {{not allowed in coroutine}} |
| 276 | } |
| 277 | template void mixed_coreturn_template(void_tag, bool, int); // expected-note {{requested here}} |
| 278 | |
| 279 | template <class T> |
| 280 | void mixed_coreturn_template2(bool b, T) { |
| 281 | if (b) |
| 282 | co_return v; // expected-note {{use of 'co_return'}} |
| 283 | // expected-error@-1 {{use of undeclared identifier 'v'}} |
| 284 | else |
| 285 | return; // expected-error {{not allowed in coroutine}} |
| 286 | } |
| 287 | |
| Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 288 | struct CtorDtor { |
| 289 | CtorDtor() { |
| 290 | co_yield 0; // expected-error {{'co_yield' cannot be used in a constructor}} |
| 291 | } |
| Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 292 | CtorDtor(awaitable a) { |
| Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 293 | // The spec doesn't say this is ill-formed, but it must be. |
| Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 294 | co_await a; // expected-error {{'co_await' cannot be used in a constructor}} |
| Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 295 | } |
| 296 | ~CtorDtor() { |
| 297 | co_return 0; // expected-error {{'co_return' cannot be used in a destructor}} |
| 298 | } |
| 299 | // FIXME: The spec says this is ill-formed. |
| 300 | void operator=(CtorDtor&) { |
| Eric Fiselier | c8efda7 | 2016-10-27 18:43:28 +0000 | [diff] [blame] | 301 | co_yield 0; // expected-error {{'co_yield' cannot be used in a copy assignment operator}} |
| 302 | } |
| 303 | void operator=(CtorDtor const &) { |
| 304 | co_yield 0; // expected-error {{'co_yield' cannot be used in a copy assignment operator}} |
| 305 | } |
| 306 | void operator=(CtorDtor &&) { |
| 307 | co_await a; // expected-error {{'co_await' cannot be used in a move assignment operator}} |
| 308 | } |
| 309 | void operator=(CtorDtor const &&) { |
| 310 | co_await a; // expected-error {{'co_await' cannot be used in a move assignment operator}} |
| 311 | } |
| 312 | void operator=(int) { |
| 313 | co_await a; // OK. Not a special member |
| Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 314 | } |
| 315 | }; |
| 316 | |
| Richard Smith | 744b224 | 2015-11-20 02:54:01 +0000 | [diff] [blame] | 317 | void unevaluated() { |
| 318 | decltype(co_await a); // expected-error {{cannot be used in an unevaluated context}} |
| 319 | sizeof(co_await a); // expected-error {{cannot be used in an unevaluated context}} |
| 320 | typeid(co_await a); // expected-error {{cannot be used in an unevaluated context}} |
| 321 | decltype(co_yield a); // expected-error {{cannot be used in an unevaluated context}} |
| 322 | sizeof(co_yield a); // expected-error {{cannot be used in an unevaluated context}} |
| 323 | typeid(co_yield a); // expected-error {{cannot be used in an unevaluated context}} |
| 324 | } |
| 325 | |
| Brian Gesiak | c1b173a | 2018-06-23 18:01:02 +0000 | [diff] [blame] | 326 | // [expr.await]p2: "An await-expression shall not appear in a default argument." |
| 327 | // FIXME: A better diagnostic would explicitly state that default arguments are |
| 328 | // not allowed. A user may not understand that this is "outside a function." |
| 329 | void default_argument(int arg = co_await 0) {} // expected-error {{'co_await' cannot be used outside a function}} |
| 330 | |
| Eric Fiselier | c8efda7 | 2016-10-27 18:43:28 +0000 | [diff] [blame] | 331 | constexpr auto constexpr_deduced_return_coroutine() { |
| Richard Smith | 744b224 | 2015-11-20 02:54:01 +0000 | [diff] [blame] | 332 | co_yield 0; // expected-error {{'co_yield' cannot be used in a constexpr function}} |
| Eric Fiselier | c8efda7 | 2016-10-27 18:43:28 +0000 | [diff] [blame] | 333 | // expected-error@-1 {{'co_yield' cannot be used in a function with a deduced return type}} |
| Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | void varargs_coroutine(const char *, ...) { |
| Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 337 | co_await a; // expected-error {{'co_await' cannot be used in a varargs function}} |
| 338 | } |
| 339 | |
| Eric Fiselier | c8efda7 | 2016-10-27 18:43:28 +0000 | [diff] [blame] | 340 | auto deduced_return_coroutine() { |
| 341 | co_await a; // expected-error {{'co_await' cannot be used in a function with a deduced return type}} |
| 342 | } |
| 343 | |
| Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 344 | struct outer {}; |
| Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 345 | struct await_arg_1 {}; |
| 346 | struct await_arg_2 {}; |
| 347 | |
| 348 | namespace adl_ns { |
| 349 | struct coawait_arg_type {}; |
| 350 | awaitable operator co_await(coawait_arg_type); |
| 351 | } |
| Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 352 | |
| 353 | namespace dependent_operator_co_await_lookup { |
| 354 | template<typename T> void await_template(T t) { |
| 355 | // no unqualified lookup results |
| 356 | co_await t; // expected-error {{no member named 'await_ready' in 'dependent_operator_co_await_lookup::not_awaitable'}} |
| 357 | // expected-error@-1 {{call to function 'operator co_await' that is neither visible in the template definition nor found by argument-dependent lookup}} |
| 358 | }; |
| 359 | template void await_template(awaitable); |
| 360 | |
| 361 | struct indirectly_awaitable { indirectly_awaitable(outer); }; |
| 362 | awaitable operator co_await(indirectly_awaitable); // expected-note {{should be declared prior to}} |
| 363 | template void await_template(indirectly_awaitable); |
| 364 | |
| 365 | struct not_awaitable {}; |
| 366 | template void await_template(not_awaitable); // expected-note {{instantiation}} |
| 367 | |
| 368 | template<typename T> void await_template_2(T t) { |
| 369 | // one unqualified lookup result |
| 370 | co_await t; |
| 371 | }; |
| 372 | template void await_template(outer); // expected-note {{instantiation}} |
| 373 | template void await_template_2(outer); |
| Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 374 | |
| 375 | struct transform_awaitable {}; |
| 376 | struct transformed {}; |
| 377 | |
| 378 | struct transform_promise { |
| 379 | typedef transform_awaitable await_arg; |
| 380 | coro<transform_promise> get_return_object(); |
| 381 | transformed initial_suspend(); |
| 382 | ::adl_ns::coawait_arg_type final_suspend(); |
| 383 | transformed await_transform(transform_awaitable); |
| Eric Fiselier | a9fdb34 | 2017-03-23 00:33:33 +0000 | [diff] [blame] | 384 | void unhandled_exception(); |
| Eric Fiselier | fc50f62 | 2017-05-25 14:59:39 +0000 | [diff] [blame] | 385 | void return_void(); |
| Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 386 | }; |
| 387 | template <class AwaitArg> |
| 388 | struct basic_promise { |
| 389 | typedef AwaitArg await_arg; |
| 390 | coro<basic_promise> get_return_object(); |
| 391 | awaitable initial_suspend(); |
| 392 | awaitable final_suspend(); |
| Eric Fiselier | a9fdb34 | 2017-03-23 00:33:33 +0000 | [diff] [blame] | 393 | void unhandled_exception(); |
| Eric Fiselier | fc50f62 | 2017-05-25 14:59:39 +0000 | [diff] [blame] | 394 | void return_void(); |
| Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 395 | }; |
| 396 | |
| 397 | awaitable operator co_await(await_arg_1); |
| 398 | |
| 399 | template <typename T, typename U> |
| 400 | coro<T> await_template_3(U t) { |
| 401 | co_await t; |
| 402 | } |
| 403 | |
| 404 | template coro<basic_promise<await_arg_1>> await_template_3<basic_promise<await_arg_1>>(await_arg_1); |
| 405 | |
| 406 | template <class T, int I = 0> |
| 407 | struct dependent_member { |
| 408 | coro<T> mem_fn() const { |
| 409 | co_await typename T::await_arg{}; // expected-error {{call to function 'operator co_await'}}} |
| 410 | } |
| 411 | template <class U> |
| 412 | coro<T> dep_mem_fn(U t) { |
| 413 | co_await t; |
| 414 | } |
| 415 | }; |
| 416 | |
| 417 | template <> |
| 418 | struct dependent_member<long> { |
| 419 | // FIXME this diagnostic is terrible |
| 420 | coro<transform_promise> mem_fn() const { // expected-error {{no member named 'await_ready' in 'dependent_operator_co_await_lookup::transformed'}} |
| 421 | // expected-note@-1 {{call to 'initial_suspend' implicitly required by the initial suspend point}} |
| 422 | // expected-note@+1 {{function is a coroutine due to use of 'co_await' here}} |
| 423 | co_await transform_awaitable{}; |
| 424 | // expected-error@-1 {{no member named 'await_ready'}} |
| 425 | } |
| 426 | template <class R, class U> |
| 427 | coro<R> dep_mem_fn(U u) { co_await u; } |
| 428 | }; |
| 429 | |
| 430 | awaitable operator co_await(await_arg_2); // expected-note {{'operator co_await' should be declared prior to the call site}} |
| 431 | |
| 432 | template struct dependent_member<basic_promise<await_arg_1>, 0>; |
| 433 | template struct dependent_member<basic_promise<await_arg_2>, 0>; // expected-note {{in instantiation}} |
| 434 | |
| 435 | template <> |
| 436 | coro<transform_promise> |
| 437 | // FIXME this diagnostic is terrible |
| 438 | dependent_member<long>::dep_mem_fn<transform_promise>(int) { // expected-error {{no member named 'await_ready' in 'dependent_operator_co_await_lookup::transformed'}} |
| 439 | //expected-note@-1 {{call to 'initial_suspend' implicitly required by the initial suspend point}} |
| 440 | //expected-note@+1 {{function is a coroutine due to use of 'co_await' here}} |
| 441 | co_await transform_awaitable{}; |
| 442 | // expected-error@-1 {{no member named 'await_ready'}} |
| 443 | } |
| 444 | |
| 445 | void operator co_await(transform_awaitable) = delete; |
| 446 | awaitable operator co_await(transformed); |
| 447 | |
| 448 | template coro<transform_promise> |
| 449 | dependent_member<long>::dep_mem_fn<transform_promise>(transform_awaitable); |
| 450 | |
| 451 | template <> |
| 452 | coro<transform_promise> dependent_member<long>::dep_mem_fn<transform_promise>(long) { |
| 453 | co_await transform_awaitable{}; |
| 454 | } |
| 455 | |
| 456 | template <> |
| 457 | struct dependent_member<int> { |
| 458 | coro<transform_promise> mem_fn() const { |
| 459 | co_await transform_awaitable{}; |
| 460 | } |
| 461 | }; |
| 462 | |
| 463 | template coro<transform_promise> await_template_3<transform_promise>(transform_awaitable); |
| 464 | template struct dependent_member<transform_promise>; |
| 465 | template coro<transform_promise> dependent_member<transform_promise>::dep_mem_fn(transform_awaitable); |
| Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 466 | } |
| Richard Smith | 10610f7 | 2015-11-20 22:57:24 +0000 | [diff] [blame] | 467 | |
| Richard Smith | 71d403e | 2015-11-22 07:33:28 +0000 | [diff] [blame] | 468 | struct yield_fn_tag {}; |
| Gor Nishanov | 3e048bb | 2016-10-04 00:31:16 +0000 | [diff] [blame] | 469 | template <> |
| 470 | struct std::experimental::coroutine_traits<void, yield_fn_tag> { |
| Richard Smith | 71d403e | 2015-11-22 07:33:28 +0000 | [diff] [blame] | 471 | struct promise_type { |
| 472 | // FIXME: add an await_transform overload for functions |
| 473 | awaitable yield_value(int()); |
| 474 | void return_value(int()); |
| Richard Smith | 2af65c4 | 2015-11-24 02:34:39 +0000 | [diff] [blame] | 475 | |
| 476 | suspend_never initial_suspend(); |
| 477 | suspend_never final_suspend(); |
| 478 | void get_return_object(); |
| Eric Fiselier | a9fdb34 | 2017-03-23 00:33:33 +0000 | [diff] [blame] | 479 | void unhandled_exception(); |
| Richard Smith | 71d403e | 2015-11-22 07:33:28 +0000 | [diff] [blame] | 480 | }; |
| 481 | }; |
| 482 | |
| Richard Smith | 10610f7 | 2015-11-20 22:57:24 +0000 | [diff] [blame] | 483 | namespace placeholder { |
| Richard Smith | 71d403e | 2015-11-22 07:33:28 +0000 | [diff] [blame] | 484 | awaitable f(), f(int); // expected-note 4{{possible target}} |
| 485 | int g(), g(int); // expected-note 2{{candidate}} |
| Richard Smith | 10610f7 | 2015-11-20 22:57:24 +0000 | [diff] [blame] | 486 | void x() { |
| 487 | co_await f; // expected-error {{reference to overloaded function}} |
| 488 | } |
| 489 | void y() { |
| Richard Smith | 71d403e | 2015-11-22 07:33:28 +0000 | [diff] [blame] | 490 | co_yield g; // expected-error {{no matching member function for call to 'yield_value'}} |
| Richard Smith | 10610f7 | 2015-11-20 22:57:24 +0000 | [diff] [blame] | 491 | } |
| 492 | void z() { |
| 493 | co_await a; |
| Richard Smith | 71d403e | 2015-11-22 07:33:28 +0000 | [diff] [blame] | 494 | co_return g; // expected-error {{address of overloaded function 'g' does not match required type 'int'}} |
| 495 | } |
| 496 | |
| 497 | void x(yield_fn_tag) { |
| 498 | co_await f; // expected-error {{reference to overloaded function}} |
| 499 | } |
| 500 | void y(yield_fn_tag) { |
| 501 | co_yield g; |
| 502 | } |
| 503 | void z(yield_fn_tag) { |
| 504 | co_await a; |
| 505 | co_return g; |
| Richard Smith | 10610f7 | 2015-11-20 22:57:24 +0000 | [diff] [blame] | 506 | } |
| 507 | } |
| Richard Smith | 2af65c4 | 2015-11-24 02:34:39 +0000 | [diff] [blame] | 508 | |
| 509 | struct bad_promise_1 { |
| 510 | suspend_always initial_suspend(); |
| 511 | suspend_always final_suspend(); |
| Eric Fiselier | a9fdb34 | 2017-03-23 00:33:33 +0000 | [diff] [blame] | 512 | void unhandled_exception(); |
| Eric Fiselier | fc50f62 | 2017-05-25 14:59:39 +0000 | [diff] [blame] | 513 | void return_void(); |
| Richard Smith | 2af65c4 | 2015-11-24 02:34:39 +0000 | [diff] [blame] | 514 | }; |
| 515 | coro<bad_promise_1> missing_get_return_object() { // expected-error {{no member named 'get_return_object' in 'bad_promise_1'}} |
| 516 | co_await a; |
| 517 | } |
| 518 | |
| 519 | struct bad_promise_2 { |
| 520 | coro<bad_promise_2> get_return_object(); |
| Gor Nishanov | d450726 | 2018-03-27 20:38:19 +0000 | [diff] [blame] | 521 | suspend_always final_suspend(); |
| Eric Fiselier | a9fdb34 | 2017-03-23 00:33:33 +0000 | [diff] [blame] | 522 | void unhandled_exception(); |
| Eric Fiselier | fc50f62 | 2017-05-25 14:59:39 +0000 | [diff] [blame] | 523 | void return_void(); |
| Richard Smith | 2af65c4 | 2015-11-24 02:34:39 +0000 | [diff] [blame] | 524 | }; |
| Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 525 | // FIXME: This shouldn't happen twice |
| Richard Smith | 2af65c4 | 2015-11-24 02:34:39 +0000 | [diff] [blame] | 526 | coro<bad_promise_2> missing_initial_suspend() { // expected-error {{no member named 'initial_suspend' in 'bad_promise_2'}} |
| 527 | co_await a; |
| 528 | } |
| 529 | |
| 530 | struct bad_promise_3 { |
| 531 | coro<bad_promise_3> get_return_object(); |
| Gor Nishanov | d450726 | 2018-03-27 20:38:19 +0000 | [diff] [blame] | 532 | suspend_always initial_suspend(); |
| Eric Fiselier | a9fdb34 | 2017-03-23 00:33:33 +0000 | [diff] [blame] | 533 | void unhandled_exception(); |
| Eric Fiselier | fc50f62 | 2017-05-25 14:59:39 +0000 | [diff] [blame] | 534 | void return_void(); |
| Richard Smith | 2af65c4 | 2015-11-24 02:34:39 +0000 | [diff] [blame] | 535 | }; |
| 536 | coro<bad_promise_3> missing_final_suspend() { // expected-error {{no member named 'final_suspend' in 'bad_promise_3'}} |
| 537 | co_await a; |
| 538 | } |
| 539 | |
| 540 | struct bad_promise_4 { |
| 541 | coro<bad_promise_4> get_return_object(); |
| 542 | not_awaitable initial_suspend(); |
| 543 | suspend_always final_suspend(); |
| Eric Fiselier | fc50f62 | 2017-05-25 14:59:39 +0000 | [diff] [blame] | 544 | void return_void(); |
| Richard Smith | 2af65c4 | 2015-11-24 02:34:39 +0000 | [diff] [blame] | 545 | }; |
| 546 | // FIXME: This diagnostic is terrible. |
| 547 | coro<bad_promise_4> bad_initial_suspend() { // expected-error {{no member named 'await_ready' in 'not_awaitable'}} |
| Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 548 | // expected-note@-1 {{call to 'initial_suspend' implicitly required by the initial suspend point}} |
| 549 | co_await a; // expected-note {{function is a coroutine due to use of 'co_await' here}} |
| Richard Smith | 2af65c4 | 2015-11-24 02:34:39 +0000 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | struct bad_promise_5 { |
| 553 | coro<bad_promise_5> get_return_object(); |
| 554 | suspend_always initial_suspend(); |
| 555 | not_awaitable final_suspend(); |
| Eric Fiselier | fc50f62 | 2017-05-25 14:59:39 +0000 | [diff] [blame] | 556 | void return_void(); |
| Richard Smith | 2af65c4 | 2015-11-24 02:34:39 +0000 | [diff] [blame] | 557 | }; |
| 558 | // FIXME: This diagnostic is terrible. |
| 559 | coro<bad_promise_5> bad_final_suspend() { // expected-error {{no member named 'await_ready' in 'not_awaitable'}} |
| Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 560 | // expected-note@-1 {{call to 'final_suspend' implicitly required by the final suspend point}} |
| 561 | co_await a; // expected-note {{function is a coroutine due to use of 'co_await' here}} |
| Richard Smith | 2af65c4 | 2015-11-24 02:34:39 +0000 | [diff] [blame] | 562 | } |
| Eric Fiselier | 7d5773e | 2016-09-30 22:38:31 +0000 | [diff] [blame] | 563 | |
| Eric Fiselier | 709d1b3 | 2016-10-27 07:30:31 +0000 | [diff] [blame] | 564 | struct bad_promise_6 { |
| 565 | coro<bad_promise_6> get_return_object(); |
| 566 | suspend_always initial_suspend(); |
| 567 | suspend_always final_suspend(); |
| Eric Fiselier | a9fdb34 | 2017-03-23 00:33:33 +0000 | [diff] [blame] | 568 | void unhandled_exception(); |
| Eric Fiselier | fc50f62 | 2017-05-25 14:59:39 +0000 | [diff] [blame] | 569 | void return_void(); // expected-note 2 {{member 'return_void' first declared here}} |
| 570 | void return_value(int) const; // expected-note 2 {{member 'return_value' first declared here}} |
| Eric Fiselier | 709d1b3 | 2016-10-27 07:30:31 +0000 | [diff] [blame] | 571 | void return_value(int); |
| 572 | }; |
| 573 | coro<bad_promise_6> bad_implicit_return() { // expected-error {{'bad_promise_6' declares both 'return_value' and 'return_void'}} |
| 574 | co_await a; |
| 575 | } |
| 576 | |
| Eric Fiselier | bee782b | 2017-04-03 19:21:00 +0000 | [diff] [blame] | 577 | template <class T> |
| 578 | coro<T> bad_implicit_return_dependent(T) { // expected-error {{'bad_promise_6' declares both 'return_value' and 'return_void'}} |
| 579 | co_await a; |
| 580 | } |
| 581 | template coro<bad_promise_6> bad_implicit_return_dependent(bad_promise_6); // expected-note {{in instantiation}} |
| 582 | |
| Gor Nishanov | 29ff638 | 2017-05-24 14:34:19 +0000 | [diff] [blame] | 583 | struct bad_promise_7 { // expected-note 2 {{defined here}} |
| Eric Fiselier | 709d1b3 | 2016-10-27 07:30:31 +0000 | [diff] [blame] | 584 | coro<bad_promise_7> get_return_object(); |
| 585 | suspend_always initial_suspend(); |
| 586 | suspend_always final_suspend(); |
| 587 | void return_void(); |
| Eric Fiselier | 709d1b3 | 2016-10-27 07:30:31 +0000 | [diff] [blame] | 588 | }; |
| Eric Fiselier | a9fdb34 | 2017-03-23 00:33:33 +0000 | [diff] [blame] | 589 | coro<bad_promise_7> no_unhandled_exception() { // expected-error {{'bad_promise_7' is required to declare the member 'unhandled_exception()'}} |
| Eric Fiselier | 709d1b3 | 2016-10-27 07:30:31 +0000 | [diff] [blame] | 590 | co_await a; |
| 591 | } |
| 592 | |
| Eric Fiselier | bee782b | 2017-04-03 19:21:00 +0000 | [diff] [blame] | 593 | template <class T> |
| 594 | coro<T> no_unhandled_exception_dependent(T) { // expected-error {{'bad_promise_7' is required to declare the member 'unhandled_exception()'}} |
| 595 | co_await a; |
| 596 | } |
| 597 | template coro<bad_promise_7> no_unhandled_exception_dependent(bad_promise_7); // expected-note {{in instantiation}} |
| 598 | |
| Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 599 | struct bad_promise_base { |
| 600 | private: |
| 601 | void return_void(); |
| 602 | }; |
| 603 | struct bad_promise_8 : bad_promise_base { |
| Eric Fiselier | 709d1b3 | 2016-10-27 07:30:31 +0000 | [diff] [blame] | 604 | coro<bad_promise_8> get_return_object(); |
| 605 | suspend_always initial_suspend(); |
| 606 | suspend_always final_suspend(); |
| Eric Fiselier | bee782b | 2017-04-03 19:21:00 +0000 | [diff] [blame] | 607 | void unhandled_exception() __attribute__((unavailable)); // expected-note 2 {{made unavailable}} |
| 608 | void unhandled_exception() const; // expected-note 2 {{candidate}} |
| 609 | void unhandled_exception(void *) const; // expected-note 2 {{requires 1 argument, but 0 were provided}} |
| Eric Fiselier | 709d1b3 | 2016-10-27 07:30:31 +0000 | [diff] [blame] | 610 | }; |
| Eric Fiselier | a9fdb34 | 2017-03-23 00:33:33 +0000 | [diff] [blame] | 611 | coro<bad_promise_8> calls_unhandled_exception() { |
| 612 | // expected-error@-1 {{call to unavailable member function 'unhandled_exception'}} |
| Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 613 | // FIXME: also warn about private 'return_void' here. Even though building |
| Eric Fiselier | a9fdb34 | 2017-03-23 00:33:33 +0000 | [diff] [blame] | 614 | // the call to unhandled_exception has already failed. |
| Eric Fiselier | 709d1b3 | 2016-10-27 07:30:31 +0000 | [diff] [blame] | 615 | co_await a; |
| 616 | } |
| Eric Fiselier | 7d5773e | 2016-09-30 22:38:31 +0000 | [diff] [blame] | 617 | |
| Eric Fiselier | bee782b | 2017-04-03 19:21:00 +0000 | [diff] [blame] | 618 | template <class T> |
| 619 | coro<T> calls_unhandled_exception_dependent(T) { |
| 620 | // expected-error@-1 {{call to unavailable member function 'unhandled_exception'}} |
| 621 | co_await a; |
| 622 | } |
| 623 | template coro<bad_promise_8> calls_unhandled_exception_dependent(bad_promise_8); // expected-note {{in instantiation}} |
| 624 | |
| Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 625 | struct bad_promise_9 { |
| 626 | coro<bad_promise_9> get_return_object(); |
| 627 | suspend_always initial_suspend(); |
| 628 | suspend_always final_suspend(); |
| 629 | void await_transform(void *); // expected-note {{candidate}} |
| 630 | awaitable await_transform(int) __attribute__((unavailable)); // expected-note {{explicitly made unavailable}} |
| 631 | void return_void(); |
| Eric Fiselier | a9fdb34 | 2017-03-23 00:33:33 +0000 | [diff] [blame] | 632 | void unhandled_exception(); |
| Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 633 | }; |
| 634 | coro<bad_promise_9> calls_await_transform() { |
| 635 | co_await 42; // expected-error {{call to unavailable member function 'await_transform'}} |
| 636 | // expected-note@-1 {{call to 'await_transform' implicitly required by 'co_await' here}} |
| 637 | } |
| 638 | |
| 639 | struct bad_promise_10 { |
| 640 | coro<bad_promise_10> get_return_object(); |
| 641 | suspend_always initial_suspend(); |
| 642 | suspend_always final_suspend(); |
| 643 | int await_transform; |
| 644 | void return_void(); |
| Eric Fiselier | a9fdb34 | 2017-03-23 00:33:33 +0000 | [diff] [blame] | 645 | void unhandled_exception(); |
| Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 646 | }; |
| 647 | coro<bad_promise_10> bad_coawait() { |
| 648 | // FIXME this diagnostic is terrible |
| 649 | co_await 42; // expected-error {{called object type 'int' is not a function or function pointer}} |
| 650 | // expected-note@-1 {{call to 'await_transform' implicitly required by 'co_await' here}} |
| 651 | } |
| 652 | |
| 653 | struct call_operator { |
| 654 | template <class... Args> |
| 655 | awaitable operator()(Args...) const { return a; } |
| 656 | }; |
| 657 | void ret_void(); |
| 658 | struct good_promise_1 { |
| 659 | coro<good_promise_1> get_return_object(); |
| 660 | suspend_always initial_suspend(); |
| 661 | suspend_always final_suspend(); |
| Eric Fiselier | a9fdb34 | 2017-03-23 00:33:33 +0000 | [diff] [blame] | 662 | void unhandled_exception(); |
| Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 663 | static const call_operator await_transform; |
| 664 | using Fn = void (*)(); |
| 665 | Fn return_void = ret_void; |
| 666 | }; |
| 667 | const call_operator good_promise_1::await_transform; |
| 668 | coro<good_promise_1> ok_static_coawait() { |
| 669 | // FIXME this diagnostic is terrible |
| 670 | co_await 42; |
| 671 | } |
| 672 | |
| Gor Nishanov | 3e048bb | 2016-10-04 00:31:16 +0000 | [diff] [blame] | 673 | template<> struct std::experimental::coroutine_traits<int, int, const char**> |
| Eric Fiselier | 7d5773e | 2016-09-30 22:38:31 +0000 | [diff] [blame] | 674 | { using promise_type = promise; }; |
| 675 | |
| Eric Fiselier | c8efda7 | 2016-10-27 18:43:28 +0000 | [diff] [blame] | 676 | int main(int, const char**) { |
| 677 | co_await a; // expected-error {{'co_await' cannot be used in the 'main' function}} |
| Eric Fiselier | 7d5773e | 2016-09-30 22:38:31 +0000 | [diff] [blame] | 678 | } |
| Gor Nishanov | 6dcb0eb | 2017-03-09 03:09:43 +0000 | [diff] [blame] | 679 | |
| 680 | struct good_promise_2 { |
| 681 | float get_return_object(); |
| 682 | suspend_always initial_suspend(); |
| 683 | suspend_always final_suspend(); |
| 684 | void return_void(); |
| Eric Fiselier | a9fdb34 | 2017-03-23 00:33:33 +0000 | [diff] [blame] | 685 | void unhandled_exception(); |
| Gor Nishanov | 6dcb0eb | 2017-03-09 03:09:43 +0000 | [diff] [blame] | 686 | }; |
| 687 | template<> struct std::experimental::coroutine_handle<good_promise_2> {}; |
| 688 | |
| 689 | template<> struct std::experimental::coroutine_traits<float> |
| 690 | { using promise_type = good_promise_2; }; |
| 691 | |
| 692 | float badly_specialized_coro_handle() { // expected-error {{std::experimental::coroutine_handle missing a member named 'from_address'}} |
| 693 | //expected-note@-1 {{call to 'initial_suspend' implicitly required by the initial suspend point}} |
| 694 | co_return; //expected-note {{function is a coroutine due to use of 'co_return' here}} |
| 695 | } |
| Gor Nishanov | 3aa9eb3 | 2017-03-27 23:36:59 +0000 | [diff] [blame] | 696 | |
| Eric Fiselier | f692e7d | 2017-04-18 03:12:48 +0000 | [diff] [blame] | 697 | namespace std { |
| 698 | struct nothrow_t {}; |
| 699 | constexpr nothrow_t nothrow = {}; |
| 700 | } |
| 701 | |
| 702 | using SizeT = decltype(sizeof(int)); |
| 703 | |
| 704 | void* operator new(SizeT __sz, const std::nothrow_t&) noexcept; |
| 705 | void operator delete(void* __p, const std::nothrow_t&) noexcept; |
| 706 | |
| 707 | |
| 708 | |
| Gor Nishanov | 3aa9eb3 | 2017-03-27 23:36:59 +0000 | [diff] [blame] | 709 | struct promise_on_alloc_failure_tag {}; |
| 710 | |
| 711 | template<> |
| 712 | struct std::experimental::coroutine_traits<int, promise_on_alloc_failure_tag> { |
| 713 | struct promise_type { |
| 714 | int get_return_object() {} |
| 715 | suspend_always initial_suspend() { return {}; } |
| 716 | suspend_always final_suspend() { return {}; } |
| 717 | void return_void() {} |
| 718 | int get_return_object_on_allocation_failure(); // expected-error{{'promise_type': 'get_return_object_on_allocation_failure()' must be a static member function}} |
| 719 | void unhandled_exception(); |
| 720 | }; |
| 721 | }; |
| 722 | |
| 723 | extern "C" int f(promise_on_alloc_failure_tag) { |
| 724 | co_return; //expected-note {{function is a coroutine due to use of 'co_return' here}} |
| 725 | } |
| Eric Fiselier | bee782b | 2017-04-03 19:21:00 +0000 | [diff] [blame] | 726 | |
| 727 | struct bad_promise_11 { |
| 728 | coro<bad_promise_11> get_return_object(); |
| 729 | suspend_always initial_suspend(); |
| 730 | suspend_always final_suspend(); |
| 731 | void unhandled_exception(); |
| 732 | void return_void(); |
| 733 | |
| 734 | private: |
| 735 | static coro<bad_promise_11> get_return_object_on_allocation_failure(); // expected-note 2 {{declared private here}} |
| 736 | }; |
| 737 | coro<bad_promise_11> private_alloc_failure_handler() { |
| 738 | // expected-error@-1 {{'get_return_object_on_allocation_failure' is a private member of 'bad_promise_11'}} |
| 739 | co_return; // FIXME: Add a "declared coroutine here" note. |
| 740 | } |
| 741 | |
| 742 | template <class T> |
| 743 | coro<T> dependent_private_alloc_failure_handler(T) { |
| 744 | // expected-error@-1 {{'get_return_object_on_allocation_failure' is a private member of 'bad_promise_11'}} |
| 745 | co_return; // FIXME: Add a "declared coroutine here" note. |
| 746 | } |
| 747 | template coro<bad_promise_11> dependent_private_alloc_failure_handler(bad_promise_11); |
| 748 | // expected-note@-1 {{requested here}} |
| Eric Fiselier | f692e7d | 2017-04-18 03:12:48 +0000 | [diff] [blame] | 749 | |
| 750 | struct bad_promise_12 { |
| 751 | coro<bad_promise_12> get_return_object(); |
| 752 | suspend_always initial_suspend(); |
| 753 | suspend_always final_suspend(); |
| 754 | void unhandled_exception(); |
| 755 | void return_void(); |
| 756 | static coro<bad_promise_12> get_return_object_on_allocation_failure(); |
| 757 | |
| 758 | static void* operator new(SizeT); |
| 759 | // expected-error@-1 2 {{'operator new' is required to have a non-throwing noexcept specification when the promise type declares 'get_return_object_on_allocation_failure()'}} |
| 760 | }; |
| 761 | coro<bad_promise_12> throwing_in_class_new() { // expected-note {{call to 'operator new' implicitly required by coroutine function here}} |
| 762 | co_return; |
| 763 | } |
| 764 | |
| 765 | template <class T> |
| 766 | coro<T> dependent_throwing_in_class_new(T) { // expected-note {{call to 'operator new' implicitly required by coroutine function here}} |
| 767 | co_return; |
| 768 | } |
| 769 | template coro<bad_promise_12> dependent_throwing_in_class_new(bad_promise_12); // expected-note {{requested here}} |
| 770 | |
| 771 | |
| 772 | struct good_promise_13 { |
| 773 | coro<good_promise_13> get_return_object(); |
| 774 | suspend_always initial_suspend(); |
| 775 | suspend_always final_suspend(); |
| 776 | void unhandled_exception(); |
| 777 | void return_void(); |
| 778 | static coro<good_promise_13> get_return_object_on_allocation_failure(); |
| 779 | }; |
| 780 | coro<good_promise_13> uses_nothrow_new() { |
| 781 | co_return; |
| 782 | } |
| 783 | |
| 784 | template <class T> |
| 785 | coro<T> dependent_uses_nothrow_new(T) { |
| 786 | co_return; |
| 787 | } |
| 788 | template coro<good_promise_13> dependent_uses_nothrow_new(good_promise_13); |
| Gor Nishanov | 6a47068 | 2017-05-22 20:22:23 +0000 | [diff] [blame] | 789 | |
| Brian Gesiak | 9860622 | 2018-02-15 20:37:22 +0000 | [diff] [blame] | 790 | struct good_promise_custom_new_operator { |
| 791 | coro<good_promise_custom_new_operator> get_return_object(); |
| 792 | suspend_always initial_suspend(); |
| 793 | suspend_always final_suspend(); |
| 794 | void return_void(); |
| 795 | void unhandled_exception(); |
| Brian Gesiak | 521b13b | 2018-02-16 14:11:27 +0000 | [diff] [blame] | 796 | void *operator new(SizeT, double, float, int); |
| Brian Gesiak | 9860622 | 2018-02-15 20:37:22 +0000 | [diff] [blame] | 797 | }; |
| 798 | |
| 799 | coro<good_promise_custom_new_operator> |
| 800 | good_coroutine_calls_custom_new_operator(double, float, int) { |
| 801 | co_return; |
| 802 | } |
| 803 | |
| 804 | struct coroutine_nonstatic_member_struct; |
| 805 | |
| 806 | struct good_promise_nonstatic_member_custom_new_operator { |
| 807 | coro<good_promise_nonstatic_member_custom_new_operator> get_return_object(); |
| 808 | suspend_always initial_suspend(); |
| 809 | suspend_always final_suspend(); |
| 810 | void return_void(); |
| 811 | void unhandled_exception(); |
| Brian Gesiak | 521b13b | 2018-02-16 14:11:27 +0000 | [diff] [blame] | 812 | void *operator new(SizeT, coroutine_nonstatic_member_struct &, double); |
| Brian Gesiak | 9860622 | 2018-02-15 20:37:22 +0000 | [diff] [blame] | 813 | }; |
| 814 | |
| Brian Gesiak | 9860622 | 2018-02-15 20:37:22 +0000 | [diff] [blame] | 815 | struct good_promise_noexcept_custom_new_operator { |
| 816 | static coro<good_promise_noexcept_custom_new_operator> get_return_object_on_allocation_failure(); |
| 817 | coro<good_promise_noexcept_custom_new_operator> get_return_object(); |
| 818 | suspend_always initial_suspend(); |
| 819 | suspend_always final_suspend(); |
| 820 | void return_void(); |
| 821 | void unhandled_exception(); |
| Brian Gesiak | 521b13b | 2018-02-16 14:11:27 +0000 | [diff] [blame] | 822 | void *operator new(SizeT, double, float, int) noexcept; |
| Brian Gesiak | 9860622 | 2018-02-15 20:37:22 +0000 | [diff] [blame] | 823 | }; |
| 824 | |
| 825 | coro<good_promise_noexcept_custom_new_operator> |
| 826 | good_coroutine_calls_noexcept_custom_new_operator(double, float, int) { |
| 827 | co_return; |
| 828 | } |
| 829 | |
| Gor Nishanov | 6a47068 | 2017-05-22 20:22:23 +0000 | [diff] [blame] | 830 | struct mismatch_gro_type_tag1 {}; |
| 831 | template<> |
| 832 | struct std::experimental::coroutine_traits<int, mismatch_gro_type_tag1> { |
| 833 | struct promise_type { |
| Eric Fiselier | fc50f62 | 2017-05-25 14:59:39 +0000 | [diff] [blame] | 834 | void get_return_object() {} //expected-note {{member 'get_return_object' declared here}} |
| Gor Nishanov | 6a47068 | 2017-05-22 20:22:23 +0000 | [diff] [blame] | 835 | suspend_always initial_suspend() { return {}; } |
| 836 | suspend_always final_suspend() { return {}; } |
| 837 | void return_void() {} |
| 838 | void unhandled_exception(); |
| 839 | }; |
| 840 | }; |
| 841 | |
| Gor Nishanov | 07ac63f | 2018-05-28 18:08:47 +0000 | [diff] [blame] | 842 | extern "C" int f(mismatch_gro_type_tag1) { |
| Gor Nishanov | 6a47068 | 2017-05-22 20:22:23 +0000 | [diff] [blame] | 843 | // expected-error@-1 {{cannot initialize return object of type 'int' with an rvalue of type 'void'}} |
| 844 | co_return; //expected-note {{function is a coroutine due to use of 'co_return' here}} |
| 845 | } |
| 846 | |
| 847 | struct mismatch_gro_type_tag2 {}; |
| 848 | template<> |
| 849 | struct std::experimental::coroutine_traits<int, mismatch_gro_type_tag2> { |
| 850 | struct promise_type { |
| Eric Fiselier | fc50f62 | 2017-05-25 14:59:39 +0000 | [diff] [blame] | 851 | void *get_return_object() {} //expected-note {{member 'get_return_object' declared here}} |
| Gor Nishanov | 6a47068 | 2017-05-22 20:22:23 +0000 | [diff] [blame] | 852 | suspend_always initial_suspend() { return {}; } |
| 853 | suspend_always final_suspend() { return {}; } |
| 854 | void return_void() {} |
| 855 | void unhandled_exception(); |
| 856 | }; |
| 857 | }; |
| 858 | |
| Gor Nishanov | 07ac63f | 2018-05-28 18:08:47 +0000 | [diff] [blame] | 859 | extern "C" int f(mismatch_gro_type_tag2) { |
| Gor Nishanov | 6a47068 | 2017-05-22 20:22:23 +0000 | [diff] [blame] | 860 | // expected-error@-1 {{cannot initialize return object of type 'int' with an lvalue of type 'void *'}} |
| 861 | co_return; //expected-note {{function is a coroutine due to use of 'co_return' here}} |
| 862 | } |
| 863 | |
| 864 | struct mismatch_gro_type_tag3 {}; |
| 865 | template<> |
| 866 | struct std::experimental::coroutine_traits<int, mismatch_gro_type_tag3> { |
| 867 | struct promise_type { |
| 868 | int get_return_object() {} |
| Eric Fiselier | fc50f62 | 2017-05-25 14:59:39 +0000 | [diff] [blame] | 869 | static void get_return_object_on_allocation_failure() {} //expected-note {{member 'get_return_object_on_allocation_failure' declared here}} |
| Gor Nishanov | 6a47068 | 2017-05-22 20:22:23 +0000 | [diff] [blame] | 870 | suspend_always initial_suspend() { return {}; } |
| 871 | suspend_always final_suspend() { return {}; } |
| 872 | void return_void() {} |
| 873 | void unhandled_exception(); |
| 874 | }; |
| 875 | }; |
| 876 | |
| Gor Nishanov | 07ac63f | 2018-05-28 18:08:47 +0000 | [diff] [blame] | 877 | extern "C" int f(mismatch_gro_type_tag3) { |
| Gor Nishanov | 6a47068 | 2017-05-22 20:22:23 +0000 | [diff] [blame] | 878 | // expected-error@-1 {{cannot initialize return object of type 'int' with an rvalue of type 'void'}} |
| 879 | co_return; //expected-note {{function is a coroutine due to use of 'co_return' here}} |
| 880 | } |
| 881 | |
| 882 | |
| 883 | struct mismatch_gro_type_tag4 {}; |
| 884 | template<> |
| 885 | struct std::experimental::coroutine_traits<int, mismatch_gro_type_tag4> { |
| 886 | struct promise_type { |
| 887 | int get_return_object() {} |
| Eric Fiselier | fc50f62 | 2017-05-25 14:59:39 +0000 | [diff] [blame] | 888 | static char *get_return_object_on_allocation_failure() {} //expected-note {{member 'get_return_object_on_allocation_failure' declared}} |
| Gor Nishanov | 6a47068 | 2017-05-22 20:22:23 +0000 | [diff] [blame] | 889 | suspend_always initial_suspend() { return {}; } |
| 890 | suspend_always final_suspend() { return {}; } |
| 891 | void return_void() {} |
| 892 | void unhandled_exception(); |
| 893 | }; |
| 894 | }; |
| 895 | |
| Gor Nishanov | 07ac63f | 2018-05-28 18:08:47 +0000 | [diff] [blame] | 896 | extern "C" int f(mismatch_gro_type_tag4) { |
| Gor Nishanov | 6a47068 | 2017-05-22 20:22:23 +0000 | [diff] [blame] | 897 | // expected-error@-1 {{cannot initialize return object of type 'int' with an rvalue of type 'char *'}} |
| 898 | co_return; //expected-note {{function is a coroutine due to use of 'co_return' here}} |
| 899 | } |
| 900 | |
| Eric Fiselier | fc50f62 | 2017-05-25 14:59:39 +0000 | [diff] [blame] | 901 | struct bad_promise_no_return_func { // expected-note {{'bad_promise_no_return_func' defined here}} |
| Eric Fiselier | da8f9b5 | 2017-05-25 02:16:53 +0000 | [diff] [blame] | 902 | coro<bad_promise_no_return_func> get_return_object(); |
| 903 | suspend_always initial_suspend(); |
| 904 | suspend_always final_suspend(); |
| 905 | void unhandled_exception(); |
| 906 | }; |
| Eric Fiselier | fc50f62 | 2017-05-25 14:59:39 +0000 | [diff] [blame] | 907 | // FIXME: The PDTS currently specifies this as UB, technically forbidding a |
| 908 | // diagnostic. |
| Eric Fiselier | da8f9b5 | 2017-05-25 02:16:53 +0000 | [diff] [blame] | 909 | coro<bad_promise_no_return_func> no_return_value_or_return_void() { |
| Eric Fiselier | fc50f62 | 2017-05-25 14:59:39 +0000 | [diff] [blame] | 910 | // expected-error@-1 {{'bad_promise_no_return_func' must declare either 'return_value' or 'return_void'}} |
| Eric Fiselier | da8f9b5 | 2017-05-25 02:16:53 +0000 | [diff] [blame] | 911 | co_await a; |
| 912 | } |
| Eric Fiselier | d978e53 | 2017-05-28 18:21:12 +0000 | [diff] [blame] | 913 | |
| 914 | struct bad_await_suspend_return { |
| 915 | bool await_ready(); |
| Eric Fiselier | 84ee7ff | 2017-05-31 23:41:11 +0000 | [diff] [blame] | 916 | // expected-error@+1 {{return type of 'await_suspend' is required to be 'void' or 'bool' (have 'char')}} |
| Eric Fiselier | d978e53 | 2017-05-28 18:21:12 +0000 | [diff] [blame] | 917 | char await_suspend(std::experimental::coroutine_handle<>); |
| 918 | void await_resume(); |
| 919 | }; |
| 920 | struct bad_await_ready_return { |
| Eric Fiselier | 84ee7ff | 2017-05-31 23:41:11 +0000 | [diff] [blame] | 921 | // expected-note@+1 {{return type of 'await_ready' is required to be contextually convertible to 'bool'}} |
| Eric Fiselier | d978e53 | 2017-05-28 18:21:12 +0000 | [diff] [blame] | 922 | void await_ready(); |
| 923 | bool await_suspend(std::experimental::coroutine_handle<>); |
| 924 | void await_resume(); |
| 925 | }; |
| 926 | struct await_ready_explicit_bool { |
| 927 | struct BoolT { |
| 928 | explicit operator bool() const; |
| 929 | }; |
| 930 | BoolT await_ready(); |
| 931 | void await_suspend(std::experimental::coroutine_handle<>); |
| 932 | void await_resume(); |
| 933 | }; |
| Eric Fiselier | 84ee7ff | 2017-05-31 23:41:11 +0000 | [diff] [blame] | 934 | template <class SuspendTy> |
| 935 | struct await_suspend_type_test { |
| 936 | bool await_ready(); |
| 937 | // expected-error@+2 {{return type of 'await_suspend' is required to be 'void' or 'bool' (have 'bool &')}} |
| 938 | // expected-error@+1 {{return type of 'await_suspend' is required to be 'void' or 'bool' (have 'bool &&')}} |
| 939 | SuspendTy await_suspend(std::experimental::coroutine_handle<>); |
| 940 | void await_resume(); |
| 941 | }; |
| Eric Fiselier | d978e53 | 2017-05-28 18:21:12 +0000 | [diff] [blame] | 942 | void test_bad_suspend() { |
| 943 | { |
| 944 | // FIXME: The actual error emitted here is terrible, and no number of notes can save it. |
| 945 | bad_await_ready_return a; |
| 946 | // expected-error@+1 {{value of type 'void' is not contextually convertible to 'bool'}} |
| 947 | co_await a; // expected-note {{call to 'await_ready' implicitly required by coroutine function here}} |
| 948 | } |
| 949 | { |
| 950 | bad_await_suspend_return b; |
| 951 | co_await b; // expected-note {{call to 'await_suspend' implicitly required by coroutine function here}} |
| 952 | } |
| 953 | { |
| 954 | await_ready_explicit_bool c; |
| 955 | co_await c; // OK |
| 956 | } |
| Eric Fiselier | 84ee7ff | 2017-05-31 23:41:11 +0000 | [diff] [blame] | 957 | { |
| 958 | await_suspend_type_test<bool &&> a; |
| 959 | await_suspend_type_test<bool &> b; |
| 960 | await_suspend_type_test<const void> c; |
| 961 | await_suspend_type_test<const volatile bool> d; |
| 962 | co_await a; // expected-note {{call to 'await_suspend' implicitly required by coroutine function here}} |
| 963 | co_await b; // expected-note {{call to 'await_suspend' implicitly required by coroutine function here}} |
| 964 | co_await c; // OK |
| 965 | co_await d; // OK |
| 966 | } |
| Eric Fiselier | d978e53 | 2017-05-28 18:21:12 +0000 | [diff] [blame] | 967 | } |
| Eric Fiselier | de7943b | 2017-06-03 00:22:18 +0000 | [diff] [blame] | 968 | |
| 969 | template <int ID = 0> |
| 970 | struct NoCopy { |
| 971 | NoCopy(NoCopy const&) = delete; // expected-note 2 {{deleted here}} |
| 972 | }; |
| 973 | template <class T, class U> |
| 974 | void test_dependent_param(T t, U) { |
| 975 | // expected-error@-1 {{call to deleted constructor of 'NoCopy<0>'}} |
| 976 | // expected-error@-2 {{call to deleted constructor of 'NoCopy<1>'}} |
| 977 | ((void)t); |
| 978 | co_return 42; |
| 979 | } |
| 980 | template void test_dependent_param(NoCopy<0>, NoCopy<1>); // expected-note {{requested here}} |
| Eric Fiselier | 166c6e6 | 2017-07-10 01:27:22 +0000 | [diff] [blame] | 981 | |
| 982 | namespace CoroHandleMemberFunctionTest { |
| 983 | struct CoroMemberTag {}; |
| 984 | struct BadCoroMemberTag {}; |
| 985 | |
| 986 | template <class T, class U> |
| 987 | constexpr bool IsSameV = false; |
| 988 | template <class T> |
| 989 | constexpr bool IsSameV<T, T> = true; |
| 990 | |
| 991 | template <class T> |
| 992 | struct TypeTest { |
| 993 | template <class U> |
| 994 | static constexpr bool IsSame = IsSameV<T, U>; |
| 995 | |
| 996 | template <class... Args> |
| 997 | static constexpr bool MatchesArgs = IsSameV<T, |
| 998 | std::experimental::coroutine_traits<CoroMemberTag, Args...>>; |
| 999 | }; |
| 1000 | |
| 1001 | template <class T> |
| 1002 | struct AwaitReturnsType { |
| 1003 | bool await_ready() const; |
| 1004 | void await_suspend(...) const; |
| 1005 | T await_resume() const; |
| 1006 | }; |
| 1007 | |
| 1008 | template <class... CoroTraitsArgs> |
| 1009 | struct CoroMemberPromise { |
| 1010 | using TraitsT = std::experimental::coroutine_traits<CoroTraitsArgs...>; |
| 1011 | using TypeTestT = TypeTest<TraitsT>; |
| 1012 | using AwaitTestT = AwaitReturnsType<TypeTestT>; |
| 1013 | |
| 1014 | CoroMemberTag get_return_object(); |
| 1015 | suspend_always initial_suspend(); |
| 1016 | suspend_always final_suspend(); |
| 1017 | |
| 1018 | AwaitTestT yield_value(int); |
| 1019 | |
| 1020 | void return_void(); |
| 1021 | void unhandled_exception(); |
| 1022 | }; |
| 1023 | |
| 1024 | } // namespace CoroHandleMemberFunctionTest |
| 1025 | |
| 1026 | template <class... Args> |
| 1027 | struct ::std::experimental::coroutine_traits<CoroHandleMemberFunctionTest::CoroMemberTag, Args...> { |
| 1028 | using promise_type = CoroHandleMemberFunctionTest::CoroMemberPromise<CoroHandleMemberFunctionTest::CoroMemberTag, Args...>; |
| 1029 | }; |
| 1030 | |
| 1031 | namespace CoroHandleMemberFunctionTest { |
| 1032 | struct TestType { |
| 1033 | |
| 1034 | CoroMemberTag test_qual() { |
| 1035 | auto TC = co_yield 0; |
| 1036 | static_assert(TC.MatchesArgs<TestType &>, ""); |
| 1037 | static_assert(!TC.MatchesArgs<TestType>, ""); |
| 1038 | static_assert(!TC.MatchesArgs<TestType *>, ""); |
| 1039 | } |
| 1040 | |
| 1041 | CoroMemberTag test_sanity(int *) const { |
| 1042 | auto TC = co_yield 0; |
| 1043 | static_assert(TC.MatchesArgs<const TestType &>, ""); // expected-error {{static_assert failed}} |
| 1044 | static_assert(TC.MatchesArgs<const TestType &>, ""); // expected-error {{static_assert failed}} |
| 1045 | static_assert(TC.MatchesArgs<const TestType &, int *>, ""); |
| 1046 | } |
| 1047 | |
| 1048 | CoroMemberTag test_qual(int *, const float &&, volatile void *volatile) const { |
| 1049 | auto TC = co_yield 0; |
| 1050 | static_assert(TC.MatchesArgs<const TestType &, int *, const float &&, volatile void *volatile>, ""); |
| 1051 | } |
| 1052 | |
| 1053 | CoroMemberTag test_qual() const volatile { |
| 1054 | auto TC = co_yield 0; |
| 1055 | static_assert(TC.MatchesArgs<const volatile TestType &>, ""); |
| 1056 | } |
| 1057 | |
| 1058 | CoroMemberTag test_ref_qual() & { |
| 1059 | auto TC = co_yield 0; |
| 1060 | static_assert(TC.MatchesArgs<TestType &>, ""); |
| 1061 | } |
| 1062 | CoroMemberTag test_ref_qual() const & { |
| 1063 | auto TC = co_yield 0; |
| 1064 | static_assert(TC.MatchesArgs<TestType const &>, ""); |
| 1065 | } |
| 1066 | CoroMemberTag test_ref_qual() && { |
| 1067 | auto TC = co_yield 0; |
| 1068 | static_assert(TC.MatchesArgs<TestType &&>, ""); |
| 1069 | } |
| 1070 | CoroMemberTag test_ref_qual(const char *&) const volatile && { |
| 1071 | auto TC = co_yield 0; |
| 1072 | static_assert(TC.MatchesArgs<TestType const volatile &&, const char *&>, ""); |
| 1073 | } |
| 1074 | |
| 1075 | CoroMemberTag test_args(int) { |
| 1076 | auto TC = co_yield 0; |
| 1077 | static_assert(TC.MatchesArgs<TestType &, int>, ""); |
| 1078 | } |
| 1079 | CoroMemberTag test_args(int, long &, void *) const { |
| 1080 | auto TC = co_yield 0; |
| 1081 | static_assert(TC.MatchesArgs<TestType const &, int, long &, void *>, ""); |
| 1082 | } |
| 1083 | |
| 1084 | template <class... Args> |
| 1085 | CoroMemberTag test_member_template(Args...) const && { |
| 1086 | auto TC = co_yield 0; |
| 1087 | static_assert(TC.template MatchesArgs<TestType const &&, Args...>, ""); |
| 1088 | } |
| 1089 | |
| 1090 | static CoroMemberTag test_static() { |
| 1091 | auto TC = co_yield 0; |
| 1092 | static_assert(TC.MatchesArgs<>, ""); |
| 1093 | static_assert(!TC.MatchesArgs<TestType>, ""); |
| 1094 | static_assert(!TC.MatchesArgs<TestType &>, ""); |
| 1095 | static_assert(!TC.MatchesArgs<TestType *>, ""); |
| 1096 | } |
| 1097 | |
| 1098 | static CoroMemberTag test_static(volatile void *const, char &&) { |
| 1099 | auto TC = co_yield 0; |
| 1100 | static_assert(TC.MatchesArgs<volatile void *const, char &&>, ""); |
| 1101 | } |
| 1102 | |
| 1103 | template <class Dummy> |
| 1104 | static CoroMemberTag test_static_template(const char *volatile &, unsigned) { |
| 1105 | auto TC = co_yield 0; |
| 1106 | using TCT = decltype(TC); |
| 1107 | static_assert(TCT::MatchesArgs<const char *volatile &, unsigned>, ""); |
| 1108 | static_assert(!TCT::MatchesArgs<TestType &, const char *volatile &, unsigned>, ""); |
| 1109 | } |
| 1110 | |
| 1111 | BadCoroMemberTag test_diagnostics() { |
| 1112 | // expected-error@-1 {{this function cannot be a coroutine: 'std::experimental::coroutine_traits<CoroHandleMemberFunctionTest::BadCoroMemberTag, CoroHandleMemberFunctionTest::TestType &>' has no member named 'promise_type'}} |
| 1113 | co_return; |
| 1114 | } |
| 1115 | BadCoroMemberTag test_diagnostics(int) const && { |
| 1116 | // expected-error@-1 {{this function cannot be a coroutine: 'std::experimental::coroutine_traits<CoroHandleMemberFunctionTest::BadCoroMemberTag, const CoroHandleMemberFunctionTest::TestType &&, int>' has no member named 'promise_type'}} |
| 1117 | co_return; |
| 1118 | } |
| 1119 | |
| 1120 | static BadCoroMemberTag test_static_diagnostics(long *) { |
| 1121 | // expected-error@-1 {{this function cannot be a coroutine: 'std::experimental::coroutine_traits<CoroHandleMemberFunctionTest::BadCoroMemberTag, long *>' has no member named 'promise_type'}} |
| 1122 | co_return; |
| 1123 | } |
| 1124 | }; |
| 1125 | |
| 1126 | template CoroMemberTag TestType::test_member_template(long, const char *) const &&; |
| 1127 | template CoroMemberTag TestType::test_static_template<void>(const char *volatile &, unsigned); |
| 1128 | |
| 1129 | template <class... Args> |
| 1130 | struct DepTestType { |
| 1131 | |
| 1132 | CoroMemberTag test_sanity(int *) const { |
| 1133 | auto TC = co_yield 0; |
| 1134 | static_assert(TC.template MatchesArgs<const DepTestType &>, ""); // expected-error {{static_assert failed}} |
| 1135 | static_assert(TC.template MatchesArgs<>, ""); // expected-error {{static_assert failed}} |
| 1136 | static_assert(TC.template MatchesArgs<const DepTestType &, int *>, ""); |
| 1137 | } |
| 1138 | |
| 1139 | CoroMemberTag test_qual() { |
| 1140 | auto TC = co_yield 0; |
| 1141 | static_assert(TC.template MatchesArgs<DepTestType &>, ""); |
| 1142 | static_assert(!TC.template MatchesArgs<DepTestType>, ""); |
| 1143 | static_assert(!TC.template MatchesArgs<DepTestType *>, ""); |
| 1144 | } |
| 1145 | |
| 1146 | CoroMemberTag test_qual(int *, const float &&, volatile void *volatile) const { |
| 1147 | auto TC = co_yield 0; |
| 1148 | static_assert(TC.template MatchesArgs<const DepTestType &, int *, const float &&, volatile void *volatile>, ""); |
| 1149 | } |
| 1150 | |
| 1151 | CoroMemberTag test_qual() const volatile { |
| 1152 | auto TC = co_yield 0; |
| 1153 | static_assert(TC.template MatchesArgs<const volatile DepTestType &>, ""); |
| 1154 | } |
| 1155 | |
| 1156 | CoroMemberTag test_ref_qual() & { |
| 1157 | auto TC = co_yield 0; |
| 1158 | static_assert(TC.template MatchesArgs<DepTestType &>, ""); |
| 1159 | } |
| 1160 | CoroMemberTag test_ref_qual() const & { |
| 1161 | auto TC = co_yield 0; |
| 1162 | static_assert(TC.template MatchesArgs<DepTestType const &>, ""); |
| 1163 | } |
| 1164 | CoroMemberTag test_ref_qual() && { |
| 1165 | auto TC = co_yield 0; |
| 1166 | static_assert(TC.template MatchesArgs<DepTestType &&>, ""); |
| 1167 | } |
| 1168 | CoroMemberTag test_ref_qual(const char *&) const volatile && { |
| 1169 | auto TC = co_yield 0; |
| 1170 | static_assert(TC.template MatchesArgs<DepTestType const volatile &&, const char *&>, ""); |
| 1171 | } |
| 1172 | |
| 1173 | CoroMemberTag test_args(int) { |
| 1174 | auto TC = co_yield 0; |
| 1175 | static_assert(TC.template MatchesArgs<DepTestType &, int>, ""); |
| 1176 | } |
| 1177 | CoroMemberTag test_args(int, long &, void *) const { |
| 1178 | auto TC = co_yield 0; |
| 1179 | static_assert(TC.template MatchesArgs<DepTestType const &, int, long &, void *>, ""); |
| 1180 | } |
| 1181 | |
| 1182 | template <class... UArgs> |
| 1183 | CoroMemberTag test_member_template(UArgs...) const && { |
| 1184 | auto TC = co_yield 0; |
| 1185 | static_assert(TC.template MatchesArgs<DepTestType const &&, UArgs...>, ""); |
| 1186 | } |
| 1187 | |
| 1188 | static CoroMemberTag test_static() { |
| 1189 | auto TC = co_yield 0; |
| 1190 | using TCT = decltype(TC); |
| 1191 | static_assert(TCT::MatchesArgs<>, ""); |
| 1192 | static_assert(!TCT::MatchesArgs<DepTestType>, ""); |
| 1193 | static_assert(!TCT::MatchesArgs<DepTestType &>, ""); |
| 1194 | static_assert(!TCT::MatchesArgs<DepTestType *>, ""); |
| 1195 | |
| 1196 | // Ensure diagnostics are actually being generated here |
| 1197 | static_assert(TCT::MatchesArgs<int>, ""); // expected-error {{static_assert failed}} |
| 1198 | } |
| 1199 | |
| 1200 | static CoroMemberTag test_static(volatile void *const, char &&) { |
| 1201 | auto TC = co_yield 0; |
| 1202 | using TCT = decltype(TC); |
| 1203 | static_assert(TCT::MatchesArgs<volatile void *const, char &&>, ""); |
| 1204 | } |
| 1205 | |
| 1206 | template <class Dummy> |
| 1207 | static CoroMemberTag test_static_template(const char *volatile &, unsigned) { |
| 1208 | auto TC = co_yield 0; |
| 1209 | using TCT = decltype(TC); |
| 1210 | static_assert(TCT::MatchesArgs<const char *volatile &, unsigned>, ""); |
| 1211 | static_assert(!TCT::MatchesArgs<DepTestType &, const char *volatile &, unsigned>, ""); |
| 1212 | } |
| 1213 | }; |
| 1214 | |
| 1215 | template struct DepTestType<int>; // expected-note {{requested here}} |
| 1216 | template CoroMemberTag DepTestType<int>::test_member_template(long, const char *) const &&; |
| 1217 | |
| 1218 | template CoroMemberTag DepTestType<int>::test_static_template<void>(const char *volatile &, unsigned); |
| 1219 | |
| Brian Gesiak | 61f4ac9 | 2018-01-24 22:15:42 +0000 | [diff] [blame] | 1220 | struct bad_promise_deleted_constructor { |
| 1221 | // expected-note@+1 {{'bad_promise_deleted_constructor' has been explicitly marked deleted here}} |
| 1222 | bad_promise_deleted_constructor() = delete; |
| 1223 | coro<bad_promise_deleted_constructor> get_return_object(); |
| 1224 | suspend_always initial_suspend(); |
| 1225 | suspend_always final_suspend(); |
| 1226 | void return_void(); |
| 1227 | void unhandled_exception(); |
| 1228 | }; |
| 1229 | |
| 1230 | coro<bad_promise_deleted_constructor> |
| 1231 | bad_coroutine_calls_deleted_promise_constructor() { |
| 1232 | // expected-error@-1 {{call to deleted constructor of 'std::experimental::coroutine_traits<coro<CoroHandleMemberFunctionTest::bad_promise_deleted_constructor>>::promise_type' (aka 'CoroHandleMemberFunctionTest::bad_promise_deleted_constructor')}} |
| 1233 | co_return; |
| 1234 | } |
| 1235 | |
| 1236 | // Test that, when the promise type has a constructor whose signature matches |
| 1237 | // that of the coroutine function, that constructor is used. If no matching |
| 1238 | // constructor exists, the default constructor is used as a fallback. If no |
| 1239 | // matching constructors exist at all, an error is emitted. This is an |
| 1240 | // experimental feature that will be proposed for the Coroutines TS. |
| 1241 | |
| 1242 | struct good_promise_default_constructor { |
| 1243 | good_promise_default_constructor(double, float, int); |
| 1244 | good_promise_default_constructor() = default; |
| 1245 | coro<good_promise_default_constructor> get_return_object(); |
| 1246 | suspend_always initial_suspend(); |
| 1247 | suspend_always final_suspend(); |
| 1248 | void return_void(); |
| 1249 | void unhandled_exception(); |
| 1250 | }; |
| 1251 | |
| 1252 | coro<good_promise_default_constructor> |
| 1253 | good_coroutine_calls_default_constructor() { |
| 1254 | co_return; |
| 1255 | } |
| 1256 | |
| Gor Nishanov | 07ac63f | 2018-05-28 18:08:47 +0000 | [diff] [blame] | 1257 | struct some_class; |
| 1258 | |
| Brian Gesiak | 61f4ac9 | 2018-01-24 22:15:42 +0000 | [diff] [blame] | 1259 | struct good_promise_custom_constructor { |
| Gor Nishanov | 07ac63f | 2018-05-28 18:08:47 +0000 | [diff] [blame] | 1260 | good_promise_custom_constructor(some_class&, float, int); |
| Brian Gesiak | 61f4ac9 | 2018-01-24 22:15:42 +0000 | [diff] [blame] | 1261 | good_promise_custom_constructor(double, float, int); |
| 1262 | good_promise_custom_constructor() = delete; |
| 1263 | coro<good_promise_custom_constructor> get_return_object(); |
| 1264 | suspend_always initial_suspend(); |
| 1265 | suspend_always final_suspend(); |
| 1266 | void return_void(); |
| 1267 | void unhandled_exception(); |
| 1268 | }; |
| 1269 | |
| 1270 | coro<good_promise_custom_constructor> |
| 1271 | good_coroutine_calls_custom_constructor(double, float, int) { |
| 1272 | co_return; |
| 1273 | } |
| 1274 | |
| Gor Nishanov | 07ac63f | 2018-05-28 18:08:47 +0000 | [diff] [blame] | 1275 | struct some_class { |
| 1276 | coro<good_promise_custom_constructor> |
| 1277 | good_coroutine_calls_custom_constructor(float, int) { |
| 1278 | co_return; |
| 1279 | } |
| 1280 | coro<good_promise_custom_constructor> |
| 1281 | static good_coroutine_calls_custom_constructor(double, float, int) { |
| 1282 | co_return; |
| 1283 | } |
| 1284 | }; |
| 1285 | |
| Brian Gesiak | 61f4ac9 | 2018-01-24 22:15:42 +0000 | [diff] [blame] | 1286 | struct bad_promise_no_matching_constructor { |
| 1287 | bad_promise_no_matching_constructor(int, int, int); |
| Gor Nishanov | 07ac63f | 2018-05-28 18:08:47 +0000 | [diff] [blame] | 1288 | // expected-note@+1 2 {{'bad_promise_no_matching_constructor' has been explicitly marked deleted here}} |
| Brian Gesiak | 61f4ac9 | 2018-01-24 22:15:42 +0000 | [diff] [blame] | 1289 | bad_promise_no_matching_constructor() = delete; |
| 1290 | coro<bad_promise_no_matching_constructor> get_return_object(); |
| 1291 | suspend_always initial_suspend(); |
| 1292 | suspend_always final_suspend(); |
| 1293 | void return_void(); |
| 1294 | void unhandled_exception(); |
| 1295 | }; |
| 1296 | |
| 1297 | coro<bad_promise_no_matching_constructor> |
| 1298 | bad_coroutine_calls_with_no_matching_constructor(int, int) { |
| 1299 | // expected-error@-1 {{call to deleted constructor of 'std::experimental::coroutine_traits<coro<CoroHandleMemberFunctionTest::bad_promise_no_matching_constructor>, int, int>::promise_type' (aka 'CoroHandleMemberFunctionTest::bad_promise_no_matching_constructor')}} |
| 1300 | co_return; |
| 1301 | } |
| 1302 | |
| Gor Nishanov | 07ac63f | 2018-05-28 18:08:47 +0000 | [diff] [blame] | 1303 | struct some_class2 { |
| 1304 | coro<bad_promise_no_matching_constructor> |
| 1305 | bad_coroutine_calls_with_no_matching_constructor(int, int, int) { |
| 1306 | // expected-error@-1 {{call to deleted constructor}} |
| 1307 | co_return; |
| 1308 | } |
| 1309 | }; |
| 1310 | |
| Eric Fiselier | 166c6e6 | 2017-07-10 01:27:22 +0000 | [diff] [blame] | 1311 | } // namespace CoroHandleMemberFunctionTest |
| Eric Fiselier | 16269a8 | 2018-03-27 00:58:16 +0000 | [diff] [blame] | 1312 | |
| Eric Fiselier | 16269a8 | 2018-03-27 00:58:16 +0000 | [diff] [blame] | 1313 | class awaitable_no_unused_warn { |
| 1314 | public: |
| 1315 | using handle_type = std::experimental::coroutine_handle<>; |
| 1316 | constexpr bool await_ready() { return false; } |
| 1317 | void await_suspend(handle_type) noexcept {} |
| 1318 | int await_resume() { return 1; } |
| 1319 | }; |
| 1320 | |
| 1321 | |
| 1322 | class awaitable_unused_warn { |
| 1323 | public: |
| 1324 | using handle_type = std::experimental::coroutine_handle<>; |
| 1325 | constexpr bool await_ready() { return false; } |
| 1326 | void await_suspend(handle_type) noexcept {} |
| 1327 | [[nodiscard]] |
| 1328 | int await_resume() { return 1; } |
| 1329 | }; |
| 1330 | |
| Eric Fiselier | 855c092 | 2018-03-27 03:33:06 +0000 | [diff] [blame] | 1331 | template <class Await> |
| 1332 | struct check_warning_promise { |
| 1333 | coro<check_warning_promise> get_return_object(); |
| 1334 | Await initial_suspend(); |
| 1335 | Await final_suspend(); |
| 1336 | Await yield_value(int); |
| 1337 | void return_void(); |
| 1338 | void unhandled_exception(); |
| 1339 | }; |
| 1340 | |
| 1341 | |
| 1342 | coro<check_warning_promise<awaitable_no_unused_warn>> |
| 1343 | test_no_unused_warning() { |
| Eric Fiselier | 16269a8 | 2018-03-27 00:58:16 +0000 | [diff] [blame] | 1344 | co_await awaitable_no_unused_warn(); |
| Eric Fiselier | 855c092 | 2018-03-27 03:33:06 +0000 | [diff] [blame] | 1345 | co_yield 42; |
| 1346 | } |
| 1347 | |
| 1348 | coro<check_warning_promise<awaitable_unused_warn>> |
| 1349 | test_unused_warning() { |
| Eric Fiselier | 16269a8 | 2018-03-27 00:58:16 +0000 | [diff] [blame] | 1350 | co_await awaitable_unused_warn(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}} |
| Eric Fiselier | 855c092 | 2018-03-27 03:33:06 +0000 | [diff] [blame] | 1351 | co_yield 42; // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}} |
| Eric Fiselier | 16269a8 | 2018-03-27 00:58:16 +0000 | [diff] [blame] | 1352 | } |
| Gor Nishanov | d450726 | 2018-03-27 20:38:19 +0000 | [diff] [blame] | 1353 | |
| 1354 | struct missing_await_ready { |
| 1355 | void await_suspend(std::experimental::coroutine_handle<>); |
| 1356 | void await_resume(); |
| 1357 | }; |
| 1358 | struct missing_await_suspend { |
| 1359 | bool await_ready(); |
| 1360 | void await_resume(); |
| 1361 | }; |
| 1362 | struct missing_await_resume { |
| 1363 | bool await_ready(); |
| 1364 | void await_suspend(std::experimental::coroutine_handle<>); |
| 1365 | }; |
| 1366 | |
| 1367 | void test_missing_awaitable_members() { |
| 1368 | co_await missing_await_ready{}; // expected-error {{no member named 'await_ready' in 'missing_await_ready'}} |
| 1369 | co_await missing_await_suspend{}; // expected-error {{no member named 'await_suspend' in 'missing_await_suspend'}} |
| 1370 | co_await missing_await_resume{}; // expected-error {{no member named 'await_resume' in 'missing_await_resume'}} |
| 1371 | } |