Debian Codegraph @

clang/test/AST/Interp/lambda.cpp

raw
1 // RUN: %clang_cc1 -fexperimental-new-constant-interpreter -verify=expected,both -std=c++20 %s
2 // RUN: %clang_cc1 -verify=ref,both -std=c++20 %s
3
4 constexpr int a = 12;
5 constexpr int f = [c = a]() { return c; }();
6 static_assert(f == a);
7
8
9 constexpr int inc() {
10 int a = 10;
11 auto f = [&a]() {
12 ++a;
13 };
14
15 f();f();
16
17 return a;
18 }
19 static_assert(inc() == 12);
20
21 constexpr int add(int a, int b) {
22 auto doIt = [a, b](int c) {
23 return a + b + c;
24 };
25
26 return doIt(2);
27 }
28 static_assert(add(4, 5) == 11);
29
30
31 constexpr int add2(int a, int b) {
32 auto doIt = [a, b](int c) {
33 auto bar = [a]() { return a; };
34 auto bar2 = [b]() { return b; };
35
36 return bar() + bar2() + c;
37 };
38
39 return doIt(2);
40 }
41 static_assert(add2(4, 5) == 11);
42
43
44 constexpr int div(int a, int b) {
45 auto f = [=]() {
46 return a / b; // both-note {{division by zero}}
47 };
48
49 return f(); // expected-note {{in call to '&f->operator()()'}} \
50 // ref-note {{in call to 'f.operator()()'}}
51 }
52 static_assert(div(8, 2) == 4);
53 static_assert(div(8, 0) == 4); // both-error {{not an integral constant expression}} \
54 // both-note {{in call to 'div(8, 0)'}}
55
56
57 struct F {
58 float f;
59 };
60
61 constexpr float captureStruct() {
62 F someF = {1.0};
63
64 auto p = [someF]() {
65 return someF.f;
66 };
67
68 return p();
69 }
70
71 static_assert(captureStruct() == 1.0);
72
73
74 int constexpr FunCase() {
75 return [x = 10] {
76 decltype(x) y; // type int b/c not odr use
77 // refers to original init-capture
78 auto &z = x; // type const int & b/c odr use
79 // refers to lambdas copy of x
80 y = 10; // Ok
81 //z = 10; // Ill-formed
82 return y;
83 }();
84 }
85
86 constexpr int WC = FunCase();
87
88
89 namespace LambdaParams {
90 template<typename T>
91 constexpr void callThis(T t) {
92 return t();
93 }
94
95 constexpr int foo() {
96 int a = 0;
97 auto f = [&a]() { ++a; };
98
99 callThis(f);
100
101 return a;
102 }
103 static_assert(foo() == 1);
104 }
105
106 namespace StaticInvoker {
107 constexpr int sv1(int i) {
108 auto l = []() { return 12; };
109 int (*fp)() = l;
110 return fp();
111 }
112 static_assert(sv1(12) == 12);
113
114 constexpr int sv2(int i) {
115 auto l = [](int m, float f, void *A) { return m; };
116 int (*fp)(int, float, void*) = l;
117 return fp(i, 4.0f, nullptr);
118 }
119 static_assert(sv2(12) == 12);
120
121 constexpr int sv3(int i) {
122 auto l = [](int m, const int &n) { return m; };
123 int (*fp)(int, const int &) = l;
124 return fp(i, 3);
125 }
126 static_assert(sv3(12) == 12);
127
128 constexpr int sv4(int i) {
129 auto l = [](int &m) { return m; };
130 int (*fp)(int&) = l;
131 return fp(i);
132 }
133 static_assert(sv4(12) == 12);
134
135 constexpr int sv5(int i) {
136 struct F { int a; float f; };
137 auto l = [](int m, F f) { return m; };
138 int (*fp)(int, F) = l;
139 return fp(i, F{12, 14.0});
140 }
141 static_assert(sv5(12) == 12);
142
143 constexpr int sv6(int i) {
144 struct F { int a;
145 constexpr F(int a) : a(a) {}
146 };
147
148 auto l = [](int m) { return F(12); };
149 F (*fp)(int) = l;
150 F f = fp(i);
151
152 return fp(i).a;
153 }
154 static_assert(sv6(12) == 12);
155
156
157 /// A generic lambda.
158 auto GL = [](auto a) { return a; };
159 constexpr char (*fp2)(char) = GL;
160 static_assert(fp2('3') == '3', "");
161
162 struct GLS {
163 int a;
164 };
165 auto GL2 = [](auto a) { return GLS{a}; };
166 constexpr GLS (*fp3)(char) = GL2;
167 static_assert(fp3('3').a == '3', "");
168 }
169
170 namespace LambdasAsParams {
171 template<typename F>
172 constexpr auto call(F f) {
173 return f();
174 }
175 static_assert(call([](){ return 1;}) == 1);
176 static_assert(call([](){ return 2;}) == 2);
177
178
179 constexpr unsigned L = call([](){ return 12;});
180 static_assert(L == 12);
181
182
183 constexpr float heh() {
184 auto a = []() {
185 return 1.0;
186 };
187
188 return static_cast<float>(a());
189 }
190 static_assert(heh() == 1.0);
191 }
192
193 namespace ThisCapture {
194 class Foo {
195 public:
196 int b = 32;
197 int a;
198
199 constexpr Foo() : a([this](){ return b + 1;}()) {}
200
201 constexpr int Aplus2() const {
202 auto F = [this]() {
203 return a + 2;
204 };
205
206 return F();
207 }
208 };
209 constexpr Foo F;
210 static_assert(F.a == 33, "");
211 static_assert(F.Aplus2() == (33 + 2), "");
212 }
213
214 namespace GH62611 {
215 template <auto A = [](auto x){}>
216 struct C {
217 static constexpr auto B = A;
218 };
219
220 int test() {
221 C<>::B(42);
222 return 0;
223 }
224 }
225
226 namespace LambdaToAPValue {
227 void wrapper() {
228 constexpr auto f = []() constexpr {
229 return 0;
230 };
231
232 constexpr auto g = [f]() constexpr {
233 return f();
234 };
235 static_assert(g() == f(), "");
236 }
237 }
238
239 namespace ns2_capture_this_byval {
240 struct S {
241 int s;
242 constexpr S(int s) : s{s} { }
243 constexpr auto f(S o) {
244 return [*this,o] (auto a) { return s + o.s + a.s; };
245 }
246 };
247
248 constexpr auto L = S{5}.f(S{10});
249 static_assert(L(S{100}) == 115, "");
250 } // end test_captures_1::ns2_capture_this_byval
251
252 namespace CaptureDefaults {
253 struct S {
254 int x;
255 };
256
257 constexpr auto f = [x = S{10}]() {
258 return x.x;
259 };
260 static_assert(f() == 10, "");
261
262 constexpr auto f2 = [x = 3]() {
263 return x;
264 };
265 static_assert(f2() == 3, "");
266 }
267
268 constexpr auto t4 = ([x=42]() consteval { return x; }());
269 static_assert(t4 == 42, "");
270
271 namespace InvalidCapture {
272
273 int &f(int *p);
274 char &f(...);
275 void g() {
276 int n = -1; // both-note {{declared here}}
277 [=] {
278 int arr[n]; // both-warning {{variable length arrays in C++ are a Clang extension}} \
279 both-note {{read of non-const variable 'n' is not allowed in a constant expression}}
280 } ();
281 }
282 }
283
284 constexpr int fn() {
285 int Capture = 42;
286 return [=]() constexpr { return Capture; }();
287 }
288 static_assert(fn() == 42, "");