blob: 9bc0fa31b3d0db859e9644d8d4c981c0e2bb2bde [file] [log] [blame]
Eric Fiseliera9fdb342017-03-23 00:33:33 +00001// RUN: %clang_cc1 -std=c++14 -fcoroutines-ts -verify %s -fcxx-exceptions -fexceptions
Richard Smithcfd53b42015-10-22 06:13:50 +00002
Eric Fiseliera5465282016-09-29 21:47:39 +00003void no_coroutine_traits_bad_arg_await() {
Gor Nishanov3e048bb2016-10-04 00:31:16 +00004 co_await a; // expected-error {{include <experimental/coroutine>}}
Eric Fiseliera5465282016-09-29 21:47:39 +00005 // expected-error@-1 {{use of undeclared identifier 'a'}}
6}
7
8void no_coroutine_traits_bad_arg_yield() {
Gor Nishanov3e048bb2016-10-04 00:31:16 +00009 co_yield a; // expected-error {{include <experimental/coroutine>}}
Eric Fiseliera5465282016-09-29 21:47:39 +000010 // expected-error@-1 {{use of undeclared identifier 'a'}}
11}
12
13
14void no_coroutine_traits_bad_arg_return() {
Gor Nishanov3e048bb2016-10-04 00:31:16 +000015 co_return a; // expected-error {{include <experimental/coroutine>}}
Eric Fiseliera5465282016-09-29 21:47:39 +000016 // expected-error@-1 {{use of undeclared identifier 'a'}}
17}
18
Richard Smith9f690bd2015-10-27 06:02:45 +000019void no_coroutine_traits() {
Gor Nishanov6dcb0eb2017-03-09 03:09:43 +000020 co_await 4; // expected-error {{std::experimental::coroutine_traits type was not found; include <experimental/coroutine>}}
Richard Smith9f690bd2015-10-27 06:02:45 +000021}
22
23namespace std {
Gor Nishanov3e048bb2016-10-04 00:31:16 +000024namespace experimental {
Eric Fiselier166c6e62017-07-10 01:27:22 +000025
26template <class... Args>
27struct void_t_imp {
28 using type = void;
29};
30template <class... Args>
31using void_t = typename void_t_imp<Args...>::type;
32
33template <class T, class = void>
34struct traits_sfinae_base {};
35
36template <class T>
37struct traits_sfinae_base<T, void_t<typename T::promise_type>> {
38 using promise_type = typename T::promise_type;
39};
40
41template <class Ret, class... Args>
42struct coroutine_traits : public traits_sfinae_base<Ret> {};
Gor Nishanov6dcb0eb2017-03-09 03:09:43 +000043}} // namespace std::experimental
Richard Smith9f690bd2015-10-27 06:02:45 +000044
Richard Smith2af65c42015-11-24 02:34:39 +000045template<typename Promise> struct coro {};
Gor Nishanov3e048bb2016-10-04 00:31:16 +000046template <typename Promise, typename... Ps>
47struct std::experimental::coroutine_traits<coro<Promise>, Ps...> {
Richard Smith2af65c42015-11-24 02:34:39 +000048 using promise_type = Promise;
49};
50
Gor Nishanov6dcb0eb2017-03-09 03:09:43 +000051struct awaitable {
52 bool await_ready();
53 template <typename F> void await_suspend(F);
54 void await_resume();
55} a;
56
57struct suspend_always {
58 bool await_ready() { return false; }
59 template <typename F> void await_suspend(F);
60 void await_resume() {}
61};
62
63struct suspend_never {
64 bool await_ready() { return true; }
65 template <typename F> void await_suspend(F);
66 void await_resume() {}
67};
68
Gor Nishanovdb419a62017-09-05 19:31:52 +000069struct auto_await_suspend {
70 bool await_ready();
71 template <typename F> auto await_suspend(F) {}
72 void await_resume();
73};
74
Eric Fiselier166c6e62017-07-10 01:27:22 +000075struct DummyVoidTag {};
76DummyVoidTag 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 Smith9f690bd2015-10-27 06:02:45 +000078}
79
Gor Nishanov3e048bb2016-10-04 00:31:16 +000080template <typename... T>
81struct std::experimental::coroutine_traits<int, T...> {};
Richard Smith9f690bd2015-10-27 06:02:45 +000082
Eric Fiselier89bf0e72017-03-06 22:52:28 +000083int 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 Smith9b2f53e2015-11-19 02:36:35 +000085}
86
Gor Nishanov3e048bb2016-10-04 00:31:16 +000087template <>
88struct std::experimental::coroutine_traits<double, double> { typedef int promise_type; };
Eric Fiselier89bf0e72017-03-06 22:52:28 +000089double 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 Smith9f690bd2015-10-27 06:02:45 +000091}
92
Gor Nishanov3e048bb2016-10-04 00:31:16 +000093template <>
94struct std::experimental::coroutine_traits<double, int> {
Richard Smith23da82c2015-11-20 22:40:06 +000095 struct promise_type {};
96};
Eric Fiselier20f25cb2017-03-06 23:38:15 +000097double bad_promise_type_2(int) { // expected-error {{no member named 'initial_suspend'}}
Gor Nishanov3e048bb2016-10-04 00:31:16 +000098 co_yield 0; // expected-error {{no member named 'yield_value' in 'std::experimental::coroutine_traits<double, int>::promise_type'}}
Richard Smith23da82c2015-11-20 22:40:06 +000099}
100
Eric Fiselier89bf0e72017-03-06 22:52:28 +0000101struct promise; // expected-note {{forward declaration}}
Eric Fiselier98131312016-10-06 21:23:38 +0000102struct promise_void;
103struct void_tag {};
Gor Nishanov3e048bb2016-10-04 00:31:16 +0000104template <typename... T>
105struct std::experimental::coroutine_traits<void, T...> { using promise_type = promise; };
Eric Fiselier98131312016-10-06 21:23:38 +0000106template <typename... T>
107struct std::experimental::coroutine_traits<void, void_tag, T...>
108{ using promise_type = promise_void; };
Richard Smith9f690bd2015-10-27 06:02:45 +0000109
Eric Fiselier20f25cb2017-03-06 23:38:15 +0000110// FIXME: This diagnostic is terrible.
Eric Fiselier89bf0e72017-03-06 22:52:28 +0000111void undefined_promise() { // expected-error {{this function cannot be a coroutine: 'experimental::coroutine_traits<void>::promise_type' (aka 'promise') is an incomplete type}}
Richard Smith9f690bd2015-10-27 06:02:45 +0000112 co_await a;
113}
114
Richard Smithae3d1472015-11-20 22:47:10 +0000115struct yielded_thing { const char *p; short a, b; };
Richard Smith23da82c2015-11-20 22:40:06 +0000116
Richard Smithd7bed4d2015-11-22 02:57:17 +0000117struct not_awaitable {};
118
Richard Smith23da82c2015-11-20 22:40:06 +0000119struct promise {
Richard Smith2af65c42015-11-24 02:34:39 +0000120 void get_return_object();
121 suspend_always initial_suspend();
122 suspend_always final_suspend();
Richard Smith71d403e2015-11-22 07:33:28 +0000123 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 Smith71d403e2015-11-22 07:33:28 +0000126 void return_value(int); // expected-note 2{{here}}
Eric Fiseliera9fdb342017-03-23 00:33:33 +0000127 void unhandled_exception();
Richard Smith23da82c2015-11-20 22:40:06 +0000128};
129
Eric Fiselier98131312016-10-06 21:23:38 +0000130struct promise_void {
131 void get_return_object();
132 suspend_always initial_suspend();
133 suspend_always final_suspend();
Eric Fiselier98131312016-10-06 21:23:38 +0000134 void return_void();
Eric Fiseliera9fdb342017-03-23 00:33:33 +0000135 void unhandled_exception();
Eric Fiselier98131312016-10-06 21:23:38 +0000136};
137
Gor Nishanov6dcb0eb2017-03-09 03:09:43 +0000138void 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
143namespace std {
144namespace experimental {
145template <class PromiseType = void>
146struct coroutine_handle {
147 static coroutine_handle from_address(void *);
148};
149template <>
150struct 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 Smith23da82c2015-11-20 22:40:06 +0000157void yield() {
158 co_yield 0;
Richard Smithae3d1472015-11-20 22:47:10 +0000159 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 Smith23da82c2015-11-20 22:40:06 +0000163 co_yield "foo"; // expected-error {{no matching}}
Richard Smithd7bed4d2015-11-22 02:57:17 +0000164 co_yield 1.0;
165 co_yield yield; // expected-error {{no member named 'await_ready' in 'not_awaitable'}}
Richard Smith23da82c2015-11-20 22:40:06 +0000166}
Richard Smith9f690bd2015-10-27 06:02:45 +0000167
Gor Nishanovdb419a62017-09-05 19:31:52 +0000168void check_auto_await_suspend() {
169 co_await auto_await_suspend{}; // Should compile successfully.
170}
171
Richard Smith4ba66602015-11-22 07:05:16 +0000172void coreturn(int n) {
173 co_await a;
174 if (n == 0)
175 co_return 3;
176 if (n == 1)
Eric Fiselier98131312016-10-06 21:23:38 +0000177 co_return {4}; // expected-warning {{braces around scalar initializer}}
Richard Smith4ba66602015-11-22 07:05:16 +0000178 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 Fiselier98131312016-10-06 21:23:38 +0000180 co_return 42;
Richard Smith4ba66602015-11-22 07:05:16 +0000181}
182
Eric Fiselier8d409e82017-03-09 05:01:31 +0000183template <class T>
184void co_await_non_dependent_arg(T) {
185 co_await a;
186}
187template void co_await_non_dependent_arg(int);
188
Richard Smithcfd53b42015-10-22 06:13:50 +0000189void mixed_yield() {
Richard Smith9f690bd2015-10-27 06:02:45 +0000190 co_yield 0; // expected-note {{use of 'co_yield'}}
191 return; // expected-error {{not allowed in coroutine}}
Richard Smithcfd53b42015-10-22 06:13:50 +0000192}
193
Eric Fiseliercac0a592017-03-11 02:35:37 +0000194void 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
200template <class T>
201void 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
207template <class T>
208void 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
214template <class T>
215void 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 Smithcfd53b42015-10-22 06:13:50 +0000221void mixed_await() {
Richard Smith9f690bd2015-10-27 06:02:45 +0000222 co_await a; // expected-note {{use of 'co_await'}}
223 return; // expected-error {{not allowed in coroutine}}
Richard Smithcfd53b42015-10-22 06:13:50 +0000224}
225
Eric Fiseliercac0a592017-03-11 02:35:37 +0000226void 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
232template <class T>
233void 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
239template <class T>
240void 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}
245template void mixed_await_template2(long); // expected-note {{requested here}}
246
Eric Fiselier98131312016-10-06 21:23:38 +0000247void only_coreturn(void_tag) {
Gor Nishanovd97f6bf2017-01-10 00:08:31 +0000248 co_return; // OK
Richard Smithcfd53b42015-10-22 06:13:50 +0000249}
250
Eric Fiselier98131312016-10-06 21:23:38 +0000251void mixed_coreturn(void_tag, bool b) {
Richard Smithcfd53b42015-10-22 06:13:50 +0000252 if (b)
Richard Smith9f690bd2015-10-27 06:02:45 +0000253 co_return; // expected-note {{use of 'co_return'}}
Richard Smithcfd53b42015-10-22 06:13:50 +0000254 else
Richard Smith9f690bd2015-10-27 06:02:45 +0000255 return; // expected-error {{not allowed in coroutine}}
Richard Smithcfd53b42015-10-22 06:13:50 +0000256}
257
Eric Fiseliercac0a592017-03-11 02:35:37 +0000258void 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
266template <class T>
267void 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}
274template void mixed_coreturn_template(void_tag, bool, int); // expected-note {{requested here}}
275
276template <class T>
277void 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 Smithcfd53b42015-10-22 06:13:50 +0000285struct CtorDtor {
286 CtorDtor() {
287 co_yield 0; // expected-error {{'co_yield' cannot be used in a constructor}}
288 }
Richard Smith9f690bd2015-10-27 06:02:45 +0000289 CtorDtor(awaitable a) {
Richard Smithcfd53b42015-10-22 06:13:50 +0000290 // The spec doesn't say this is ill-formed, but it must be.
Richard Smith9f690bd2015-10-27 06:02:45 +0000291 co_await a; // expected-error {{'co_await' cannot be used in a constructor}}
Richard Smithcfd53b42015-10-22 06:13:50 +0000292 }
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 Fiselierc8efda72016-10-27 18:43:28 +0000298 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 Smithcfd53b42015-10-22 06:13:50 +0000311 }
312};
313
Richard Smith744b2242015-11-20 02:54:01 +0000314void 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 Fiselierc8efda72016-10-27 18:43:28 +0000323constexpr auto constexpr_deduced_return_coroutine() {
Richard Smith744b2242015-11-20 02:54:01 +0000324 co_yield 0; // expected-error {{'co_yield' cannot be used in a constexpr function}}
Eric Fiselierc8efda72016-10-27 18:43:28 +0000325 // expected-error@-1 {{'co_yield' cannot be used in a function with a deduced return type}}
Richard Smithcfd53b42015-10-22 06:13:50 +0000326}
327
328void varargs_coroutine(const char *, ...) {
Richard Smith9f690bd2015-10-27 06:02:45 +0000329 co_await a; // expected-error {{'co_await' cannot be used in a varargs function}}
330}
331
Eric Fiselierc8efda72016-10-27 18:43:28 +0000332auto 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 Smith9f690bd2015-10-27 06:02:45 +0000336struct outer {};
Eric Fiselier20f25cb2017-03-06 23:38:15 +0000337struct await_arg_1 {};
338struct await_arg_2 {};
339
340namespace adl_ns {
341struct coawait_arg_type {};
342awaitable operator co_await(coawait_arg_type);
343}
Richard Smith9f690bd2015-10-27 06:02:45 +0000344
345namespace 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 Fiselier20f25cb2017-03-06 23:38:15 +0000366
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 Fiseliera9fdb342017-03-23 00:33:33 +0000376 void unhandled_exception();
Eric Fiselierfc50f622017-05-25 14:59:39 +0000377 void return_void();
Eric Fiselier20f25cb2017-03-06 23:38:15 +0000378 };
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 Fiseliera9fdb342017-03-23 00:33:33 +0000385 void unhandled_exception();
Eric Fiselierfc50f622017-05-25 14:59:39 +0000386 void return_void();
Eric Fiselier20f25cb2017-03-06 23:38:15 +0000387 };
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 Smithcfd53b42015-10-22 06:13:50 +0000458}
Richard Smith10610f72015-11-20 22:57:24 +0000459
Richard Smith71d403e2015-11-22 07:33:28 +0000460struct yield_fn_tag {};
Gor Nishanov3e048bb2016-10-04 00:31:16 +0000461template <>
462struct std::experimental::coroutine_traits<void, yield_fn_tag> {
Richard Smith71d403e2015-11-22 07:33:28 +0000463 struct promise_type {
464 // FIXME: add an await_transform overload for functions
465 awaitable yield_value(int());
466 void return_value(int());
Richard Smith2af65c42015-11-24 02:34:39 +0000467
468 suspend_never initial_suspend();
469 suspend_never final_suspend();
470 void get_return_object();
Eric Fiseliera9fdb342017-03-23 00:33:33 +0000471 void unhandled_exception();
Richard Smith71d403e2015-11-22 07:33:28 +0000472 };
473};
474
Richard Smith10610f72015-11-20 22:57:24 +0000475namespace placeholder {
Richard Smith71d403e2015-11-22 07:33:28 +0000476 awaitable f(), f(int); // expected-note 4{{possible target}}
477 int g(), g(int); // expected-note 2{{candidate}}
Richard Smith10610f72015-11-20 22:57:24 +0000478 void x() {
479 co_await f; // expected-error {{reference to overloaded function}}
480 }
481 void y() {
Richard Smith71d403e2015-11-22 07:33:28 +0000482 co_yield g; // expected-error {{no matching member function for call to 'yield_value'}}
Richard Smith10610f72015-11-20 22:57:24 +0000483 }
484 void z() {
485 co_await a;
Richard Smith71d403e2015-11-22 07:33:28 +0000486 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 Smith10610f72015-11-20 22:57:24 +0000498 }
499}
Richard Smith2af65c42015-11-24 02:34:39 +0000500
501struct bad_promise_1 {
502 suspend_always initial_suspend();
503 suspend_always final_suspend();
Eric Fiseliera9fdb342017-03-23 00:33:33 +0000504 void unhandled_exception();
Eric Fiselierfc50f622017-05-25 14:59:39 +0000505 void return_void();
Richard Smith2af65c42015-11-24 02:34:39 +0000506};
507coro<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
511struct 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 Fiseliera9fdb342017-03-23 00:33:33 +0000515 void unhandled_exception();
Eric Fiselierfc50f622017-05-25 14:59:39 +0000516 void return_void();
Richard Smith2af65c42015-11-24 02:34:39 +0000517};
Eric Fiselier20f25cb2017-03-06 23:38:15 +0000518// FIXME: This shouldn't happen twice
Richard Smith2af65c42015-11-24 02:34:39 +0000519coro<bad_promise_2> missing_initial_suspend() { // expected-error {{no member named 'initial_suspend' in 'bad_promise_2'}}
520 co_await a;
521}
522
523struct 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 Fiseliera9fdb342017-03-23 00:33:33 +0000527 void unhandled_exception();
Eric Fiselierfc50f622017-05-25 14:59:39 +0000528 void return_void();
Richard Smith2af65c42015-11-24 02:34:39 +0000529};
530coro<bad_promise_3> missing_final_suspend() { // expected-error {{no member named 'final_suspend' in 'bad_promise_3'}}
531 co_await a;
532}
533
534struct bad_promise_4 {
535 coro<bad_promise_4> get_return_object();
536 not_awaitable initial_suspend();
537 suspend_always final_suspend();
Eric Fiselierfc50f622017-05-25 14:59:39 +0000538 void return_void();
Richard Smith2af65c42015-11-24 02:34:39 +0000539};
540// FIXME: This diagnostic is terrible.
541coro<bad_promise_4> bad_initial_suspend() { // expected-error {{no member named 'await_ready' in 'not_awaitable'}}
Eric Fiselier20f25cb2017-03-06 23:38:15 +0000542 // 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 Smith2af65c42015-11-24 02:34:39 +0000544}
545
546struct bad_promise_5 {
547 coro<bad_promise_5> get_return_object();
548 suspend_always initial_suspend();
549 not_awaitable final_suspend();
Eric Fiselierfc50f622017-05-25 14:59:39 +0000550 void return_void();
Richard Smith2af65c42015-11-24 02:34:39 +0000551};
552// FIXME: This diagnostic is terrible.
553coro<bad_promise_5> bad_final_suspend() { // expected-error {{no member named 'await_ready' in 'not_awaitable'}}
Eric Fiselier20f25cb2017-03-06 23:38:15 +0000554 // 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 Smith2af65c42015-11-24 02:34:39 +0000556}
Eric Fiselier7d5773e2016-09-30 22:38:31 +0000557
Eric Fiselier709d1b32016-10-27 07:30:31 +0000558struct bad_promise_6 {
559 coro<bad_promise_6> get_return_object();
560 suspend_always initial_suspend();
561 suspend_always final_suspend();
Eric Fiseliera9fdb342017-03-23 00:33:33 +0000562 void unhandled_exception();
Eric Fiselierfc50f622017-05-25 14:59:39 +0000563 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 Fiselier709d1b32016-10-27 07:30:31 +0000565 void return_value(int);
566};
567coro<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 Fiselierbee782b2017-04-03 19:21:00 +0000571template <class T>
572coro<T> bad_implicit_return_dependent(T) { // expected-error {{'bad_promise_6' declares both 'return_value' and 'return_void'}}
573 co_await a;
574}
575template coro<bad_promise_6> bad_implicit_return_dependent(bad_promise_6); // expected-note {{in instantiation}}
576
Gor Nishanov29ff6382017-05-24 14:34:19 +0000577struct bad_promise_7 { // expected-note 2 {{defined here}}
Eric Fiselier709d1b32016-10-27 07:30:31 +0000578 coro<bad_promise_7> get_return_object();
579 suspend_always initial_suspend();
580 suspend_always final_suspend();
581 void return_void();
Eric Fiselier709d1b32016-10-27 07:30:31 +0000582};
Eric Fiseliera9fdb342017-03-23 00:33:33 +0000583coro<bad_promise_7> no_unhandled_exception() { // expected-error {{'bad_promise_7' is required to declare the member 'unhandled_exception()'}}
Eric Fiselier709d1b32016-10-27 07:30:31 +0000584 co_await a;
585}
586
Eric Fiselierbee782b2017-04-03 19:21:00 +0000587template <class T>
588coro<T> no_unhandled_exception_dependent(T) { // expected-error {{'bad_promise_7' is required to declare the member 'unhandled_exception()'}}
589 co_await a;
590}
591template coro<bad_promise_7> no_unhandled_exception_dependent(bad_promise_7); // expected-note {{in instantiation}}
592
Eric Fiselier20f25cb2017-03-06 23:38:15 +0000593struct bad_promise_base {
594private:
595 void return_void();
596};
597struct bad_promise_8 : bad_promise_base {
Eric Fiselier709d1b32016-10-27 07:30:31 +0000598 coro<bad_promise_8> get_return_object();
599 suspend_always initial_suspend();
600 suspend_always final_suspend();
Eric Fiselierbee782b2017-04-03 19:21:00 +0000601 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 Fiselier709d1b32016-10-27 07:30:31 +0000604};
Eric Fiseliera9fdb342017-03-23 00:33:33 +0000605coro<bad_promise_8> calls_unhandled_exception() {
606 // expected-error@-1 {{call to unavailable member function 'unhandled_exception'}}
Eric Fiselier20f25cb2017-03-06 23:38:15 +0000607 // FIXME: also warn about private 'return_void' here. Even though building
Eric Fiseliera9fdb342017-03-23 00:33:33 +0000608 // the call to unhandled_exception has already failed.
Eric Fiselier709d1b32016-10-27 07:30:31 +0000609 co_await a;
610}
Eric Fiselier7d5773e2016-09-30 22:38:31 +0000611
Eric Fiselierbee782b2017-04-03 19:21:00 +0000612template <class T>
613coro<T> calls_unhandled_exception_dependent(T) {
614 // expected-error@-1 {{call to unavailable member function 'unhandled_exception'}}
615 co_await a;
616}
617template coro<bad_promise_8> calls_unhandled_exception_dependent(bad_promise_8); // expected-note {{in instantiation}}
618
Eric Fiselier20f25cb2017-03-06 23:38:15 +0000619struct 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 Fiseliera9fdb342017-03-23 00:33:33 +0000626 void unhandled_exception();
Eric Fiselier20f25cb2017-03-06 23:38:15 +0000627};
628coro<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
633struct 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 Fiseliera9fdb342017-03-23 00:33:33 +0000639 void unhandled_exception();
Eric Fiselier20f25cb2017-03-06 23:38:15 +0000640};
641coro<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
647struct call_operator {
648 template <class... Args>
649 awaitable operator()(Args...) const { return a; }
650};
651void ret_void();
652struct good_promise_1 {
653 coro<good_promise_1> get_return_object();
654 suspend_always initial_suspend();
655 suspend_always final_suspend();
Eric Fiseliera9fdb342017-03-23 00:33:33 +0000656 void unhandled_exception();
Eric Fiselier20f25cb2017-03-06 23:38:15 +0000657 static const call_operator await_transform;
658 using Fn = void (*)();
659 Fn return_void = ret_void;
660};
661const call_operator good_promise_1::await_transform;
662coro<good_promise_1> ok_static_coawait() {
663 // FIXME this diagnostic is terrible
664 co_await 42;
665}
666
Gor Nishanov3e048bb2016-10-04 00:31:16 +0000667template<> struct std::experimental::coroutine_traits<int, int, const char**>
Eric Fiselier7d5773e2016-09-30 22:38:31 +0000668{ using promise_type = promise; };
669
Eric Fiselierc8efda72016-10-27 18:43:28 +0000670int main(int, const char**) {
671 co_await a; // expected-error {{'co_await' cannot be used in the 'main' function}}
Eric Fiselier7d5773e2016-09-30 22:38:31 +0000672}
Gor Nishanov6dcb0eb2017-03-09 03:09:43 +0000673
674struct good_promise_2 {
675 float get_return_object();
676 suspend_always initial_suspend();
677 suspend_always final_suspend();
678 void return_void();
Eric Fiseliera9fdb342017-03-23 00:33:33 +0000679 void unhandled_exception();
Gor Nishanov6dcb0eb2017-03-09 03:09:43 +0000680};
681template<> struct std::experimental::coroutine_handle<good_promise_2> {};
682
683template<> struct std::experimental::coroutine_traits<float>
684{ using promise_type = good_promise_2; };
685
686float 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 Nishanov3aa9eb32017-03-27 23:36:59 +0000690
Eric Fiselierf692e7d2017-04-18 03:12:48 +0000691namespace std {
692 struct nothrow_t {};
693 constexpr nothrow_t nothrow = {};
694}
695
696using SizeT = decltype(sizeof(int));
697
698void* operator new(SizeT __sz, const std::nothrow_t&) noexcept;
699void operator delete(void* __p, const std::nothrow_t&) noexcept;
700
701
702
Gor Nishanov3aa9eb32017-03-27 23:36:59 +0000703struct promise_on_alloc_failure_tag {};
704
705template<>
706struct 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
717extern "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 Fiselierbee782b2017-04-03 19:21:00 +0000720
721struct 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
728private:
729 static coro<bad_promise_11> get_return_object_on_allocation_failure(); // expected-note 2 {{declared private here}}
730};
731coro<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
736template <class T>
737coro<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}
741template coro<bad_promise_11> dependent_private_alloc_failure_handler(bad_promise_11);
742// expected-note@-1 {{requested here}}
Eric Fiselierf692e7d2017-04-18 03:12:48 +0000743
744struct 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};
755coro<bad_promise_12> throwing_in_class_new() { // expected-note {{call to 'operator new' implicitly required by coroutine function here}}
756 co_return;
757}
758
759template <class T>
760coro<T> dependent_throwing_in_class_new(T) { // expected-note {{call to 'operator new' implicitly required by coroutine function here}}
761 co_return;
762}
763template coro<bad_promise_12> dependent_throwing_in_class_new(bad_promise_12); // expected-note {{requested here}}
764
765
766struct 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};
774coro<good_promise_13> uses_nothrow_new() {
775 co_return;
776}
777
778template <class T>
779coro<T> dependent_uses_nothrow_new(T) {
780 co_return;
781}
782template coro<good_promise_13> dependent_uses_nothrow_new(good_promise_13);
Gor Nishanov6a470682017-05-22 20:22:23 +0000783
Brian Gesiak98606222018-02-15 20:37:22 +0000784struct 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
793coro<good_promise_custom_new_operator>
794good_coroutine_calls_custom_new_operator(double, float, int) {
795 co_return;
796}
797
798struct coroutine_nonstatic_member_struct;
799
800struct 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
809struct 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
819struct 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
832struct 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
842coro<bad_promise_mismatched_custom_new_operator>
843bad_coroutine_calls_mismatched_custom_new_operator(double) {
844 // expected-error@-1 {{no matching function for call to 'operator new'}}
845 co_return;
846}
847
848struct 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
859coro<bad_promise_throwing_custom_new_operator>
860bad_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
865struct 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
875coro<good_promise_noexcept_custom_new_operator>
876good_coroutine_calls_noexcept_custom_new_operator(double, float, int) {
877 co_return;
878}
879
Gor Nishanov6a470682017-05-22 20:22:23 +0000880struct mismatch_gro_type_tag1 {};
881template<>
882struct std::experimental::coroutine_traits<int, mismatch_gro_type_tag1> {
883 struct promise_type {
Eric Fiselierfc50f622017-05-25 14:59:39 +0000884 void get_return_object() {} //expected-note {{member 'get_return_object' declared here}}
Gor Nishanov6a470682017-05-22 20:22:23 +0000885 suspend_always initial_suspend() { return {}; }
886 suspend_always final_suspend() { return {}; }
887 void return_void() {}
888 void unhandled_exception();
889 };
890};
891
892extern "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
897struct mismatch_gro_type_tag2 {};
898template<>
899struct std::experimental::coroutine_traits<int, mismatch_gro_type_tag2> {
900 struct promise_type {
Eric Fiselierfc50f622017-05-25 14:59:39 +0000901 void *get_return_object() {} //expected-note {{member 'get_return_object' declared here}}
Gor Nishanov6a470682017-05-22 20:22:23 +0000902 suspend_always initial_suspend() { return {}; }
903 suspend_always final_suspend() { return {}; }
904 void return_void() {}
905 void unhandled_exception();
906 };
907};
908
909extern "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
914struct mismatch_gro_type_tag3 {};
915template<>
916struct std::experimental::coroutine_traits<int, mismatch_gro_type_tag3> {
917 struct promise_type {
918 int get_return_object() {}
Eric Fiselierfc50f622017-05-25 14:59:39 +0000919 static void get_return_object_on_allocation_failure() {} //expected-note {{member 'get_return_object_on_allocation_failure' declared here}}
Gor Nishanov6a470682017-05-22 20:22:23 +0000920 suspend_always initial_suspend() { return {}; }
921 suspend_always final_suspend() { return {}; }
922 void return_void() {}
923 void unhandled_exception();
924 };
925};
926
927extern "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
933struct mismatch_gro_type_tag4 {};
934template<>
935struct std::experimental::coroutine_traits<int, mismatch_gro_type_tag4> {
936 struct promise_type {
937 int get_return_object() {}
Eric Fiselierfc50f622017-05-25 14:59:39 +0000938 static char *get_return_object_on_allocation_failure() {} //expected-note {{member 'get_return_object_on_allocation_failure' declared}}
Gor Nishanov6a470682017-05-22 20:22:23 +0000939 suspend_always initial_suspend() { return {}; }
940 suspend_always final_suspend() { return {}; }
941 void return_void() {}
942 void unhandled_exception();
943 };
944};
945
946extern "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 Fiselierfc50f622017-05-25 14:59:39 +0000951struct bad_promise_no_return_func { // expected-note {{'bad_promise_no_return_func' defined here}}
Eric Fiselierda8f9b52017-05-25 02:16:53 +0000952 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 Fiselierfc50f622017-05-25 14:59:39 +0000957// FIXME: The PDTS currently specifies this as UB, technically forbidding a
958// diagnostic.
Eric Fiselierda8f9b52017-05-25 02:16:53 +0000959coro<bad_promise_no_return_func> no_return_value_or_return_void() {
Eric Fiselierfc50f622017-05-25 14:59:39 +0000960 // expected-error@-1 {{'bad_promise_no_return_func' must declare either 'return_value' or 'return_void'}}
Eric Fiselierda8f9b52017-05-25 02:16:53 +0000961 co_await a;
962}
Eric Fiselierd978e532017-05-28 18:21:12 +0000963
964struct bad_await_suspend_return {
965 bool await_ready();
Eric Fiselier84ee7ff2017-05-31 23:41:11 +0000966 // expected-error@+1 {{return type of 'await_suspend' is required to be 'void' or 'bool' (have 'char')}}
Eric Fiselierd978e532017-05-28 18:21:12 +0000967 char await_suspend(std::experimental::coroutine_handle<>);
968 void await_resume();
969};
970struct bad_await_ready_return {
Eric Fiselier84ee7ff2017-05-31 23:41:11 +0000971 // expected-note@+1 {{return type of 'await_ready' is required to be contextually convertible to 'bool'}}
Eric Fiselierd978e532017-05-28 18:21:12 +0000972 void await_ready();
973 bool await_suspend(std::experimental::coroutine_handle<>);
974 void await_resume();
975};
976struct 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 Fiselier84ee7ff2017-05-31 23:41:11 +0000984template <class SuspendTy>
985struct 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 Fiselierd978e532017-05-28 18:21:12 +0000992void 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 Fiselier84ee7ff2017-05-31 23:41:11 +00001007 {
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 Fiselierd978e532017-05-28 18:21:12 +00001017}
Eric Fiselierde7943b2017-06-03 00:22:18 +00001018
1019template <int ID = 0>
1020struct NoCopy {
1021 NoCopy(NoCopy const&) = delete; // expected-note 2 {{deleted here}}
1022};
1023template <class T, class U>
1024void 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}
1030template void test_dependent_param(NoCopy<0>, NoCopy<1>); // expected-note {{requested here}}
Eric Fiselier166c6e62017-07-10 01:27:22 +00001031
1032namespace CoroHandleMemberFunctionTest {
1033struct CoroMemberTag {};
1034struct BadCoroMemberTag {};
1035
1036template <class T, class U>
1037constexpr bool IsSameV = false;
1038template <class T>
1039constexpr bool IsSameV<T, T> = true;
1040
1041template <class T>
1042struct 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
1051template <class T>
1052struct AwaitReturnsType {
1053 bool await_ready() const;
1054 void await_suspend(...) const;
1055 T await_resume() const;
1056};
1057
1058template <class... CoroTraitsArgs>
1059struct 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
1076template <class... Args>
1077struct ::std::experimental::coroutine_traits<CoroHandleMemberFunctionTest::CoroMemberTag, Args...> {
1078 using promise_type = CoroHandleMemberFunctionTest::CoroMemberPromise<CoroHandleMemberFunctionTest::CoroMemberTag, Args...>;
1079};
1080
1081namespace CoroHandleMemberFunctionTest {
1082struct 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
1176template CoroMemberTag TestType::test_member_template(long, const char *) const &&;
1177template CoroMemberTag TestType::test_static_template<void>(const char *volatile &, unsigned);
1178
1179template <class... Args>
1180struct 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
1265template struct DepTestType<int>; // expected-note {{requested here}}
1266template CoroMemberTag DepTestType<int>::test_member_template(long, const char *) const &&;
1267
1268template CoroMemberTag DepTestType<int>::test_static_template<void>(const char *volatile &, unsigned);
1269
Brian Gesiak61f4ac92018-01-24 22:15:42 +00001270struct 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
1280coro<bad_promise_deleted_constructor>
1281bad_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
1292struct 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
1302coro<good_promise_default_constructor>
1303good_coroutine_calls_default_constructor() {
1304 co_return;
1305}
1306
1307struct 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
1317coro<good_promise_custom_constructor>
1318good_coroutine_calls_custom_constructor(double, float, int) {
1319 co_return;
1320}
1321
1322struct 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
1333coro<bad_promise_no_matching_constructor>
1334bad_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 Fiselier166c6e62017-07-10 01:27:22 +00001339} // namespace CoroHandleMemberFunctionTest