blob: d5968fcd30393994000f48f947e1f7302fc2051d [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
Richard Smith23da82c2015-11-20 22:40:06 +000032template<> struct std::coroutine_traits<double, int> {
33 struct promise_type {};
34};
35double 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 Smith9f690bd2015-10-27 06:02:45 +000039struct promise; // expected-note {{forward declaration}}
40template<typename ...T> struct std::coroutine_traits<void, T...> { using promise_type = promise; };
41
42 // FIXME: This diagnostic is terrible.
43void undefined_promise() { // expected-error {{variable has incomplete type 'promise_type'}}
44 co_await a;
45}
46
Richard Smithae3d1472015-11-20 22:47:10 +000047struct yielded_thing { const char *p; short a, b; };
Richard Smith23da82c2015-11-20 22:40:06 +000048
Richard Smithd7bed4d2015-11-22 02:57:17 +000049struct not_awaitable {};
50
Richard Smith23da82c2015-11-20 22:40:06 +000051struct promise {
52 awaitable yield_value(int); // expected-note {{candidate}}
53 awaitable yield_value(yielded_thing); // expected-note {{candidate}}
Richard Smithd7bed4d2015-11-22 02:57:17 +000054 not_awaitable yield_value(void()); // expected-note {{candidate}}
Richard Smith23da82c2015-11-20 22:40:06 +000055};
56
57void yield() {
58 co_yield 0;
Richard Smithae3d1472015-11-20 22:47:10 +000059 co_yield {"foo", 1, 2};
60 co_yield {1e100}; // expected-error {{cannot be narrowed}} expected-note {{explicit cast}} expected-warning {{changes value}} expected-warning {{braces around scalar}}
61 co_yield {"foo", __LONG_LONG_MAX__}; // expected-error {{cannot be narrowed}} expected-note {{explicit cast}} expected-warning {{changes value}}
62 co_yield {"foo"};
Richard Smith23da82c2015-11-20 22:40:06 +000063 co_yield "foo"; // expected-error {{no matching}}
Richard Smithd7bed4d2015-11-22 02:57:17 +000064 co_yield 1.0;
65 co_yield yield; // expected-error {{no member named 'await_ready' in 'not_awaitable'}}
Richard Smith23da82c2015-11-20 22:40:06 +000066}
Richard Smith9f690bd2015-10-27 06:02:45 +000067
Richard Smithcfd53b42015-10-22 06:13:50 +000068void mixed_yield() {
Richard Smith9f690bd2015-10-27 06:02:45 +000069 co_yield 0; // expected-note {{use of 'co_yield'}}
70 return; // expected-error {{not allowed in coroutine}}
Richard Smithcfd53b42015-10-22 06:13:50 +000071}
72
73void mixed_await() {
Richard Smith9f690bd2015-10-27 06:02:45 +000074 co_await a; // expected-note {{use of 'co_await'}}
75 return; // expected-error {{not allowed in coroutine}}
Richard Smithcfd53b42015-10-22 06:13:50 +000076}
77
78void only_coreturn() {
Richard Smith9f690bd2015-10-27 06:02:45 +000079 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 +000080}
81
82void mixed_coreturn(bool b) {
Richard Smithcfd53b42015-10-22 06:13:50 +000083 if (b)
Richard Smith9f690bd2015-10-27 06:02:45 +000084 // expected-warning@+1 {{'co_return' used in a function that uses neither}}
85 co_return; // expected-note {{use of 'co_return'}}
Richard Smithcfd53b42015-10-22 06:13:50 +000086 else
Richard Smith9f690bd2015-10-27 06:02:45 +000087 return; // expected-error {{not allowed in coroutine}}
Richard Smithcfd53b42015-10-22 06:13:50 +000088}
89
90struct CtorDtor {
91 CtorDtor() {
92 co_yield 0; // expected-error {{'co_yield' cannot be used in a constructor}}
93 }
Richard Smith9f690bd2015-10-27 06:02:45 +000094 CtorDtor(awaitable a) {
Richard Smithcfd53b42015-10-22 06:13:50 +000095 // The spec doesn't say this is ill-formed, but it must be.
Richard Smith9f690bd2015-10-27 06:02:45 +000096 co_await a; // expected-error {{'co_await' cannot be used in a constructor}}
Richard Smithcfd53b42015-10-22 06:13:50 +000097 }
98 ~CtorDtor() {
99 co_return 0; // expected-error {{'co_return' cannot be used in a destructor}}
100 }
101 // FIXME: The spec says this is ill-formed.
102 void operator=(CtorDtor&) {
103 co_yield 0;
104 }
105};
106
Richard Smith744b2242015-11-20 02:54:01 +0000107void unevaluated() {
108 decltype(co_await a); // expected-error {{cannot be used in an unevaluated context}}
109 sizeof(co_await a); // expected-error {{cannot be used in an unevaluated context}}
110 typeid(co_await a); // expected-error {{cannot be used in an unevaluated context}}
111 decltype(co_yield a); // expected-error {{cannot be used in an unevaluated context}}
112 sizeof(co_yield a); // expected-error {{cannot be used in an unevaluated context}}
113 typeid(co_yield a); // expected-error {{cannot be used in an unevaluated context}}
114}
115
116constexpr void constexpr_coroutine() {
117 co_yield 0; // expected-error {{'co_yield' cannot be used in a constexpr function}}
Richard Smithcfd53b42015-10-22 06:13:50 +0000118}
119
120void varargs_coroutine(const char *, ...) {
Richard Smith9f690bd2015-10-27 06:02:45 +0000121 co_await a; // expected-error {{'co_await' cannot be used in a varargs function}}
122}
123
124struct outer {};
125
126namespace dependent_operator_co_await_lookup {
127 template<typename T> void await_template(T t) {
128 // no unqualified lookup results
129 co_await t; // expected-error {{no member named 'await_ready' in 'dependent_operator_co_await_lookup::not_awaitable'}}
130 // expected-error@-1 {{call to function 'operator co_await' that is neither visible in the template definition nor found by argument-dependent lookup}}
131 };
132 template void await_template(awaitable);
133
134 struct indirectly_awaitable { indirectly_awaitable(outer); };
135 awaitable operator co_await(indirectly_awaitable); // expected-note {{should be declared prior to}}
136 template void await_template(indirectly_awaitable);
137
138 struct not_awaitable {};
139 template void await_template(not_awaitable); // expected-note {{instantiation}}
140
141 template<typename T> void await_template_2(T t) {
142 // one unqualified lookup result
143 co_await t;
144 };
145 template void await_template(outer); // expected-note {{instantiation}}
146 template void await_template_2(outer);
Richard Smithcfd53b42015-10-22 06:13:50 +0000147}
Richard Smith10610f72015-11-20 22:57:24 +0000148
149namespace placeholder {
150 awaitable f(), f(int); // expected-note 2{{possible target}}
151 int g(), g(int); // expected-note 4{{possible target}}
152 void x() {
153 co_await f; // expected-error {{reference to overloaded function}}
154 }
155 void y() {
156 co_yield g; // expected-error {{reference to overloaded function}}
157 }
158 void z() {
159 co_await a;
160 co_return g; // expected-error {{reference to overloaded function}}
161 }
162}