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 | ae3d147 | 2015-11-20 22:47:10 +0000 | [diff] [blame] | 47 | struct yielded_thing { const char *p; short a, b; }; |
Richard Smith | 23da82c | 2015-11-20 22:40:06 +0000 | [diff] [blame] | 48 | |
Richard Smith | d7bed4d | 2015-11-22 02:57:17 +0000 | [diff] [blame] | 49 | struct not_awaitable {}; |
| 50 | |
Richard Smith | 23da82c | 2015-11-20 22:40:06 +0000 | [diff] [blame] | 51 | struct promise { |
Richard Smith | 71d403e | 2015-11-22 07:33:28 +0000 | [diff] [blame^] | 52 | awaitable yield_value(int); // expected-note 2{{candidate}} |
| 53 | awaitable yield_value(yielded_thing); // expected-note 2{{candidate}} |
| 54 | not_awaitable yield_value(void()); // expected-note 2{{candidate}} |
Richard Smith | 4ba6660 | 2015-11-22 07:05:16 +0000 | [diff] [blame] | 55 | void return_void(); |
Richard Smith | 71d403e | 2015-11-22 07:33:28 +0000 | [diff] [blame^] | 56 | void return_value(int); // expected-note 2{{here}} |
Richard Smith | 23da82c | 2015-11-20 22:40:06 +0000 | [diff] [blame] | 57 | }; |
| 58 | |
| 59 | void yield() { |
| 60 | co_yield 0; |
Richard Smith | ae3d147 | 2015-11-20 22:47:10 +0000 | [diff] [blame] | 61 | co_yield {"foo", 1, 2}; |
| 62 | co_yield {1e100}; // expected-error {{cannot be narrowed}} expected-note {{explicit cast}} expected-warning {{changes value}} expected-warning {{braces around scalar}} |
| 63 | co_yield {"foo", __LONG_LONG_MAX__}; // expected-error {{cannot be narrowed}} expected-note {{explicit cast}} expected-warning {{changes value}} |
| 64 | co_yield {"foo"}; |
Richard Smith | 23da82c | 2015-11-20 22:40:06 +0000 | [diff] [blame] | 65 | co_yield "foo"; // expected-error {{no matching}} |
Richard Smith | d7bed4d | 2015-11-22 02:57:17 +0000 | [diff] [blame] | 66 | co_yield 1.0; |
| 67 | 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] | 68 | } |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 69 | |
Richard Smith | 4ba6660 | 2015-11-22 07:05:16 +0000 | [diff] [blame] | 70 | void coreturn(int n) { |
| 71 | co_await a; |
| 72 | if (n == 0) |
| 73 | co_return 3; |
| 74 | if (n == 1) |
| 75 | co_return {4}; |
| 76 | if (n == 2) |
| 77 | co_return "foo"; // expected-error {{cannot initialize a parameter of type 'int' with an lvalue of type 'const char [4]'}} |
| 78 | co_return; |
| 79 | } |
| 80 | |
Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 81 | void mixed_yield() { |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 82 | co_yield 0; // expected-note {{use of 'co_yield'}} |
| 83 | return; // expected-error {{not allowed in coroutine}} |
Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | void mixed_await() { |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 87 | co_await a; // expected-note {{use of 'co_await'}} |
| 88 | return; // expected-error {{not allowed in coroutine}} |
Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | void only_coreturn() { |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 92 | 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] | 93 | } |
| 94 | |
| 95 | void mixed_coreturn(bool b) { |
Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 96 | if (b) |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 97 | // expected-warning@+1 {{'co_return' used in a function that uses neither}} |
| 98 | co_return; // expected-note {{use of 'co_return'}} |
Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 99 | else |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 100 | return; // expected-error {{not allowed in coroutine}} |
Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | struct CtorDtor { |
| 104 | CtorDtor() { |
| 105 | co_yield 0; // expected-error {{'co_yield' cannot be used in a constructor}} |
| 106 | } |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 107 | CtorDtor(awaitable a) { |
Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 108 | // 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] | 109 | 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] | 110 | } |
| 111 | ~CtorDtor() { |
| 112 | co_return 0; // expected-error {{'co_return' cannot be used in a destructor}} |
| 113 | } |
| 114 | // FIXME: The spec says this is ill-formed. |
| 115 | void operator=(CtorDtor&) { |
| 116 | co_yield 0; |
| 117 | } |
| 118 | }; |
| 119 | |
Richard Smith | 744b224 | 2015-11-20 02:54:01 +0000 | [diff] [blame] | 120 | void unevaluated() { |
| 121 | decltype(co_await a); // expected-error {{cannot be used in an unevaluated context}} |
| 122 | sizeof(co_await a); // expected-error {{cannot be used in an unevaluated context}} |
| 123 | typeid(co_await a); // expected-error {{cannot be used in an unevaluated context}} |
| 124 | decltype(co_yield a); // expected-error {{cannot be used in an unevaluated context}} |
| 125 | sizeof(co_yield a); // expected-error {{cannot be used in an unevaluated context}} |
| 126 | typeid(co_yield a); // expected-error {{cannot be used in an unevaluated context}} |
| 127 | } |
| 128 | |
| 129 | constexpr void constexpr_coroutine() { |
| 130 | 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] | 131 | } |
| 132 | |
| 133 | void varargs_coroutine(const char *, ...) { |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 134 | co_await a; // expected-error {{'co_await' cannot be used in a varargs function}} |
| 135 | } |
| 136 | |
| 137 | struct outer {}; |
| 138 | |
| 139 | namespace dependent_operator_co_await_lookup { |
| 140 | template<typename T> void await_template(T t) { |
| 141 | // no unqualified lookup results |
| 142 | co_await t; // expected-error {{no member named 'await_ready' in 'dependent_operator_co_await_lookup::not_awaitable'}} |
| 143 | // expected-error@-1 {{call to function 'operator co_await' that is neither visible in the template definition nor found by argument-dependent lookup}} |
| 144 | }; |
| 145 | template void await_template(awaitable); |
| 146 | |
| 147 | struct indirectly_awaitable { indirectly_awaitable(outer); }; |
| 148 | awaitable operator co_await(indirectly_awaitable); // expected-note {{should be declared prior to}} |
| 149 | template void await_template(indirectly_awaitable); |
| 150 | |
| 151 | struct not_awaitable {}; |
| 152 | template void await_template(not_awaitable); // expected-note {{instantiation}} |
| 153 | |
| 154 | template<typename T> void await_template_2(T t) { |
| 155 | // one unqualified lookup result |
| 156 | co_await t; |
| 157 | }; |
| 158 | template void await_template(outer); // expected-note {{instantiation}} |
| 159 | template void await_template_2(outer); |
Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 160 | } |
Richard Smith | 10610f7 | 2015-11-20 22:57:24 +0000 | [diff] [blame] | 161 | |
Richard Smith | 71d403e | 2015-11-22 07:33:28 +0000 | [diff] [blame^] | 162 | struct yield_fn_tag {}; |
| 163 | template<> struct std::coroutine_traits<void, yield_fn_tag> { |
| 164 | struct promise_type { |
| 165 | // FIXME: add an await_transform overload for functions |
| 166 | awaitable yield_value(int()); |
| 167 | void return_value(int()); |
| 168 | }; |
| 169 | }; |
| 170 | |
Richard Smith | 10610f7 | 2015-11-20 22:57:24 +0000 | [diff] [blame] | 171 | namespace placeholder { |
Richard Smith | 71d403e | 2015-11-22 07:33:28 +0000 | [diff] [blame^] | 172 | awaitable f(), f(int); // expected-note 4{{possible target}} |
| 173 | int g(), g(int); // expected-note 2{{candidate}} |
Richard Smith | 10610f7 | 2015-11-20 22:57:24 +0000 | [diff] [blame] | 174 | void x() { |
| 175 | co_await f; // expected-error {{reference to overloaded function}} |
| 176 | } |
| 177 | void y() { |
Richard Smith | 71d403e | 2015-11-22 07:33:28 +0000 | [diff] [blame^] | 178 | 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] | 179 | } |
| 180 | void z() { |
| 181 | co_await a; |
Richard Smith | 71d403e | 2015-11-22 07:33:28 +0000 | [diff] [blame^] | 182 | co_return g; // expected-error {{address of overloaded function 'g' does not match required type 'int'}} |
| 183 | } |
| 184 | |
| 185 | void x(yield_fn_tag) { |
| 186 | co_await f; // expected-error {{reference to overloaded function}} |
| 187 | } |
| 188 | void y(yield_fn_tag) { |
| 189 | co_yield g; |
| 190 | } |
| 191 | void z(yield_fn_tag) { |
| 192 | co_await a; |
| 193 | co_return g; |
Richard Smith | 10610f7 | 2015-11-20 22:57:24 +0000 | [diff] [blame] | 194 | } |
| 195 | } |