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 -fsyntax-only -verify -fblocks %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}}
160 int arr
[m
]; // expected-error{{negative size}}
164 int arr
[m
]; // expected-error{{negative size}}
168 int arr
[m
]; // expected-error{{negative size}}
172 int arr
[m
]; // expected-error{{negative size}}
179 unsigned int result
= 0;
180 auto l
= [&]() { ++result
; };
183 namespace ModifyingCapture
{
187 n
= 1; // expected-error {{cannot assign to a variable captured by copy in a non-mutable lambda}}
192 namespace VariadicPackExpansion
{
193 template<typename T
, typename U
> using Fst
= T
;
194 template<typename
...Ts
> bool g(Fst
<bool, Ts
> ...bools
);
195 template<typename
...Ts
> bool f(Ts
&&...ts
) {
196 return g
<Ts
...>([&ts
] {
204 int a
= 5, b
= 2, c
= 3;
210 template<typename
...Ts
> sink(Ts
&&...) {}
213 template<typename
...Ts
> void local_class() {
221 Ts
g() { return *this; };
229 struct X
{}; struct Y
{};
230 template void local_class
<X
, Y
>();
232 template<typename
...Ts
> void nested(Ts
...ts
) {
234 // Each expansion of this lambda implicitly captures all of 'ts', because
235 // the inner lambda also expands 'ts'.
237 return ts
+ [&] { return f(ts
...); } ();
241 template void nested(int, int, int);
243 template<typename
...Ts
> void nested2(Ts
...ts
) { // expected-note 2{{here}}
244 // Capture all 'ts', use only one.
245 f([&ts
...] { return ts
; } ()...);
246 // Capture each 'ts', use it.
247 f([&ts
] { return ts
; } ()...);
248 // Capture all 'ts', use all of them.
249 f([&ts
...] { return (int)f(ts
...); } ());
250 // Capture each 'ts', use all of them. Ill-formed. In more detail:
252 // We instantiate two lambdas here; the first captures ts$0, the second
253 // captures ts$1. Both of them reference both ts parameters, so both are
254 // ill-formed because ts can't be implicitly captured.
256 // FIXME: This diagnostic does not explain what's happening. We should
257 // specify which 'ts' we're referring to in its diagnostic name. We should
258 // also say which slice of the pack expansion is being performed in the
259 // instantiation backtrace.
260 f([&ts
] { return (int)f(ts
...); } ()...); // \
261 // expected-error 2{{'ts' cannot be implicitly captured}} \
262 // expected-note 2{{lambda expression begins here}} \
263 // expected-note 4 {{capture 'ts' by}}
265 template void nested2(int); // ok
266 template void nested2(int, int); // expected-note {{in instantiation of}}
271 auto x
= PR13860UndeclaredIdentifier(); // expected-error {{use of undeclared identifier 'PR13860UndeclaredIdentifier'}}
273 static_assert(sizeof(y
), "");
282 auto f
= [](void) { return __func__
; }; // no-warning
293 namespace TypeDeduction
{
297 S
&&t
= [&] { return s
; } ();
298 #if __cplusplus > 201103L
299 S
&&u
= [&] () -> auto { return s
; } ();
305 namespace lambdas_in_NSDMIs
{
309 T t2
= ([](int a
) { return [](int b
) { return b
; };})(t
)(t
);
313 namespace non_template
{
316 int t2
= ([](int a
) { return [](int b
) { return b
; };})(t
)(t
);
322 // PR18477: don't try to capture 'this' from an NSDMI encountered while parsing
324 namespace NSDMIs_in_lambdas
{
325 template<typename T
> struct S
{ int a
= 0; int b
= a
; };
326 void f() { []() { S
<int> s
; }; }
328 auto x
= []{ struct S
{ int n
, m
= n
; }; };
329 auto y
= [&]{ struct S
{ int n
, m
= n
; }; }; // expected-error {{non-local lambda expression cannot have a capture-default}}
330 void g() { auto z
= [&]{ struct S
{ int n
, m
= n
; }; }; }
333 namespace CaptureIncomplete
{
334 struct Incomplete
; // expected-note 2{{forward decl}}
335 void g(const Incomplete
&a
);
336 void f(Incomplete
&a
) {
337 (void) [a
] {}; // expected-error {{incomplete}}
340 (void) [=] { g(a
); }; // expected-error {{incomplete}}
341 (void) [&] { f(a
); };
345 namespace CaptureAbstract
{
347 virtual void f() = 0; // expected-note {{unimplemented}}
356 S
&s
= const_cast<T
&>(t
);
357 // FIXME: Once we properly compute odr-use per DR712, this should be
358 // accepted (and should not capture 's').
359 [=] { return s
.n
; }; // expected-error {{abstract}}
364 auto l
= [=]{}; // expected-error {{non-local lambda expression cannot have a capture-default}}
368 int (*f())[true ? 1 : ([=]{ return n
; }(), 0)];
369 // expected-error@-1 {{non-local lambda expression cannot have a capture-default}}
370 // expected-error@-2 {{invalid use of non-static data member 'n'}}
371 // expected-cxx14-error@-3 {{a lambda expression may not appear inside of a constant expression}}
372 int g(int k
= ([=]{ return n
; }(), 0));
373 // expected-error@-1 {{non-local lambda expression cannot have a capture-default}}
374 // expected-error@-2 {{invalid use of non-static data member 'n'}}
376 int a
= [=]{ return n
; }(); // ok
377 int b
= [=]{ return [=]{ return n
; }(); }(); // ok
378 int c
= []{ int k
= 0; return [=]{ return k
; }(); }(); // ok
379 int d
= [] { return [=] { return n
; }(); }(); // expected-error {{'this' cannot be implicitly captured in this context}} expected-note {{explicitly capture 'this'}}
384 template<typename T
> void f() {
386 (void) [=]{ int n
= t
; }; // expected-error {{deleted}}
389 template void f
<int>();
392 NoCopy(const NoCopy
&) = delete; // expected-note {{deleted}}
393 operator int() const;
395 template void f
<NoCopy
>(); // expected-note {{instantiation}}
399 auto x
= [&x
]{}; // expected-error {{cannot appear in its own init}}
403 template <class L
, int X
= sizeof(L
)>
406 template <typename
... Args
>
407 void Logger(Args
&&... args
) {
408 auto len
= Invalid_Function((args
)...);
409 // expected-error@-1 {{use of undeclared identifier 'Invalid_Function'}}
415 // expected-note@-1 {{in instantiation of function template specialization 'PR20731::Logger<>' requested here}}
418 template <typename T
>
421 // expected-error@-1 {{field has incomplete type 'void'}}
424 template <typename F
>
426 auto a
= A
<decltype(f())>{};
427 // expected-note@-1 {{in instantiation of template class 'PR20731::A<void>' requested here}}
428 auto xf
= [a
, f
]() {};
433 // expected-note-re@-1 {{in instantiation of function template specialization 'PR20731::g<(lambda at {{.*}}>' requested here}}
436 template <class _Rp
> struct function
{
439 static_assert(sizeof(_Fp
) > 0, "Type must be complete.");
443 template <typename T
> void p(T t
) {
444 auto l
= some_undefined_function(t
);
445 // expected-error@-1 {{use of undeclared identifier 'some_undefined_function'}}
446 function
<void()>(([l
]() {}));
449 // expected-note@-1 {{in instantiation of function template specialization 'PR20731::p<int>' requested here}}
452 namespace lambda_in_default_mem_init
{
453 template<typename T
> void f() {
454 struct S
{ int n
= []{ return 0; }(); };
456 template void f
<int>();
458 template<typename T
> void g() {
459 struct S
{ int n
= [](int n
){ return n
; }(0); };
461 template void g
<int>();
464 namespace error_in_transform_prototype
{
467 // expected-error@+2 {{type 'int' cannot be used prior to '::' because it has no members}}
468 // expected-error@+1 {{no member named 'ns' in 'error_in_transform_prototype::S'}}
469 auto x
= [](typename
T::ns::type
&k
) {};
473 f(5); // expected-note {{requested here}}
474 f(S()); // expected-note {{requested here}}
479 template<typename Fn
> struct fun
: Fn
{
481 using Fn::operator();
483 template<typename Fn
> fun
<Fn
> wrap(Fn fn
);
484 auto x
= wrap([](){});
489 void Method(char c
= []()->char {
492 int Method() { return 0; }
502 template <class> struct A
{
503 void f(int x
= []() {
518 namespace rdar22032373
{
520 auto blk
= [](bool b
) {
522 return undeclared_error
; // expected-error {{use of undeclared identifier}}
526 return undef(); // expected-error {{use of undeclared identifier}}
527 return 0; // verify no init_conversion_failed diagnostic emitted.
532 namespace nested_lambda
{
539 auto inner
= [](S
<num
> &X
) {};
545 struct A
{ template <class T
> A(T
); };
550 A a
= [&] { int y
= x
; };
551 A b
= [&] { [&] { [&] { int y
= x
; }; }; };
552 A d
= [&](auto param
) { int y
= x
; };
553 A e
= [&](auto param
) { [&] { [&](auto param2
) { int y
= x
; }; }; };
558 template <class T
> struct C
{
561 A f
= [&] { int y
= x
; };
572 int name1
; // expected-note {{'name1' declared here}}
576 S1(T t
) { s
= sizeof(t
); }
581 auto s0
= S1
{[name
=]() {}}; // expected-error 2 {{expected expression}}
582 auto s1
= S1
{[name
=name
]() {}}; // expected-error {{use of undeclared identifier 'name'; did you mean 'name1'?}}
586 namespace PR25627_dont_odr_use_local_consts
{
588 template<int> struct X
{};
592 (void) [] { X
<N
> x
; };
596 namespace ConversionOperatorDoesNotHaveDeducedReturnType
{
598 auto y
= [](auto &v
) -> void { v
.n
= 0; };
599 using T
= decltype(x
);
600 using U
= decltype(y
);
601 using ExpectedTypeT
= void (*)(int);
603 using ExpectedTypeU
= void (*)(T
&);
606 #if __cplusplus > 201402L
607 friend constexpr auto T::operator()(int) const;
608 friend constexpr T::operator ExpectedTypeT() const noexcept
;
611 friend constexpr void U::operator()(T
&) const;
612 // FIXME: This should not match; the return type is specified as behaving
613 // "as if it were a decltype-specifier denoting the return type of
614 // [operator()]", which is not equivalent to this alias template.
616 friend constexpr U::operator ExpectedTypeU
<T
>() const noexcept
;
618 friend auto T::operator()(int) const;
619 friend T::operator ExpectedTypeT() const;
622 friend void U::operator()(T
&) const;
623 // FIXME: This should not match, as above.
625 friend U::operator ExpectedTypeU
<T
>() const;
632 // Should be OK: lambda's call operator is a friend.
633 void use(X
&x
) { y(x
); }
635 // This used to crash in return type deduction for the conversion opreator.
636 struct A
{ int n
; void f() { +[](decltype(n
)) {}; } };
639 namespace TypoCorrection
{
640 template <typename T
> struct X
{};
641 // expected-note@-1 {{template parameter is declared here}}
643 template <typename T
>
644 void Run(const int& points
) {
645 // expected-note@-1 {{'points' declared here}}
646 auto outer_lambda
= []() {
647 auto inner_lambda
= [](const X
<Points
>&) {};
648 // expected-error@-1 {{use of undeclared identifier 'Points'; did you mean 'points'?}}
649 // expected-error@-2 {{template argument for template type parameter must be a type}}
654 void operator_parens() {
655 [&](int x
){ operator()(); }(0); // expected-error {{undeclared 'operator()'}}
658 namespace captured_name
{
660 union { // expected-note {{'' declared here}}
663 [] { return i
; }; // expected-error {{variable '' cannot be implicitly captured in a lambda with no capture-default specified}}
664 // expected-note@-1 {{lambda expression begins here}}
665 // expected-note@-2 2 {{default capture by}}