Clang Project

clang_source_code/test/SemaCXX/cxx1z-constexpr-lambdas.cpp
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
6namespace 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
11auto L = [] { };
12constexpr int foo(decltype(L) l) { return 0; }
13
14}
15
16#ifndef CPP14_AND_EARLIER
17namespace test_constexpr_checking {
18
19namespace 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
24namespace 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
30namespace test_constexpr_call {
31
32namespace ns1 {
33  auto L = [](int I) { return I; };
34  static_assert(L(3) == 3);
35} // end ns1
36namespace ns2 {
37  auto L = [](auto a) { return a; };
38  static_assert(L(3) == 3);
39  static_assert(L(3.14) == 3.14);
40}
41namespace 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
49namespace test_captureless_lambda {
50void f() {
51  const char c = 'c';
52  auto L = [] { return c; };
53  constexpr char C = L();
54}
55  
56void 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
63namespace test_conversion_function_for_non_capturing_lambdas {
64
65namespace ns1 {
66auto L = [](int i) { return i; };
67constexpr int (*fpi)(int) = L;
68static_assert(fpi(3) == 3);
69auto GL = [](auto a) { return a; };
70
71constexpr char (*fp2)(char) = GL;
72constexpr double (*fp3)(double) = GL;
73constexpr const char* (*fp4)(const char*) = GL;
74static_assert(fp2('3') == '3');
75static_assert(fp3(3.14) == 3.14);
76constexpr const char *Str = "abc";
77static_assert(fp4(Str) == Str);
78
79auto NCL = [](int i) { static int j; return j; }; //expected-note{{declared here}}
80constexpr int (*fp5)(int) = NCL;
81constexpr int I =  //expected-error{{must be initialized by a constant expression}}
82                  fp5(5); //expected-note{{non-constexpr function}} 
83
84namespace test_dont_always_instantiate_constexpr_templates {
85
86auto explicit_return_type = [](auto x) -> int { return x.get(); };
87decltype(explicit_return_type(0)) c;  // OK
88
89auto deduced_return_type = [](auto x) { return x.get(); }; //expected-error{{not a structure or union}}
90decltype(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
99namespace test_lambda_is_cce {
100namespace ns1_simple_lambda {
101
102namespace ns0 {
103constexpr int I = [](auto a) { return a; }(10);
104
105static_assert(I == 10); 
106static_assert(10 == [](auto a) { return a; }(10));
107static_assert(3.14 == [](auto a) { return a; }(3.14));
108
109} //end ns0
110
111namespace ns1 {
112constexpr 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}
121constexpr auto L = f(3);
122constexpr auto M =  L("abc") + L(nullptr);
123
124static_assert(M == sizeof(int) * 2 + sizeof(double) * 2 + sizeof(nullptr) + sizeof(const char*));
125
126} // end ns1
127
128namespace ns2 {
129constexpr auto f(int i) {
130  auto L = [](auto a) { return a + a; };
131  return L;
132}
133constexpr auto L = f(3);
134constexpr int I = L(6);
135static_assert(I == 12);
136} // end ns2
137
138namespace contained_lambdas_call_operator_is_not_constexpr {
139constexpr 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
149constexpr auto L = f(3);
150
151constexpr 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
160namespace test_captures_1 {
161namespace ns1 {
162constexpr 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}
169constexpr auto M = f(3);  
170
171static_assert(M(10) == 19);
172
173} // end test_captures_1::ns1
174
175namespace ns2 {
176
177constexpr 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
191constexpr auto L = foo(3);
192
193} // end test_captures_1::ns2
194namespace ns3 {
195
196constexpr 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
212constexpr auto L = foo(3);
213} // end test_captures_1::ns3
214
215namespace ns2_capture_this_byval {
216struct 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
224constexpr auto L = S{5}.f(S{10});
225static_assert(L(S{100}) == 115);
226} // end test_captures_1::ns2_capture_this_byval
227
228namespace ns2_capture_this_byref {
229
230struct S {
231  int s;
232  constexpr S(int s) : s{s} { }
233  constexpr auto f() const {
234    return [this] { return s; };
235  }
236};
237
238constexpr S SObj{5};
239constexpr auto L = SObj.f();
240constexpr int I = L();
241static_assert(I == 5);
242
243} // end ns2_capture_this_byref
244
245} // end test_captures_1
246
247namespace test_capture_array {
248namespace ns1 {
249constexpr 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}
256constexpr auto L = f(3);
257static_assert(L(0).x == 3);
258static_assert(L(0).y == 9);
259static_assert(L(1).x == 6);
260static_assert(L(1).y == 9);
261} // end ns1
262
263} // end test_capture_array
264namespace 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
273namespace PR36054 {
274constexpr int fn() {
275  int Capture = 42;
276  return [=]() constexpr { return Capture; }();
277}
278
279static_assert(fn() == 42, "");
280
281template <class T>
282constexpr int tfn() {
283  int Capture = 42;
284  return [=]() constexpr { return Capture; }();
285}
286
287static_assert(tfn<int>() == 42, "");
288
289constexpr int gfn() {
290  int Capture = 42;
291  return [=](auto P) constexpr { return Capture + P; }(58);
292}
293
294static_assert(gfn() == 100, "");
295
296constexpr 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
303static_assert(OtherCaptures(), "");
304} // namespace PR36054
305
306#endif // ndef CPP14_AND_EARLIER
307