xref: /minix3/external/bsd/llvm/dist/clang/test/SemaCXX/lambda-expressions.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc // RUN: %clang_cc1 -std=c++11 -Wno-unused-value -fsyntax-only -verify -fblocks %s
2f4a2713aSLionel Sambuc // RUN: %clang_cc1 -std=c++1y -Wno-unused-value -fsyntax-only -verify -fblocks %s
3f4a2713aSLionel Sambuc 
4f4a2713aSLionel Sambuc namespace std { class type_info; };
5f4a2713aSLionel Sambuc 
6f4a2713aSLionel Sambuc namespace ExplicitCapture {
7f4a2713aSLionel Sambuc   class C {
8f4a2713aSLionel Sambuc     int Member;
9f4a2713aSLionel Sambuc 
10f4a2713aSLionel Sambuc     static void Overload(int);
11f4a2713aSLionel Sambuc     void Overload();
12f4a2713aSLionel Sambuc     virtual C& Overload(float);
13f4a2713aSLionel Sambuc 
ImplicitThisCapture()14f4a2713aSLionel Sambuc     void ImplicitThisCapture() {
15f4a2713aSLionel Sambuc       [](){(void)Member;}; // expected-error {{'this' cannot be implicitly captured in this context}}
16f4a2713aSLionel Sambuc       [&](){(void)Member;};
17f4a2713aSLionel Sambuc 
18f4a2713aSLionel Sambuc       [this](){(void)Member;};
19f4a2713aSLionel Sambuc       [this]{[this]{};};
20f4a2713aSLionel Sambuc       []{[this]{};};// expected-error {{'this' cannot be implicitly captured in this context}}
21f4a2713aSLionel Sambuc       []{Overload(3);};
22f4a2713aSLionel Sambuc       []{Overload();}; // expected-error {{'this' cannot be implicitly captured in this context}}
23f4a2713aSLionel Sambuc       []{(void)typeid(Overload());};
24f4a2713aSLionel Sambuc       []{(void)typeid(Overload(.5f));};// expected-error {{'this' cannot be implicitly captured in this context}}
25f4a2713aSLionel Sambuc     }
26f4a2713aSLionel Sambuc   };
27f4a2713aSLionel Sambuc 
f()28f4a2713aSLionel Sambuc   void f() {
29f4a2713aSLionel Sambuc     [this] () {}; // expected-error {{'this' cannot be captured in this context}}
30f4a2713aSLionel Sambuc   }
31f4a2713aSLionel Sambuc }
32f4a2713aSLionel Sambuc 
33f4a2713aSLionel Sambuc namespace ReturnDeduction {
test()34f4a2713aSLionel Sambuc   void test() {
35f4a2713aSLionel Sambuc     [](){ return 1; };
36f4a2713aSLionel Sambuc     [](){ return 1; };
37f4a2713aSLionel Sambuc     [](){ return ({return 1; 1;}); };
38f4a2713aSLionel Sambuc     [](){ return ({return 'c'; 1;}); }; // expected-error {{must match previous return type}}
39f4a2713aSLionel Sambuc     []()->int{ return 'c'; return 1; };
40f4a2713aSLionel Sambuc     [](){ return 'c'; return 1; };  // expected-error {{must match previous return type}}
41f4a2713aSLionel Sambuc     []() { return; return (void)0; };
42f4a2713aSLionel Sambuc     [](){ return 1; return 1; };
43f4a2713aSLionel Sambuc   }
44f4a2713aSLionel Sambuc }
45f4a2713aSLionel Sambuc 
46f4a2713aSLionel Sambuc namespace ImplicitCapture {
test()47f4a2713aSLionel Sambuc   void test() {
48f4a2713aSLionel Sambuc     int a = 0; // expected-note 5 {{declared}}
49f4a2713aSLionel Sambuc     []() { return a; }; // expected-error {{variable 'a' cannot be implicitly captured in a lambda with no capture-default specified}} expected-note {{begins here}}
50f4a2713aSLionel Sambuc     [&]() { return a; };
51f4a2713aSLionel Sambuc     [=]() { return a; };
52f4a2713aSLionel Sambuc     [=]() { int* b = &a; }; // expected-error {{cannot initialize a variable of type 'int *' with an rvalue of type 'const int *'}}
53f4a2713aSLionel Sambuc     [=]() { return [&]() { return a; }; };
54f4a2713aSLionel Sambuc     []() { 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}}
55f4a2713aSLionel Sambuc     []() { 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}}
56f4a2713aSLionel Sambuc     []() { 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}}
57f4a2713aSLionel Sambuc     [=]() { return [&a] { return a; }; }; //
58f4a2713aSLionel Sambuc 
59f4a2713aSLionel Sambuc     const int b = 2;
60f4a2713aSLionel Sambuc     []() { return b; };
61f4a2713aSLionel Sambuc 
62f4a2713aSLionel Sambuc     union { // expected-note {{declared}}
63f4a2713aSLionel Sambuc       int c;
64f4a2713aSLionel Sambuc       float d;
65f4a2713aSLionel Sambuc     };
66f4a2713aSLionel Sambuc     d = 3;
67f4a2713aSLionel Sambuc     [=]() { return c; }; // expected-error {{unnamed variable cannot be implicitly captured in a lambda expression}}
68f4a2713aSLionel Sambuc 
69f4a2713aSLionel Sambuc     __block int e; // expected-note 3 {{declared}}
70f4a2713aSLionel Sambuc     [&]() { return e; }; // expected-error {{__block variable 'e' cannot be captured in a lambda expression}}
71f4a2713aSLionel Sambuc     [&e]() { return e; }; // expected-error 2 {{__block variable 'e' cannot be captured in a lambda expression}}
72f4a2713aSLionel Sambuc 
73f4a2713aSLionel Sambuc     int f[10]; // expected-note {{declared}}
74f4a2713aSLionel Sambuc     [&]() { return f[2]; };
75f4a2713aSLionel Sambuc     (void) ^{ return []() { return f[2]; }; }; // expected-error {{variable 'f' cannot be implicitly captured in a lambda with no capture-default specified}} \
76f4a2713aSLionel Sambuc     // expected-note{{lambda expression begins here}}
77f4a2713aSLionel Sambuc 
78f4a2713aSLionel Sambuc     struct G { G(); G(G&); int a; }; // expected-note 6 {{not viable}}
79f4a2713aSLionel Sambuc     G g;
80f4a2713aSLionel Sambuc     [=]() { const G* gg = &g; return gg->a; };
81f4a2713aSLionel Sambuc     [=]() { return [=]{ const G* gg = &g; return gg->a; }(); }; // expected-error {{no matching constructor for initialization of 'G'}}
82f4a2713aSLionel Sambuc     (void)^{ return [=]{ const G* gg = &g; return gg->a; }(); }; // expected-error 2 {{no matching constructor for initialization of 'const G'}}
83f4a2713aSLionel Sambuc 
84f4a2713aSLionel Sambuc     const int h = a; // expected-note {{declared}}
85f4a2713aSLionel Sambuc     []() { return h; }; // expected-error {{variable 'h' cannot be implicitly captured in a lambda with no capture-default specified}} expected-note {{lambda expression begins here}}
86f4a2713aSLionel Sambuc 
87f4a2713aSLionel Sambuc     // References can appear in constant expressions if they are initialized by
88f4a2713aSLionel Sambuc     // reference constant expressions.
89f4a2713aSLionel Sambuc     int i;
90f4a2713aSLionel Sambuc     int &ref_i = i; // expected-note {{declared}}
91f4a2713aSLionel Sambuc     [] { 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}}
92f4a2713aSLionel Sambuc 
93f4a2713aSLionel Sambuc     static int j;
94f4a2713aSLionel Sambuc     int &ref_j = j;
95f4a2713aSLionel Sambuc     [] { return ref_j; }; // ok
96f4a2713aSLionel Sambuc   }
97f4a2713aSLionel Sambuc }
98f4a2713aSLionel Sambuc 
99f4a2713aSLionel Sambuc namespace PR12031 {
100f4a2713aSLionel Sambuc   struct X {
101f4a2713aSLionel Sambuc     template<typename T>
102f4a2713aSLionel Sambuc     X(const T&);
103f4a2713aSLionel Sambuc     ~X();
104f4a2713aSLionel Sambuc   };
105f4a2713aSLionel Sambuc 
106f4a2713aSLionel Sambuc   void f(int i, X x);
g()107f4a2713aSLionel Sambuc   void g() {
108f4a2713aSLionel Sambuc     const int v = 10;
109f4a2713aSLionel Sambuc     f(v, [](){});
110f4a2713aSLionel Sambuc   }
111f4a2713aSLionel Sambuc }
112f4a2713aSLionel Sambuc 
113f4a2713aSLionel Sambuc namespace Array {
114f4a2713aSLionel Sambuc   int &f(int *p);
115f4a2713aSLionel Sambuc   char &f(...);
g()116f4a2713aSLionel Sambuc   void g() {
117f4a2713aSLionel Sambuc     int n = -1;
118f4a2713aSLionel Sambuc     [=] {
119f4a2713aSLionel Sambuc       int arr[n]; // VLA
120f4a2713aSLionel Sambuc     } ();
121f4a2713aSLionel Sambuc 
122f4a2713aSLionel Sambuc     const int m = -1;
123f4a2713aSLionel Sambuc     [] {
124f4a2713aSLionel Sambuc       int arr[m]; // expected-error{{negative size}}
125f4a2713aSLionel Sambuc     } ();
126f4a2713aSLionel Sambuc 
127f4a2713aSLionel Sambuc     [&] {
128f4a2713aSLionel Sambuc       int arr[m]; // expected-error{{negative size}}
129f4a2713aSLionel Sambuc     } ();
130f4a2713aSLionel Sambuc 
131f4a2713aSLionel Sambuc     [=] {
132f4a2713aSLionel Sambuc       int arr[m]; // expected-error{{negative size}}
133f4a2713aSLionel Sambuc     } ();
134f4a2713aSLionel Sambuc 
135f4a2713aSLionel Sambuc     [m] {
136f4a2713aSLionel Sambuc       int arr[m]; // expected-error{{negative size}}
137f4a2713aSLionel Sambuc     } ();
138f4a2713aSLionel Sambuc   }
139f4a2713aSLionel Sambuc }
140f4a2713aSLionel Sambuc 
PR12248()141f4a2713aSLionel Sambuc void PR12248()
142f4a2713aSLionel Sambuc {
143f4a2713aSLionel Sambuc   unsigned int result = 0;
144f4a2713aSLionel Sambuc   auto l = [&]() { ++result; };
145f4a2713aSLionel Sambuc }
146f4a2713aSLionel Sambuc 
147f4a2713aSLionel Sambuc namespace ModifyingCapture {
test()148f4a2713aSLionel Sambuc   void test() {
149f4a2713aSLionel Sambuc     int n = 0;
150f4a2713aSLionel Sambuc     [=] {
151f4a2713aSLionel Sambuc       n = 1; // expected-error {{cannot assign to a variable captured by copy in a non-mutable lambda}}
152f4a2713aSLionel Sambuc     };
153f4a2713aSLionel Sambuc   }
154f4a2713aSLionel Sambuc }
155f4a2713aSLionel Sambuc 
156f4a2713aSLionel Sambuc namespace VariadicPackExpansion {
157f4a2713aSLionel Sambuc   template<typename T, typename U> using Fst = T;
158f4a2713aSLionel Sambuc   template<typename...Ts> bool g(Fst<bool, Ts> ...bools);
f(Ts &&...ts)159f4a2713aSLionel Sambuc   template<typename...Ts> bool f(Ts &&...ts) {
160f4a2713aSLionel Sambuc     return g<Ts...>([&ts] {
161f4a2713aSLionel Sambuc       if (!ts)
162f4a2713aSLionel Sambuc         return false;
163f4a2713aSLionel Sambuc       --ts;
164f4a2713aSLionel Sambuc       return true;
165f4a2713aSLionel Sambuc     } () ...);
166f4a2713aSLionel Sambuc   }
h()167f4a2713aSLionel Sambuc   void h() {
168f4a2713aSLionel Sambuc     int a = 5, b = 2, c = 3;
169f4a2713aSLionel Sambuc     while (f(a, b, c)) {
170f4a2713aSLionel Sambuc     }
171f4a2713aSLionel Sambuc   }
172f4a2713aSLionel Sambuc 
173f4a2713aSLionel Sambuc   struct sink {
sinkVariadicPackExpansion::sink174f4a2713aSLionel Sambuc     template<typename...Ts> sink(Ts &&...) {}
175f4a2713aSLionel Sambuc   };
176f4a2713aSLionel Sambuc 
local_class()177f4a2713aSLionel Sambuc   template<typename...Ts> void local_class() {
178f4a2713aSLionel Sambuc     sink {
179f4a2713aSLionel Sambuc       [] (Ts t) {
180f4a2713aSLionel Sambuc         struct S : Ts {
181f4a2713aSLionel Sambuc           void f(Ts t) {
182f4a2713aSLionel Sambuc             Ts &that = *this;
183f4a2713aSLionel Sambuc             that = t;
184f4a2713aSLionel Sambuc           }
185f4a2713aSLionel Sambuc           Ts g() { return *this; };
186f4a2713aSLionel Sambuc         };
187f4a2713aSLionel Sambuc         S s;
188f4a2713aSLionel Sambuc         s.f(t);
189f4a2713aSLionel Sambuc         return s;
190f4a2713aSLionel Sambuc       } (Ts()).g() ...
191f4a2713aSLionel Sambuc     };
192f4a2713aSLionel Sambuc   };
193f4a2713aSLionel Sambuc   struct X {}; struct Y {};
194f4a2713aSLionel Sambuc   template void local_class<X, Y>();
195f4a2713aSLionel Sambuc 
nested(Ts...ts)196f4a2713aSLionel Sambuc   template<typename...Ts> void nested(Ts ...ts) {
197f4a2713aSLionel Sambuc     f(
198f4a2713aSLionel Sambuc       // Each expansion of this lambda implicitly captures all of 'ts', because
199f4a2713aSLionel Sambuc       // the inner lambda also expands 'ts'.
200f4a2713aSLionel Sambuc       [&] {
201f4a2713aSLionel Sambuc         return ts + [&] { return f(ts...); } ();
202f4a2713aSLionel Sambuc       } () ...
203f4a2713aSLionel Sambuc     );
204f4a2713aSLionel Sambuc   }
205f4a2713aSLionel Sambuc   template void nested(int, int, int);
206f4a2713aSLionel Sambuc 
nested2(Ts...ts)207f4a2713aSLionel Sambuc   template<typename...Ts> void nested2(Ts ...ts) { // expected-note 2{{here}}
208f4a2713aSLionel Sambuc     // Capture all 'ts', use only one.
209f4a2713aSLionel Sambuc     f([&ts...] { return ts; } ()...);
210f4a2713aSLionel Sambuc     // Capture each 'ts', use it.
211f4a2713aSLionel Sambuc     f([&ts] { return ts; } ()...);
212f4a2713aSLionel Sambuc     // Capture all 'ts', use all of them.
213f4a2713aSLionel Sambuc     f([&ts...] { return (int)f(ts...); } ());
214f4a2713aSLionel Sambuc     // Capture each 'ts', use all of them. Ill-formed. In more detail:
215f4a2713aSLionel Sambuc     //
216f4a2713aSLionel Sambuc     // We instantiate two lambdas here; the first captures ts$0, the second
217f4a2713aSLionel Sambuc     // captures ts$1. Both of them reference both ts parameters, so both are
218f4a2713aSLionel Sambuc     // ill-formed because ts can't be implicitly captured.
219f4a2713aSLionel Sambuc     //
220f4a2713aSLionel Sambuc     // FIXME: This diagnostic does not explain what's happening. We should
221f4a2713aSLionel Sambuc     // specify which 'ts' we're referring to in its diagnostic name. We should
222f4a2713aSLionel Sambuc     // also say which slice of the pack expansion is being performed in the
223f4a2713aSLionel Sambuc     // instantiation backtrace.
224f4a2713aSLionel Sambuc     f([&ts] { return (int)f(ts...); } ()...); // \
225f4a2713aSLionel Sambuc     // expected-error 2{{'ts' cannot be implicitly captured}} \
226f4a2713aSLionel Sambuc     // expected-note 2{{lambda expression begins here}}
227f4a2713aSLionel Sambuc   }
228f4a2713aSLionel Sambuc   template void nested2(int); // ok
229f4a2713aSLionel Sambuc   template void nested2(int, int); // expected-note {{in instantiation of}}
230f4a2713aSLionel Sambuc }
231f4a2713aSLionel Sambuc 
232f4a2713aSLionel Sambuc namespace PR13860 {
foo()233f4a2713aSLionel Sambuc   void foo() {
234f4a2713aSLionel Sambuc     auto x = PR13860UndeclaredIdentifier(); // expected-error {{use of undeclared identifier 'PR13860UndeclaredIdentifier'}}
235f4a2713aSLionel Sambuc     auto y = [x]() { };
236f4a2713aSLionel Sambuc     static_assert(sizeof(y), "");
237f4a2713aSLionel Sambuc   }
238f4a2713aSLionel Sambuc }
239f4a2713aSLionel Sambuc 
240f4a2713aSLionel Sambuc namespace PR13854 {
__anonef18302f4102(void)241f4a2713aSLionel Sambuc   auto l = [](void){};
242f4a2713aSLionel Sambuc }
243f4a2713aSLionel Sambuc 
244f4a2713aSLionel Sambuc namespace PR14518 {
__anonef18302f4202(void) 245f4a2713aSLionel Sambuc   auto f = [](void) { return __func__; }; // no-warning
246f4a2713aSLionel Sambuc }
247f4a2713aSLionel Sambuc 
248f4a2713aSLionel Sambuc namespace PR16708 {
__anonef18302f4302() 249f4a2713aSLionel Sambuc   auto L = []() {
250f4a2713aSLionel Sambuc     auto ret = 0;
251f4a2713aSLionel Sambuc     return ret;
252f4a2713aSLionel Sambuc     return 0;
253f4a2713aSLionel Sambuc   };
254f4a2713aSLionel Sambuc }
255f4a2713aSLionel Sambuc 
256f4a2713aSLionel Sambuc namespace TypeDeduction {
257f4a2713aSLionel Sambuc   struct S {};
f()258f4a2713aSLionel Sambuc   void f() {
259f4a2713aSLionel Sambuc     const S s {};
260f4a2713aSLionel Sambuc     S &&t = [&] { return s; } ();
261*0a6a1f1dSLionel Sambuc #if __cplusplus > 201103L
262f4a2713aSLionel Sambuc     S &&u = [&] () -> auto { return s; } ();
263f4a2713aSLionel Sambuc #endif
264f4a2713aSLionel Sambuc   }
265f4a2713aSLionel Sambuc }
266f4a2713aSLionel Sambuc 
267f4a2713aSLionel Sambuc 
268f4a2713aSLionel Sambuc namespace lambdas_in_NSDMIs {
269f4a2713aSLionel Sambuc   template<class T>
270f4a2713aSLionel Sambuc   struct L {
271f4a2713aSLionel Sambuc       T t{};
__anonef18302f4702(int b) 272f4a2713aSLionel Sambuc       T t2 = ([](int a) { return [](int b) { return b; };})(t)(t);
273f4a2713aSLionel Sambuc   };
274f4a2713aSLionel Sambuc   L<int> l;
275f4a2713aSLionel Sambuc 
276f4a2713aSLionel Sambuc   namespace non_template {
277f4a2713aSLionel Sambuc     struct L {
278f4a2713aSLionel Sambuc       int t = 0;
__anonef18302f4802lambdas_in_NSDMIs::non_template::L279f4a2713aSLionel Sambuc       int t2 = ([](int a) { return [](int b) { return b; };})(t)(t);
280f4a2713aSLionel Sambuc     };
281f4a2713aSLionel Sambuc     L l;
282f4a2713aSLionel Sambuc   }
283f4a2713aSLionel Sambuc }
284*0a6a1f1dSLionel Sambuc 
285*0a6a1f1dSLionel Sambuc // PR18477: don't try to capture 'this' from an NSDMI encountered while parsing
286*0a6a1f1dSLionel Sambuc // a lambda.
287*0a6a1f1dSLionel Sambuc namespace NSDMIs_in_lambdas {
288*0a6a1f1dSLionel Sambuc   template<typename T> struct S { int a = 0; int b = a; };
f()289*0a6a1f1dSLionel Sambuc   void f() { []() { S<int> s; }; }
290*0a6a1f1dSLionel Sambuc 
__anonef18302f4b02null291*0a6a1f1dSLionel Sambuc   auto x = []{ struct S { int n, m = n; }; };
__anonef18302f4c02null292*0a6a1f1dSLionel Sambuc   auto y = [&]{ struct S { int n, m = n; }; }; // expected-error {{non-local lambda expression cannot have a capture-default}}
g()293*0a6a1f1dSLionel Sambuc   void g() { auto z = [&]{ struct S { int n, m = n; }; }; }
294*0a6a1f1dSLionel Sambuc }
295*0a6a1f1dSLionel Sambuc 
296*0a6a1f1dSLionel Sambuc namespace CaptureIncomplete {
297*0a6a1f1dSLionel Sambuc   struct Incomplete; // expected-note 2{{forward decl}}
298*0a6a1f1dSLionel Sambuc   void g(const Incomplete &a);
f(Incomplete & a)299*0a6a1f1dSLionel Sambuc   void f(Incomplete &a) {
300*0a6a1f1dSLionel Sambuc     (void) [a] {}; // expected-error {{incomplete}}
301*0a6a1f1dSLionel Sambuc     (void) [&a] {};
302*0a6a1f1dSLionel Sambuc 
303*0a6a1f1dSLionel Sambuc     (void) [=] { g(a); }; // expected-error {{incomplete}}
304*0a6a1f1dSLionel Sambuc     (void) [&] { f(a); };
305*0a6a1f1dSLionel Sambuc   }
306*0a6a1f1dSLionel Sambuc }
307*0a6a1f1dSLionel Sambuc 
308*0a6a1f1dSLionel Sambuc namespace CaptureAbstract {
309*0a6a1f1dSLionel Sambuc   struct S {
310*0a6a1f1dSLionel Sambuc     virtual void f() = 0; // expected-note {{unimplemented}}
311*0a6a1f1dSLionel Sambuc     int n = 0;
312*0a6a1f1dSLionel Sambuc   };
313*0a6a1f1dSLionel Sambuc   struct T : S {
TCaptureAbstract::T314*0a6a1f1dSLionel Sambuc     constexpr T() {}
315*0a6a1f1dSLionel Sambuc     void f();
316*0a6a1f1dSLionel Sambuc   };
f()317*0a6a1f1dSLionel Sambuc   void f() {
318*0a6a1f1dSLionel Sambuc     constexpr T t = T();
319*0a6a1f1dSLionel Sambuc     S &s = const_cast<T&>(t);
320*0a6a1f1dSLionel Sambuc     // FIXME: Once we properly compute odr-use per DR712, this should be
321*0a6a1f1dSLionel Sambuc     // accepted (and should not capture 's').
322*0a6a1f1dSLionel Sambuc     [=] { return s.n; }; // expected-error {{abstract}}
323*0a6a1f1dSLionel Sambuc   }
324*0a6a1f1dSLionel Sambuc }
325*0a6a1f1dSLionel Sambuc 
326*0a6a1f1dSLionel Sambuc namespace PR18128 {
__anonef18302f5302null327*0a6a1f1dSLionel Sambuc   auto l = [=]{}; // expected-error {{non-local lambda expression cannot have a capture-default}}
328*0a6a1f1dSLionel Sambuc 
329*0a6a1f1dSLionel Sambuc   struct S {
330*0a6a1f1dSLionel Sambuc     int n;
__anonef18302f5402PR18128::S331*0a6a1f1dSLionel Sambuc     int (*f())[true ? 1 : ([=]{ return n; }(), 0)];
332*0a6a1f1dSLionel Sambuc     // expected-error@-1 {{non-local lambda expression cannot have a capture-default}}
333*0a6a1f1dSLionel Sambuc     // expected-error@-2 {{invalid use of non-static data member 'n'}}
334*0a6a1f1dSLionel Sambuc     // expected-error@-3 {{a lambda expression may not appear inside of a constant expression}}
__anonef18302f5502PR18128::S335*0a6a1f1dSLionel Sambuc     int g(int k = ([=]{ return n; }(), 0));
336*0a6a1f1dSLionel Sambuc     // expected-error@-1 {{non-local lambda expression cannot have a capture-default}}
337*0a6a1f1dSLionel Sambuc     // expected-error@-2 {{invalid use of non-static data member 'n'}}
338*0a6a1f1dSLionel Sambuc 
__anonef18302f5602PR18128::S339*0a6a1f1dSLionel Sambuc     int a = [=]{ return n; }(); // ok
__anonef18302f5702PR18128::S340*0a6a1f1dSLionel Sambuc     int b = [=]{ return [=]{ return n; }(); }(); // ok
__anonef18302f5902PR18128::S341*0a6a1f1dSLionel Sambuc     int c = []{ int k = 0; return [=]{ return k; }(); }(); // ok
__anonef18302f5b02PR18128::S342*0a6a1f1dSLionel Sambuc     int d = []{ return [=]{ return n; }(); }(); // expected-error {{'this' cannot be implicitly captured in this context}}
343*0a6a1f1dSLionel Sambuc   };
344*0a6a1f1dSLionel Sambuc }
345*0a6a1f1dSLionel Sambuc 
346*0a6a1f1dSLionel Sambuc namespace PR18473 {
f()347*0a6a1f1dSLionel Sambuc   template<typename T> void f() {
348*0a6a1f1dSLionel Sambuc     T t(0);
349*0a6a1f1dSLionel Sambuc     (void) [=]{ int n = t; }; // expected-error {{deleted}}
350*0a6a1f1dSLionel Sambuc   }
351*0a6a1f1dSLionel Sambuc 
352*0a6a1f1dSLionel Sambuc   template void f<int>();
353*0a6a1f1dSLionel Sambuc   struct NoCopy {
354*0a6a1f1dSLionel Sambuc     NoCopy(int);
355*0a6a1f1dSLionel Sambuc     NoCopy(const NoCopy &) = delete; // expected-note {{deleted}}
356*0a6a1f1dSLionel Sambuc     operator int() const;
357*0a6a1f1dSLionel Sambuc   };
358*0a6a1f1dSLionel Sambuc   template void f<NoCopy>(); // expected-note {{instantiation}}
359*0a6a1f1dSLionel Sambuc }
360*0a6a1f1dSLionel Sambuc 
PR19249()361*0a6a1f1dSLionel Sambuc void PR19249() {
362*0a6a1f1dSLionel Sambuc   auto x = [&x]{}; // expected-error {{cannot appear in its own init}}
363*0a6a1f1dSLionel Sambuc }
364*0a6a1f1dSLionel Sambuc 
365*0a6a1f1dSLionel Sambuc namespace PR20731 {
366*0a6a1f1dSLionel Sambuc template <class L, int X = sizeof(L)>
367*0a6a1f1dSLionel Sambuc void Job(L l);
368*0a6a1f1dSLionel Sambuc 
369*0a6a1f1dSLionel Sambuc template <typename... Args>
Logger(Args &&...args)370*0a6a1f1dSLionel Sambuc void Logger(Args &&... args) {
371*0a6a1f1dSLionel Sambuc   auto len = Invalid_Function((args)...);
372*0a6a1f1dSLionel Sambuc   // expected-error@-1 {{use of undeclared identifier 'Invalid_Function'}}
373*0a6a1f1dSLionel Sambuc   Job([len]() {});
374*0a6a1f1dSLionel Sambuc }
375*0a6a1f1dSLionel Sambuc 
GetMethod()376*0a6a1f1dSLionel Sambuc void GetMethod() {
377*0a6a1f1dSLionel Sambuc   Logger();
378*0a6a1f1dSLionel Sambuc   // expected-note@-1 {{in instantiation of function template specialization 'PR20731::Logger<>' requested here}}
379*0a6a1f1dSLionel Sambuc }
380*0a6a1f1dSLionel Sambuc 
381*0a6a1f1dSLionel Sambuc template <typename T>
382*0a6a1f1dSLionel Sambuc struct A {
383*0a6a1f1dSLionel Sambuc   T t;
384*0a6a1f1dSLionel Sambuc   // expected-error@-1 {{field has incomplete type 'void'}}
385*0a6a1f1dSLionel Sambuc };
386*0a6a1f1dSLionel Sambuc 
387*0a6a1f1dSLionel Sambuc template <typename F>
g(F f)388*0a6a1f1dSLionel Sambuc void g(F f) {
389*0a6a1f1dSLionel Sambuc   auto a = A<decltype(f())>{};
390*0a6a1f1dSLionel Sambuc   // expected-note@-1 {{in instantiation of template class 'PR20731::A<void>' requested here}}
391*0a6a1f1dSLionel Sambuc   auto xf = [a, f]() {};
392*0a6a1f1dSLionel Sambuc   int x = sizeof(xf);
393*0a6a1f1dSLionel Sambuc };
f()394*0a6a1f1dSLionel Sambuc void f() {
395*0a6a1f1dSLionel Sambuc   g([] {});
396*0a6a1f1dSLionel Sambuc   // expected-note-re@-1 {{in instantiation of function template specialization 'PR20731::g<(lambda at {{.*}}>' requested here}}
397*0a6a1f1dSLionel Sambuc }
398*0a6a1f1dSLionel Sambuc 
399*0a6a1f1dSLionel Sambuc template <class _Rp> struct function {
400*0a6a1f1dSLionel Sambuc   template <class _Fp>
functionPR20731::function401*0a6a1f1dSLionel Sambuc   function(_Fp) {
402*0a6a1f1dSLionel Sambuc     static_assert(sizeof(_Fp) > 0, "Type must be complete.");
403*0a6a1f1dSLionel Sambuc   }
404*0a6a1f1dSLionel Sambuc };
405*0a6a1f1dSLionel Sambuc 
p(T t)406*0a6a1f1dSLionel Sambuc template <typename T> void p(T t) {
407*0a6a1f1dSLionel Sambuc   auto l = some_undefined_function(t);
408*0a6a1f1dSLionel Sambuc   // expected-error@-1 {{use of undeclared identifier 'some_undefined_function'}}
409*0a6a1f1dSLionel Sambuc   function<void()>(([l]() {}));
410*0a6a1f1dSLionel Sambuc }
q()411*0a6a1f1dSLionel Sambuc void q() { p(0); }
412*0a6a1f1dSLionel Sambuc // expected-note@-1 {{in instantiation of function template specialization 'PR20731::p<int>' requested here}}
413*0a6a1f1dSLionel Sambuc }
414*0a6a1f1dSLionel Sambuc 
415*0a6a1f1dSLionel Sambuc namespace lambda_in_default_mem_init {
f()416*0a6a1f1dSLionel Sambuc   template<typename T> void f() {
417*0a6a1f1dSLionel Sambuc     struct S { int n = []{ return 0; }(); };
418*0a6a1f1dSLionel Sambuc   }
419*0a6a1f1dSLionel Sambuc   template void f<int>();
420*0a6a1f1dSLionel Sambuc 
g()421*0a6a1f1dSLionel Sambuc   template<typename T> void g() {
422*0a6a1f1dSLionel Sambuc     struct S { int n = [](int n){ return n; }(0); };
423*0a6a1f1dSLionel Sambuc   }
424*0a6a1f1dSLionel Sambuc   template void g<int>();
425*0a6a1f1dSLionel Sambuc }
426*0a6a1f1dSLionel Sambuc 
427*0a6a1f1dSLionel Sambuc namespace error_in_transform_prototype {
428*0a6a1f1dSLionel Sambuc   template<class T>
f(T t)429*0a6a1f1dSLionel Sambuc   void f(T t) {
430*0a6a1f1dSLionel Sambuc     // expected-error@+2 {{type 'int' cannot be used prior to '::' because it has no members}}
431*0a6a1f1dSLionel Sambuc     // expected-error@+1 {{no member named 'ns' in 'error_in_transform_prototype::S'}}
432*0a6a1f1dSLionel Sambuc     auto x = [](typename T::ns::type &k) {};
433*0a6a1f1dSLionel Sambuc   }
434*0a6a1f1dSLionel Sambuc   class S {};
foo()435*0a6a1f1dSLionel Sambuc   void foo() {
436*0a6a1f1dSLionel Sambuc     f(5); // expected-note {{requested here}}
437*0a6a1f1dSLionel Sambuc     f(S()); // expected-note {{requested here}}
438*0a6a1f1dSLionel Sambuc   }
439*0a6a1f1dSLionel Sambuc }
440*0a6a1f1dSLionel Sambuc 
441*0a6a1f1dSLionel Sambuc namespace PR21857 {
442*0a6a1f1dSLionel Sambuc   template<typename Fn> struct fun : Fn {
443*0a6a1f1dSLionel Sambuc     fun() = default;
444*0a6a1f1dSLionel Sambuc     using Fn::operator();
445*0a6a1f1dSLionel Sambuc   };
446*0a6a1f1dSLionel Sambuc   template<typename Fn> fun<Fn> wrap(Fn fn);
__anonef18302f6602()447*0a6a1f1dSLionel Sambuc   auto x = wrap([](){});
448*0a6a1f1dSLionel Sambuc }
449