1 // RUN: %clang_cc1 -std=c++14 -Wno-unused-value -fsyntax-only -verify -verify=expected-cxx14 -fblocks %s
2 // RUN: %clang_cc1 -std=c++17 -Wno-unused-value -verify -ast-dump -fblocks %s | FileCheck %s
4 namespace std
{ class type_info
; };
6 namespace ExplicitCapture
{
10 static void Overload(int);
12 virtual C
& Overload(float);
14 void ImplicitThisCapture() {
15 []() { (void)Member
; }; // expected-error {{'this' cannot be implicitly captured in this context}} expected-note {{explicitly capture 'this'}}
16 const int var
= []() {(void)Member
; return 0; }(); // expected-error {{'this' cannot be implicitly captured in this context}} expected-note {{explicitly capture 'this'}}
19 [this](){(void)Member
;};
21 []{[this]{};};// expected-error {{'this' cannot be implicitly captured in this context}}
23 [] { Overload(); }; // expected-error {{'this' cannot be implicitly captured in this context}} expected-note {{explicitly capture 'this'}}
24 []{(void)typeid(Overload());};
25 [] { (void)typeid(Overload(.5f
)); }; // expected-error {{'this' cannot be implicitly captured in this context}} expected-note {{explicitly capture 'this'}}
30 [this] () {}; // expected-error {{'this' cannot be captured in this context}}
34 namespace ReturnDeduction
{
38 [](){ return ({return 1; 1;}); };
39 [](){ return ({return 'c'; 1;}); }; // expected-error {{must match previous return type}}
40 []()->int{ return 'c'; return 1; };
41 [](){ return 'c'; return 1; }; // expected-error {{must match previous return type}}
42 []() { return; return (void)0; };
43 [](){ return 1; return 1; };
47 namespace ImplicitCapture
{
49 int a
= 0; // expected-note 5 {{declared}}
50 []() { 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}}
53 [=]() { int* b
= &a
; }; // expected-error {{cannot initialize a variable of type 'int *' with an rvalue of type 'const int *'}}
54 [=]() { return [&]() { return a
; }; };
55 []() { 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}}
56 []() { 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}}
57 []() { 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}}
58 [=]() { return [&a
] { return a
; }; }; //
63 union { // expected-note {{declared}}
68 [=]() { return c
; }; // expected-error {{unnamed variable cannot be implicitly captured in a lambda expression}}
70 __block
int e
; // expected-note 2{{declared}}
71 [&]() { return e
; }; // expected-error {{__block variable 'e' cannot be captured in a lambda expression}}
72 [&e
]() { return e
; }; // expected-error {{__block variable 'e' cannot be captured in a lambda expression}}
74 int f
[10]; // expected-note {{declared}}
75 [&]() { return f
[2]; };
76 (void) ^{ return []() { return f
[2]; }; }; // expected-error {{variable 'f' cannot be implicitly captured in a lambda with no capture-default specified}} \
77 // expected-note{{lambda expression begins here}} expected-note 2 {{capture 'f' by}} expected-note 2 {{default capture by}}
79 struct G
{ G(); G(G
&); int a
; }; // expected-note 6 {{not viable}}
81 [=]() { const G
* gg
= &g
; return gg
->a
; };
82 [=]() { return [=]{ const G
* gg
= &g
; return gg
->a
; }(); }; // expected-error {{no matching constructor for initialization of 'G'}}
83 (void)^{ return [=]{ const G
* gg
= &g
; return gg
->a
; }(); }; // expected-error 2 {{no matching constructor for initialization of 'const G'}}
85 const int h
= a
; // expected-note {{declared}}
86 []() { 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}}
88 // References can appear in constant expressions if they are initialized by
89 // reference constant expressions.
91 int &ref_i
= i
; // expected-note {{declared}}
92 [] { 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}}
96 [] { return ref_j
; }; // ok
100 namespace SpecialMembers
{
102 auto a
= []{}; // expected-note 2{{here}} expected-note 2{{candidate}}
103 decltype(a
) b
; // expected-error {{no matching constructor}}
105 decltype(a
) d
= static_cast<decltype(a
)&&>(a
);
106 a
= a
; // expected-error {{copy assignment operator is implicitly deleted}}
107 a
= static_cast<decltype(a
)&&>(a
); // expected-error {{copy assignment operator is implicitly deleted}}
110 P(const P
&) = delete; //expected-note {{deleted here}} // expected-cxx14-note {{deleted here}}
113 ~Q() = delete; // expected-note {{deleted here}}
116 R(const R
&) = default;
118 R
&operator=(const R
&) = delete;
119 R
&operator=(R
&&) = delete;
121 void g(P
&p
, Q
&q
, R
&r
) {
122 // FIXME: The note attached to the second error here is just amazingly bad.
123 auto pp
= [p
]{}; // expected-error {{deleted constructor}} expected-cxx14-error {{deleted copy constructor of '(lambda}}
124 // expected-cxx14-note-re@-1 {{copy constructor of '(lambda at {{.*}})' is implicitly deleted because field '' has a deleted copy constructor}}
125 auto qq
= [q
]{}; // expected-error {{deleted function}} expected-note {{because}}
127 auto a
= [r
]{}; // expected-note 2{{here}}
129 decltype(a
) c
= static_cast<decltype(a
)&&>(a
); // ok, copies R
130 a
= a
; // expected-error {{copy assignment operator is implicitly deleted}}
131 a
= static_cast<decltype(a
)&&>(a
); // expected-error {{copy assignment operator is implicitly deleted}}
153 int n
= -1; // expected-note {{declared here}}
155 int arr
[n
]; // expected-warning {{variable length arrays in C++ are a Clang extension}} \
156 expected
-note
{{read of non
-const variable
'n' is
not allowed in a constant expression
}}
161 int arr
[m
]; // expected-error{{negative size}}
165 int arr
[m
]; // expected-error{{negative size}}
169 int arr
[m
]; // expected-error{{negative size}}
173 int arr
[m
]; // expected-error{{negative size}}
180 unsigned int result
= 0;
181 auto l
= [&]() { ++result
; };
184 namespace ModifyingCapture
{
188 n
= 1; // expected-error {{cannot assign to a variable captured by copy in a non-mutable lambda}}
193 namespace VariadicPackExpansion
{
194 template<typename T
, typename U
> using Fst
= T
;
195 template<typename
...Ts
> bool g(Fst
<bool, Ts
> ...bools
);
196 template<typename
...Ts
> bool f(Ts
&&...ts
) {
197 return g
<Ts
...>([&ts
] {
205 int a
= 5, b
= 2, c
= 3;
211 template<typename
...Ts
> sink(Ts
&&...) {}
214 template<typename
...Ts
> void local_class() {
222 Ts
g() { return *this; };
230 struct X
{}; struct Y
{};
231 template void local_class
<X
, Y
>();
233 template<typename
...Ts
> void nested(Ts
...ts
) {
235 // Each expansion of this lambda implicitly captures all of 'ts', because
236 // the inner lambda also expands 'ts'.
238 return ts
+ [&] { return f(ts
...); } ();
242 template void nested(int, int, int);
244 template<typename
...Ts
> void nested2(Ts
...ts
) { // expected-note 2{{here}}
245 // Capture all 'ts', use only one.
246 f([&ts
...] { return ts
; } ()...);
247 // Capture each 'ts', use it.
248 f([&ts
] { return ts
; } ()...);
249 // Capture all 'ts', use all of them.
250 f([&ts
...] { return (int)f(ts
...); } ());
251 // Capture each 'ts', use all of them. Ill-formed. In more detail:
253 // We instantiate two lambdas here; the first captures ts$0, the second
254 // captures ts$1. Both of them reference both ts parameters, so both are
255 // ill-formed because ts can't be implicitly captured.
257 // FIXME: This diagnostic does not explain what's happening. We should
258 // specify which 'ts' we're referring to in its diagnostic name. We should
259 // also say which slice of the pack expansion is being performed in the
260 // instantiation backtrace.
261 f([&ts
] { return (int)f(ts
...); } ()...); // \
262 // expected-error 2{{'ts' cannot be implicitly captured}} \
263 // expected-note 2{{lambda expression begins here}} \
264 // expected-note 4 {{capture 'ts' by}} \
265 // expected-note 2 {{while substituting into a lambda}}
267 template void nested2(int); // ok
268 template void nested2(int, int); // expected-note 2 {{in instantiation of}}
273 auto x
= PR13860UndeclaredIdentifier(); // expected-error {{use of undeclared identifier 'PR13860UndeclaredIdentifier'}}
275 static_assert(sizeof(y
), "");
284 auto f
= [](void) { return __func__
; }; // no-warning
295 namespace TypeDeduction
{
299 S
&&t
= [&] { return s
; } ();
300 #if __cplusplus > 201103L
301 S
&&u
= [&] () -> auto { return s
; } ();
307 namespace lambdas_in_NSDMIs
{
311 T t2
= ([](int a
) { return [](int b
) { return b
; };})(t
)(t
);
315 namespace non_template
{
318 int t2
= ([](int a
) { return [](int b
) { return b
; };})(t
)(t
);
324 // PR18477: don't try to capture 'this' from an NSDMI encountered while parsing
326 namespace NSDMIs_in_lambdas
{
327 template<typename T
> struct S
{ int a
= 0; int b
= a
; };
328 void f() { []() { S
<int> s
; }; }
330 auto x
= []{ struct S
{ int n
, m
= n
; }; };
331 auto y
= [&]{ struct S
{ int n
, m
= n
; }; }; // expected-error {{non-local lambda expression cannot have a capture-default}}
332 void g() { auto z
= [&]{ struct S
{ int n
, m
= n
; }; }; }
335 namespace CaptureIncomplete
{
336 struct Incomplete
; // expected-note 2{{forward decl}}
337 void g(const Incomplete
&a
);
338 void f(Incomplete
&a
) {
339 (void) [a
] {}; // expected-error {{incomplete}}
342 (void) [=] { g(a
); }; // expected-error {{incomplete}}
343 (void) [&] { f(a
); };
347 namespace CaptureAbstract
{
349 virtual void f() = 0; // expected-note {{unimplemented}}
358 S
&s
= const_cast<T
&>(t
);
359 // FIXME: Once we properly compute odr-use per DR712, this should be
360 // accepted (and should not capture 's').
361 [=] { return s
.n
; }; // expected-error {{abstract}}
366 auto l
= [=]{}; // expected-error {{non-local lambda expression cannot have a capture-default}}
370 int (*f())[true ? 1 : ([=]{ return n
; }(), 0)];
371 // expected-error@-1 {{non-local lambda expression cannot have a capture-default}}
372 // expected-error@-2 {{invalid use of non-static data member 'n'}}
373 // expected-cxx14-error@-3 {{a lambda expression may not appear inside of a constant expression}}
374 int g(int k
= ([=]{ return n
; }(), 0));
375 // expected-error@-1 {{non-local lambda expression cannot have a capture-default}}
376 // expected-error@-2 {{invalid use of non-static data member 'n'}}
378 int a
= [=]{ return n
; }(); // ok
379 int b
= [=]{ return [=]{ return n
; }(); }(); // ok
380 int c
= []{ int k
= 0; return [=]{ return k
; }(); }(); // ok
381 int d
= [] { return [=] { return n
; }(); }(); // expected-error {{'this' cannot be implicitly captured in this context}} expected-note {{explicitly capture 'this'}}
388 int a
= (4, []() { return n
; }()); // expected-error {{'this' cannot be implicitly captured in this context}} \
389 // expected-note {{explicitly capture 'this'}}
394 template<typename T
> void f() {
396 (void) [=]{ int n
= t
; }; // expected-error {{deleted}} expected-note {{while substituting into a lambda}}
399 template void f
<int>();
402 NoCopy(const NoCopy
&) = delete; // expected-note {{deleted}}
403 operator int() const;
405 template void f
<NoCopy
>(); // expected-note {{instantiation}}
409 auto x
= [&x
]{}; // expected-error {{cannot appear in its own init}}
413 template <class L
, int X
= sizeof(L
)>
416 template <typename
... Args
>
417 void Logger(Args
&&... args
) {
418 auto len
= Invalid_Function((args
)...);
419 // expected-error@-1 {{use of undeclared identifier 'Invalid_Function'}}
425 // expected-note@-1 {{in instantiation of function template specialization 'PR20731::Logger<>' requested here}}
428 template <typename T
>
431 // expected-error@-1 {{field has incomplete type 'void'}}
434 template <typename F
>
436 auto a
= A
<decltype(f())>{};
437 // expected-note@-1 {{in instantiation of template class 'PR20731::A<void>' requested here}}
438 auto xf
= [a
, f
]() {};
443 // expected-note-re@-1 {{in instantiation of function template specialization 'PR20731::g<(lambda at {{.*}}>' requested here}}
446 template <class _Rp
> struct function
{
449 static_assert(sizeof(_Fp
) > 0, "Type must be complete.");
453 template <typename T
> void p(T t
) {
454 auto l
= some_undefined_function(t
);
455 // expected-error@-1 {{use of undeclared identifier 'some_undefined_function'}}
456 function
<void()>(([l
]() {}));
459 // expected-note@-1 {{in instantiation of function template specialization 'PR20731::p<int>' requested here}}
462 namespace lambda_in_default_mem_init
{
463 template<typename T
> void f() {
464 struct S
{ int n
= []{ return 0; }(); };
466 template void f
<int>();
468 template<typename T
> void g() {
469 struct S
{ int n
= [](int n
){ return n
; }(0); };
471 template void g
<int>();
474 namespace error_in_transform_prototype
{
477 // expected-error@+2 {{type 'int' cannot be used prior to '::' because it has no members}}
478 // expected-error@+1 {{no member named 'ns' in 'error_in_transform_prototype::S'}}
479 auto x
= [](typename
T::ns::type
&k
) {}; // expected-note 2 {{while substituting into a lambda}}
483 f(5); // expected-note {{requested here}}
484 f(S()); // expected-note {{requested here}}
489 template<typename Fn
> struct fun
: Fn
{
491 using Fn::operator();
493 template<typename Fn
> fun
<Fn
> wrap(Fn fn
);
494 auto x
= wrap([](){});
499 void Method(char c
= []()->char {
502 int Method() { return 0; }
512 template <class> struct A
{
513 void f(int x
= []() {
527 namespace rdar22032373
{
529 auto blk
= [](bool b
) {
531 return undeclared_error
; // expected-error {{use of undeclared identifier}}
535 return undef(); // expected-error {{use of undeclared identifier}}
536 return 0; // verify no init_conversion_failed diagnostic emitted.
541 namespace nested_lambda
{
548 auto inner
= [](S
<num
> &X
) {};
554 struct A
{ template <class T
> A(T
); };
559 A a
= [&] { int y
= x
; };
560 A b
= [&] { [&] { [&] { int y
= x
; }; }; };
561 A d
= [&](auto param
) { int y
= x
; };
562 A e
= [&](auto param
) { [&] { [&](auto param2
) { int y
= x
; }; }; };
567 template <class T
> struct C
{
570 A f
= [&] { int y
= x
; };
581 int name1
; // expected-note {{'name1' declared here}}
585 S1(T t
) { s
= sizeof(t
); }
590 auto s0
= S1
{[name
=]() {}}; // expected-error 2 {{expected expression}}
591 auto s1
= S1
{[name
=name
]() {}}; // expected-error {{use of undeclared identifier 'name'; did you mean 'name1'?}}
595 namespace PR25627_dont_odr_use_local_consts
{
597 template<int> struct X
{};
601 (void) [] { X
<N
> x
; };
605 namespace ConversionOperatorDoesNotHaveDeducedReturnType
{
607 auto y
= [](auto &v
) -> void { v
.n
= 0; };
608 using T
= decltype(x
);
609 using U
= decltype(y
);
610 using ExpectedTypeT
= void (*)(int);
612 using ExpectedTypeU
= void (*)(T
&);
615 #if __cplusplus > 201402L
616 friend constexpr auto T::operator()(int) const;
617 friend constexpr T::operator ExpectedTypeT() const noexcept
;
620 friend constexpr void U::operator()(T
&) const;
621 // FIXME: This should not match; the return type is specified as behaving
622 // "as if it were a decltype-specifier denoting the return type of
623 // [operator()]", which is not equivalent to this alias template.
625 friend constexpr U::operator ExpectedTypeU
<T
>() const noexcept
;
627 friend auto T::operator()(int) const;
628 friend T::operator ExpectedTypeT() const;
631 friend void U::operator()(T
&) const;
632 // FIXME: This should not match, as above.
634 friend U::operator ExpectedTypeU
<T
>() const;
641 // Should be OK: lambda's call operator is a friend.
642 void use(X
&x
) { y(x
); }
644 // This used to crash in return type deduction for the conversion opreator.
645 struct A
{ int n
; void f() { +[](decltype(n
)) {}; } };
648 namespace TypoCorrection
{
649 template <typename T
> struct X
{};
650 // expected-note@-1 {{template parameter is declared here}}
652 template <typename T
>
653 void Run(const int& points
) {
654 // expected-note@-1 {{'points' declared here}}
655 auto outer_lambda
= []() {
656 auto inner_lambda
= [](const X
<Points
>&) {};
657 // expected-error@-1 {{use of undeclared identifier 'Points'; did you mean 'points'?}}
658 // expected-error@-2 {{template argument for template type parameter must be a type}}
663 void operator_parens() {
664 [&](int x
){ operator()(); }(0); // expected-error {{undeclared 'operator()'}}
667 namespace captured_name
{
669 union { // expected-note {{'' declared here}}
672 [] { return i
; }; // expected-error {{variable '' cannot be implicitly captured in a lambda with no capture-default specified}}
673 // expected-note@-1 {{lambda expression begins here}}
674 // expected-note@-2 2 {{default capture by}}
679 // Lambdas should not try to capture
680 // function parameters that are used in enable_if
681 struct StringLiteral
{
683 StringLiteral(const char (&array
)[N
])
684 __attribute__((enable_if(__builtin_strlen(array
) == 2,
685 "invalid string literal")));
688 namespace cpp_attribute
{
689 struct StringLiteral
{
691 StringLiteral(const char (&array
)[N
]) [[clang::annotate_type("test", array
)]];
696 [[maybe_unused
]] auto y
= [&](decltype(StringLiteral("xx"))) {};
697 [[maybe_unused
]] auto z
= [&](decltype(cpp_attribute::StringLiteral("xx"))) {};
702 #if __cplusplus > 201402L
706 // `&i` in default initializer causes implicit `this` access.
710 static_assert([]() constexpr {
712 return r
.p
!= nullptr;
714 } // namespace GH60936
717 // Call operator attributes refering to a variable should
718 // be properly handled after D124351
721 (void)[=][[gnu::aligned(i
)]] () {}; // expected-warning{{C++23 extension}}
722 // CHECK: AlignedAttr
723 // CHECK-NEXT: ConstantExpr
724 // CHECK-NEXT: value: Int 2
728 auto a
= []()__attribute__((b(({ return 0; })))){}; // expected-warning {{unknown attribute 'b' ignored}}
732 constexpr auto test
= 42;
733 auto lambda
= (test
, []() noexcept(true) {});