Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -std=c++14 -fcoroutines -verify %s |
| 2 | |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 3 | struct awaitable { |
| 4 | bool await_ready(); |
| 5 | void await_suspend(); // FIXME: coroutine_handle |
| 6 | void await_resume(); |
| 7 | } a; |
| 8 | |
| 9 | void no_coroutine_traits() { |
| 10 | co_await a; // expected-error {{need to include <coroutine>}} |
| 11 | } |
| 12 | |
| 13 | namespace std { |
| 14 | template<typename ...T> struct coroutine_traits; // expected-note {{declared here}} |
| 15 | }; |
| 16 | |
| 17 | void no_specialization() { |
| 18 | co_await a; // expected-error {{implicit instantiation of undefined template 'std::coroutine_traits<void>'}} |
| 19 | } |
| 20 | |
| 21 | template<typename ...T> struct std::coroutine_traits<int, T...> {}; |
| 22 | |
| 23 | int no_promise_type() { |
Richard Smith | 9b2f53e | 2015-11-19 02:36:35 +0000 | [diff] [blame] | 24 | co_await a; // expected-error {{this function cannot be a coroutine: 'std::coroutine_traits<int>' has no member named 'promise_type'}} |
| 25 | } |
| 26 | |
| 27 | template<> struct std::coroutine_traits<double, double> { typedef int promise_type; }; |
| 28 | double bad_promise_type(double) { |
| 29 | co_await a; // expected-error {{this function cannot be a coroutine: 'std::coroutine_traits<double, double>::promise_type' (aka 'int') is not a class}} |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 30 | } |
| 31 | |
Richard Smith | 23da82c | 2015-11-20 22:40:06 +0000 | [diff] [blame^] | 32 | template<> struct std::coroutine_traits<double, int> { |
| 33 | struct promise_type {}; |
| 34 | }; |
| 35 | double bad_promise_type_2(int) { |
| 36 | co_yield 0; // expected-error {{no member named 'yield_value' in 'std::coroutine_traits<double, int>::promise_type'}} |
| 37 | } |
| 38 | |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 39 | struct promise; // expected-note {{forward declaration}} |
| 40 | template<typename ...T> struct std::coroutine_traits<void, T...> { using promise_type = promise; }; |
| 41 | |
| 42 | // FIXME: This diagnostic is terrible. |
| 43 | void undefined_promise() { // expected-error {{variable has incomplete type 'promise_type'}} |
| 44 | co_await a; |
| 45 | } |
| 46 | |
Richard Smith | 23da82c | 2015-11-20 22:40:06 +0000 | [diff] [blame^] | 47 | struct yielded_thing { int a, b, c; const char *p; }; |
| 48 | |
| 49 | struct promise { |
| 50 | awaitable yield_value(int); // expected-note {{candidate}} |
| 51 | awaitable yield_value(yielded_thing); // expected-note {{candidate}} |
| 52 | }; |
| 53 | |
| 54 | void yield() { |
| 55 | co_yield 0; |
| 56 | co_yield {1, 2, 3, "foo"}; // FIXME expected-error {{expected expression}} |
| 57 | co_yield "foo"; // expected-error {{no matching}} |
| 58 | } |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 59 | |
Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 60 | void mixed_yield() { |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 61 | co_yield 0; // expected-note {{use of 'co_yield'}} |
| 62 | return; // expected-error {{not allowed in coroutine}} |
Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | void mixed_await() { |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 66 | co_await a; // expected-note {{use of 'co_await'}} |
| 67 | return; // expected-error {{not allowed in coroutine}} |
Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | void only_coreturn() { |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 71 | co_return; // expected-warning {{'co_return' used in a function that uses neither 'co_await' nor 'co_yield'}} |
Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | void mixed_coreturn(bool b) { |
Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 75 | if (b) |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 76 | // expected-warning@+1 {{'co_return' used in a function that uses neither}} |
| 77 | co_return; // expected-note {{use of 'co_return'}} |
Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 78 | else |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 79 | return; // expected-error {{not allowed in coroutine}} |
Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | struct CtorDtor { |
| 83 | CtorDtor() { |
| 84 | co_yield 0; // expected-error {{'co_yield' cannot be used in a constructor}} |
| 85 | } |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 86 | CtorDtor(awaitable a) { |
Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 87 | // 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] | 88 | 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] | 89 | } |
| 90 | ~CtorDtor() { |
| 91 | co_return 0; // expected-error {{'co_return' cannot be used in a destructor}} |
| 92 | } |
| 93 | // FIXME: The spec says this is ill-formed. |
| 94 | void operator=(CtorDtor&) { |
| 95 | co_yield 0; |
| 96 | } |
| 97 | }; |
| 98 | |
Richard Smith | 744b224 | 2015-11-20 02:54:01 +0000 | [diff] [blame] | 99 | void unevaluated() { |
| 100 | decltype(co_await a); // expected-error {{cannot be used in an unevaluated context}} |
| 101 | sizeof(co_await a); // expected-error {{cannot be used in an unevaluated context}} |
| 102 | typeid(co_await a); // expected-error {{cannot be used in an unevaluated context}} |
| 103 | decltype(co_yield a); // expected-error {{cannot be used in an unevaluated context}} |
| 104 | sizeof(co_yield a); // expected-error {{cannot be used in an unevaluated context}} |
| 105 | typeid(co_yield a); // expected-error {{cannot be used in an unevaluated context}} |
| 106 | } |
| 107 | |
| 108 | constexpr void constexpr_coroutine() { |
| 109 | co_yield 0; // expected-error {{'co_yield' cannot be used in a constexpr function}} |
Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | void varargs_coroutine(const char *, ...) { |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 113 | co_await a; // expected-error {{'co_await' cannot be used in a varargs function}} |
| 114 | } |
| 115 | |
| 116 | struct outer {}; |
| 117 | |
| 118 | namespace dependent_operator_co_await_lookup { |
| 119 | template<typename T> void await_template(T t) { |
| 120 | // no unqualified lookup results |
| 121 | co_await t; // expected-error {{no member named 'await_ready' in 'dependent_operator_co_await_lookup::not_awaitable'}} |
| 122 | // expected-error@-1 {{call to function 'operator co_await' that is neither visible in the template definition nor found by argument-dependent lookup}} |
| 123 | }; |
| 124 | template void await_template(awaitable); |
| 125 | |
| 126 | struct indirectly_awaitable { indirectly_awaitable(outer); }; |
| 127 | awaitable operator co_await(indirectly_awaitable); // expected-note {{should be declared prior to}} |
| 128 | template void await_template(indirectly_awaitable); |
| 129 | |
| 130 | struct not_awaitable {}; |
| 131 | template void await_template(not_awaitable); // expected-note {{instantiation}} |
| 132 | |
| 133 | template<typename T> void await_template_2(T t) { |
| 134 | // one unqualified lookup result |
| 135 | co_await t; |
| 136 | }; |
| 137 | template void await_template(outer); // expected-note {{instantiation}} |
| 138 | template void await_template_2(outer); |
Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 139 | } |