| Eric Fiselier | a9fdb34 | 2017-03-23 00:33:33 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -std=c++14 -fcoroutines-ts -verify %s -fcxx-exceptions -fexceptions |
| 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}; |
| 160 | co_yield {1e100}; // expected-error {{cannot be narrowed}} expected-note {{explicit cast}} expected-warning {{changes value}} expected-warning {{braces around scalar}} |
| 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(); |
| 513 | // FIXME: We shouldn't offer a typo-correction here! |
| 514 | suspend_always final_suspend(); // expected-note {{here}} |
| Eric Fiselier | a9fdb34 | 2017-03-23 00:33:33 +0000 | [diff] [blame] | 515 | void unhandled_exception(); |
| Eric Fiselier | fc50f62 | 2017-05-25 14:59:39 +0000 | [diff] [blame] | 516 | void return_void(); |
| Richard Smith | 2af65c4 | 2015-11-24 02:34:39 +0000 | [diff] [blame] | 517 | }; |
| Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 518 | // FIXME: This shouldn't happen twice |
| Richard Smith | 2af65c4 | 2015-11-24 02:34:39 +0000 | [diff] [blame] | 519 | coro<bad_promise_2> missing_initial_suspend() { // expected-error {{no member named 'initial_suspend' in 'bad_promise_2'}} |
| 520 | co_await a; |
| 521 | } |
| 522 | |
| 523 | struct bad_promise_3 { |
| 524 | coro<bad_promise_3> get_return_object(); |
| 525 | // FIXME: We shouldn't offer a typo-correction here! |
| 526 | suspend_always initial_suspend(); // expected-note {{here}} |
| Eric Fiselier | a9fdb34 | 2017-03-23 00:33:33 +0000 | [diff] [blame] | 527 | void unhandled_exception(); |
| Eric Fiselier | fc50f62 | 2017-05-25 14:59:39 +0000 | [diff] [blame] | 528 | void return_void(); |
| Richard Smith | 2af65c4 | 2015-11-24 02:34:39 +0000 | [diff] [blame] | 529 | }; |
| 530 | coro<bad_promise_3> missing_final_suspend() { // expected-error {{no member named 'final_suspend' in 'bad_promise_3'}} |
| 531 | co_await a; |
| 532 | } |
| 533 | |
| 534 | struct bad_promise_4 { |
| 535 | coro<bad_promise_4> get_return_object(); |
| 536 | not_awaitable initial_suspend(); |
| 537 | suspend_always final_suspend(); |
| Eric Fiselier | fc50f62 | 2017-05-25 14:59:39 +0000 | [diff] [blame] | 538 | void return_void(); |
| Richard Smith | 2af65c4 | 2015-11-24 02:34:39 +0000 | [diff] [blame] | 539 | }; |
| 540 | // FIXME: This diagnostic is terrible. |
| 541 | 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] | 542 | // expected-note@-1 {{call to 'initial_suspend' implicitly required by the initial suspend point}} |
| 543 | 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] | 544 | } |
| 545 | |
| 546 | struct bad_promise_5 { |
| 547 | coro<bad_promise_5> get_return_object(); |
| 548 | suspend_always initial_suspend(); |
| 549 | not_awaitable final_suspend(); |
| Eric Fiselier | fc50f62 | 2017-05-25 14:59:39 +0000 | [diff] [blame] | 550 | void return_void(); |
| Richard Smith | 2af65c4 | 2015-11-24 02:34:39 +0000 | [diff] [blame] | 551 | }; |
| 552 | // FIXME: This diagnostic is terrible. |
| 553 | 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] | 554 | // expected-note@-1 {{call to 'final_suspend' implicitly required by the final suspend point}} |
| 555 | 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] | 556 | } |
| Eric Fiselier | 7d5773e | 2016-09-30 22:38:31 +0000 | [diff] [blame] | 557 | |
| Eric Fiselier | 709d1b3 | 2016-10-27 07:30:31 +0000 | [diff] [blame] | 558 | struct bad_promise_6 { |
| 559 | coro<bad_promise_6> get_return_object(); |
| 560 | suspend_always initial_suspend(); |
| 561 | suspend_always final_suspend(); |
| Eric Fiselier | a9fdb34 | 2017-03-23 00:33:33 +0000 | [diff] [blame] | 562 | void unhandled_exception(); |
| Eric Fiselier | fc50f62 | 2017-05-25 14:59:39 +0000 | [diff] [blame] | 563 | void return_void(); // expected-note 2 {{member 'return_void' first declared here}} |
| 564 | 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] | 565 | void return_value(int); |
| 566 | }; |
| 567 | coro<bad_promise_6> bad_implicit_return() { // expected-error {{'bad_promise_6' declares both 'return_value' and 'return_void'}} |
| 568 | co_await a; |
| 569 | } |
| 570 | |
| Eric Fiselier | bee782b | 2017-04-03 19:21:00 +0000 | [diff] [blame] | 571 | template <class T> |
| 572 | coro<T> bad_implicit_return_dependent(T) { // expected-error {{'bad_promise_6' declares both 'return_value' and 'return_void'}} |
| 573 | co_await a; |
| 574 | } |
| 575 | template coro<bad_promise_6> bad_implicit_return_dependent(bad_promise_6); // expected-note {{in instantiation}} |
| 576 | |
| Gor Nishanov | 29ff638 | 2017-05-24 14:34:19 +0000 | [diff] [blame] | 577 | struct bad_promise_7 { // expected-note 2 {{defined here}} |
| Eric Fiselier | 709d1b3 | 2016-10-27 07:30:31 +0000 | [diff] [blame] | 578 | coro<bad_promise_7> get_return_object(); |
| 579 | suspend_always initial_suspend(); |
| 580 | suspend_always final_suspend(); |
| 581 | void return_void(); |
| Eric Fiselier | 709d1b3 | 2016-10-27 07:30:31 +0000 | [diff] [blame] | 582 | }; |
| Eric Fiselier | a9fdb34 | 2017-03-23 00:33:33 +0000 | [diff] [blame] | 583 | 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] | 584 | co_await a; |
| 585 | } |
| 586 | |
| Eric Fiselier | bee782b | 2017-04-03 19:21:00 +0000 | [diff] [blame] | 587 | template <class T> |
| 588 | coro<T> no_unhandled_exception_dependent(T) { // expected-error {{'bad_promise_7' is required to declare the member 'unhandled_exception()'}} |
| 589 | co_await a; |
| 590 | } |
| 591 | template coro<bad_promise_7> no_unhandled_exception_dependent(bad_promise_7); // expected-note {{in instantiation}} |
| 592 | |
| Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 593 | struct bad_promise_base { |
| 594 | private: |
| 595 | void return_void(); |
| 596 | }; |
| 597 | struct bad_promise_8 : bad_promise_base { |
| Eric Fiselier | 709d1b3 | 2016-10-27 07:30:31 +0000 | [diff] [blame] | 598 | coro<bad_promise_8> get_return_object(); |
| 599 | suspend_always initial_suspend(); |
| 600 | suspend_always final_suspend(); |
| Eric Fiselier | bee782b | 2017-04-03 19:21:00 +0000 | [diff] [blame] | 601 | void unhandled_exception() __attribute__((unavailable)); // expected-note 2 {{made unavailable}} |
| 602 | void unhandled_exception() const; // expected-note 2 {{candidate}} |
| 603 | 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] | 604 | }; |
| Eric Fiselier | a9fdb34 | 2017-03-23 00:33:33 +0000 | [diff] [blame] | 605 | coro<bad_promise_8> calls_unhandled_exception() { |
| 606 | // expected-error@-1 {{call to unavailable member function 'unhandled_exception'}} |
| Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 607 | // FIXME: also warn about private 'return_void' here. Even though building |
| Eric Fiselier | a9fdb34 | 2017-03-23 00:33:33 +0000 | [diff] [blame] | 608 | // the call to unhandled_exception has already failed. |
| Eric Fiselier | 709d1b3 | 2016-10-27 07:30:31 +0000 | [diff] [blame] | 609 | co_await a; |
| 610 | } |
| Eric Fiselier | 7d5773e | 2016-09-30 22:38:31 +0000 | [diff] [blame] | 611 | |
| Eric Fiselier | bee782b | 2017-04-03 19:21:00 +0000 | [diff] [blame] | 612 | template <class T> |
| 613 | coro<T> calls_unhandled_exception_dependent(T) { |
| 614 | // expected-error@-1 {{call to unavailable member function 'unhandled_exception'}} |
| 615 | co_await a; |
| 616 | } |
| 617 | template coro<bad_promise_8> calls_unhandled_exception_dependent(bad_promise_8); // expected-note {{in instantiation}} |
| 618 | |
| Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 619 | struct bad_promise_9 { |
| 620 | coro<bad_promise_9> get_return_object(); |
| 621 | suspend_always initial_suspend(); |
| 622 | suspend_always final_suspend(); |
| 623 | void await_transform(void *); // expected-note {{candidate}} |
| 624 | awaitable await_transform(int) __attribute__((unavailable)); // expected-note {{explicitly made unavailable}} |
| 625 | void return_void(); |
| Eric Fiselier | a9fdb34 | 2017-03-23 00:33:33 +0000 | [diff] [blame] | 626 | void unhandled_exception(); |
| Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 627 | }; |
| 628 | coro<bad_promise_9> calls_await_transform() { |
| 629 | co_await 42; // expected-error {{call to unavailable member function 'await_transform'}} |
| 630 | // expected-note@-1 {{call to 'await_transform' implicitly required by 'co_await' here}} |
| 631 | } |
| 632 | |
| 633 | struct bad_promise_10 { |
| 634 | coro<bad_promise_10> get_return_object(); |
| 635 | suspend_always initial_suspend(); |
| 636 | suspend_always final_suspend(); |
| 637 | int await_transform; |
| 638 | void return_void(); |
| Eric Fiselier | a9fdb34 | 2017-03-23 00:33:33 +0000 | [diff] [blame] | 639 | void unhandled_exception(); |
| Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 640 | }; |
| 641 | coro<bad_promise_10> bad_coawait() { |
| 642 | // FIXME this diagnostic is terrible |
| 643 | co_await 42; // expected-error {{called object type 'int' is not a function or function pointer}} |
| 644 | // expected-note@-1 {{call to 'await_transform' implicitly required by 'co_await' here}} |
| 645 | } |
| 646 | |
| 647 | struct call_operator { |
| 648 | template <class... Args> |
| 649 | awaitable operator()(Args...) const { return a; } |
| 650 | }; |
| 651 | void ret_void(); |
| 652 | struct good_promise_1 { |
| 653 | coro<good_promise_1> get_return_object(); |
| 654 | suspend_always initial_suspend(); |
| 655 | suspend_always final_suspend(); |
| Eric Fiselier | a9fdb34 | 2017-03-23 00:33:33 +0000 | [diff] [blame] | 656 | void unhandled_exception(); |
| Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 657 | static const call_operator await_transform; |
| 658 | using Fn = void (*)(); |
| 659 | Fn return_void = ret_void; |
| 660 | }; |
| 661 | const call_operator good_promise_1::await_transform; |
| 662 | coro<good_promise_1> ok_static_coawait() { |
| 663 | // FIXME this diagnostic is terrible |
| 664 | co_await 42; |
| 665 | } |
| 666 | |
| Gor Nishanov | 3e048bb | 2016-10-04 00:31:16 +0000 | [diff] [blame] | 667 | template<> struct std::experimental::coroutine_traits<int, int, const char**> |
| Eric Fiselier | 7d5773e | 2016-09-30 22:38:31 +0000 | [diff] [blame] | 668 | { using promise_type = promise; }; |
| 669 | |
| Eric Fiselier | c8efda7 | 2016-10-27 18:43:28 +0000 | [diff] [blame] | 670 | int main(int, const char**) { |
| 671 | 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] | 672 | } |
| Gor Nishanov | 6dcb0eb | 2017-03-09 03:09:43 +0000 | [diff] [blame] | 673 | |
| 674 | struct good_promise_2 { |
| 675 | float get_return_object(); |
| 676 | suspend_always initial_suspend(); |
| 677 | suspend_always final_suspend(); |
| 678 | void return_void(); |
| Eric Fiselier | a9fdb34 | 2017-03-23 00:33:33 +0000 | [diff] [blame] | 679 | void unhandled_exception(); |
| Gor Nishanov | 6dcb0eb | 2017-03-09 03:09:43 +0000 | [diff] [blame] | 680 | }; |
| 681 | template<> struct std::experimental::coroutine_handle<good_promise_2> {}; |
| 682 | |
| 683 | template<> struct std::experimental::coroutine_traits<float> |
| 684 | { using promise_type = good_promise_2; }; |
| 685 | |
| 686 | float badly_specialized_coro_handle() { // expected-error {{std::experimental::coroutine_handle missing a member named 'from_address'}} |
| 687 | //expected-note@-1 {{call to 'initial_suspend' implicitly required by the initial suspend point}} |
| 688 | co_return; //expected-note {{function is a coroutine due to use of 'co_return' here}} |
| 689 | } |
| Gor Nishanov | 3aa9eb3 | 2017-03-27 23:36:59 +0000 | [diff] [blame] | 690 | |
| Eric Fiselier | f692e7d | 2017-04-18 03:12:48 +0000 | [diff] [blame] | 691 | namespace std { |
| 692 | struct nothrow_t {}; |
| 693 | constexpr nothrow_t nothrow = {}; |
| 694 | } |
| 695 | |
| 696 | using SizeT = decltype(sizeof(int)); |
| 697 | |
| 698 | void* operator new(SizeT __sz, const std::nothrow_t&) noexcept; |
| 699 | void operator delete(void* __p, const std::nothrow_t&) noexcept; |
| 700 | |
| 701 | |
| 702 | |
| Gor Nishanov | 3aa9eb3 | 2017-03-27 23:36:59 +0000 | [diff] [blame] | 703 | struct promise_on_alloc_failure_tag {}; |
| 704 | |
| 705 | template<> |
| 706 | struct std::experimental::coroutine_traits<int, promise_on_alloc_failure_tag> { |
| 707 | struct promise_type { |
| 708 | int get_return_object() {} |
| 709 | suspend_always initial_suspend() { return {}; } |
| 710 | suspend_always final_suspend() { return {}; } |
| 711 | void return_void() {} |
| 712 | int get_return_object_on_allocation_failure(); // expected-error{{'promise_type': 'get_return_object_on_allocation_failure()' must be a static member function}} |
| 713 | void unhandled_exception(); |
| 714 | }; |
| 715 | }; |
| 716 | |
| 717 | extern "C" int f(promise_on_alloc_failure_tag) { |
| 718 | co_return; //expected-note {{function is a coroutine due to use of 'co_return' here}} |
| 719 | } |
| Eric Fiselier | bee782b | 2017-04-03 19:21:00 +0000 | [diff] [blame] | 720 | |
| 721 | struct bad_promise_11 { |
| 722 | coro<bad_promise_11> get_return_object(); |
| 723 | suspend_always initial_suspend(); |
| 724 | suspend_always final_suspend(); |
| 725 | void unhandled_exception(); |
| 726 | void return_void(); |
| 727 | |
| 728 | private: |
| 729 | static coro<bad_promise_11> get_return_object_on_allocation_failure(); // expected-note 2 {{declared private here}} |
| 730 | }; |
| 731 | coro<bad_promise_11> private_alloc_failure_handler() { |
| 732 | // expected-error@-1 {{'get_return_object_on_allocation_failure' is a private member of 'bad_promise_11'}} |
| 733 | co_return; // FIXME: Add a "declared coroutine here" note. |
| 734 | } |
| 735 | |
| 736 | template <class T> |
| 737 | coro<T> dependent_private_alloc_failure_handler(T) { |
| 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 | template coro<bad_promise_11> dependent_private_alloc_failure_handler(bad_promise_11); |
| 742 | // expected-note@-1 {{requested here}} |
| Eric Fiselier | f692e7d | 2017-04-18 03:12:48 +0000 | [diff] [blame] | 743 | |
| 744 | struct bad_promise_12 { |
| 745 | coro<bad_promise_12> get_return_object(); |
| 746 | suspend_always initial_suspend(); |
| 747 | suspend_always final_suspend(); |
| 748 | void unhandled_exception(); |
| 749 | void return_void(); |
| 750 | static coro<bad_promise_12> get_return_object_on_allocation_failure(); |
| 751 | |
| 752 | static void* operator new(SizeT); |
| 753 | // 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()'}} |
| 754 | }; |
| 755 | coro<bad_promise_12> throwing_in_class_new() { // expected-note {{call to 'operator new' implicitly required by coroutine function here}} |
| 756 | co_return; |
| 757 | } |
| 758 | |
| 759 | template <class T> |
| 760 | coro<T> dependent_throwing_in_class_new(T) { // expected-note {{call to 'operator new' implicitly required by coroutine function here}} |
| 761 | co_return; |
| 762 | } |
| 763 | template coro<bad_promise_12> dependent_throwing_in_class_new(bad_promise_12); // expected-note {{requested here}} |
| 764 | |
| 765 | |
| 766 | struct good_promise_13 { |
| 767 | coro<good_promise_13> get_return_object(); |
| 768 | suspend_always initial_suspend(); |
| 769 | suspend_always final_suspend(); |
| 770 | void unhandled_exception(); |
| 771 | void return_void(); |
| 772 | static coro<good_promise_13> get_return_object_on_allocation_failure(); |
| 773 | }; |
| 774 | coro<good_promise_13> uses_nothrow_new() { |
| 775 | co_return; |
| 776 | } |
| 777 | |
| 778 | template <class T> |
| 779 | coro<T> dependent_uses_nothrow_new(T) { |
| 780 | co_return; |
| 781 | } |
| 782 | template coro<good_promise_13> dependent_uses_nothrow_new(good_promise_13); |
| Gor Nishanov | 6a47068 | 2017-05-22 20:22:23 +0000 | [diff] [blame] | 783 | |
| Brian Gesiak | 9860622 | 2018-02-15 20:37:22 +0000 | [diff] [blame^] | 784 | struct good_promise_custom_new_operator { |
| 785 | coro<good_promise_custom_new_operator> get_return_object(); |
| 786 | suspend_always initial_suspend(); |
| 787 | suspend_always final_suspend(); |
| 788 | void return_void(); |
| 789 | void unhandled_exception(); |
| 790 | void *operator new(unsigned long, double, float, int); |
| 791 | }; |
| 792 | |
| 793 | coro<good_promise_custom_new_operator> |
| 794 | good_coroutine_calls_custom_new_operator(double, float, int) { |
| 795 | co_return; |
| 796 | } |
| 797 | |
| 798 | struct coroutine_nonstatic_member_struct; |
| 799 | |
| 800 | struct good_promise_nonstatic_member_custom_new_operator { |
| 801 | coro<good_promise_nonstatic_member_custom_new_operator> get_return_object(); |
| 802 | suspend_always initial_suspend(); |
| 803 | suspend_always final_suspend(); |
| 804 | void return_void(); |
| 805 | void unhandled_exception(); |
| 806 | void *operator new(unsigned long, coroutine_nonstatic_member_struct &, double); |
| 807 | }; |
| 808 | |
| 809 | struct bad_promise_nonstatic_member_mismatched_custom_new_operator { |
| 810 | coro<bad_promise_nonstatic_member_mismatched_custom_new_operator> get_return_object(); |
| 811 | suspend_always initial_suspend(); |
| 812 | suspend_always final_suspend(); |
| 813 | void return_void(); |
| 814 | void unhandled_exception(); |
| 815 | // expected-note@+1 {{candidate function not viable: requires 2 arguments, but 1 was provided}} |
| 816 | void *operator new(unsigned long, double); |
| 817 | }; |
| 818 | |
| 819 | struct coroutine_nonstatic_member_struct { |
| 820 | coro<good_promise_nonstatic_member_custom_new_operator> |
| 821 | good_coroutine_calls_nonstatic_member_custom_new_operator(double) { |
| 822 | co_return; |
| 823 | } |
| 824 | |
| 825 | coro<bad_promise_nonstatic_member_mismatched_custom_new_operator> |
| 826 | bad_coroutine_calls_nonstatic_member_mistmatched_custom_new_operator(double) { |
| 827 | // expected-error@-1 {{no matching function for call to 'operator new'}} |
| 828 | co_return; |
| 829 | } |
| 830 | }; |
| 831 | |
| 832 | struct bad_promise_mismatched_custom_new_operator { |
| 833 | coro<bad_promise_mismatched_custom_new_operator> get_return_object(); |
| 834 | suspend_always initial_suspend(); |
| 835 | suspend_always final_suspend(); |
| 836 | void return_void(); |
| 837 | void unhandled_exception(); |
| 838 | // expected-note@+1 {{candidate function not viable: requires 4 arguments, but 1 was provided}} |
| 839 | void *operator new(unsigned long, double, float, int); |
| 840 | }; |
| 841 | |
| 842 | coro<bad_promise_mismatched_custom_new_operator> |
| 843 | bad_coroutine_calls_mismatched_custom_new_operator(double) { |
| 844 | // expected-error@-1 {{no matching function for call to 'operator new'}} |
| 845 | co_return; |
| 846 | } |
| 847 | |
| 848 | struct bad_promise_throwing_custom_new_operator { |
| 849 | static coro<bad_promise_throwing_custom_new_operator> get_return_object_on_allocation_failure(); |
| 850 | coro<bad_promise_throwing_custom_new_operator> get_return_object(); |
| 851 | suspend_always initial_suspend(); |
| 852 | suspend_always final_suspend(); |
| 853 | void return_void(); |
| 854 | void unhandled_exception(); |
| 855 | // expected-error@+1 {{'operator new' is required to have a non-throwing noexcept specification when the promise type declares 'get_return_object_on_allocation_failure()'}} |
| 856 | void *operator new(unsigned long, double, float, int); |
| 857 | }; |
| 858 | |
| 859 | coro<bad_promise_throwing_custom_new_operator> |
| 860 | bad_coroutine_calls_throwing_custom_new_operator(double, float, int) { |
| 861 | // expected-note@-1 {{call to 'operator new' implicitly required by coroutine function here}} |
| 862 | co_return; |
| 863 | } |
| 864 | |
| 865 | struct good_promise_noexcept_custom_new_operator { |
| 866 | static coro<good_promise_noexcept_custom_new_operator> get_return_object_on_allocation_failure(); |
| 867 | coro<good_promise_noexcept_custom_new_operator> get_return_object(); |
| 868 | suspend_always initial_suspend(); |
| 869 | suspend_always final_suspend(); |
| 870 | void return_void(); |
| 871 | void unhandled_exception(); |
| 872 | void *operator new(unsigned long, double, float, int) noexcept; |
| 873 | }; |
| 874 | |
| 875 | coro<good_promise_noexcept_custom_new_operator> |
| 876 | good_coroutine_calls_noexcept_custom_new_operator(double, float, int) { |
| 877 | co_return; |
| 878 | } |
| 879 | |
| Gor Nishanov | 6a47068 | 2017-05-22 20:22:23 +0000 | [diff] [blame] | 880 | struct mismatch_gro_type_tag1 {}; |
| 881 | template<> |
| 882 | struct std::experimental::coroutine_traits<int, mismatch_gro_type_tag1> { |
| 883 | struct promise_type { |
| Eric Fiselier | fc50f62 | 2017-05-25 14:59:39 +0000 | [diff] [blame] | 884 | void get_return_object() {} //expected-note {{member 'get_return_object' declared here}} |
| Gor Nishanov | 6a47068 | 2017-05-22 20:22:23 +0000 | [diff] [blame] | 885 | suspend_always initial_suspend() { return {}; } |
| 886 | suspend_always final_suspend() { return {}; } |
| 887 | void return_void() {} |
| 888 | void unhandled_exception(); |
| 889 | }; |
| 890 | }; |
| 891 | |
| 892 | extern "C" int f(mismatch_gro_type_tag1) { |
| 893 | // expected-error@-1 {{cannot initialize return object of type 'int' with an rvalue of type 'void'}} |
| 894 | co_return; //expected-note {{function is a coroutine due to use of 'co_return' here}} |
| 895 | } |
| 896 | |
| 897 | struct mismatch_gro_type_tag2 {}; |
| 898 | template<> |
| 899 | struct std::experimental::coroutine_traits<int, mismatch_gro_type_tag2> { |
| 900 | struct promise_type { |
| Eric Fiselier | fc50f62 | 2017-05-25 14:59:39 +0000 | [diff] [blame] | 901 | void *get_return_object() {} //expected-note {{member 'get_return_object' declared here}} |
| Gor Nishanov | 6a47068 | 2017-05-22 20:22:23 +0000 | [diff] [blame] | 902 | suspend_always initial_suspend() { return {}; } |
| 903 | suspend_always final_suspend() { return {}; } |
| 904 | void return_void() {} |
| 905 | void unhandled_exception(); |
| 906 | }; |
| 907 | }; |
| 908 | |
| 909 | extern "C" int f(mismatch_gro_type_tag2) { |
| 910 | // expected-error@-1 {{cannot initialize return object of type 'int' with an lvalue of type 'void *'}} |
| 911 | co_return; //expected-note {{function is a coroutine due to use of 'co_return' here}} |
| 912 | } |
| 913 | |
| 914 | struct mismatch_gro_type_tag3 {}; |
| 915 | template<> |
| 916 | struct std::experimental::coroutine_traits<int, mismatch_gro_type_tag3> { |
| 917 | struct promise_type { |
| 918 | int get_return_object() {} |
| Eric Fiselier | fc50f62 | 2017-05-25 14:59:39 +0000 | [diff] [blame] | 919 | 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] | 920 | suspend_always initial_suspend() { return {}; } |
| 921 | suspend_always final_suspend() { return {}; } |
| 922 | void return_void() {} |
| 923 | void unhandled_exception(); |
| 924 | }; |
| 925 | }; |
| 926 | |
| 927 | extern "C" int f(mismatch_gro_type_tag3) { |
| 928 | // expected-error@-1 {{cannot initialize return object of type 'int' with an rvalue of type 'void'}} |
| 929 | co_return; //expected-note {{function is a coroutine due to use of 'co_return' here}} |
| 930 | } |
| 931 | |
| 932 | |
| 933 | struct mismatch_gro_type_tag4 {}; |
| 934 | template<> |
| 935 | struct std::experimental::coroutine_traits<int, mismatch_gro_type_tag4> { |
| 936 | struct promise_type { |
| 937 | int get_return_object() {} |
| Eric Fiselier | fc50f62 | 2017-05-25 14:59:39 +0000 | [diff] [blame] | 938 | 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] | 939 | suspend_always initial_suspend() { return {}; } |
| 940 | suspend_always final_suspend() { return {}; } |
| 941 | void return_void() {} |
| 942 | void unhandled_exception(); |
| 943 | }; |
| 944 | }; |
| 945 | |
| 946 | extern "C" int f(mismatch_gro_type_tag4) { |
| 947 | // expected-error@-1 {{cannot initialize return object of type 'int' with an rvalue of type 'char *'}} |
| 948 | co_return; //expected-note {{function is a coroutine due to use of 'co_return' here}} |
| 949 | } |
| 950 | |
| Eric Fiselier | fc50f62 | 2017-05-25 14:59:39 +0000 | [diff] [blame] | 951 | 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] | 952 | coro<bad_promise_no_return_func> get_return_object(); |
| 953 | suspend_always initial_suspend(); |
| 954 | suspend_always final_suspend(); |
| 955 | void unhandled_exception(); |
| 956 | }; |
| Eric Fiselier | fc50f62 | 2017-05-25 14:59:39 +0000 | [diff] [blame] | 957 | // FIXME: The PDTS currently specifies this as UB, technically forbidding a |
| 958 | // diagnostic. |
| Eric Fiselier | da8f9b5 | 2017-05-25 02:16:53 +0000 | [diff] [blame] | 959 | coro<bad_promise_no_return_func> no_return_value_or_return_void() { |
| Eric Fiselier | fc50f62 | 2017-05-25 14:59:39 +0000 | [diff] [blame] | 960 | // 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] | 961 | co_await a; |
| 962 | } |
| Eric Fiselier | d978e53 | 2017-05-28 18:21:12 +0000 | [diff] [blame] | 963 | |
| 964 | struct bad_await_suspend_return { |
| 965 | bool await_ready(); |
| Eric Fiselier | 84ee7ff | 2017-05-31 23:41:11 +0000 | [diff] [blame] | 966 | // 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] | 967 | char await_suspend(std::experimental::coroutine_handle<>); |
| 968 | void await_resume(); |
| 969 | }; |
| 970 | struct bad_await_ready_return { |
| Eric Fiselier | 84ee7ff | 2017-05-31 23:41:11 +0000 | [diff] [blame] | 971 | // 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] | 972 | void await_ready(); |
| 973 | bool await_suspend(std::experimental::coroutine_handle<>); |
| 974 | void await_resume(); |
| 975 | }; |
| 976 | struct await_ready_explicit_bool { |
| 977 | struct BoolT { |
| 978 | explicit operator bool() const; |
| 979 | }; |
| 980 | BoolT await_ready(); |
| 981 | void await_suspend(std::experimental::coroutine_handle<>); |
| 982 | void await_resume(); |
| 983 | }; |
| Eric Fiselier | 84ee7ff | 2017-05-31 23:41:11 +0000 | [diff] [blame] | 984 | template <class SuspendTy> |
| 985 | struct await_suspend_type_test { |
| 986 | bool await_ready(); |
| 987 | // expected-error@+2 {{return type of 'await_suspend' is required to be 'void' or 'bool' (have 'bool &')}} |
| 988 | // expected-error@+1 {{return type of 'await_suspend' is required to be 'void' or 'bool' (have 'bool &&')}} |
| 989 | SuspendTy await_suspend(std::experimental::coroutine_handle<>); |
| 990 | void await_resume(); |
| 991 | }; |
| Eric Fiselier | d978e53 | 2017-05-28 18:21:12 +0000 | [diff] [blame] | 992 | void test_bad_suspend() { |
| 993 | { |
| 994 | // FIXME: The actual error emitted here is terrible, and no number of notes can save it. |
| 995 | bad_await_ready_return a; |
| 996 | // expected-error@+1 {{value of type 'void' is not contextually convertible to 'bool'}} |
| 997 | co_await a; // expected-note {{call to 'await_ready' implicitly required by coroutine function here}} |
| 998 | } |
| 999 | { |
| 1000 | bad_await_suspend_return b; |
| 1001 | co_await b; // expected-note {{call to 'await_suspend' implicitly required by coroutine function here}} |
| 1002 | } |
| 1003 | { |
| 1004 | await_ready_explicit_bool c; |
| 1005 | co_await c; // OK |
| 1006 | } |
| Eric Fiselier | 84ee7ff | 2017-05-31 23:41:11 +0000 | [diff] [blame] | 1007 | { |
| 1008 | await_suspend_type_test<bool &&> a; |
| 1009 | await_suspend_type_test<bool &> b; |
| 1010 | await_suspend_type_test<const void> c; |
| 1011 | await_suspend_type_test<const volatile bool> d; |
| 1012 | co_await a; // expected-note {{call to 'await_suspend' implicitly required by coroutine function here}} |
| 1013 | co_await b; // expected-note {{call to 'await_suspend' implicitly required by coroutine function here}} |
| 1014 | co_await c; // OK |
| 1015 | co_await d; // OK |
| 1016 | } |
| Eric Fiselier | d978e53 | 2017-05-28 18:21:12 +0000 | [diff] [blame] | 1017 | } |
| Eric Fiselier | de7943b | 2017-06-03 00:22:18 +0000 | [diff] [blame] | 1018 | |
| 1019 | template <int ID = 0> |
| 1020 | struct NoCopy { |
| 1021 | NoCopy(NoCopy const&) = delete; // expected-note 2 {{deleted here}} |
| 1022 | }; |
| 1023 | template <class T, class U> |
| 1024 | void test_dependent_param(T t, U) { |
| 1025 | // expected-error@-1 {{call to deleted constructor of 'NoCopy<0>'}} |
| 1026 | // expected-error@-2 {{call to deleted constructor of 'NoCopy<1>'}} |
| 1027 | ((void)t); |
| 1028 | co_return 42; |
| 1029 | } |
| 1030 | 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] | 1031 | |
| 1032 | namespace CoroHandleMemberFunctionTest { |
| 1033 | struct CoroMemberTag {}; |
| 1034 | struct BadCoroMemberTag {}; |
| 1035 | |
| 1036 | template <class T, class U> |
| 1037 | constexpr bool IsSameV = false; |
| 1038 | template <class T> |
| 1039 | constexpr bool IsSameV<T, T> = true; |
| 1040 | |
| 1041 | template <class T> |
| 1042 | struct TypeTest { |
| 1043 | template <class U> |
| 1044 | static constexpr bool IsSame = IsSameV<T, U>; |
| 1045 | |
| 1046 | template <class... Args> |
| 1047 | static constexpr bool MatchesArgs = IsSameV<T, |
| 1048 | std::experimental::coroutine_traits<CoroMemberTag, Args...>>; |
| 1049 | }; |
| 1050 | |
| 1051 | template <class T> |
| 1052 | struct AwaitReturnsType { |
| 1053 | bool await_ready() const; |
| 1054 | void await_suspend(...) const; |
| 1055 | T await_resume() const; |
| 1056 | }; |
| 1057 | |
| 1058 | template <class... CoroTraitsArgs> |
| 1059 | struct CoroMemberPromise { |
| 1060 | using TraitsT = std::experimental::coroutine_traits<CoroTraitsArgs...>; |
| 1061 | using TypeTestT = TypeTest<TraitsT>; |
| 1062 | using AwaitTestT = AwaitReturnsType<TypeTestT>; |
| 1063 | |
| 1064 | CoroMemberTag get_return_object(); |
| 1065 | suspend_always initial_suspend(); |
| 1066 | suspend_always final_suspend(); |
| 1067 | |
| 1068 | AwaitTestT yield_value(int); |
| 1069 | |
| 1070 | void return_void(); |
| 1071 | void unhandled_exception(); |
| 1072 | }; |
| 1073 | |
| 1074 | } // namespace CoroHandleMemberFunctionTest |
| 1075 | |
| 1076 | template <class... Args> |
| 1077 | struct ::std::experimental::coroutine_traits<CoroHandleMemberFunctionTest::CoroMemberTag, Args...> { |
| 1078 | using promise_type = CoroHandleMemberFunctionTest::CoroMemberPromise<CoroHandleMemberFunctionTest::CoroMemberTag, Args...>; |
| 1079 | }; |
| 1080 | |
| 1081 | namespace CoroHandleMemberFunctionTest { |
| 1082 | struct TestType { |
| 1083 | |
| 1084 | CoroMemberTag test_qual() { |
| 1085 | auto TC = co_yield 0; |
| 1086 | static_assert(TC.MatchesArgs<TestType &>, ""); |
| 1087 | static_assert(!TC.MatchesArgs<TestType>, ""); |
| 1088 | static_assert(!TC.MatchesArgs<TestType *>, ""); |
| 1089 | } |
| 1090 | |
| 1091 | CoroMemberTag test_sanity(int *) const { |
| 1092 | auto TC = co_yield 0; |
| 1093 | static_assert(TC.MatchesArgs<const TestType &>, ""); // expected-error {{static_assert failed}} |
| 1094 | static_assert(TC.MatchesArgs<const TestType &>, ""); // expected-error {{static_assert failed}} |
| 1095 | static_assert(TC.MatchesArgs<const TestType &, int *>, ""); |
| 1096 | } |
| 1097 | |
| 1098 | CoroMemberTag test_qual(int *, const float &&, volatile void *volatile) const { |
| 1099 | auto TC = co_yield 0; |
| 1100 | static_assert(TC.MatchesArgs<const TestType &, int *, const float &&, volatile void *volatile>, ""); |
| 1101 | } |
| 1102 | |
| 1103 | CoroMemberTag test_qual() const volatile { |
| 1104 | auto TC = co_yield 0; |
| 1105 | static_assert(TC.MatchesArgs<const volatile TestType &>, ""); |
| 1106 | } |
| 1107 | |
| 1108 | CoroMemberTag test_ref_qual() & { |
| 1109 | auto TC = co_yield 0; |
| 1110 | static_assert(TC.MatchesArgs<TestType &>, ""); |
| 1111 | } |
| 1112 | CoroMemberTag test_ref_qual() const & { |
| 1113 | auto TC = co_yield 0; |
| 1114 | static_assert(TC.MatchesArgs<TestType const &>, ""); |
| 1115 | } |
| 1116 | CoroMemberTag test_ref_qual() && { |
| 1117 | auto TC = co_yield 0; |
| 1118 | static_assert(TC.MatchesArgs<TestType &&>, ""); |
| 1119 | } |
| 1120 | CoroMemberTag test_ref_qual(const char *&) const volatile && { |
| 1121 | auto TC = co_yield 0; |
| 1122 | static_assert(TC.MatchesArgs<TestType const volatile &&, const char *&>, ""); |
| 1123 | } |
| 1124 | |
| 1125 | CoroMemberTag test_args(int) { |
| 1126 | auto TC = co_yield 0; |
| 1127 | static_assert(TC.MatchesArgs<TestType &, int>, ""); |
| 1128 | } |
| 1129 | CoroMemberTag test_args(int, long &, void *) const { |
| 1130 | auto TC = co_yield 0; |
| 1131 | static_assert(TC.MatchesArgs<TestType const &, int, long &, void *>, ""); |
| 1132 | } |
| 1133 | |
| 1134 | template <class... Args> |
| 1135 | CoroMemberTag test_member_template(Args...) const && { |
| 1136 | auto TC = co_yield 0; |
| 1137 | static_assert(TC.template MatchesArgs<TestType const &&, Args...>, ""); |
| 1138 | } |
| 1139 | |
| 1140 | static CoroMemberTag test_static() { |
| 1141 | auto TC = co_yield 0; |
| 1142 | static_assert(TC.MatchesArgs<>, ""); |
| 1143 | static_assert(!TC.MatchesArgs<TestType>, ""); |
| 1144 | static_assert(!TC.MatchesArgs<TestType &>, ""); |
| 1145 | static_assert(!TC.MatchesArgs<TestType *>, ""); |
| 1146 | } |
| 1147 | |
| 1148 | static CoroMemberTag test_static(volatile void *const, char &&) { |
| 1149 | auto TC = co_yield 0; |
| 1150 | static_assert(TC.MatchesArgs<volatile void *const, char &&>, ""); |
| 1151 | } |
| 1152 | |
| 1153 | template <class Dummy> |
| 1154 | static CoroMemberTag test_static_template(const char *volatile &, unsigned) { |
| 1155 | auto TC = co_yield 0; |
| 1156 | using TCT = decltype(TC); |
| 1157 | static_assert(TCT::MatchesArgs<const char *volatile &, unsigned>, ""); |
| 1158 | static_assert(!TCT::MatchesArgs<TestType &, const char *volatile &, unsigned>, ""); |
| 1159 | } |
| 1160 | |
| 1161 | BadCoroMemberTag test_diagnostics() { |
| 1162 | // expected-error@-1 {{this function cannot be a coroutine: 'std::experimental::coroutine_traits<CoroHandleMemberFunctionTest::BadCoroMemberTag, CoroHandleMemberFunctionTest::TestType &>' has no member named 'promise_type'}} |
| 1163 | co_return; |
| 1164 | } |
| 1165 | BadCoroMemberTag test_diagnostics(int) const && { |
| 1166 | // 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'}} |
| 1167 | co_return; |
| 1168 | } |
| 1169 | |
| 1170 | static BadCoroMemberTag test_static_diagnostics(long *) { |
| 1171 | // expected-error@-1 {{this function cannot be a coroutine: 'std::experimental::coroutine_traits<CoroHandleMemberFunctionTest::BadCoroMemberTag, long *>' has no member named 'promise_type'}} |
| 1172 | co_return; |
| 1173 | } |
| 1174 | }; |
| 1175 | |
| 1176 | template CoroMemberTag TestType::test_member_template(long, const char *) const &&; |
| 1177 | template CoroMemberTag TestType::test_static_template<void>(const char *volatile &, unsigned); |
| 1178 | |
| 1179 | template <class... Args> |
| 1180 | struct DepTestType { |
| 1181 | |
| 1182 | CoroMemberTag test_sanity(int *) const { |
| 1183 | auto TC = co_yield 0; |
| 1184 | static_assert(TC.template MatchesArgs<const DepTestType &>, ""); // expected-error {{static_assert failed}} |
| 1185 | static_assert(TC.template MatchesArgs<>, ""); // expected-error {{static_assert failed}} |
| 1186 | static_assert(TC.template MatchesArgs<const DepTestType &, int *>, ""); |
| 1187 | } |
| 1188 | |
| 1189 | CoroMemberTag test_qual() { |
| 1190 | auto TC = co_yield 0; |
| 1191 | static_assert(TC.template MatchesArgs<DepTestType &>, ""); |
| 1192 | static_assert(!TC.template MatchesArgs<DepTestType>, ""); |
| 1193 | static_assert(!TC.template MatchesArgs<DepTestType *>, ""); |
| 1194 | } |
| 1195 | |
| 1196 | CoroMemberTag test_qual(int *, const float &&, volatile void *volatile) const { |
| 1197 | auto TC = co_yield 0; |
| 1198 | static_assert(TC.template MatchesArgs<const DepTestType &, int *, const float &&, volatile void *volatile>, ""); |
| 1199 | } |
| 1200 | |
| 1201 | CoroMemberTag test_qual() const volatile { |
| 1202 | auto TC = co_yield 0; |
| 1203 | static_assert(TC.template MatchesArgs<const volatile DepTestType &>, ""); |
| 1204 | } |
| 1205 | |
| 1206 | CoroMemberTag test_ref_qual() & { |
| 1207 | auto TC = co_yield 0; |
| 1208 | static_assert(TC.template MatchesArgs<DepTestType &>, ""); |
| 1209 | } |
| 1210 | CoroMemberTag test_ref_qual() const & { |
| 1211 | auto TC = co_yield 0; |
| 1212 | static_assert(TC.template MatchesArgs<DepTestType const &>, ""); |
| 1213 | } |
| 1214 | CoroMemberTag test_ref_qual() && { |
| 1215 | auto TC = co_yield 0; |
| 1216 | static_assert(TC.template MatchesArgs<DepTestType &&>, ""); |
| 1217 | } |
| 1218 | CoroMemberTag test_ref_qual(const char *&) const volatile && { |
| 1219 | auto TC = co_yield 0; |
| 1220 | static_assert(TC.template MatchesArgs<DepTestType const volatile &&, const char *&>, ""); |
| 1221 | } |
| 1222 | |
| 1223 | CoroMemberTag test_args(int) { |
| 1224 | auto TC = co_yield 0; |
| 1225 | static_assert(TC.template MatchesArgs<DepTestType &, int>, ""); |
| 1226 | } |
| 1227 | CoroMemberTag test_args(int, long &, void *) const { |
| 1228 | auto TC = co_yield 0; |
| 1229 | static_assert(TC.template MatchesArgs<DepTestType const &, int, long &, void *>, ""); |
| 1230 | } |
| 1231 | |
| 1232 | template <class... UArgs> |
| 1233 | CoroMemberTag test_member_template(UArgs...) const && { |
| 1234 | auto TC = co_yield 0; |
| 1235 | static_assert(TC.template MatchesArgs<DepTestType const &&, UArgs...>, ""); |
| 1236 | } |
| 1237 | |
| 1238 | static CoroMemberTag test_static() { |
| 1239 | auto TC = co_yield 0; |
| 1240 | using TCT = decltype(TC); |
| 1241 | static_assert(TCT::MatchesArgs<>, ""); |
| 1242 | static_assert(!TCT::MatchesArgs<DepTestType>, ""); |
| 1243 | static_assert(!TCT::MatchesArgs<DepTestType &>, ""); |
| 1244 | static_assert(!TCT::MatchesArgs<DepTestType *>, ""); |
| 1245 | |
| 1246 | // Ensure diagnostics are actually being generated here |
| 1247 | static_assert(TCT::MatchesArgs<int>, ""); // expected-error {{static_assert failed}} |
| 1248 | } |
| 1249 | |
| 1250 | static CoroMemberTag test_static(volatile void *const, char &&) { |
| 1251 | auto TC = co_yield 0; |
| 1252 | using TCT = decltype(TC); |
| 1253 | static_assert(TCT::MatchesArgs<volatile void *const, char &&>, ""); |
| 1254 | } |
| 1255 | |
| 1256 | template <class Dummy> |
| 1257 | static CoroMemberTag test_static_template(const char *volatile &, unsigned) { |
| 1258 | auto TC = co_yield 0; |
| 1259 | using TCT = decltype(TC); |
| 1260 | static_assert(TCT::MatchesArgs<const char *volatile &, unsigned>, ""); |
| 1261 | static_assert(!TCT::MatchesArgs<DepTestType &, const char *volatile &, unsigned>, ""); |
| 1262 | } |
| 1263 | }; |
| 1264 | |
| 1265 | template struct DepTestType<int>; // expected-note {{requested here}} |
| 1266 | template CoroMemberTag DepTestType<int>::test_member_template(long, const char *) const &&; |
| 1267 | |
| 1268 | template CoroMemberTag DepTestType<int>::test_static_template<void>(const char *volatile &, unsigned); |
| 1269 | |
| Brian Gesiak | 61f4ac9 | 2018-01-24 22:15:42 +0000 | [diff] [blame] | 1270 | struct bad_promise_deleted_constructor { |
| 1271 | // expected-note@+1 {{'bad_promise_deleted_constructor' has been explicitly marked deleted here}} |
| 1272 | bad_promise_deleted_constructor() = delete; |
| 1273 | coro<bad_promise_deleted_constructor> get_return_object(); |
| 1274 | suspend_always initial_suspend(); |
| 1275 | suspend_always final_suspend(); |
| 1276 | void return_void(); |
| 1277 | void unhandled_exception(); |
| 1278 | }; |
| 1279 | |
| 1280 | coro<bad_promise_deleted_constructor> |
| 1281 | bad_coroutine_calls_deleted_promise_constructor() { |
| 1282 | // 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')}} |
| 1283 | co_return; |
| 1284 | } |
| 1285 | |
| 1286 | // Test that, when the promise type has a constructor whose signature matches |
| 1287 | // that of the coroutine function, that constructor is used. If no matching |
| 1288 | // constructor exists, the default constructor is used as a fallback. If no |
| 1289 | // matching constructors exist at all, an error is emitted. This is an |
| 1290 | // experimental feature that will be proposed for the Coroutines TS. |
| 1291 | |
| 1292 | struct good_promise_default_constructor { |
| 1293 | good_promise_default_constructor(double, float, int); |
| 1294 | good_promise_default_constructor() = default; |
| 1295 | coro<good_promise_default_constructor> get_return_object(); |
| 1296 | suspend_always initial_suspend(); |
| 1297 | suspend_always final_suspend(); |
| 1298 | void return_void(); |
| 1299 | void unhandled_exception(); |
| 1300 | }; |
| 1301 | |
| 1302 | coro<good_promise_default_constructor> |
| 1303 | good_coroutine_calls_default_constructor() { |
| 1304 | co_return; |
| 1305 | } |
| 1306 | |
| 1307 | struct good_promise_custom_constructor { |
| 1308 | good_promise_custom_constructor(double, float, int); |
| 1309 | good_promise_custom_constructor() = delete; |
| 1310 | coro<good_promise_custom_constructor> get_return_object(); |
| 1311 | suspend_always initial_suspend(); |
| 1312 | suspend_always final_suspend(); |
| 1313 | void return_void(); |
| 1314 | void unhandled_exception(); |
| 1315 | }; |
| 1316 | |
| 1317 | coro<good_promise_custom_constructor> |
| 1318 | good_coroutine_calls_custom_constructor(double, float, int) { |
| 1319 | co_return; |
| 1320 | } |
| 1321 | |
| 1322 | struct bad_promise_no_matching_constructor { |
| 1323 | bad_promise_no_matching_constructor(int, int, int); |
| 1324 | // expected-note@+1 {{'bad_promise_no_matching_constructor' has been explicitly marked deleted here}} |
| 1325 | bad_promise_no_matching_constructor() = delete; |
| 1326 | coro<bad_promise_no_matching_constructor> get_return_object(); |
| 1327 | suspend_always initial_suspend(); |
| 1328 | suspend_always final_suspend(); |
| 1329 | void return_void(); |
| 1330 | void unhandled_exception(); |
| 1331 | }; |
| 1332 | |
| 1333 | coro<bad_promise_no_matching_constructor> |
| 1334 | bad_coroutine_calls_with_no_matching_constructor(int, int) { |
| 1335 | // 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')}} |
| 1336 | co_return; |
| 1337 | } |
| 1338 | |
| Eric Fiselier | 166c6e6 | 2017-07-10 01:27:22 +0000 | [diff] [blame] | 1339 | } // namespace CoroHandleMemberFunctionTest |