1 // RUN: %clang_cc1 -std=c++1z -verify -fsyntax-only -fblocks %s -fcxx-exceptions
2 // RUN: %clang_cc1 -std=c++20 -verify -fsyntax-only -fblocks %s -fcxx-exceptions
3 // RUN: %clang_cc1 -std=c++1z -verify -fsyntax-only -fblocks -fdelayed-template-parsing %s -fcxx-exceptions
4 // RUN: %clang_cc1 -std=c++14 -verify -fsyntax-only -fblocks %s -DCPP14_AND_EARLIER -fcxx-exceptions
5 
6 
7 namespace test_lambda_is_literal {
8 #ifdef CPP14_AND_EARLIER
9 //expected-error@+4{{not a literal type}}
10 //expected-note@+2{{lambda closure types are non-literal types before C++17}}
11 #endif
__anonf2a229c90102null12 auto L = [] { };
foo(decltype(L)l)13 constexpr int foo(decltype(L) l) { return 0; }
14 
15 }
16 
17 #ifndef CPP14_AND_EARLIER
18 namespace test_constexpr_checking {
19 
20 namespace ns1 {
21   struct NonLit { ~NonLit(); };  //expected-note{{not literal}}
__anonf2a229c90202(NonLit NL) 22   auto L = [](NonLit NL) constexpr { }; //expected-error{{not a literal type}}
23 } // end ns1
24 
25 namespace ns2 {
__anonf2a229c90302(int I) 26   auto L = [](int I) constexpr { if (I == 5) asm("non-constexpr");  };
27 #if __cpp_constexpr < 201907L
28   //expected-warning@-2{{use of this statement in a constexpr function is a C++20 extension}}
29 #endif
30 } // end ns1
31 
32 // This is not constexpr until C++20, as the requirements on constexpr
33 // functions don't permit try-catch blocks.
34 #if __cplusplus <= 201703L
35 // expected-error@#try-catch {{constant expression}}
36 // expected-note@#try-catch {{non-constexpr function 'operator()'}}
37 // expected-note@#try-catch {{declared here}}
38 #endif
__anonf2a229c90402null39 constexpr int try_catch = [] { // #try-catch
40   try { return 0; } catch (...) { return 1; }
41 }();
42 
43 // These lambdas have constexpr operator() even though they can never produce a
44 // constant expression.
__anonf2a229c90502null45 auto never_constant_1 = [] { // expected-note {{here}}
46   volatile int n = 0;
47   return n;
48 };
__anonf2a229c90602() 49 auto never_constant_2 = [] () -> int { // expected-note {{here}}
50 };
51 struct test_never_constant {
52   #if __cplusplus >= 201703L
53   // expected-error@+3 {{non-constexpr declaration of 'operator()' follows constexpr declaration}}
54   // expected-error@+3 {{non-constexpr declaration of 'operator()' follows constexpr declaration}}
55   #endif
56   friend auto decltype(never_constant_1)::operator()() const;
57   friend int decltype(never_constant_2)::operator()() const;
58 };
59 
60 } // end ns test_constexpr_checking
61 
62 namespace test_constexpr_call {
63 
64 namespace ns1 {
__anonf2a229c90702(int I) 65   auto L = [](int I) { return I; };
66   static_assert(L(3) == 3);
67 } // end ns1
68 namespace ns2 {
__anonf2a229c90802(auto a) 69   auto L = [](auto a) { return a; };
70   static_assert(L(3) == 3);
71   static_assert(L(3.14) == 3.14);
72 }
73 namespace ns3 {
__anonf2a229c90902(auto a) 74   auto L = [](auto a) { asm("non-constexpr"); return a; };
75   constexpr int I =  //expected-error{{must be initialized by a constant expression}}
76       L(3);
77 #if __cpp_constexpr < 201907L
78 //expected-note@-2{{non-constexpr function}}
79 //expected-note@-5{{declared here}}
80 #else
81 //expected-note@-7{{subexpression not valid in a constant expression}}
82 //expected-note@-6{{in call to}}
83 #endif
84 }
85 
86 } // end ns test_constexpr_call
87 
88 namespace test_captureless_lambda {
f()89 void f() {
90   const char c = 'c';
91   auto L = [] { return c; };
92   constexpr char C = L();
93 }
94 
f(char c)95 void f(char c) { //expected-note{{declared here}}
96   auto L = [] { return c; }; //expected-error{{cannot be implicitly captured}} expected-note{{lambda expression begins here}}
97   int I = L();
98 }
99 
100 }
101 
102 namespace test_conversion_function_for_non_capturing_lambdas {
103 
104 namespace ns1 {
__anonf2a229c90c02(int i) 105 auto L = [](int i) { return i; };
106 constexpr int (*fpi)(int) = L;
107 static_assert(fpi(3) == 3);
__anonf2a229c90d02(auto a) 108 auto GL = [](auto a) { return a; };
109 
110 constexpr char (*fp2)(char) = GL;
111 constexpr double (*fp3)(double) = GL;
112 constexpr const char* (*fp4)(const char*) = GL;
113 static_assert(fp2('3') == '3');
114 static_assert(fp3(3.14) == 3.14);
115 constexpr const char *Str = "abc";
116 static_assert(fp4(Str) == Str);
117 
__anonf2a229c90e02(int i) 118 auto NCL = [](int i) { static int j; return j; }; //expected-note{{declared here}}
119 constexpr int (*fp5)(int) = NCL;
120 constexpr int I =  //expected-error{{must be initialized by a constant expression}}
121                   fp5(5); //expected-note{{non-constexpr function}}
122 
123 namespace test_dont_always_instantiate_constexpr_templates {
124 
__anonf2a229c90f02(auto x) 125 auto explicit_return_type = [](auto x) -> int { return x.get(); };
126 decltype(explicit_return_type(0)) c;  // OK
127 
__anonf2a229c91002(auto x) 128 auto deduced_return_type = [](auto x) { return x.get(); }; //expected-error{{not a structure or union}}
129 decltype(deduced_return_type(0)) d;  //expected-note{{requested here}}
130 
131 
132 
133 } // end ns test_dont_always_instantiate_constexpr_templates
134 } // end ns1
135 
136 } // end ns test_conversion_function_for_non_capturing_lambdas
137 
138 namespace test_lambda_is_cce {
139 namespace ns1_simple_lambda {
140 
141 namespace ns0 {
__anonf2a229c91102(auto a) 142 constexpr int I = [](auto a) { return a; }(10);
143 
144 static_assert(I == 10);
__anonf2a229c91202(auto a) 145 static_assert(10 == [](auto a) { return a; }(10));
__anonf2a229c91302(auto a) 146 static_assert(3.14 == [](auto a) { return a; }(3.14));
147 
148 } //end ns0
149 
150 namespace ns1 {
f(int i)151 constexpr auto f(int i) {
152   double d = 3.14;
153   auto L = [=](auto a) {
154     int Isz = sizeof(i);
155     return sizeof(i) + sizeof(a) + sizeof(d);
156   };
157   int I = L("abc") + L(nullptr);
158   return L;
159 }
160 constexpr auto L = f(3);
161 constexpr auto M =  L("abc") + L(nullptr);
162 
163 static_assert(M == sizeof(int) * 2 + sizeof(double) * 2 + sizeof(nullptr) + sizeof(const char*));
164 
165 } // end ns1
166 
167 namespace ns2 {
f(int i)168 constexpr auto f(int i) {
169   auto L = [](auto a) { return a + a; };
170   return L;
171 }
172 constexpr auto L = f(3);
173 constexpr int I = L(6);
174 static_assert(I == 12);
175 } // end ns2
176 
177 namespace contained_lambdas_call_operator_is_not_constexpr {
f(int i)178 constexpr auto f(int i) {
179   double d = 3.14;
180   auto L = [=](auto a) {
181     int Isz = sizeof(i);
182     asm("hello");
183     return sizeof(i) + sizeof(a) + sizeof(d);
184   };
185   return L;
186 }
187 
188 constexpr auto L = f(3);
189 
190 constexpr auto M =  // expected-error{{must be initialized by}}
191     L("abc");
192 #if __cpp_constexpr < 201907L
193 //expected-note@-2{{non-constexpr function}}
194 //expected-note@-14{{declared here}}
195 #else
196 //expected-note@-14{{subexpression not valid in a constant expression}}
197 //expected-note@-6{{in call to}}
198 #endif
199 } // end ns contained_lambdas_call_operator_is_not_constexpr
200 
201 
202 
203 } // end ns1_simple_lambda
204 
205 namespace test_captures_1 {
206 namespace ns1 {
f(int i)207 constexpr auto f(int i) {
208   struct S { int x; } s = { i * 2 };
209   auto L = [=](auto a) {
210     return i + s.x + a;
211   };
212   return L;
213 }
214 constexpr auto M = f(3);
215 
216 static_assert(M(10) == 19);
217 
218 } // end test_captures_1::ns1
219 
220 namespace ns2 {
221 
foo(int n)222 constexpr auto foo(int n) {
223   auto L = [i = n] (auto N) mutable {
224     if (!N(i)) throw "error";
225     return [&i] {
226       return ++i;
227     };
228   };
229   auto M = L([n](int p) { return p == n; });
230   M(); M();
231   L([n](int p) { return p == n + 2; });
232 
233   return L;
234 }
235 
236 constexpr auto L = foo(3);
237 
238 } // end test_captures_1::ns2
239 namespace ns3 {
240 
foo(int n)241 constexpr auto foo(int n) {
242   auto L = [i = n] (auto N) mutable {
243     if (!N(i)) throw "error";
244     return [&i] {
245       return [i]() mutable {
246         return ++i;
247       };
248     };
249   };
250   auto M = L([n](int p) { return p == n; });
251   M()(); M()();
252   L([n](int p) { return p == n; });
253 
254   return L;
255 }
256 
257 constexpr auto L = foo(3);
258 } // end test_captures_1::ns3
259 
260 namespace ns2_capture_this_byval {
261 struct S {
262   int s;
Stest_lambda_is_cce::test_captures_1::ns2_capture_this_byval::S263   constexpr S(int s) : s{s} { }
ftest_lambda_is_cce::test_captures_1::ns2_capture_this_byval::S264   constexpr auto f(S o) {
265     return [*this,o] (auto a) { return s + o.s + a.s; };
266   }
267 };
268 
269 constexpr auto L = S{5}.f(S{10});
270 static_assert(L(S{100}) == 115);
271 } // end test_captures_1::ns2_capture_this_byval
272 
273 namespace ns2_capture_this_byref {
274 
275 struct S {
276   int s;
Stest_lambda_is_cce::test_captures_1::ns2_capture_this_byref::S277   constexpr S(int s) : s{s} { }
ftest_lambda_is_cce::test_captures_1::ns2_capture_this_byref::S278   constexpr auto f() const {
279     return [this] { return s; };
280   }
281 };
282 
283 constexpr S SObj{5};
284 constexpr auto L = SObj.f();
285 constexpr int I = L();
286 static_assert(I == 5);
287 
288 } // end ns2_capture_this_byref
289 
290 } // end test_captures_1
291 
292 namespace test_capture_array {
293 namespace ns1 {
f(int I)294 constexpr auto f(int I) {
295   int arr[] = { I, I *2, I * 3 };
296   auto L1 = [&] (auto a) { return arr[a]; };
297   int r = L1(2);
298   struct X { int x, y; };
299   return [=](auto a) { return X{arr[a],r}; };
300 }
301 constexpr auto L = f(3);
302 static_assert(L(0).x == 3);
303 static_assert(L(0).y == 9);
304 static_assert(L(1).x == 6);
305 static_assert(L(1).y == 9);
306 } // end ns1
307 
308 } // end test_capture_array
309 namespace ns1_test_lvalue_type {
f()310   void f() {
311     volatile int n;
312     constexpr bool B = [&]{ return &n; }() == &n; // should be accepted
313   }
314 } // end ns1_unimplemented
315 
316 } // end ns test_lambda_is_cce
317 
318 namespace PR36054 {
fn()319 constexpr int fn() {
320   int Capture = 42;
321   return [=]() constexpr { return Capture; }();
322 }
323 
324 static_assert(fn() == 42, "");
325 
326 template <class T>
tfn()327 constexpr int tfn() {
328   int Capture = 42;
329   return [=]() constexpr { return Capture; }();
330 }
331 
332 static_assert(tfn<int>() == 42, "");
333 
gfn()334 constexpr int gfn() {
335   int Capture = 42;
336   return [=](auto P) constexpr { return Capture + P; }(58);
337 }
338 
339 static_assert(gfn() == 100, "");
340 
OtherCaptures()341 constexpr bool OtherCaptures() {
342   int Capture = 42;
343   constexpr auto Outer = [](auto P) constexpr { return 42 + P; };
344   auto Inner = [&](auto O) constexpr { return O(58) + Capture; };
345   return Inner(Outer) == 142;
346 }
347 
348 static_assert(OtherCaptures(), "");
349 } // namespace PR36054
350 
351 #endif // ndef CPP14_AND_EARLIER
352