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
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
11 namespace std
{ class type_info
; };
13 namespace ExplicitCapture
{
17 static void Overload(int);
19 virtual C
& Overload(float);
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'}}
26 [this](){(void)Member
;};
28 []{[this]{};};// expected-error {{'this' cannot be implicitly captured in this context}}
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'}}
37 [this] () {}; // expected-error {{'this' cannot be captured in this context}}
41 namespace ReturnDeduction
{
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; };
54 namespace ImplicitCapture
{
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}}
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
; }; }; //
70 union { // expected-note {{declared}}
75 [=]() { return c
; }; // expected-error {{unnamed variable cannot be implicitly captured in a lambda expression}}
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}}
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}}
86 struct G
{ G(); G(G
&); int a
; }; // expected-note 6 {{not viable}}
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'}}
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}}
95 // References can appear in constant expressions if they are initialized by
96 // reference constant expressions.
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}}
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}}
107 namespace SpecialMembers
{
109 auto a
= []{}; // expected-note 2{{here}} expected-note {{candidate}} not-cxx03-note {{candidate}}
110 decltype(a
) b
; // expected-error {{no matching constructor}}
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}}
117 P(const P
&) = delete; //expected-note {{deleted here}} // expected-cxx14-note {{deleted here}}
120 ~Q() = delete; // expected-note {{deleted here}}
123 R(const R
&) = default;
125 R
&operator=(const R
&) = delete;
126 R
&operator=(R
&&) = delete;
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}}
134 auto a
= [r
]{}; // expected-note 2{{here}}
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}}
160 int n
= -1; // expected-note {{declared here}}
162 int arr
[n
]; // expected-warning {{variable length arrays in C++ are a Clang extension}} \
163 expected
-note
{{read of non
-const variable
'n' is
not allowed in a constant expression
}}
168 int arr
[m
]; // expected-error{{negative size}}
172 int arr
[m
]; // expected-error{{negative size}}
176 int arr
[m
]; // expected-error{{negative size}}
180 int arr
[m
]; // expected-error{{negative size}}
187 unsigned int result
= 0;
188 auto l
= [&]() { ++result
; };
191 namespace ModifyingCapture
{
195 n
= 1; // expected-error {{cannot assign to a variable captured by copy in a non-mutable lambda}}
200 namespace VariadicPackExpansion
{
201 template<typename T
, typename U
> using Fst
= T
;
202 template<typename
...Ts
> bool g(Fst
<bool, Ts
> ...bools
);
203 template<typename
...Ts
> bool f(Ts
&&...ts
) {
204 return g
<Ts
...>([&ts
] {
212 int a
= 5, b
= 2, c
= 3;
218 template<typename
...Ts
> sink(Ts
&&...) {}
221 template<typename
...Ts
> void local_class() {
229 Ts
g() { return *this; };
237 struct X
{}; struct Y
{};
238 template void local_class
<X
, Y
>();
240 template<typename
...Ts
> void nested(Ts
...ts
) {
242 // Each expansion of this lambda implicitly captures all of 'ts', because
243 // the inner lambda also expands 'ts'.
245 return ts
+ [&] { return f(ts
...); } ();
249 template void nested(int, int, int);
251 template<typename
...Ts
> void nested2(Ts
...ts
) { // expected-note 2{{here}}
252 // Capture all 'ts', use only one.
253 f([&ts
...] { return ts
; } ()...);
254 // Capture each 'ts', use it.
255 f([&ts
] { return ts
; } ()...);
256 // Capture all 'ts', use all of them.
257 f([&ts
...] { return (int)f(ts
...); } ());
258 // Capture each 'ts', use all of them. Ill-formed. In more detail:
260 // We instantiate two lambdas here; the first captures ts$0, the second
261 // captures ts$1. Both of them reference both ts parameters, so both are
262 // ill-formed because ts can't be implicitly captured.
264 // FIXME: This diagnostic does not explain what's happening. We should
265 // specify which 'ts' we're referring to in its diagnostic name. We should
266 // also say which slice of the pack expansion is being performed in the
267 // instantiation backtrace.
268 f([&ts
] { return (int)f(ts
...); } ()...); // \
269 // expected-error 2{{'ts' cannot be implicitly captured}} \
270 // expected-note 2{{lambda expression begins here}} \
271 // expected-note 4 {{capture 'ts' by}} \
272 // expected-note 2 {{while substituting into a lambda}}
274 template void nested2(int); // ok
275 template void nested2(int, int); // expected-note 2 {{in instantiation of}}
280 auto x
= PR13860UndeclaredIdentifier(); // expected-error {{use of undeclared identifier 'PR13860UndeclaredIdentifier'}}
282 static_assert(sizeof(y
), "");
291 auto f
= [](void) { return __func__
; }; // no-warning
302 namespace TypeDeduction
{
306 S
&&t
= [&] { return s
; } ();
307 #if __cplusplus > 201103L
308 S
&&u
= [&] () -> auto { return s
; } ();
314 namespace lambdas_in_NSDMIs
{
318 T t2
= ([](int a
) { return [](int b
) { return b
; };})(t
)(t
);
322 namespace non_template
{
325 int t2
= ([](int a
) { return [](int b
) { return b
; };})(t
)(t
);
331 // PR18477: don't try to capture 'this' from an NSDMI encountered while parsing
333 namespace NSDMIs_in_lambdas
{
334 template<typename T
> struct S
{ int a
= 0; int b
= a
; };
335 void f() { []() { S
<int> s
; }; }
337 auto x
= []{ struct S
{ int n
, m
= n
; }; };
338 auto y
= [&]{ struct S
{ int n
, m
= n
; }; }; // expected-error {{non-local lambda expression cannot have a capture-default}}
339 void g() { auto z
= [&]{ struct S
{ int n
, m
= n
; }; }; }
342 namespace CaptureIncomplete
{
343 struct Incomplete
; // expected-note 2{{forward decl}}
344 void g(const Incomplete
&a
);
345 void f(Incomplete
&a
) {
346 (void) [a
] {}; // expected-error {{incomplete}}
349 (void) [=] { g(a
); }; // expected-error {{incomplete}}
350 (void) [&] { f(a
); };
354 #if __cplusplus >= 201103L
355 namespace CaptureAbstract
{
357 virtual void f() = 0; // expected-note {{unimplemented}}
366 S
&s
= const_cast<T
&>(t
);
367 // FIXME: Once we properly compute odr-use per DR712, this should be
368 // accepted (and should not capture 's').
369 [=] { return s
.n
; }; // expected-error {{abstract}}
375 auto l
= [=]{}; // expected-error {{non-local lambda expression cannot have a capture-default}}
379 int (*f())[true ? 1 : ([=]{ return n
; }(), 0)];
380 // expected-error@-1 {{non-local lambda expression cannot have a capture-default}}
381 // expected-error@-2 {{invalid use of non-static data member 'n'}}
382 // expected-cxx14-error@-3 {{a lambda expression may not appear inside of a constant expression}}
383 // cxx03-error@-4 {{function declaration cannot have variably modified type}}
384 // cxx03-warning@-5 {{variable length arrays in C++ are a Clang extension}}
385 int g(int k
= ([=]{ 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'}}
389 int a
= [=]{ return n
; }(); // ok
390 int b
= [=]{ return [=]{ return n
; }(); }(); // ok
391 int c
= []{ int k
= 0; return [=]{ return k
; }(); }(); // ok
392 int d
= [] { return [=] { return n
; }(); }(); // expected-error {{'this' cannot be implicitly captured in this context}} expected-note {{explicitly capture 'this'}}
399 int a
= (4, []() { return n
; }()); // expected-error {{'this' cannot be implicitly captured in this context}} \
400 // expected-note {{explicitly capture 'this'}}
405 template<typename T
> void f() {
407 (void) [=]{ int n
= t
; }; // expected-error {{deleted}}
410 template void f
<int>();
413 NoCopy(const NoCopy
&) = delete; // expected-note {{deleted}}
414 operator int() const;
416 template void f
<NoCopy
>(); // expected-note {{instantiation}}
420 auto x
= [&x
]{}; // expected-error {{cannot appear in its own init}}
424 template <class L
, int X
= sizeof(L
)>
427 template <typename
... Args
>
428 void Logger(Args
&&... args
) {
429 auto len
= Invalid_Function((args
)...);
430 // expected-error@-1 {{use of undeclared identifier 'Invalid_Function'}}
436 // expected-note@-1 {{in instantiation of function template specialization 'PR20731::Logger<>' requested here}}
439 template <typename T
>
442 // expected-error@-1 {{field has incomplete type 'void'}}
445 template <typename F
>
447 auto a
= A
<decltype(f())>();
448 // expected-note@-1 {{in instantiation of template class 'PR20731::A<void>' requested here}}
449 auto xf
= [a
, f
]() {};
453 g([] {}); // cxx03-warning {{template argument uses local type}}
454 // expected-note-re@-1 {{in instantiation of function template specialization 'PR20731::g<(lambda at {{.*}}>' requested here}}
457 template <class _Rp
> struct function
{
460 static_assert(sizeof(_Fp
) > 0, "Type must be complete.");
464 template <typename T
> void p(T t
) {
465 auto l
= some_undefined_function(t
);
466 // expected-error@-1 {{use of undeclared identifier 'some_undefined_function'}}
467 function
<void()>(([l
]() {}));
470 // expected-note@-1 {{in instantiation of function template specialization 'PR20731::p<int>' requested here}}
473 namespace lambda_in_default_mem_init
{
474 template<typename T
> void f() {
475 struct S
{ int n
= []{ return 0; }(); };
477 template void f
<int>();
479 template<typename T
> void g() {
480 struct S
{ int n
= [](int n
){ return n
; }(0); };
482 template void g
<int>();
485 namespace error_in_transform_prototype
{
488 // expected-error@+2 {{type 'int' cannot be used prior to '::' because it has no members}}
489 // expected-error@+1 {{no member named 'ns' in 'error_in_transform_prototype::S'}}
490 auto x
= [](typename
T::ns::type
&k
) {};
494 f(5); // expected-note {{requested here}}
495 f(S()); // expected-note {{requested here}}
500 template<typename Fn
> struct fun
: Fn
{
502 using Fn::operator();
504 template<typename Fn
> fun
<Fn
> wrap(Fn fn
); // cxx03-warning {{template argument uses unnamed type}}
505 auto x
= wrap([](){}); // cxx03-warning {{template argument uses unnamed type}} cxx03-note 2 {{unnamed type used in template argument was declared here}}
510 void Method(char c
= []()->char {
513 int Method() { return 0; }
523 template <class> struct A
{
524 void f(int x
= []() {
538 namespace rdar22032373
{
540 auto blk
= [](bool b
) {
542 return undeclared_error
; // expected-error {{use of undeclared identifier}}
546 return undef(); // expected-error {{use of undeclared identifier}}
547 return 0; // verify no init_conversion_failed diagnostic emitted.
552 namespace nested_lambda
{
559 auto inner
= [](S
<num
> &X
) {};
565 struct A
{ template <class T
> A(T
); };
570 A a
= [&] { int y
= x
; };
571 A b
= [&] { [&] { [&] { int y
= x
; }; }; };
572 A d
= [&](auto param
) { int y
= x
; }; // cxx03-cxx11-error {{'auto' not allowed in lambda parameter}}
573 A e
= [&](auto param
) { [&] { [&](auto param2
) { int y
= x
; }; }; }; // cxx03-cxx11-error 2 {{'auto' not allowed in lambda parameter}}
578 template <class T
> struct C
{
581 A f
= [&] { int y
= x
; };
592 int name1
; // expected-note {{'name1' declared here}}
596 S1(T t
) { s
= sizeof(t
); }
601 auto s0
= S1([name
=]() {}); // expected-error {{expected expression}}
602 auto s1
= S1([name
=name
]() {}); // expected-error {{use of undeclared identifier 'name'; did you mean 'name1'?}}
603 // cxx03-cxx11-warning@-1 {{initialized lambda captures are a C++14 extension}}
607 namespace PR25627_dont_odr_use_local_consts
{
609 template<int> struct X
{};
613 (void) [] { X
<N
> x
; };
621 [x
](){}; // expected-error{{class member 'x' cannot appear in capture list as it is not a variable}}
626 namespace ConversionOperatorDoesNotHaveDeducedReturnType
{
628 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}}
629 using T
= decltype(x
);
630 using U
= decltype(y
);
631 using ExpectedTypeT
= void (*)(int);
633 using ExpectedTypeU
= void (*)(T
&);
636 #if __cplusplus > 201402L
637 friend constexpr auto T::operator()(int) const;
638 friend constexpr T::operator ExpectedTypeT() const noexcept
;
641 friend constexpr void U::operator()(T
&) const;
642 // FIXME: This should not match; the return type is specified as behaving
643 // "as if it were a decltype-specifier denoting the return type of
644 // [operator()]", which is not equivalent to this alias template.
646 friend constexpr U::operator ExpectedTypeU
<T
>() const noexcept
;
648 friend auto T::operator()(int) const; // cxx11-error {{'auto' return without trailing return type; deduced return types are a C++14 extension}} \
649 cxx03
-error
{{'auto' not allowed in function
return type
}}
650 friend T::operator ExpectedTypeT() const;
653 friend void U::operator()(T
&) const; // cxx03-cxx11-error {{friend declaration of 'operator()' does not match any declaration}}
654 // FIXME: This should not match, as above.
656 friend U::operator ExpectedTypeU
<T
>() const; // cxx03-cxx11-error {{friend declaration of 'operator void (*)(type-parameter-0-0 &)' does not match any declaration}}
663 // Should be OK in C++14 and later: lambda's call operator is a friend.
664 void use(X
&x
) { y(x
); } // cxx03-cxx11-error {{no matching function for call to object}}
666 // This used to crash in return type deduction for the conversion opreator.
667 struct A
{ int n
; void f() { +[](decltype(n
)) {}; } };
670 namespace TypoCorrection
{
671 template <typename T
> struct X
{};
672 // expected-note@-1 {{template parameter is declared here}}
674 template <typename T
>
675 void Run(const int& points
) {
676 // expected-note@-1 {{'points' declared here}}
677 auto outer_lambda
= []() {
678 auto inner_lambda
= [](const X
<Points
>&) {};
679 // expected-error@-1 {{use of undeclared identifier 'Points'; did you mean 'points'?}}
680 // expected-error@-2 {{template argument for template type parameter must be a type}}
685 void operator_parens() {
686 [&](int x
){ operator()(); }(0); // expected-error {{undeclared 'operator()'}}
689 namespace captured_name
{
691 union { // expected-note {{'' declared here}}
694 [] { return i
; }; // expected-error {{variable '' cannot be implicitly captured in a lambda with no capture-default specified}}
695 // expected-note@-1 {{lambda expression begins here}}
696 // expected-note@-2 2 {{default capture by}}
701 // Lambdas should not try to capture
702 // function parameters that are used in enable_if
703 struct StringLiteral
{
705 StringLiteral(const char (&array
)[N
]) // cxx03-note {{declared here}}
706 __attribute__((enable_if(__builtin_strlen(array
) == 2, // cxx03-error {{'enable_if' attribute expression never produces a constant expression}} cxx03-note {{read of variable}}
707 "invalid string literal")));
710 namespace cpp_attribute
{
711 struct StringLiteral
{
713 StringLiteral(const char (&array
)[N
]) [[clang::annotate_type("test", array
)]];
718 [[maybe_unused
]] auto y
= [&](decltype(StringLiteral("xx"))) {}; // cxx03-note {{in instantiation of function template specialization}}
719 [[maybe_unused
]] auto z
= [&](decltype(cpp_attribute::StringLiteral("xx"))) {};
724 #if __cplusplus > 201402L
728 // `&i` in default initializer causes implicit `this` access.
732 static_assert([]() constexpr {
734 return r
.p
!= nullptr;
736 } // namespace GH60936
739 // Call operator attributes refering to a variable should
740 // be properly handled after D124351
741 #if __cplusplus >= 201103L
744 (void)[=][[gnu::aligned(i
)]] () {}; // expected-warning{{C++23 extension}}
745 // CHECK: AlignedAttr
746 // CHECK-NEXT: ConstantExpr
747 // CHECK-NEXT: value: Int 2
752 auto a
= []()__attribute__((b(({ return 0; })))){}; // expected-warning {{unknown attribute 'b' ignored}}
755 #if __cplusplus >= 201103L
757 constexpr auto test
= 42;
758 auto lambda
= (test
, []() noexcept(true) {});
762 // FIXME: This currently causes clang to crash in C++11 mode.
763 #if __cplusplus >= 201402L
765 auto l
= [](auto a
) { return 1; };
766 using type
= decltype(l
);
769 auto type::operator()(int a
) const { // expected-error{{lambda call operator should not be explicitly specialized or instantiated}}
770 return c
; // expected-error {{use of undeclared identifier 'c'}}
773 auto ll
= [](auto a
) { return 1; }; // expected-error{{lambda call operator should not be explicitly specialized or instantiated}}
774 using t
= decltype(ll
);
775 template auto t::operator()<int>(int a
) const; // expected-note {{in instantiation}}