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() { |
| 24 | co_await a; // expected-error {{this function cannot be a coroutine: 'coroutine_traits<int>' has no member named 'promise_type'}} |
| 25 | } |
| 26 | |
| 27 | struct promise; // expected-note {{forward declaration}} |
| 28 | template<typename ...T> struct std::coroutine_traits<void, T...> { using promise_type = promise; }; |
| 29 | |
| 30 | // FIXME: This diagnostic is terrible. |
| 31 | void undefined_promise() { // expected-error {{variable has incomplete type 'promise_type'}} |
| 32 | co_await a; |
| 33 | } |
| 34 | |
| 35 | struct promise {}; |
| 36 | |
Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 37 | void mixed_yield() { |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame^] | 38 | co_yield 0; // expected-note {{use of 'co_yield'}} |
| 39 | return; // expected-error {{not allowed in coroutine}} |
Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | void mixed_await() { |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame^] | 43 | co_await a; // expected-note {{use of 'co_await'}} |
| 44 | return; // expected-error {{not allowed in coroutine}} |
Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | void only_coreturn() { |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame^] | 48 | 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] | 49 | } |
| 50 | |
| 51 | void mixed_coreturn(bool b) { |
Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 52 | if (b) |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame^] | 53 | // expected-warning@+1 {{'co_return' used in a function that uses neither}} |
| 54 | co_return; // expected-note {{use of 'co_return'}} |
Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 55 | else |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame^] | 56 | return; // expected-error {{not allowed in coroutine}} |
Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | struct CtorDtor { |
| 60 | CtorDtor() { |
| 61 | co_yield 0; // expected-error {{'co_yield' cannot be used in a constructor}} |
| 62 | } |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame^] | 63 | CtorDtor(awaitable a) { |
Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 64 | // 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^] | 65 | 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] | 66 | } |
| 67 | ~CtorDtor() { |
| 68 | co_return 0; // expected-error {{'co_return' cannot be used in a destructor}} |
| 69 | } |
| 70 | // FIXME: The spec says this is ill-formed. |
| 71 | void operator=(CtorDtor&) { |
| 72 | co_yield 0; |
| 73 | } |
| 74 | }; |
| 75 | |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame^] | 76 | constexpr void constexpr_coroutine() { // expected-error {{never produces a constant expression}} |
| 77 | co_yield 0; // expected-error {{'co_yield' cannot be used in a constexpr function}} expected-note {{subexpression}} |
Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | void varargs_coroutine(const char *, ...) { |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame^] | 81 | co_await a; // expected-error {{'co_await' cannot be used in a varargs function}} |
| 82 | } |
| 83 | |
| 84 | struct outer {}; |
| 85 | |
| 86 | namespace dependent_operator_co_await_lookup { |
| 87 | template<typename T> void await_template(T t) { |
| 88 | // no unqualified lookup results |
| 89 | co_await t; // expected-error {{no member named 'await_ready' in 'dependent_operator_co_await_lookup::not_awaitable'}} |
| 90 | // expected-error@-1 {{call to function 'operator co_await' that is neither visible in the template definition nor found by argument-dependent lookup}} |
| 91 | }; |
| 92 | template void await_template(awaitable); |
| 93 | |
| 94 | struct indirectly_awaitable { indirectly_awaitable(outer); }; |
| 95 | awaitable operator co_await(indirectly_awaitable); // expected-note {{should be declared prior to}} |
| 96 | template void await_template(indirectly_awaitable); |
| 97 | |
| 98 | struct not_awaitable {}; |
| 99 | template void await_template(not_awaitable); // expected-note {{instantiation}} |
| 100 | |
| 101 | template<typename T> void await_template_2(T t) { |
| 102 | // one unqualified lookup result |
| 103 | co_await t; |
| 104 | }; |
| 105 | template void await_template(outer); // expected-note {{instantiation}} |
| 106 | template void await_template_2(outer); |
Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 107 | } |