1 | // RUN: %clang_cc1 -std=c++1z -verify -fsyntax-only -fblocks %s -fcxx-exceptions |
2 | // RUN: %clang_cc1 -std=c++1z -verify -fsyntax-only -fblocks -fdelayed-template-parsing %s -fcxx-exceptions |
3 | // RUN: %clang_cc1 -std=c++14 -verify -fsyntax-only -fblocks %s -DCPP14_AND_EARLIER -fcxx-exceptions |
4 | |
5 | |
6 | namespace test_lambda_is_literal { |
7 | #ifdef CPP14_AND_EARLIER |
8 | //expected-error@+4{{not a literal type}} |
9 | //expected-note@+2{{lambda closure types are non-literal types before C++17}} |
10 | #endif |
11 | auto L = [] { }; |
12 | constexpr int foo(decltype(L) l) { return 0; } |
13 | |
14 | } |
15 | |
16 | #ifndef CPP14_AND_EARLIER |
17 | namespace test_constexpr_checking { |
18 | |
19 | namespace ns1 { |
20 | struct NonLit { ~NonLit(); }; //expected-note{{not literal}} |
21 | auto L = [](NonLit NL) constexpr { }; //expected-error{{not a literal type}} |
22 | } // end ns1 |
23 | |
24 | namespace ns2 { |
25 | auto L = [](int I) constexpr { asm("non-constexpr"); }; //expected-error{{not allowed in constexpr function}} |
26 | } // end ns1 |
27 | |
28 | } // end ns test_constexpr_checking |
29 | |
30 | namespace test_constexpr_call { |
31 | |
32 | namespace ns1 { |
33 | auto L = [](int I) { return I; }; |
34 | static_assert(L(3) == 3); |
35 | } // end ns1 |
36 | namespace ns2 { |
37 | auto L = [](auto a) { return a; }; |
38 | static_assert(L(3) == 3); |
39 | static_assert(L(3.14) == 3.14); |
40 | } |
41 | namespace ns3 { |
42 | auto L = [](auto a) { asm("non-constexpr"); return a; }; //expected-note{{declared here}} |
43 | constexpr int I = //expected-error{{must be initialized by a constant expression}} |
44 | L(3); //expected-note{{non-constexpr function}} |
45 | } |
46 | |
47 | } // end ns test_constexpr_call |
48 | |
49 | namespace test_captureless_lambda { |
50 | void f() { |
51 | const char c = 'c'; |
52 | auto L = [] { return c; }; |
53 | constexpr char C = L(); |
54 | } |
55 | |
56 | void f(char c) { //expected-note{{declared here}} |
57 | auto L = [] { return c; }; //expected-error{{cannot be implicitly captured}} expected-note{{lambda expression begins here}} |
58 | int I = L(); |
59 | } |
60 | |
61 | } |
62 | |
63 | namespace test_conversion_function_for_non_capturing_lambdas { |
64 | |
65 | namespace ns1 { |
66 | auto L = [](int i) { return i; }; |
67 | constexpr int (*fpi)(int) = L; |
68 | static_assert(fpi(3) == 3); |
69 | auto GL = [](auto a) { return a; }; |
70 | |
71 | constexpr char (*fp2)(char) = GL; |
72 | constexpr double (*fp3)(double) = GL; |
73 | constexpr const char* (*fp4)(const char*) = GL; |
74 | static_assert(fp2('3') == '3'); |
75 | static_assert(fp3(3.14) == 3.14); |
76 | constexpr const char *Str = "abc"; |
77 | static_assert(fp4(Str) == Str); |
78 | |
79 | auto NCL = [](int i) { static int j; return j; }; //expected-note{{declared here}} |
80 | constexpr int (*fp5)(int) = NCL; |
81 | constexpr int I = //expected-error{{must be initialized by a constant expression}} |
82 | fp5(5); //expected-note{{non-constexpr function}} |
83 | |
84 | namespace test_dont_always_instantiate_constexpr_templates { |
85 | |
86 | auto explicit_return_type = [](auto x) -> int { return x.get(); }; |
87 | decltype(explicit_return_type(0)) c; // OK |
88 | |
89 | auto deduced_return_type = [](auto x) { return x.get(); }; //expected-error{{not a structure or union}} |
90 | decltype(deduced_return_type(0)) d; //expected-note{{requested here}} |
91 | |
92 | |
93 | |
94 | } // end ns test_dont_always_instantiate_constexpr_templates |
95 | } // end ns1 |
96 | |
97 | } // end ns test_conversion_function_for_non_capturing_lambdas |
98 | |
99 | namespace test_lambda_is_cce { |
100 | namespace ns1_simple_lambda { |
101 | |
102 | namespace ns0 { |
103 | constexpr int I = [](auto a) { return a; }(10); |
104 | |
105 | static_assert(I == 10); |
106 | static_assert(10 == [](auto a) { return a; }(10)); |
107 | static_assert(3.14 == [](auto a) { return a; }(3.14)); |
108 | |
109 | } //end ns0 |
110 | |
111 | namespace ns1 { |
112 | constexpr auto f(int i) { |
113 | double d = 3.14; |
114 | auto L = [=](auto a) { |
115 | int Isz = sizeof(i); |
116 | return sizeof(i) + sizeof(a) + sizeof(d); |
117 | }; |
118 | int I = L("abc") + L(nullptr); |
119 | return L; |
120 | } |
121 | constexpr auto L = f(3); |
122 | constexpr auto M = L("abc") + L(nullptr); |
123 | |
124 | static_assert(M == sizeof(int) * 2 + sizeof(double) * 2 + sizeof(nullptr) + sizeof(const char*)); |
125 | |
126 | } // end ns1 |
127 | |
128 | namespace ns2 { |
129 | constexpr auto f(int i) { |
130 | auto L = [](auto a) { return a + a; }; |
131 | return L; |
132 | } |
133 | constexpr auto L = f(3); |
134 | constexpr int I = L(6); |
135 | static_assert(I == 12); |
136 | } // end ns2 |
137 | |
138 | namespace contained_lambdas_call_operator_is_not_constexpr { |
139 | constexpr auto f(int i) { |
140 | double d = 3.14; |
141 | auto L = [=](auto a) { //expected-note{{declared here}} |
142 | int Isz = sizeof(i); |
143 | asm("hello"); |
144 | return sizeof(i) + sizeof(a) + sizeof(d); |
145 | }; |
146 | return L; |
147 | } |
148 | |
149 | constexpr auto L = f(3); |
150 | |
151 | constexpr auto M = // expected-error{{must be initialized by}} |
152 | L("abc"); //expected-note{{non-constexpr function}} |
153 | |
154 | } // end ns contained_lambdas_call_operator_is_not_constexpr |
155 | |
156 | |
157 | |
158 | } // end ns1_simple_lambda |
159 | |
160 | namespace test_captures_1 { |
161 | namespace ns1 { |
162 | constexpr auto f(int i) { |
163 | struct S { int x; } s = { i * 2 }; |
164 | auto L = [=](auto a) { |
165 | return i + s.x + a; |
166 | }; |
167 | return L; |
168 | } |
169 | constexpr auto M = f(3); |
170 | |
171 | static_assert(M(10) == 19); |
172 | |
173 | } // end test_captures_1::ns1 |
174 | |
175 | namespace ns2 { |
176 | |
177 | constexpr auto foo(int n) { |
178 | auto L = [i = n] (auto N) mutable { |
179 | if (!N(i)) throw "error"; |
180 | return [&i] { |
181 | return ++i; |
182 | }; |
183 | }; |
184 | auto M = L([n](int p) { return p == n; }); |
185 | M(); M(); |
186 | L([n](int p) { return p == n + 2; }); |
187 | |
188 | return L; |
189 | } |
190 | |
191 | constexpr auto L = foo(3); |
192 | |
193 | } // end test_captures_1::ns2 |
194 | namespace ns3 { |
195 | |
196 | constexpr auto foo(int n) { |
197 | auto L = [i = n] (auto N) mutable { |
198 | if (!N(i)) throw "error"; |
199 | return [&i] { |
200 | return [i]() mutable { |
201 | return ++i; |
202 | }; |
203 | }; |
204 | }; |
205 | auto M = L([n](int p) { return p == n; }); |
206 | M()(); M()(); |
207 | L([n](int p) { return p == n; }); |
208 | |
209 | return L; |
210 | } |
211 | |
212 | constexpr auto L = foo(3); |
213 | } // end test_captures_1::ns3 |
214 | |
215 | namespace ns2_capture_this_byval { |
216 | struct S { |
217 | int s; |
218 | constexpr S(int s) : s{s} { } |
219 | constexpr auto f(S o) { |
220 | return [*this,o] (auto a) { return s + o.s + a.s; }; |
221 | } |
222 | }; |
223 | |
224 | constexpr auto L = S{5}.f(S{10}); |
225 | static_assert(L(S{100}) == 115); |
226 | } // end test_captures_1::ns2_capture_this_byval |
227 | |
228 | namespace ns2_capture_this_byref { |
229 | |
230 | struct S { |
231 | int s; |
232 | constexpr S(int s) : s{s} { } |
233 | constexpr auto f() const { |
234 | return [this] { return s; }; |
235 | } |
236 | }; |
237 | |
238 | constexpr S SObj{5}; |
239 | constexpr auto L = SObj.f(); |
240 | constexpr int I = L(); |
241 | static_assert(I == 5); |
242 | |
243 | } // end ns2_capture_this_byref |
244 | |
245 | } // end test_captures_1 |
246 | |
247 | namespace test_capture_array { |
248 | namespace ns1 { |
249 | constexpr auto f(int I) { |
250 | int arr[] = { I, I *2, I * 3 }; |
251 | auto L1 = [&] (auto a) { return arr[a]; }; |
252 | int r = L1(2); |
253 | struct X { int x, y; }; |
254 | return [=](auto a) { return X{arr[a],r}; }; |
255 | } |
256 | constexpr auto L = f(3); |
257 | static_assert(L(0).x == 3); |
258 | static_assert(L(0).y == 9); |
259 | static_assert(L(1).x == 6); |
260 | static_assert(L(1).y == 9); |
261 | } // end ns1 |
262 | |
263 | } // end test_capture_array |
264 | namespace ns1_test_lvalue_type { |
265 | void f() { |
266 | volatile int n; |
267 | constexpr bool B = [&]{ return &n; }() == &n; // should be accepted |
268 | } |
269 | } // end ns1_unimplemented |
270 | |
271 | } // end ns test_lambda_is_cce |
272 | |
273 | namespace PR36054 { |
274 | constexpr int fn() { |
275 | int Capture = 42; |
276 | return [=]() constexpr { return Capture; }(); |
277 | } |
278 | |
279 | static_assert(fn() == 42, ""); |
280 | |
281 | template <class T> |
282 | constexpr int tfn() { |
283 | int Capture = 42; |
284 | return [=]() constexpr { return Capture; }(); |
285 | } |
286 | |
287 | static_assert(tfn<int>() == 42, ""); |
288 | |
289 | constexpr int gfn() { |
290 | int Capture = 42; |
291 | return [=](auto P) constexpr { return Capture + P; }(58); |
292 | } |
293 | |
294 | static_assert(gfn() == 100, ""); |
295 | |
296 | constexpr bool OtherCaptures() { |
297 | int Capture = 42; |
298 | constexpr auto Outer = [](auto P) constexpr { return 42 + P; }; |
299 | auto Inner = [&](auto O) constexpr { return O(58) + Capture; }; |
300 | return Inner(Outer) == 142; |
301 | } |
302 | |
303 | static_assert(OtherCaptures(), ""); |
304 | } // namespace PR36054 |
305 | |
306 | #endif // ndef CPP14_AND_EARLIER |
307 | |