Debian Codegraph @

clang/test/SemaCXX/lambda-unevaluated.cpp

raw
1 // RUN: %clang_cc1 -std=c++20 %s -Wno-c++23-extensions -verify
2 // RUN: %clang_cc1 -std=c++23 %s -verify
3
4
5 template <auto> struct Nothing {};
6 Nothing<[]() { return 0; }()> nothing;
7
8 template <typename> struct NothingT {};
9 Nothing<[]() { return 0; }> nothingT;
10
11 template <typename T>
12 concept True = [] { return true; }();
13 static_assert(True<int>);
14
15 static_assert(sizeof([] { return 0; }));
16 static_assert(sizeof([] { return 0; }()));
17
18 void f() noexcept(noexcept([] { return 0; }()));
19
20 using a = decltype([] { return 0; });
21 using b = decltype([] { return 0; }());
22 using c = decltype([]() noexcept(noexcept([] { return 0; }())) { return 0; });
23 using d = decltype(sizeof([] { return 0; }));
24
25 template <auto T>
26 int unique_test1();
27 static_assert(&unique_test1<[](){}> != &unique_test1<[](){}>);
28
29 template <class T>
30 auto g(T) -> decltype([]() { T::invalid; } ());
31 auto e = g(0); // expected-error@-1{{type 'int' cannot be used prior to '::'}}
32 // expected-note@-1{{while substituting deduced template}}
33 // expected-note@-3{{while substituting into a lambda}}
34 // expected-error@-3 {{no matching function for call to 'g'}}
35 // expected-note@-5 {{substitution failure}}
36
37 template <typename T>
38 auto foo(decltype([] {
39 return [] { return T(); }();
40 })) {}
41
42 void test() {
43 foo<int>({});
44 }
45
46 template <typename T>
47 struct C {
48 template <typename U>
49 auto foo(decltype([] {
50 return [] { return T(); }();
51 })) {}
52 };
53
54 void test2() {
55 C<int>{}.foo<long>({});
56 }
57
58 namespace PR52073 {
59 // OK, these are distinct functions not redefinitions.
60 template<typename> void f(decltype([]{})) {} // expected-note {{candidate}}
61 template<typename> void f(decltype([]{})) {} // expected-note {{candidate}}
62 void use_f() { f<int>({}); } // expected-error {{ambiguous}}
63
64 // Same.
65 template<int N> void g(const char (*)[([]{ return N; })()]) {} // expected-note {{candidate}}
66 template<int N> void g(const char (*)[([]{ return N; })()]) {} // expected-note {{candidate}}
67 void use_g() { g<6>(&"hello"); } // expected-error {{ambiguous}}
68 }
69
70 namespace GH51416 {
71
72 template <class T>
73 struct A {
74 void spam(decltype([] {}));
75 };
76
77 template <class T>
78 void A<T>::spam(decltype([] {})) // expected-error{{out-of-line definition of 'spam' does not match}}
79 {}
80
81 struct B {
82 template <class T>
83 void spam(decltype([] {}));
84 };
85
86 template <class T>
87 void B::spam(decltype([] {})) {} // expected-error{{out-of-line definition of 'spam' does not match}}
88
89 } // namespace GH51416
90
91 namespace GH50376 {
92
93 template <typename T, typename Fn>
94 struct foo_t { // expected-note 2{{candidate constructor}}
95 foo_t(T ptr) {} // expected-note{{candidate constructor}}
96 };
97
98 template <typename T>
99 using alias = foo_t<T, decltype([](int) { return 0; })>;
100
101 template <typename T>
102 auto fun(T const &t) -> alias<T> {
103 return alias<T>{t}; // expected-error{{no viable conversion from returned value of type 'alias<...>'}}
104 }
105
106 void f() {
107 int i;
108 auto const error = fun(i); // expected-note{{in instantiation}}
109 }
110
111 } // namespace GH50376
112
113 namespace GH51414 {
114 template <class T> void spam(decltype([] {}) (*s)[sizeof(T)] = nullptr) {}
115 void foo() {
116 spam<int>();
117 }
118 } // namespace GH51414
119
120 namespace GH51641 {
121 template <class T>
122 void foo(decltype(+[](T) {}) lambda, T param);
123 static_assert(!__is_same(decltype(foo<int>), void));
124 } // namespace GH51641
125
126 namespace StaticLambdas {
127 template <auto> struct Nothing {};
128 Nothing<[]() static { return 0; }()> nothing;
129
130 template <typename> struct NothingT {};
131 Nothing<[]() static { return 0; }> nothingT;
132
133 template <typename T>
134 concept True = [] static { return true; }();
135 static_assert(True<int>);
136
137 static_assert(sizeof([] static { return 0; }));
138 static_assert(sizeof([] static { return 0; }()));
139
140 void f() noexcept(noexcept([] static { return 0; }()));
141
142 using a = decltype([] static { return 0; });
143 using b = decltype([] static { return 0; }());
144 using c = decltype([]() static noexcept(noexcept([] { return 0; }())) { return 0; });
145 using d = decltype(sizeof([] static { return 0; }));
146
147 }
148
149 namespace lambda_in_trailing_decltype {
150 auto x = ([](auto) -> decltype([] {}()) {}(0), 2);
151 }
152
153 namespace lambda_in_constraints {
154 struct WithFoo { static void foo(); };
155
156 template <class T>
157 concept lambda_works = requires {
158 []() { T::foo(); }; // expected-error{{type 'int' cannot be used prior to '::'}}
159 // expected-note@-1{{while substituting into a lambda expression here}}
160 // expected-note@-2{{in instantiation of requirement here}}
161 // expected-note@-4{{while substituting template arguments into constraint expression here}}
162 };
163
164 static_assert(!lambda_works<int>); // expected-note {{while checking the satisfaction of concept 'lambda_works<int>' requested here}}
165 static_assert(lambda_works<WithFoo>);
166
167 template <class T>
168 int* func(T) requires requires { []() { T::foo(); }; }; // expected-error{{type 'int' cannot be used prior to '::'}}
169 // expected-note@-1{{while substituting into a lambda expression here}}
170 // expected-note@-2{{in instantiation of requirement here}}
171 // expected-note@-3{{while substituting template arguments into constraint expression here}}
172 double* func(...);
173
174 static_assert(__is_same(decltype(func(0)), double*)); // expected-note {{while checking constraint satisfaction for template 'func<int>' required here}}
175 // expected-note@-1 {{in instantiation of function template specialization 'lambda_in_constraints::func<int>'}}
176 static_assert(__is_same(decltype(func(WithFoo())), int*));
177
178 template <class T>
179 auto direct_lambda(T) -> decltype([] { T::foo(); }) {}
180 void direct_lambda(...) {}
181
182 void recursive() {
183 direct_lambda(0); // expected-error@-4 {{type 'int' cannot be used prior to '::'}}
184 // expected-note@-1 {{while substituting deduced template arguments}}
185 // expected-note@-6 {{while substituting into a lambda}}
186 bool x = requires { direct_lambda(0); }; // expected-error@-7 {{type 'int' cannot be used prior to '::'}}
187 // expected-note@-1 {{while substituting deduced template arguments}}
188 // expected-note@-9 {{while substituting into a lambda}}
189
190 }
191 }
192
193 // GH63845: Test if we have skipped past RequiresExprBodyDecls in tryCaptureVariable().
194 namespace GH63845 {
195
196 template <bool> struct A {};
197
198 struct true_type {
199 constexpr operator bool() noexcept { return true; }
200 };
201
202 constexpr bool foo() {
203 true_type x{};
204 return requires { typename A<x>; };
205 }
206
207 static_assert(foo());
208
209 } // namespace GH63845
210
211 // GH69307: Test if we can correctly handle param decls that have yet to get into the function scope.
212 namespace GH69307 {
213
214 constexpr auto ICE() {
215 constexpr auto b = 1;
216 return [=](auto c) -> int
217 requires requires { b + c; }
218 { return 1; };
219 };
220
221 constexpr auto Ret = ICE()(1);
222
223 } // namespace GH69307
224
225 // GH88081: Test if we evaluate the requires expression with lambda captures properly.
226 namespace GH88081 {
227
228 // Test that ActOnLambdaClosureQualifiers() is called only once.
229 void foo(auto value)
230 requires requires { [&] -> decltype(value) {}; }
231 // expected-error@-1 {{non-local lambda expression cannot have a capture-default}}
232 {}
233
234 struct S { //#S
235 S(auto value) //#S-ctor
236 requires requires { [&] -> decltype(value) { return 2; }; } {} // #S-requires
237
238 static auto foo(auto value) -> decltype([&]() -> decltype(value) {}()) { return {}; } // #S-foo
239
240 // FIXME: 'value' does not constitute an ODR use here. Add a diagnostic for it.
241 static auto bar(auto value) -> decltype([&] { return value; }()) {
242 return "a"; // #bar-body
243 }
244 };
245
246 S s("a"); // #use
247 // expected-error@#S-requires {{cannot initialize return object of type 'decltype(value)' (aka 'const char *') with an rvalue of type 'int'}}
248 // expected-error@#use {{no matching constructor}}
249 // expected-note@#S-requires {{substituting into a lambda expression here}}
250 // expected-note@#S-requires {{substituting template arguments into constraint expression here}}
251 // expected-note@#S-requires {{in instantiation of requirement here}}
252 // expected-note@#use {{checking constraint satisfaction for template 'S<const char *>' required here}}
253 // expected-note@#use {{requested here}}
254 // expected-note-re@#S 2{{candidate constructor {{.*}} not viable}}
255 // expected-note@#S-ctor {{constraints not satisfied}}
256 // expected-note-re@#S-requires {{because {{.*}} would be invalid}}
257
258 void func() {
259 S::foo(42);
260 S::bar("str");
261 S::bar(0.618);
262 // expected-error-re@#bar-body {{cannot initialize return object of type {{.*}} (aka 'double') with an lvalue of type 'const char[2]'}}
263 // expected-note@-2 {{requested here}}
264 }
265
266 } // namespace GH88081