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