| 1 |
// RUN: %clang_cc1 -std=c++11 -Wno-unused-value -fsyntax-only -verify=expected,not-cxx03,cxx03-cxx11,cxx11,expected-cxx14 -fblocks %s |
| 2 |
// RUN: %clang_cc1 -std=c++03 -Wno-unused-value -fsyntax-only -verify=expected,cxx03,cxx03-cxx11,expected-cxx14 -fblocks %s -Ddecltype=__decltype -Dstatic_assert=_Static_assert -Wno-c++11-extensions |
| 3 |
// RUN: %clang_cc1 -std=c++14 -Wno-unused-value -fsyntax-only -verify=expected,not-cxx03,expected-cxx14 -fblocks %s |
| 4 |
// RUN: %clang_cc1 -std=c++17 -Wno-unused-value -verify=expected,not-cxx03 -ast-dump -fblocks %s | FileCheck %s |
| 5 |
|
| 6 |
// RUN: %clang_cc1 -std=c++11 -Wno-unused-value -fsyntax-only -verify=expected,not-cxx03,cxx03-cxx11,cxx11,expected-cxx14 -fblocks %s -fexperimental-new-constant-interpreter |
| 7 |
// RUN: %clang_cc1 -std=c++03 -Wno-unused-value -fsyntax-only -verify=expected,cxx03,cxx03-cxx11,expected-cxx14 -fblocks %s -Ddecltype=__decltype -Dstatic_assert=_Static_assert -Wno-c++11-extensions -fexperimental-new-constant-interpreter |
| 8 |
// RUN: %clang_cc1 -std=c++14 -Wno-unused-value -fsyntax-only -verify=expected,not-cxx03,expected-cxx14 -fblocks %s -fexperimental-new-constant-interpreter |
| 9 |
// RUN: %clang_cc1 -std=c++17 -Wno-unused-value -verify=expected,not-cxx03 -ast-dump -fblocks %s -fexperimental-new-constant-interpreter| FileCheck %s |
| 10 |
|
| 11 |
namespace std { class type_info; }; |
| 12 |
|
| 13 |
namespace ExplicitCapture { |
| 14 |
class C { |
| 15 |
int Member; |
| 16 |
|
| 17 |
static void Overload(int); |
| 18 |
void Overload(); |
| 19 |
virtual C& Overload(float); |
| 20 |
|
| 21 |
void ImplicitThisCapture() { |
| 22 |
[]() { (void)Member; }; // expected-error {{'this' cannot be implicitly captured in this context}} expected-note {{explicitly capture 'this'}} |
| 23 |
const int var = []() {(void)Member; return 0; }(); // expected-error {{'this' cannot be implicitly captured in this context}} expected-note {{explicitly capture 'this'}} |
| 24 |
[&](){(void)Member;}; |
| 25 |
|
| 26 |
[this](){(void)Member;}; |
| 27 |
[this]{[this]{};}; |
| 28 |
[]{[this]{};};// expected-error {{'this' cannot be implicitly captured in this context}} |
| 29 |
[]{Overload(3);}; |
| 30 |
[] { Overload(); }; // expected-error {{'this' cannot be implicitly captured in this context}} expected-note {{explicitly capture 'this'}} |
| 31 |
[]{(void)typeid(Overload());}; |
| 32 |
[] { (void)typeid(Overload(.5f)); }; // expected-error {{'this' cannot be implicitly captured in this context}} expected-note {{explicitly capture 'this'}} |
| 33 |
} |
| 34 |
}; |
| 35 |
|
| 36 |
void f() { |
| 37 |
[this] () {}; // expected-error {{'this' cannot be captured in this context}} |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
namespace ReturnDeduction { |
| 42 |
void test() { |
| 43 |
[](){ return 1; }; |
| 44 |
[](){ return 1; }; |
| 45 |
[](){ return ({return 1; 1;}); }; |
| 46 |
[](){ return ({return 'c'; 1;}); }; // expected-error {{must match previous return type}} |
| 47 |
[]()->int{ return 'c'; return 1; }; |
| 48 |
[](){ return 'c'; return 1; }; // expected-error {{must match previous return type}} |
| 49 |
[]() { return; return (void)0; }; |
| 50 |
[](){ return 1; return 1; }; |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
namespace ImplicitCapture { |
| 55 |
void test() { |
| 56 |
int a = 0; // expected-note 5 {{declared}} |
| 57 |
[]() { return a; }; // expected-error {{variable 'a' cannot be implicitly captured in a lambda with no capture-default specified}} expected-note {{begins here}} expected-note 2 {{capture 'a' by}} expected-note 2 {{default capture by}} |
| 58 |
[&]() { return a; }; |
| 59 |
[=]() { return a; }; |
| 60 |
[=]() { int* b = &a; }; // expected-error {{cannot initialize a variable of type 'int *' with an rvalue of type 'const int *'}} |
| 61 |
[=]() { return [&]() { return a; }; }; |
| 62 |
[]() { return [&]() { return a; }; }; // expected-error {{variable 'a' cannot be implicitly captured in a lambda with no capture-default specified}} expected-note {{lambda expression begins here}} expected-note 2 {{capture 'a' by}} expected-note 2 {{default capture by}} |
| 63 |
[]() { return ^{ return a; }; };// expected-error {{variable 'a' cannot be implicitly captured in a lambda with no capture-default specified}} expected-note {{lambda expression begins here}} expected-note 2 {{capture 'a' by}} expected-note 2 {{default capture by}} |
| 64 |
[]() { return [&a] { return a; }; }; // expected-error 2 {{variable 'a' cannot be implicitly captured in a lambda with no capture-default specified}} expected-note 2 {{lambda expression begins here}} expected-note 4 {{capture 'a' by}} expected-note 4 {{default capture by}} |
| 65 |
[=]() { return [&a] { return a; }; }; // |
| 66 |
|
| 67 |
const int b = 2; |
| 68 |
[]() { return b; }; |
| 69 |
|
| 70 |
union { // expected-note {{declared}} |
| 71 |
int c; |
| 72 |
float d; |
| 73 |
}; |
| 74 |
d = 3; |
| 75 |
[=]() { return c; }; // expected-error {{unnamed variable cannot be implicitly captured in a lambda expression}} |
| 76 |
|
| 77 |
__block int e; // expected-note 2{{declared}} |
| 78 |
[&]() { return e; }; // expected-error {{__block variable 'e' cannot be captured in a lambda expression}} |
| 79 |
[&e]() { return e; }; // expected-error {{__block variable 'e' cannot be captured in a lambda expression}} |
| 80 |
|
| 81 |
int f[10]; // expected-note {{declared}} |
| 82 |
[&]() { return f[2]; }; |
| 83 |
(void) ^{ return []() { return f[2]; }; }; // expected-error {{variable 'f' cannot be implicitly captured in a lambda with no capture-default specified}} \ |
| 84 |
// expected-note{{lambda expression begins here}} expected-note 2 {{capture 'f' by}} expected-note 2 {{default capture by}} |
| 85 |
|
| 86 |
struct G { G(); G(G&); int a; }; // expected-note 6 {{not viable}} |
| 87 |
G g; |
| 88 |
[=]() { const G* gg = &g; return gg->a; }; |
| 89 |
[=]() { return [=]{ const G* gg = &g; return gg->a; }(); }; // expected-error {{no matching constructor for initialization of 'G'}} |
| 90 |
(void)^{ return [=]{ const G* gg = &g; return gg->a; }(); }; // expected-error 2 {{no matching constructor for initialization of 'const G'}} |
| 91 |
|
| 92 |
const int h = a; // expected-note {{declared}} |
| 93 |
[]() { return h; }; // expected-error {{variable 'h' cannot be implicitly captured in a lambda with no capture-default specified}} expected-note {{lambda expression begins here}} expected-note 2 {{capture 'h' by}} expected-note 2 {{default capture by}} |
| 94 |
|
| 95 |
// References can appear in constant expressions if they are initialized by |
| 96 |
// reference constant expressions. |
| 97 |
int i; |
| 98 |
int &ref_i = i; // expected-note {{declared}} |
| 99 |
[] { return ref_i; }; // expected-error {{variable 'ref_i' cannot be implicitly captured in a lambda with no capture-default specified}} expected-note {{lambda expression begins here}} expected-note 2 {{capture 'ref_i' by}} expected-note 2 {{default capture by}} |
| 100 |
|
| 101 |
static int j; |
| 102 |
int &ref_j = j; // cxx03-note {{declared here}} |
| 103 |
[] { return ref_j; }; // cxx03-error {{variable 'ref_j' cannot be implicitly captured in a lambda with no capture-default specified}} cxx03-note 4 {{capture}} cxx03-note {{lambda expression begins here}} |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
namespace SpecialMembers { |
| 108 |
void f() { |
| 109 |
auto a = []{}; // expected-note 2{{here}} expected-note {{candidate}} not-cxx03-note {{candidate}} |
| 110 |
decltype(a) b; // expected-error {{no matching constructor}} |
| 111 |
decltype(a) c = a; |
| 112 |
decltype(a) d = static_cast<decltype(a)&&>(a); |
| 113 |
a = a; // expected-error {{copy assignment operator is implicitly deleted}} |
| 114 |
a = static_cast<decltype(a)&&>(a); // expected-error {{copy assignment operator is implicitly deleted}} |
| 115 |
} |
| 116 |
struct P { |
| 117 |
P(const P&) = delete; //expected-note {{deleted here}} // expected-cxx14-note {{deleted here}} |
| 118 |
}; |
| 119 |
struct Q { |
| 120 |
~Q() = delete; // expected-note {{deleted here}} |
| 121 |
}; |
| 122 |
struct R { |
| 123 |
R(const R&) = default; |
| 124 |
R(R&&) = delete; |
| 125 |
R &operator=(const R&) = delete; |
| 126 |
R &operator=(R&&) = delete; |
| 127 |
}; |
| 128 |
void g(P &p, Q &q, R &r) { |
| 129 |
// FIXME: The note attached to the second error here is just amazingly bad. |
| 130 |
auto pp = [p]{}; // expected-error {{deleted constructor}} expected-cxx14-error {{deleted copy constructor of '(lambda}} |
| 131 |
// expected-cxx14-note-re@-1 {{copy constructor of '(lambda at {{.*}})' is implicitly deleted because field '' has a deleted copy constructor}} |
| 132 |
auto qq = [q]{}; // expected-error {{deleted function}} expected-note {{because}} |
| 133 |
|
| 134 |
auto a = [r]{}; // expected-note 2{{here}} |
| 135 |
decltype(a) b = a; |
| 136 |
decltype(a) c = static_cast<decltype(a)&&>(a); // ok, copies R |
| 137 |
a = a; // expected-error {{copy assignment operator is implicitly deleted}} |
| 138 |
a = static_cast<decltype(a)&&>(a); // expected-error {{copy assignment operator is implicitly deleted}} |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
namespace PR12031 { |
| 143 |
struct X { |
| 144 |
template<typename T> |
| 145 |
X(const T&); |
| 146 |
~X(); |
| 147 |
}; |
| 148 |
|
| 149 |
void f(int i, X x); |
| 150 |
void g() { |
| 151 |
const int v = 10; |
| 152 |
f(v, [](){}); // cxx03-warning {{template argument uses local type}} \ |
| 153 |
// cxx03-note {{while substituting}} |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
namespace Array { |
| 158 |
int &f(int *p); |
| 159 |
char &f(...); |
| 160 |
void g() { |
| 161 |
int n = -1; // expected-note {{declared here}} |
| 162 |
[=] { |
| 163 |
int arr[n]; // expected-warning {{variable length arrays in C++ are a Clang extension}} \ |
| 164 |
expected-note {{read of non-const variable 'n' is not allowed in a constant expression}} |
| 165 |
} (); |
| 166 |
|
| 167 |
const int m = -1; |
| 168 |
[] { |
| 169 |
int arr[m]; // expected-error{{negative size}} |
| 170 |
} (); |
| 171 |
|
| 172 |
[&] { |
| 173 |
int arr[m]; // expected-error{{negative size}} |
| 174 |
} (); |
| 175 |
|
| 176 |
[=] { |
| 177 |
int arr[m]; // expected-error{{negative size}} |
| 178 |
} (); |
| 179 |
|
| 180 |
[m] { |
| 181 |
int arr[m]; // expected-error{{negative size}} |
| 182 |
} (); |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
void PR12248() |
| 187 |
{ |
| 188 |
unsigned int result = 0; |
| 189 |
auto l = [&]() { ++result; }; |
| 190 |
} |
| 191 |
|
| 192 |
namespace ModifyingCapture { |
| 193 |
void test() { |
| 194 |
int n = 0; |
| 195 |
[=] { |
| 196 |
n = 1; // expected-error {{cannot assign to a variable captured by copy in a non-mutable lambda}} |
| 197 |
}; |
| 198 |
const int cn = 0; |
| 199 |
// cxx03-cxx11-warning@+1 {{initialized lambda captures are a C++14 extension}} |
| 200 |
[&cnr = cn]{ // expected-note {{variable 'cnr' declared const here}} |
| 201 |
cnr = 1; // expected-error {{cannot assign to variable 'cnr' with const-qualified type 'const int &'}} |
| 202 |
}; |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
namespace VariadicPackExpansion { |
| 207 |
template<typename T, typename U> using Fst = T; |
| 208 |
template<typename...Ts> bool g(Fst<bool, Ts> ...bools); |
| 209 |
template<typename...Ts> bool f(Ts &&...ts) { |
| 210 |
return g<Ts...>([&ts] { |
| 211 |
if (!ts) |
| 212 |
return false; |
| 213 |
--ts; |
| 214 |
return true; |
| 215 |
} () ...); |
| 216 |
} |
| 217 |
void h() { |
| 218 |
int a = 5, b = 2, c = 3; |
| 219 |
while (f(a, b, c)) { |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
struct sink { |
| 224 |
template<typename...Ts> sink(Ts &&...) {} |
| 225 |
}; |
| 226 |
|
| 227 |
template<typename...Ts> void local_class() { |
| 228 |
sink s( |
| 229 |
[] (Ts t) { |
| 230 |
struct S : Ts { |
| 231 |
void f(Ts t) { |
| 232 |
Ts &that = *this; |
| 233 |
that = t; |
| 234 |
} |
| 235 |
Ts g() { return *this; }; |
| 236 |
}; |
| 237 |
S s; |
| 238 |
s.f(t); |
| 239 |
return s; |
| 240 |
} (Ts()).g() ... |
| 241 |
); |
| 242 |
}; |
| 243 |
struct X {}; struct Y {}; |
| 244 |
template void local_class<X, Y>(); |
| 245 |
|
| 246 |
template<typename...Ts> void nested(Ts ...ts) { |
| 247 |
f( |
| 248 |
// Each expansion of this lambda implicitly captures all of 'ts', because |
| 249 |
// the inner lambda also expands 'ts'. |
| 250 |
[&] { |
| 251 |
return ts + [&] { return f(ts...); } (); |
| 252 |
} () ... |
| 253 |
); |
| 254 |
} |
| 255 |
template void nested(int, int, int); |
| 256 |
|
| 257 |
template<typename...Ts> void nested2(Ts ...ts) { // expected-note 2{{here}} |
| 258 |
// Capture all 'ts', use only one. |
| 259 |
f([&ts...] { return ts; } ()...); |
| 260 |
// Capture each 'ts', use it. |
| 261 |
f([&ts] { return ts; } ()...); |
| 262 |
// Capture all 'ts', use all of them. |
| 263 |
f([&ts...] { return (int)f(ts...); } ()); |
| 264 |
// Capture each 'ts', use all of them. Ill-formed. In more detail: |
| 265 |
// |
| 266 |
// We instantiate two lambdas here; the first captures ts$0, the second |
| 267 |
// captures ts$1. Both of them reference both ts parameters, so both are |
| 268 |
// ill-formed because ts can't be implicitly captured. |
| 269 |
// |
| 270 |
// FIXME: This diagnostic does not explain what's happening. We should |
| 271 |
// specify which 'ts' we're referring to in its diagnostic name. We should |
| 272 |
// also say which slice of the pack expansion is being performed in the |
| 273 |
// instantiation backtrace. |
| 274 |
f([&ts] { return (int)f(ts...); } ()...); // \ |
| 275 |
// expected-error 2{{'ts' cannot be implicitly captured}} \ |
| 276 |
// expected-note 2{{lambda expression begins here}} \ |
| 277 |
// expected-note 4 {{capture 'ts' by}} \ |
| 278 |
// expected-note 2 {{while substituting into a lambda}} |
| 279 |
} |
| 280 |
template void nested2(int); // ok |
| 281 |
template void nested2(int, int); // expected-note 2 {{in instantiation of}} |
| 282 |
} |
| 283 |
|
| 284 |
namespace PR13860 { |
| 285 |
void foo() { |
| 286 |
auto x = PR13860UndeclaredIdentifier(); // expected-error {{use of undeclared identifier 'PR13860UndeclaredIdentifier'}} |
| 287 |
auto y = [x]() { }; |
| 288 |
static_assert(sizeof(y), ""); |
| 289 |
} |
| 290 |
} |
| 291 |
|
| 292 |
namespace PR13854 { |
| 293 |
auto l = [](void){}; |
| 294 |
} |
| 295 |
|
| 296 |
namespace PR14518 { |
| 297 |
auto f = [](void) { return __func__; }; // no-warning |
| 298 |
} |
| 299 |
|
| 300 |
namespace PR16708 { |
| 301 |
auto L = []() { |
| 302 |
auto ret = 0; |
| 303 |
return ret; |
| 304 |
return 0; |
| 305 |
}; |
| 306 |
} |
| 307 |
|
| 308 |
namespace TypeDeduction { |
| 309 |
struct S {}; |
| 310 |
void f() { |
| 311 |
const S s = S(); |
| 312 |
S &&t = [&] { return s; } (); |
| 313 |
#if __cplusplus > 201103L |
| 314 |
S &&u = [&] () -> auto { return s; } (); |
| 315 |
#endif |
| 316 |
} |
| 317 |
} |
| 318 |
|
| 319 |
|
| 320 |
namespace lambdas_in_NSDMIs { |
| 321 |
template<class T> |
| 322 |
struct L { |
| 323 |
T t = T(); |
| 324 |
T t2 = ([](int a) { return [](int b) { return b; };})(t)(t); |
| 325 |
}; |
| 326 |
L<int> l; |
| 327 |
|
| 328 |
namespace non_template { |
| 329 |
struct L { |
| 330 |
int t = 0; |
| 331 |
int t2 = ([](int a) { return [](int b) { return b; };})(t)(t); |
| 332 |
}; |
| 333 |
L l; |
| 334 |
} |
| 335 |
} |
| 336 |
|
| 337 |
// PR18477: don't try to capture 'this' from an NSDMI encountered while parsing |
| 338 |
// a lambda. |
| 339 |
namespace NSDMIs_in_lambdas { |
| 340 |
template<typename T> struct S { int a = 0; int b = a; }; |
| 341 |
void f() { []() { S<int> s; }; } |
| 342 |
|
| 343 |
auto x = []{ struct S { int n, m = n; }; }; |
| 344 |
auto y = [&]{ struct S { int n, m = n; }; }; // expected-error {{non-local lambda expression cannot have a capture-default}} |
| 345 |
void g() { auto z = [&]{ struct S { int n, m = n; }; }; } |
| 346 |
} |
| 347 |
|
| 348 |
namespace CaptureIncomplete { |
| 349 |
struct Incomplete; // expected-note 2{{forward decl}} |
| 350 |
void g(const Incomplete &a); |
| 351 |
void f(Incomplete &a) { |
| 352 |
(void) [a] {}; // expected-error {{incomplete}} |
| 353 |
(void) [&a] {}; |
| 354 |
|
| 355 |
(void) [=] { g(a); }; // expected-error {{incomplete}} |
| 356 |
(void) [&] { f(a); }; |
| 357 |
} |
| 358 |
} |
| 359 |
|
| 360 |
#if __cplusplus >= 201103L |
| 361 |
namespace CaptureAbstract { |
| 362 |
struct S { |
| 363 |
virtual void f() = 0; // expected-note {{unimplemented}} |
| 364 |
int n = 0; |
| 365 |
}; |
| 366 |
struct T : S { |
| 367 |
constexpr T() {} |
| 368 |
void f(); |
| 369 |
}; |
| 370 |
void f() { |
| 371 |
constexpr T t = T(); |
| 372 |
S &s = const_cast<T&>(t); |
| 373 |
// FIXME: Once we properly compute odr-use per DR712, this should be |
| 374 |
// accepted (and should not capture 's'). |
| 375 |
[=] { return s.n; }; // expected-error {{abstract}} |
| 376 |
} |
| 377 |
} |
| 378 |
#endif |
| 379 |
|
| 380 |
namespace PR18128 { |
| 381 |
auto l = [=]{}; // expected-error {{non-local lambda expression cannot have a capture-default}} |
| 382 |
|
| 383 |
struct S { |
| 384 |
int n; |
| 385 |
int (*f())[true ? 1 : ([=]{ return n; }(), 0)]; |
| 386 |
// expected-error@-1 {{non-local lambda expression cannot have a capture-default}} |
| 387 |
// expected-error@-2 {{invalid use of non-static data member 'n'}} |
| 388 |
// expected-cxx14-error@-3 {{a lambda expression may not appear inside of a constant expression}} |
| 389 |
// cxx03-error@-4 {{function declaration cannot have variably modified type}} |
| 390 |
// cxx03-warning@-5 {{variable length arrays in C++ are a Clang extension}} |
| 391 |
int g(int k = ([=]{ return n; }(), 0)); |
| 392 |
// expected-error@-1 {{non-local lambda expression cannot have a capture-default}} |
| 393 |
// expected-error@-2 {{invalid use of non-static data member 'n'}} |
| 394 |
|
| 395 |
int a = [=]{ return n; }(); // ok |
| 396 |
int b = [=]{ return [=]{ return n; }(); }(); // ok |
| 397 |
int c = []{ int k = 0; return [=]{ return k; }(); }(); // ok |
| 398 |
int d = [] { return [=] { return n; }(); }(); // expected-error {{'this' cannot be implicitly captured in this context}} expected-note {{explicitly capture 'this'}} |
| 399 |
}; |
| 400 |
} |
| 401 |
|
| 402 |
namespace gh67687 { |
| 403 |
struct S { |
| 404 |
int n; |
| 405 |
int a = (4, []() { return n; }()); // expected-error {{'this' cannot be implicitly captured in this context}} \ |
| 406 |
// expected-note {{explicitly capture 'this'}} |
| 407 |
}; |
| 408 |
} |
| 409 |
|
| 410 |
namespace PR18473 { |
| 411 |
template<typename T> void f() { |
| 412 |
T t(0); |
| 413 |
(void) [=]{ int n = t; }; // expected-error {{deleted}} |
| 414 |
} |
| 415 |
|
| 416 |
template void f<int>(); |
| 417 |
struct NoCopy { |
| 418 |
NoCopy(int); |
| 419 |
NoCopy(const NoCopy &) = delete; // expected-note {{deleted}} |
| 420 |
operator int() const; |
| 421 |
}; |
| 422 |
template void f<NoCopy>(); // expected-note {{instantiation}} |
| 423 |
} |
| 424 |
|
| 425 |
void PR19249() { |
| 426 |
auto x = [&x]{}; // expected-error {{cannot appear in its own init}} |
| 427 |
} |
| 428 |
|
| 429 |
namespace PR20731 { |
| 430 |
template <class L, int X = sizeof(L)> |
| 431 |
void Job(L l); |
| 432 |
|
| 433 |
template <typename... Args> |
| 434 |
void Logger(Args &&... args) { |
| 435 |
auto len = Invalid_Function((args)...); |
| 436 |
// expected-error@-1 {{use of undeclared identifier 'Invalid_Function'}} |
| 437 |
Job([len]() {}); |
| 438 |
} |
| 439 |
|
| 440 |
void GetMethod() { |
| 441 |
Logger(); |
| 442 |
// expected-note@-1 {{in instantiation of function template specialization 'PR20731::Logger<>' requested here}} |
| 443 |
} |
| 444 |
|
| 445 |
template <typename T> |
| 446 |
struct A { |
| 447 |
T t; |
| 448 |
// expected-error@-1 {{field has incomplete type 'void'}} |
| 449 |
}; |
| 450 |
|
| 451 |
template <typename F> |
| 452 |
void g(F f) { |
| 453 |
auto a = A<decltype(f())>(); |
| 454 |
// expected-note@-1 {{in instantiation of template class 'PR20731::A<void>' requested here}} |
| 455 |
auto xf = [a, f]() {}; |
| 456 |
int x = sizeof(xf); |
| 457 |
}; |
| 458 |
void f() { |
| 459 |
g([] {}); // cxx03-warning {{template argument uses local type}} |
| 460 |
// expected-note-re@-1 {{in instantiation of function template specialization 'PR20731::g<(lambda at {{.*}}>' requested here}} |
| 461 |
// cxx03-note@-2 {{while substituting deduced template arguments}} |
| 462 |
} |
| 463 |
|
| 464 |
template <class _Rp> struct function { |
| 465 |
template <class _Fp> |
| 466 |
function(_Fp) { |
| 467 |
static_assert(sizeof(_Fp) > 0, "Type must be complete."); |
| 468 |
} |
| 469 |
}; |
| 470 |
|
| 471 |
template <typename T> void p(T t) { |
| 472 |
auto l = some_undefined_function(t); |
| 473 |
// expected-error@-1 {{use of undeclared identifier 'some_undefined_function'}} |
| 474 |
function<void()>(([l]() {})); |
| 475 |
} |
| 476 |
void q() { p(0); } |
| 477 |
// expected-note@-1 {{in instantiation of function template specialization 'PR20731::p<int>' requested here}} |
| 478 |
} |
| 479 |
|
| 480 |
namespace lambda_in_default_mem_init { |
| 481 |
template<typename T> void f() { |
| 482 |
struct S { int n = []{ return 0; }(); }; |
| 483 |
} |
| 484 |
template void f<int>(); |
| 485 |
|
| 486 |
template<typename T> void g() { |
| 487 |
struct S { int n = [](int n){ return n; }(0); }; |
| 488 |
} |
| 489 |
template void g<int>(); |
| 490 |
} |
| 491 |
|
| 492 |
namespace error_in_transform_prototype { |
| 493 |
template<class T> |
| 494 |
void f(T t) { |
| 495 |
// expected-error@+2 {{type 'int' cannot be used prior to '::' because it has no members}} |
| 496 |
// expected-error@+1 {{no member named 'ns' in 'error_in_transform_prototype::S'}} |
| 497 |
auto x = [](typename T::ns::type &k) {}; |
| 498 |
} |
| 499 |
class S {}; |
| 500 |
void foo() { |
| 501 |
f(5); // expected-note {{requested here}} |
| 502 |
f(S()); // expected-note {{requested here}} |
| 503 |
} |
| 504 |
} |
| 505 |
|
| 506 |
namespace PR21857 { |
| 507 |
template<typename Fn> struct fun : Fn { |
| 508 |
fun() = default; |
| 509 |
using Fn::operator(); |
| 510 |
}; |
| 511 |
template<typename Fn> fun<Fn> wrap(Fn fn); // cxx03-warning {{template argument uses unnamed type}} |
| 512 |
auto x = wrap([](){}); // cxx03-warning {{template argument uses unnamed type}} cxx03-note 2 {{unnamed type used in template argument was declared here}} |
| 513 |
// cxx03-note@-1 {{while substituting deduced template arguments into function template}} |
| 514 |
} |
| 515 |
|
| 516 |
namespace PR13987 { |
| 517 |
class Enclosing { |
| 518 |
void Method(char c = []()->char { |
| 519 |
int d = []()->int { |
| 520 |
struct LocalClass { |
| 521 |
int Method() { return 0; } |
| 522 |
}; |
| 523 |
return 0; |
| 524 |
}(); |
| 525 |
return d; }() |
| 526 |
); |
| 527 |
}; |
| 528 |
} |
| 529 |
|
| 530 |
namespace PR23860 { |
| 531 |
template <class> struct A { |
| 532 |
void f(int x = []() { |
| 533 |
struct B { |
| 534 |
void g() {} |
| 535 |
}; |
| 536 |
return 0; |
| 537 |
}()); |
| 538 |
}; |
| 539 |
|
| 540 |
int main() { |
| 541 |
} |
| 542 |
|
| 543 |
A<int> a; |
| 544 |
} |
| 545 |
|
| 546 |
namespace rdar22032373 { |
| 547 |
void foo() { |
| 548 |
auto blk = [](bool b) { |
| 549 |
if (b) |
| 550 |
return undeclared_error; // expected-error {{use of undeclared identifier}} |
| 551 |
return 0; |
| 552 |
}; |
| 553 |
auto bar = []() { |
| 554 |
return undef(); // expected-error {{use of undeclared identifier}} |
| 555 |
return 0; // verify no init_conversion_failed diagnostic emitted. |
| 556 |
}; |
| 557 |
} |
| 558 |
} |
| 559 |
|
| 560 |
namespace nested_lambda { |
| 561 |
template <int N> |
| 562 |
class S {}; |
| 563 |
|
| 564 |
void foo() { |
| 565 |
const int num = 18; |
| 566 |
auto outer = []() { |
| 567 |
auto inner = [](S<num> &X) {}; |
| 568 |
}; |
| 569 |
} |
| 570 |
} |
| 571 |
|
| 572 |
namespace PR27994 { |
| 573 |
struct A { template <class T> A(T); }; |
| 574 |
|
| 575 |
template <class T> |
| 576 |
struct B { // #PR27994_B |
| 577 |
int x; |
| 578 |
A a = [&] { int y = x; }; // cxx03-warning {{template argument uses unnamed type}} \ |
| 579 |
// cxx03-note {{while substituting}} cxx03-note {{unnamed type used}} |
| 580 |
A b = [&] { [&] { [&] { int y = x; }; }; }; // cxx03-warning {{template argument uses unnamed type}} \ |
| 581 |
// cxx03-note {{while substituting}} cxx03-note {{unnamed type used}} |
| 582 |
A d = [&](auto param) { int y = x; }; // cxx03-cxx11-error {{'auto' not allowed in lambda parameter}} \ |
| 583 |
// cxx03-warning {{template argument uses unnamed type}} \ |
| 584 |
// cxx03-note {{while substituting}} cxx03-note {{unnamed type used}} |
| 585 |
A e = [&](auto param) { [&] { [&](auto param2) { int y = x; }; }; }; // cxx03-cxx11-error 2 {{'auto' not allowed in lambda parameter}} \ |
| 586 |
// cxx03-warning {{template argument uses unnamed type}} \ |
| 587 |
// cxx03-note {{while substituting}} cxx03-note {{unnamed type used}} |
| 588 |
}; |
| 589 |
|
| 590 |
B<int> b; |
| 591 |
// cxx03-note@#PR27994_B 4{{in instantiation of default member initializer}} |
| 592 |
// cxx03-note@-2 4{{in evaluation of exception}} |
| 593 |
|
| 594 |
template <class T> struct C { |
| 595 |
struct D { |
| 596 |
// cxx03-note@-1 {{in instantiation of default member initializer}} |
| 597 |
int x; |
| 598 |
A f = [&] { int y = x; }; // cxx03-warning {{template argument uses unnamed type}} \ |
| 599 |
// cxx03-note {{while substituting}} cxx03-note {{unnamed type used}} |
| 600 |
}; |
| 601 |
}; |
| 602 |
|
| 603 |
int func() { |
| 604 |
C<int> a; |
| 605 |
decltype(a)::D b; |
| 606 |
// cxx03-note@-1 {{in evaluation of exception}} |
| 607 |
} |
| 608 |
} |
| 609 |
|
| 610 |
namespace PR30566 { |
| 611 |
int name1; // expected-note {{'name1' declared here}} |
| 612 |
|
| 613 |
struct S1 { |
| 614 |
template<class T> |
| 615 |
S1(T t) { s = sizeof(t); } |
| 616 |
int s; |
| 617 |
}; |
| 618 |
|
| 619 |
void foo1() { |
| 620 |
auto s0 = S1([name=]() {}); // expected-error {{expected expression}} |
| 621 |
// cxx03-warning@-1 {{template argument uses local type}} \ |
| 622 |
// cxx03-note@-1 {{while substituting deduced template arguments}} |
| 623 |
auto s1 = S1([name=name]() {}); // expected-error {{use of undeclared identifier 'name'; did you mean 'name1'?}} |
| 624 |
// cxx03-cxx11-warning@-1 {{initialized lambda captures are a C++14 extension}} |
| 625 |
// cxx03-warning@-2 {{template argument uses local type}} \ |
| 626 |
// cxx03-note@-2 {{while substituting deduced template arguments}} |
| 627 |
} |
| 628 |
} |
| 629 |
|
| 630 |
namespace PR25627_dont_odr_use_local_consts { |
| 631 |
|
| 632 |
template<int> struct X {}; |
| 633 |
|
| 634 |
void foo() { |
| 635 |
const int N = 10; |
| 636 |
(void) [] { X<N> x; }; |
| 637 |
} |
| 638 |
} |
| 639 |
|
| 640 |
namespace PR94764 { |
| 641 |
struct X { |
| 642 |
int x; |
| 643 |
void foo() { |
| 644 |
[x](){}; // expected-error{{class member 'x' cannot appear in capture list as it is not a variable}} |
| 645 |
} |
| 646 |
}; |
| 647 |
} |
| 648 |
|
| 649 |
namespace ConversionOperatorDoesNotHaveDeducedReturnType { |
| 650 |
auto x = [](int){}; |
| 651 |
auto y = [](auto &v) -> void { v.n = 0; }; // cxx03-cxx11-error {{'auto' not allowed in lambda parameter}} cxx03-cxx11-note {{candidate function not viable}} cxx03-cxx11-note {{conversion candidate}} |
| 652 |
using T = decltype(x); |
| 653 |
using U = decltype(y); |
| 654 |
using ExpectedTypeT = void (*)(int); |
| 655 |
template<typename T> |
| 656 |
using ExpectedTypeU = void (*)(T&); |
| 657 |
|
| 658 |
struct X { |
| 659 |
#if __cplusplus > 201402L |
| 660 |
friend constexpr auto T::operator()(int) const; |
| 661 |
friend constexpr T::operator ExpectedTypeT() const noexcept; |
| 662 |
|
| 663 |
template<typename T> |
| 664 |
friend constexpr void U::operator()(T&) const; |
| 665 |
// FIXME: This should not match; the return type is specified as behaving |
| 666 |
// "as if it were a decltype-specifier denoting the return type of |
| 667 |
// [operator()]", which is not equivalent to this alias template. |
| 668 |
template<typename T> |
| 669 |
friend constexpr U::operator ExpectedTypeU<T>() const noexcept; |
| 670 |
#else |
| 671 |
friend auto T::operator()(int) const; // cxx11-error {{'auto' return without trailing return type; deduced return types are a C++14 extension}} \ |
| 672 |
cxx03-error {{'auto' not allowed in function return type}} |
| 673 |
friend T::operator ExpectedTypeT() const; |
| 674 |
|
| 675 |
template<typename T> |
| 676 |
friend void U::operator()(T&) const; // cxx03-cxx11-error {{friend declaration of 'operator()' does not match any declaration}} |
| 677 |
// FIXME: This should not match, as above. |
| 678 |
template<typename T> |
| 679 |
friend U::operator ExpectedTypeU<T>() const; // cxx03-cxx11-error {{friend declaration of 'operator void (*)(type-parameter-0-0 &)' does not match any declaration}} |
| 680 |
#endif |
| 681 |
|
| 682 |
private: |
| 683 |
int n; |
| 684 |
}; |
| 685 |
|
| 686 |
// Should be OK in C++14 and later: lambda's call operator is a friend. |
| 687 |
void use(X &x) { y(x); } // cxx03-cxx11-error {{no matching function for call to object}} |
| 688 |
|
| 689 |
// This used to crash in return type deduction for the conversion opreator. |
| 690 |
struct A { int n; void f() { +[](decltype(n)) {}; } }; |
| 691 |
} |
| 692 |
|
| 693 |
namespace TypoCorrection { |
| 694 |
template <typename T> struct X {}; |
| 695 |
// expected-note@-1 {{template parameter is declared here}} |
| 696 |
|
| 697 |
template <typename T> |
| 698 |
void Run(const int& points) { |
| 699 |
// expected-note@-1 {{'points' declared here}} |
| 700 |
auto outer_lambda = []() { |
| 701 |
auto inner_lambda = [](const X<Points>&) {}; |
| 702 |
// expected-error@-1 {{use of undeclared identifier 'Points'; did you mean 'points'?}} |
| 703 |
// expected-error@-2 {{template argument for template type parameter must be a type}} |
| 704 |
}; |
| 705 |
} |
| 706 |
} |
| 707 |
|
| 708 |
void operator_parens() { |
| 709 |
[&](int x){ operator()(); }(0); // expected-error {{undeclared 'operator()'}} |
| 710 |
} |
| 711 |
|
| 712 |
namespace captured_name { |
| 713 |
void Test() { |
| 714 |
union { // expected-note {{'' declared here}} |
| 715 |
int i; |
| 716 |
}; |
| 717 |
[] { return i; }; // expected-error {{variable '' cannot be implicitly captured in a lambda with no capture-default specified}} |
| 718 |
// expected-note@-1 {{lambda expression begins here}} |
| 719 |
// expected-note@-2 2 {{default capture by}} |
| 720 |
} |
| 721 |
}; |
| 722 |
|
| 723 |
namespace GH60518 { |
| 724 |
// Lambdas should not try to capture |
| 725 |
// function parameters that are used in enable_if |
| 726 |
struct StringLiteral { |
| 727 |
template <int N> |
| 728 |
StringLiteral(const char (&array)[N]) // cxx03-note {{declared here}} |
| 729 |
__attribute__((enable_if(__builtin_strlen(array) == 2, // cxx03-error {{'enable_if' attribute expression never produces a constant expression}} cxx03-note {{read of variable}} |
| 730 |
"invalid string literal"))); |
| 731 |
}; |
| 732 |
|
| 733 |
namespace cpp_attribute { |
| 734 |
struct StringLiteral { |
| 735 |
template <int N> |
| 736 |
StringLiteral(const char (&array)[N]) [[clang::annotate_type("test", array)]]; |
| 737 |
}; |
| 738 |
} |
| 739 |
|
| 740 |
void Func1() { |
| 741 |
[[maybe_unused]] auto y = [&](decltype(StringLiteral("xx"))) {}; // cxx03-note {{in instantiation of function template specialization}} |
| 742 |
[[maybe_unused]] auto z = [&](decltype(cpp_attribute::StringLiteral("xx"))) {}; |
| 743 |
} |
| 744 |
|
| 745 |
} |
| 746 |
|
| 747 |
#if __cplusplus > 201402L |
| 748 |
namespace GH60936 { |
| 749 |
struct S { |
| 750 |
int i; |
| 751 |
// `&i` in default initializer causes implicit `this` access. |
| 752 |
int *p = &i; |
| 753 |
}; |
| 754 |
|
| 755 |
static_assert([]() constexpr { |
| 756 |
S r = S{2}; |
| 757 |
return r.p != nullptr; |
| 758 |
}()); |
| 759 |
} // namespace GH60936 |
| 760 |
#endif |
| 761 |
|
| 762 |
// Call operator attributes refering to a variable should |
| 763 |
// be properly handled after D124351 |
| 764 |
#if __cplusplus >= 201103L |
| 765 |
constexpr int i = 2; |
| 766 |
void foo() { |
| 767 |
(void)[=][[gnu::aligned(i)]] () {}; // expected-warning{{C++23 extension}} |
| 768 |
// CHECK: AlignedAttr |
| 769 |
// CHECK-NEXT: ConstantExpr |
| 770 |
// CHECK-NEXT: value: Int 2 |
| 771 |
} |
| 772 |
#endif |
| 773 |
|
| 774 |
void GH48527() { |
| 775 |
auto a = []()__attribute__((b(({ return 0; })))){}; // expected-warning {{unknown attribute 'b' ignored}} |
| 776 |
} |
| 777 |
|
| 778 |
#if __cplusplus >= 201103L |
| 779 |
void GH67492() { |
| 780 |
constexpr auto test = 42; |
| 781 |
auto lambda = (test, []() noexcept(true) {}); |
| 782 |
} |
| 783 |
#endif |
| 784 |
|
| 785 |
// FIXME: This currently causes clang to crash in C++11 mode. |
| 786 |
#if __cplusplus >= 201402L |
| 787 |
namespace GH83267 { |
| 788 |
auto l = [](auto a) { return 1; }; |
| 789 |
using type = decltype(l); |
| 790 |
|
| 791 |
template<> |
| 792 |
auto type::operator()(int a) const { // expected-error{{lambda call operator should not be explicitly specialized or instantiated}} |
| 793 |
return c; // expected-error {{use of undeclared identifier 'c'}} |
| 794 |
} |
| 795 |
|
| 796 |
auto ll = [](auto a) { return 1; }; // expected-error{{lambda call operator should not be explicitly specialized or instantiated}} |
| 797 |
using t = decltype(ll); |
| 798 |
template auto t::operator()<int>(int a) const; // expected-note {{in instantiation}} |
| 799 |
|
| 800 |
} |
| 801 |
#endif |