Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -std=c++14 -fcoroutines -verify %s |
| 2 | |
Eric Fiselier | a546528 | 2016-09-29 21:47:39 +0000 | [diff] [blame] | 3 | void no_coroutine_traits_bad_arg_await() { |
| 4 | co_await a; // expected-error {{include <coroutine>}} |
| 5 | // expected-error@-1 {{use of undeclared identifier 'a'}} |
| 6 | } |
| 7 | |
| 8 | void no_coroutine_traits_bad_arg_yield() { |
| 9 | co_yield a; // expected-error {{include <coroutine>}} |
| 10 | // expected-error@-1 {{use of undeclared identifier 'a'}} |
| 11 | } |
| 12 | |
| 13 | |
| 14 | void no_coroutine_traits_bad_arg_return() { |
| 15 | co_return a; // expected-error {{include <coroutine>}} |
| 16 | // expected-error@-1 {{use of undeclared identifier 'a'}} |
| 17 | } |
| 18 | |
| 19 | |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 20 | struct awaitable { |
| 21 | bool await_ready(); |
| 22 | void await_suspend(); // FIXME: coroutine_handle |
| 23 | void await_resume(); |
| 24 | } a; |
| 25 | |
Richard Smith | 2af65c4 | 2015-11-24 02:34:39 +0000 | [diff] [blame] | 26 | struct suspend_always { |
| 27 | bool await_ready() { return false; } |
| 28 | void await_suspend() {} |
| 29 | void await_resume() {} |
| 30 | }; |
| 31 | |
| 32 | struct suspend_never { |
| 33 | bool await_ready() { return true; } |
| 34 | void await_suspend() {} |
| 35 | void await_resume() {} |
| 36 | }; |
| 37 | |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 38 | void no_coroutine_traits() { |
| 39 | co_await a; // expected-error {{need to include <coroutine>}} |
| 40 | } |
| 41 | |
| 42 | namespace std { |
| 43 | template<typename ...T> struct coroutine_traits; // expected-note {{declared here}} |
| 44 | }; |
| 45 | |
Richard Smith | 2af65c4 | 2015-11-24 02:34:39 +0000 | [diff] [blame] | 46 | template<typename Promise> struct coro {}; |
| 47 | template<typename Promise, typename... Ps> |
| 48 | struct std::coroutine_traits<coro<Promise>, Ps...> { |
| 49 | using promise_type = Promise; |
| 50 | }; |
| 51 | |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 52 | void no_specialization() { |
| 53 | co_await a; // expected-error {{implicit instantiation of undefined template 'std::coroutine_traits<void>'}} |
| 54 | } |
| 55 | |
| 56 | template<typename ...T> struct std::coroutine_traits<int, T...> {}; |
| 57 | |
| 58 | int no_promise_type() { |
Richard Smith | 9b2f53e | 2015-11-19 02:36:35 +0000 | [diff] [blame] | 59 | co_await a; // expected-error {{this function cannot be a coroutine: 'std::coroutine_traits<int>' has no member named 'promise_type'}} |
| 60 | } |
| 61 | |
| 62 | template<> struct std::coroutine_traits<double, double> { typedef int promise_type; }; |
| 63 | double bad_promise_type(double) { |
| 64 | 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] | 65 | } |
| 66 | |
Richard Smith | 23da82c | 2015-11-20 22:40:06 +0000 | [diff] [blame] | 67 | template<> struct std::coroutine_traits<double, int> { |
| 68 | struct promise_type {}; |
| 69 | }; |
| 70 | double bad_promise_type_2(int) { |
| 71 | co_yield 0; // expected-error {{no member named 'yield_value' in 'std::coroutine_traits<double, int>::promise_type'}} |
| 72 | } |
| 73 | |
Richard Smith | 2af65c4 | 2015-11-24 02:34:39 +0000 | [diff] [blame] | 74 | struct promise; // expected-note 2{{forward declaration}} |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 75 | template<typename ...T> struct std::coroutine_traits<void, T...> { using promise_type = promise; }; |
| 76 | |
| 77 | // FIXME: This diagnostic is terrible. |
| 78 | void undefined_promise() { // expected-error {{variable has incomplete type 'promise_type'}} |
Richard Smith | 2af65c4 | 2015-11-24 02:34:39 +0000 | [diff] [blame] | 79 | // FIXME: This diagnostic doesn't make any sense. |
| 80 | // expected-error@-2 {{incomplete definition of type 'promise'}} |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 81 | co_await a; |
| 82 | } |
| 83 | |
Richard Smith | ae3d147 | 2015-11-20 22:47:10 +0000 | [diff] [blame] | 84 | struct yielded_thing { const char *p; short a, b; }; |
Richard Smith | 23da82c | 2015-11-20 22:40:06 +0000 | [diff] [blame] | 85 | |
Richard Smith | d7bed4d | 2015-11-22 02:57:17 +0000 | [diff] [blame] | 86 | struct not_awaitable {}; |
| 87 | |
Richard Smith | 23da82c | 2015-11-20 22:40:06 +0000 | [diff] [blame] | 88 | struct promise { |
Richard Smith | 2af65c4 | 2015-11-24 02:34:39 +0000 | [diff] [blame] | 89 | void get_return_object(); |
| 90 | suspend_always initial_suspend(); |
| 91 | suspend_always final_suspend(); |
Richard Smith | 71d403e | 2015-11-22 07:33:28 +0000 | [diff] [blame] | 92 | awaitable yield_value(int); // expected-note 2{{candidate}} |
| 93 | awaitable yield_value(yielded_thing); // expected-note 2{{candidate}} |
| 94 | not_awaitable yield_value(void()); // expected-note 2{{candidate}} |
Richard Smith | 4ba6660 | 2015-11-22 07:05:16 +0000 | [diff] [blame] | 95 | void return_void(); |
Richard Smith | 71d403e | 2015-11-22 07:33:28 +0000 | [diff] [blame] | 96 | void return_value(int); // expected-note 2{{here}} |
Richard Smith | 23da82c | 2015-11-20 22:40:06 +0000 | [diff] [blame] | 97 | }; |
| 98 | |
| 99 | void yield() { |
| 100 | co_yield 0; |
Richard Smith | ae3d147 | 2015-11-20 22:47:10 +0000 | [diff] [blame] | 101 | co_yield {"foo", 1, 2}; |
| 102 | co_yield {1e100}; // expected-error {{cannot be narrowed}} expected-note {{explicit cast}} expected-warning {{changes value}} expected-warning {{braces around scalar}} |
| 103 | co_yield {"foo", __LONG_LONG_MAX__}; // expected-error {{cannot be narrowed}} expected-note {{explicit cast}} expected-warning {{changes value}} |
| 104 | co_yield {"foo"}; |
Richard Smith | 23da82c | 2015-11-20 22:40:06 +0000 | [diff] [blame] | 105 | co_yield "foo"; // expected-error {{no matching}} |
Richard Smith | d7bed4d | 2015-11-22 02:57:17 +0000 | [diff] [blame] | 106 | co_yield 1.0; |
| 107 | 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] | 108 | } |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 109 | |
Richard Smith | 4ba6660 | 2015-11-22 07:05:16 +0000 | [diff] [blame] | 110 | void coreturn(int n) { |
| 111 | co_await a; |
| 112 | if (n == 0) |
| 113 | co_return 3; |
| 114 | if (n == 1) |
| 115 | co_return {4}; |
| 116 | if (n == 2) |
| 117 | co_return "foo"; // expected-error {{cannot initialize a parameter of type 'int' with an lvalue of type 'const char [4]'}} |
| 118 | co_return; |
| 119 | } |
| 120 | |
Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 121 | void mixed_yield() { |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 122 | co_yield 0; // expected-note {{use of 'co_yield'}} |
| 123 | return; // expected-error {{not allowed in coroutine}} |
Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | void mixed_await() { |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 127 | co_await a; // expected-note {{use of 'co_await'}} |
| 128 | return; // expected-error {{not allowed in coroutine}} |
Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | void only_coreturn() { |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 132 | 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] | 133 | } |
| 134 | |
| 135 | void mixed_coreturn(bool b) { |
Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 136 | if (b) |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 137 | // expected-warning@+1 {{'co_return' used in a function that uses neither}} |
| 138 | co_return; // expected-note {{use of 'co_return'}} |
Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 139 | else |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 140 | return; // expected-error {{not allowed in coroutine}} |
Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | struct CtorDtor { |
| 144 | CtorDtor() { |
| 145 | co_yield 0; // expected-error {{'co_yield' cannot be used in a constructor}} |
| 146 | } |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 147 | CtorDtor(awaitable a) { |
Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 148 | // 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] | 149 | 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] | 150 | } |
| 151 | ~CtorDtor() { |
| 152 | co_return 0; // expected-error {{'co_return' cannot be used in a destructor}} |
| 153 | } |
| 154 | // FIXME: The spec says this is ill-formed. |
| 155 | void operator=(CtorDtor&) { |
| 156 | co_yield 0; |
| 157 | } |
| 158 | }; |
| 159 | |
Richard Smith | 744b224 | 2015-11-20 02:54:01 +0000 | [diff] [blame] | 160 | void unevaluated() { |
| 161 | decltype(co_await a); // expected-error {{cannot be used in an unevaluated context}} |
| 162 | sizeof(co_await a); // expected-error {{cannot be used in an unevaluated context}} |
| 163 | typeid(co_await a); // expected-error {{cannot be used in an unevaluated context}} |
| 164 | decltype(co_yield a); // expected-error {{cannot be used in an unevaluated context}} |
| 165 | sizeof(co_yield a); // expected-error {{cannot be used in an unevaluated context}} |
| 166 | typeid(co_yield a); // expected-error {{cannot be used in an unevaluated context}} |
| 167 | } |
| 168 | |
| 169 | constexpr void constexpr_coroutine() { |
| 170 | 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] | 171 | } |
| 172 | |
| 173 | void varargs_coroutine(const char *, ...) { |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 174 | co_await a; // expected-error {{'co_await' cannot be used in a varargs function}} |
| 175 | } |
| 176 | |
| 177 | struct outer {}; |
| 178 | |
| 179 | namespace dependent_operator_co_await_lookup { |
| 180 | template<typename T> void await_template(T t) { |
| 181 | // no unqualified lookup results |
| 182 | co_await t; // expected-error {{no member named 'await_ready' in 'dependent_operator_co_await_lookup::not_awaitable'}} |
| 183 | // expected-error@-1 {{call to function 'operator co_await' that is neither visible in the template definition nor found by argument-dependent lookup}} |
| 184 | }; |
| 185 | template void await_template(awaitable); |
| 186 | |
| 187 | struct indirectly_awaitable { indirectly_awaitable(outer); }; |
| 188 | awaitable operator co_await(indirectly_awaitable); // expected-note {{should be declared prior to}} |
| 189 | template void await_template(indirectly_awaitable); |
| 190 | |
| 191 | struct not_awaitable {}; |
| 192 | template void await_template(not_awaitable); // expected-note {{instantiation}} |
| 193 | |
| 194 | template<typename T> void await_template_2(T t) { |
| 195 | // one unqualified lookup result |
| 196 | co_await t; |
| 197 | }; |
| 198 | template void await_template(outer); // expected-note {{instantiation}} |
| 199 | template void await_template_2(outer); |
Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 200 | } |
Richard Smith | 10610f7 | 2015-11-20 22:57:24 +0000 | [diff] [blame] | 201 | |
Richard Smith | 71d403e | 2015-11-22 07:33:28 +0000 | [diff] [blame] | 202 | struct yield_fn_tag {}; |
| 203 | template<> struct std::coroutine_traits<void, yield_fn_tag> { |
| 204 | struct promise_type { |
| 205 | // FIXME: add an await_transform overload for functions |
| 206 | awaitable yield_value(int()); |
| 207 | void return_value(int()); |
Richard Smith | 2af65c4 | 2015-11-24 02:34:39 +0000 | [diff] [blame] | 208 | |
| 209 | suspend_never initial_suspend(); |
| 210 | suspend_never final_suspend(); |
| 211 | void get_return_object(); |
Richard Smith | 71d403e | 2015-11-22 07:33:28 +0000 | [diff] [blame] | 212 | }; |
| 213 | }; |
| 214 | |
Richard Smith | 10610f7 | 2015-11-20 22:57:24 +0000 | [diff] [blame] | 215 | namespace placeholder { |
Richard Smith | 71d403e | 2015-11-22 07:33:28 +0000 | [diff] [blame] | 216 | awaitable f(), f(int); // expected-note 4{{possible target}} |
| 217 | int g(), g(int); // expected-note 2{{candidate}} |
Richard Smith | 10610f7 | 2015-11-20 22:57:24 +0000 | [diff] [blame] | 218 | void x() { |
| 219 | co_await f; // expected-error {{reference to overloaded function}} |
| 220 | } |
| 221 | void y() { |
Richard Smith | 71d403e | 2015-11-22 07:33:28 +0000 | [diff] [blame] | 222 | 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] | 223 | } |
| 224 | void z() { |
| 225 | co_await a; |
Richard Smith | 71d403e | 2015-11-22 07:33:28 +0000 | [diff] [blame] | 226 | co_return g; // expected-error {{address of overloaded function 'g' does not match required type 'int'}} |
| 227 | } |
| 228 | |
| 229 | void x(yield_fn_tag) { |
| 230 | co_await f; // expected-error {{reference to overloaded function}} |
| 231 | } |
| 232 | void y(yield_fn_tag) { |
| 233 | co_yield g; |
| 234 | } |
| 235 | void z(yield_fn_tag) { |
| 236 | co_await a; |
| 237 | co_return g; |
Richard Smith | 10610f7 | 2015-11-20 22:57:24 +0000 | [diff] [blame] | 238 | } |
| 239 | } |
Richard Smith | 2af65c4 | 2015-11-24 02:34:39 +0000 | [diff] [blame] | 240 | |
| 241 | struct bad_promise_1 { |
| 242 | suspend_always initial_suspend(); |
| 243 | suspend_always final_suspend(); |
| 244 | }; |
| 245 | coro<bad_promise_1> missing_get_return_object() { // expected-error {{no member named 'get_return_object' in 'bad_promise_1'}} |
| 246 | co_await a; |
| 247 | } |
| 248 | |
| 249 | struct bad_promise_2 { |
| 250 | coro<bad_promise_2> get_return_object(); |
| 251 | // FIXME: We shouldn't offer a typo-correction here! |
| 252 | suspend_always final_suspend(); // expected-note {{here}} |
| 253 | }; |
| 254 | coro<bad_promise_2> missing_initial_suspend() { // expected-error {{no member named 'initial_suspend' in 'bad_promise_2'}} |
| 255 | co_await a; |
| 256 | } |
| 257 | |
| 258 | struct bad_promise_3 { |
| 259 | coro<bad_promise_3> get_return_object(); |
| 260 | // FIXME: We shouldn't offer a typo-correction here! |
| 261 | suspend_always initial_suspend(); // expected-note {{here}} |
| 262 | }; |
| 263 | coro<bad_promise_3> missing_final_suspend() { // expected-error {{no member named 'final_suspend' in 'bad_promise_3'}} |
| 264 | co_await a; |
| 265 | } |
| 266 | |
| 267 | struct bad_promise_4 { |
| 268 | coro<bad_promise_4> get_return_object(); |
| 269 | not_awaitable initial_suspend(); |
| 270 | suspend_always final_suspend(); |
| 271 | }; |
| 272 | // FIXME: This diagnostic is terrible. |
| 273 | coro<bad_promise_4> bad_initial_suspend() { // expected-error {{no member named 'await_ready' in 'not_awaitable'}} |
| 274 | co_await a; |
| 275 | } |
| 276 | |
| 277 | struct bad_promise_5 { |
| 278 | coro<bad_promise_5> get_return_object(); |
| 279 | suspend_always initial_suspend(); |
| 280 | not_awaitable final_suspend(); |
| 281 | }; |
| 282 | // FIXME: This diagnostic is terrible. |
| 283 | coro<bad_promise_5> bad_final_suspend() { // expected-error {{no member named 'await_ready' in 'not_awaitable'}} |
| 284 | co_await a; |
| 285 | } |
Eric Fiselier | 7d5773e | 2016-09-30 22:38:31 +0000 | [diff] [blame^] | 286 | |
| 287 | |
| 288 | template<> struct std::coroutine_traits<int, int, const char**> |
| 289 | { using promise_type = promise; }; |
| 290 | |
| 291 | int main(int, const char**) { // expected-error {{'main' cannot be a coroutine}} |
| 292 | co_await a; // expected-note {{function is a coroutine due to use of 'co_await' here}} |
| 293 | } |