blob: c82302c3c0708063657d0547aa572471b49bc9ae [file] [log] [blame]
Richard Smithcfd53b42015-10-22 06:13:50 +00001// RUN: %clang_cc1 -std=c++14 -fcoroutines -verify %s
2
Richard Smith9f690bd2015-10-27 06:02:45 +00003struct awaitable {
4 bool await_ready();
5 void await_suspend(); // FIXME: coroutine_handle
6 void await_resume();
7} a;
8
9void no_coroutine_traits() {
10 co_await a; // expected-error {{need to include <coroutine>}}
11}
12
13namespace std {
14 template<typename ...T> struct coroutine_traits; // expected-note {{declared here}}
15};
16
17void no_specialization() {
18 co_await a; // expected-error {{implicit instantiation of undefined template 'std::coroutine_traits<void>'}}
19}
20
21template<typename ...T> struct std::coroutine_traits<int, T...> {};
22
23int no_promise_type() {
Richard Smith9b2f53e2015-11-19 02:36:35 +000024 co_await a; // expected-error {{this function cannot be a coroutine: 'std::coroutine_traits<int>' has no member named 'promise_type'}}
25}
26
27template<> struct std::coroutine_traits<double, double> { typedef int promise_type; };
28double 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 Smith9f690bd2015-10-27 06:02:45 +000030}
31
32struct promise; // expected-note {{forward declaration}}
33template<typename ...T> struct std::coroutine_traits<void, T...> { using promise_type = promise; };
34
35 // FIXME: This diagnostic is terrible.
36void undefined_promise() { // expected-error {{variable has incomplete type 'promise_type'}}
37 co_await a;
38}
39
40struct promise {};
41
Richard Smithcfd53b42015-10-22 06:13:50 +000042void mixed_yield() {
Richard Smith9f690bd2015-10-27 06:02:45 +000043 co_yield 0; // expected-note {{use of 'co_yield'}}
44 return; // expected-error {{not allowed in coroutine}}
Richard Smithcfd53b42015-10-22 06:13:50 +000045}
46
47void mixed_await() {
Richard Smith9f690bd2015-10-27 06:02:45 +000048 co_await a; // expected-note {{use of 'co_await'}}
49 return; // expected-error {{not allowed in coroutine}}
Richard Smithcfd53b42015-10-22 06:13:50 +000050}
51
52void only_coreturn() {
Richard Smith9f690bd2015-10-27 06:02:45 +000053 co_return; // expected-warning {{'co_return' used in a function that uses neither 'co_await' nor 'co_yield'}}
Richard Smithcfd53b42015-10-22 06:13:50 +000054}
55
56void mixed_coreturn(bool b) {
Richard Smithcfd53b42015-10-22 06:13:50 +000057 if (b)
Richard Smith9f690bd2015-10-27 06:02:45 +000058 // expected-warning@+1 {{'co_return' used in a function that uses neither}}
59 co_return; // expected-note {{use of 'co_return'}}
Richard Smithcfd53b42015-10-22 06:13:50 +000060 else
Richard Smith9f690bd2015-10-27 06:02:45 +000061 return; // expected-error {{not allowed in coroutine}}
Richard Smithcfd53b42015-10-22 06:13:50 +000062}
63
64struct CtorDtor {
65 CtorDtor() {
66 co_yield 0; // expected-error {{'co_yield' cannot be used in a constructor}}
67 }
Richard Smith9f690bd2015-10-27 06:02:45 +000068 CtorDtor(awaitable a) {
Richard Smithcfd53b42015-10-22 06:13:50 +000069 // The spec doesn't say this is ill-formed, but it must be.
Richard Smith9f690bd2015-10-27 06:02:45 +000070 co_await a; // expected-error {{'co_await' cannot be used in a constructor}}
Richard Smithcfd53b42015-10-22 06:13:50 +000071 }
72 ~CtorDtor() {
73 co_return 0; // expected-error {{'co_return' cannot be used in a destructor}}
74 }
75 // FIXME: The spec says this is ill-formed.
76 void operator=(CtorDtor&) {
77 co_yield 0;
78 }
79};
80
Richard Smith9f690bd2015-10-27 06:02:45 +000081constexpr void constexpr_coroutine() { // expected-error {{never produces a constant expression}}
82 co_yield 0; // expected-error {{'co_yield' cannot be used in a constexpr function}} expected-note {{subexpression}}
Richard Smithcfd53b42015-10-22 06:13:50 +000083}
84
85void varargs_coroutine(const char *, ...) {
Richard Smith9f690bd2015-10-27 06:02:45 +000086 co_await a; // expected-error {{'co_await' cannot be used in a varargs function}}
87}
88
89struct outer {};
90
91namespace dependent_operator_co_await_lookup {
92 template<typename T> void await_template(T t) {
93 // no unqualified lookup results
94 co_await t; // expected-error {{no member named 'await_ready' in 'dependent_operator_co_await_lookup::not_awaitable'}}
95 // expected-error@-1 {{call to function 'operator co_await' that is neither visible in the template definition nor found by argument-dependent lookup}}
96 };
97 template void await_template(awaitable);
98
99 struct indirectly_awaitable { indirectly_awaitable(outer); };
100 awaitable operator co_await(indirectly_awaitable); // expected-note {{should be declared prior to}}
101 template void await_template(indirectly_awaitable);
102
103 struct not_awaitable {};
104 template void await_template(not_awaitable); // expected-note {{instantiation}}
105
106 template<typename T> void await_template_2(T t) {
107 // one unqualified lookup result
108 co_await t;
109 };
110 template void await_template(outer); // expected-note {{instantiation}}
111 template void await_template_2(outer);
Richard Smithcfd53b42015-10-22 06:13:50 +0000112}