[lld][WebAssembly] Reinstate mistakenly disabled test. NFC
[llvm-project.git] / clang / test / SemaCXX / cxx1y-init-captures.cpp
blob1c1c93c757a0df24175928a1c2df4acfcbe15e40
1 // RUN: %clang_cc1 -std=c++1y %s -verify -emit-llvm-only
2 // RUN: %clang_cc1 -std=c++1z %s -verify -emit-llvm-only
4 namespace variadic_expansion {
5 int f(int &, char &) { return 0; }
6 template<class ... Ts> char fv(Ts ... ts) { return 0; }
7 // FIXME: why do we get 2 error messages
8 template <typename ... T> void g(T &... t) { //expected-note3{{declared here}}
9 f([&a(t)]()->decltype(auto) {
10 return a;
11 }() ...);
13 auto L = [x = f([&a(t)]()->decltype(auto) { return a; }()...)]() { return x; };
14 const int y = 10;
15 auto M = [x = y,
16 &z = y](T& ... t) { };
17 auto N = [x = y,
18 &z = y, n = f(t...),
19 o = f([&a(t)](T& ... t)->decltype(auto) { return a; }(t...)...), t...](T& ... s) {
20 fv([&a(t)]()->decltype(auto) {
21 return a;
22 }() ...);
23 };
24 auto N2 = [x = y, //expected-note3{{begins here}} expected-note 6 {{default capture by}}
25 &z = y, n = f(t...),
26 o = f([&a(t)](T& ... t)->decltype(auto) { return a; }(t...)...)](T& ... s) { // expected-note 6 {{capture 't' by}}
27 fv([&a(t)]()->decltype(auto) { //expected-error 3{{captured}}
28 return a;
29 }() ...);
30 };
34 void h(int i, char c) { g(i, c); } //expected-note{{in instantiation}}
37 namespace odr_use_within_init_capture {
39 int test() {
41 { // no captures
42 const int x = 10;
43 auto L = [z = x + 2](int a) {
44 auto M = [y = x - 2](char b) {
45 return y;
47 return M;
51 { // should not capture
52 const int x = 10;
53 auto L = [&z = x](int a) {
54 return a;;
59 const int x = 10;
60 auto L = [k = x](char a) { //expected-note {{declared}}
61 return [](int b) { //expected-note {{begins}} expected-note 2 {{capture 'k' by}} expected-note 2 {{default capture by}}
62 return [j = k](int c) { //expected-error {{cannot be implicitly captured}}
63 return c;
69 const int x = 10;
70 auto L = [k = x](char a) {
71 return [=](int b) {
72 return [j = k](int c) {
73 return c;
79 const int x = 10;
80 auto L = [k = x](char a) {
81 return [k](int b) {
82 return [j = k](int c) {
83 return c;
89 return 0;
92 int run = test();
96 namespace odr_use_within_init_capture_template {
98 template<class T = int>
99 int test(T t = T{}) {
101 { // no captures
102 const T x = 10;
103 auto L = [z = x](char a) {
104 auto M = [y = x](T b) {
105 return y;
107 return M;
111 { // should not capture
112 const T x = 10;
113 auto L = [&z = x](T a) {
114 return a;;
118 { // will need to capture x in outer lambda
119 const T x = 10; //expected-note {{declared}}
120 auto L = [z = x](char a) { //expected-note {{begins}} expected-note 2 {{capture 'x' by}} expected-note 2 {{default capture by}}
121 auto M = [&y = x](T b) { //expected-error {{cannot be implicitly captured}}
122 return y;
124 return M;
127 { // will need to capture x in outer lambda
128 const T x = 10;
129 auto L = [=,z = x](char a) {
130 auto M = [&y = x](T b) {
131 return y;
133 return M;
137 { // will need to capture x in outer lambda
138 const T x = 10;
139 auto L = [x, z = x](char a) {
140 auto M = [&y = x](T b) {
141 return y;
143 return M;
146 { // will need to capture x in outer lambda
147 const int x = 10; //expected-note {{declared}}
148 auto L = [z = x](char a) { //expected-note {{begins}} expected-note 2 {{capture 'x' by}} expected-note 2 {{default capture by}}
149 auto M = [&y = x](T b) { //expected-error {{cannot be implicitly captured}}
150 return y;
152 return M;
156 // no captures
157 const T x = 10;
158 auto L = [z =
159 [z = x, &y = x](char a) { return z + y; }('a')](char a)
160 { return z; };
164 return 0;
167 int run = test(); //expected-note {{instantiation}}
171 namespace classification_of_captures_of_init_captures {
173 template <typename T>
174 void f() {
175 [a = 24] () mutable {
176 [&a] { a = 3; }();
177 }();
180 template <typename T>
181 void h() {
182 [a = 24] (auto param) mutable {
183 [&a] { a = 3; }();
184 }(42);
187 int run() {
188 f<int>();
189 h<int>();
194 namespace N3922 {
195 struct X { X(); explicit X(const X&); int n; };
196 auto a = [x{X()}] { return x.n; }; // ok
197 auto b = [x = {X()}] {}; // expected-error{{<initializer_list>}}
200 namespace init_capture_non_mutable {
201 void test(double weight) {
202 double init;
203 auto find = [max = init](auto current) {
204 max = current; // expected-error{{cannot assign to a variable captured by copy in a non-mutable lambda}}
206 find(weight); // expected-note {{in instantiation of function template specialization}}
210 namespace init_capture_undeclared_identifier {
211 auto a = [x = y]{}; // expected-error{{use of undeclared identifier 'y'}}
213 int typo_foo; // expected-note 2 {{'typo_foo' declared here}}
214 auto b = [x = typo_boo]{}; // expected-error{{use of undeclared identifier 'typo_boo'; did you mean 'typo_foo'}}
215 auto c = [x(typo_boo)]{}; // expected-error{{use of undeclared identifier 'typo_boo'; did you mean 'typo_foo'}}
218 namespace copy_evasion {
219 struct A {
220 A();
221 A(const A&) = delete;
223 auto x = [a{A()}] {};
224 #if __cplusplus >= 201702L
225 // ok, does not copy an 'A'
226 #else
227 // expected-error@-4 {{call to deleted}}
228 // expected-note@-7 {{deleted}}
229 #endif