1 // RUN: %clang_cc1 -std=c++1y -verify -fsyntax-only -fblocks -emit-llvm-only %s
2 // RUN: %clang_cc1 -std=c++2a -verify -fsyntax-only -fblocks -emit-llvm-only %s
3 // RUN: %clang_cc1 -std=c++1y -verify -fsyntax-only -fblocks -emit-llvm-only -triple i386-windows-pc %s
4 // RUN: %clang_cc1 -std=c++2a -verify -fsyntax-only -fblocks -emit-llvm-only -triple i386-windows-pc %s
5 // DONTRUNYET: %clang_cc1 -std=c++1y -verify -fsyntax-only -fblocks -fdelayed-template-parsing %s -DDELAYED_TEMPLATE_PARSING
6 // DONTRUNYET: %clang_cc1 -std=c++1y -verify -fsyntax-only -fblocks -fms-extensions %s -DMS_EXTENSIONS
7 // DONTRUNYET: %clang_cc1 -std=c++1y -verify -fsyntax-only -fblocks -fdelayed-template-parsing -fms-extensions %s -DMS_EXTENSIONS -DDELAYED_TEMPLATE_PARSING
8 
9 constexpr int ODRUSE_SZ = sizeof(char);
10 
11 template<class T, int N>
f(T,const int (&)[N])12 void f(T, const int (&)[N]) { }
13 
14 template<class T>
f(const T &,const int (&)[ODRUSE_SZ])15 void f(const T&, const int (&)[ODRUSE_SZ]) { }
16 
17 #define DEFINE_SELECTOR(x)   \
18   int selector_ ## x[sizeof(x) == ODRUSE_SZ ? ODRUSE_SZ : ODRUSE_SZ + 5]
19 
20 #define F_CALL(x, a) f(x, selector_ ## a)
21 
22 // This is a risky assumption, because if an empty class gets captured by value
23 // the lambda's size will still be '1'
24 #define ASSERT_NO_CAPTURES(L) static_assert(sizeof(L) == 1, "size of closure with no captures must be 1")
25 #define ASSERT_CLOSURE_SIZE_EXACT(L, N) static_assert(sizeof(L) == (N), "size of closure must be " #N)
26 #define ASSERT_CLOSURE_SIZE(L, N) static_assert(sizeof(L) >= (N), "size of closure must be >=" #N)
27 
28 
29 namespace sample {
30   struct X {
31     int i;
Xsample::X32     X(int i) : i(i) { }
33   };
34 }
35 
36 namespace test_transformations_in_templates {
foo(T t)37 template<class T> void foo(T t) {
38   auto L = [](auto a) { return a; };
39 }
foo2(T t)40 template<class T> void foo2(T t) {
41   auto L = [](auto a) -> void {
42     auto M = [](char b) -> void {
43       auto N = [](auto c) -> void {
44         int selector[sizeof(c) == 1 ?
45                       (sizeof(b) == 1 ? 1 : 2)
46                       : 2
47                     ]{};
48       };
49       N('a');
50     };
51   };
52   L(3.14);
53 }
54 
doit()55 void doit() {
56   foo(3);
57   foo('a');
58   foo2('A');
59 }
60 }
61 
62 namespace test_return_type_deduction {
63 
doit()64 void doit() {
65 
66   auto L = [](auto a, auto b) {
67     if ( a > b ) return a;
68     return b;
69   };
70   L(2, 4);
71   {
72     auto L2 = [](auto a, int i) {
73       return a + i;
74     };
75     L2(3.14, 2);
76   }
77   {
78     int a; //expected-note{{declared here}}
79     auto B = []() { return ^{ return a; }; }; //expected-error{{cannot be implicitly capture}}\
80                                               //expected-note{{begins here}}
81   //[](){ return ({int b = 5; return 'c'; 'x';}); };
82 
83   //auto X = ^{ return a; };
84 
85   //auto Y = []() -> auto { return 3; return 'c'; };
86 
87   }
88 }
89 }
90 
91 
92 namespace test_no_capture{
doit()93 void doit() {
94   const int x = 10; //expected-note{{declared here}}
95   {
96     // should not capture 'x' - variable undergoes lvalue-to-rvalue
97     auto L = [=](auto a) {
98       int y = x;
99       return a + y;
100     };
101     ASSERT_NO_CAPTURES(L);
102   }
103   {
104     // should not capture 'x' - even though certain instantiations require
105     auto L = [](auto a) { //expected-note{{begins here}}
106       DEFINE_SELECTOR(a);
107       F_CALL(x, a); //expected-error{{'x' cannot be implicitly captured}}
108     };
109     ASSERT_NO_CAPTURES(L);
110     L('s'); //expected-note{{in instantiation of}}
111   }
112   {
113     // Does not capture because no default capture in inner most lambda 'b'
114     auto L = [=](auto a) {
115       return [=](int p) {
116         return [](auto b) {
117           DEFINE_SELECTOR(a);
118           F_CALL(x, a);
119           return 0;
120         };
121       };
122     };
123     ASSERT_NO_CAPTURES(L);
124   }
125 }  // doit
126 } // namespace
127 
128 namespace test_capture_of_potentially_evaluated_expression {
doit()129 void doit() {
130   const int x = 5;
131   {
132     auto L = [=](auto a) {
133       DEFINE_SELECTOR(a);
134       F_CALL(x, a);
135     };
136     static_assert(sizeof(L) == 4, "Must be captured");
137   }
138   {
139     int j = 0; //expected-note{{declared}}
140     auto L = [](auto a) {  //expected-note{{begins here}}
141       return j + 1; //expected-error{{cannot be implicitly captured}}
142     };
143   }
144   {
145     const int x = 10;
146     auto L = [](auto a) {
147       //const int y = 20;
148       return [](int p) {
149         return [](auto b) {
150           DEFINE_SELECTOR(a);
151           F_CALL(x, a);
152           return 0;
153         };
154       };
155     };
156     auto M = L(3);
157     auto N = M(5);
158 
159   }
160 
161   { // if the nested capture does not implicitly or explicitly allow any captures
162     // nothing should capture - and instantiations will create errors if needed.
163     const int x = 0;
164     auto L = [=](auto a) { // <-- #A
165       const int y = 0;
166       return [](auto b) { // <-- #B
167         int c[sizeof(b)];
168         f(x, c);
169         f(y, c);
170         int i = x;
171       };
172     };
173     ASSERT_NO_CAPTURES(L);
174     auto M_int = L(2);
175     ASSERT_NO_CAPTURES(M_int);
176   }
177   { // Permutations of this example must be thoroughly tested!
178     const int x = 0;
179     sample::X cx{5};
180     auto L = [=](auto a) {
181       const int z = 3;
182       return [&,a](auto b) {
183         // expected-warning@-1 {{address of stack memory associated with local variable 'z' returned}}
184         // expected-note@#call {{in instantiation of}}
185         const int y = 5;
186         return [=](auto c) {
187           int d[sizeof(a) == sizeof(c) || sizeof(c) == sizeof(b) ? 2 : 1];
188           f(x, d);
189           f(y, d);
190           f(z, d); // expected-note {{implicitly captured by reference due to use here}}
191           decltype(a) A = a;
192           decltype(b) B = b;
193           const int &i = cx.i;
194         };
195       };
196     };
197     auto M = L(3)(3.5); // #call
198     M(3.14);
199   }
200 }
201 namespace Test_no_capture_of_clearly_no_odr_use {
foo()202 auto foo() {
203  const int x = 10;
204  auto L = [=](auto a) {
205     return  [=](auto b) {
206       return [=](auto c) {
207         int A = x;
208         return A;
209       };
210     };
211   };
212   auto M = L(1);
213   auto N = M(2.14);
214   ASSERT_NO_CAPTURES(L);
215   ASSERT_NO_CAPTURES(N);
216 
217   return 0;
218 }
219 }
220 
221 namespace Test_capture_of_odr_use_var {
foo()222 auto foo() {
223  const int x = 10;
224  auto L = [=](auto a) {
225     return  [=](auto b) {
226       return [=](auto c) {
227         int A = x;
228         const int &i = x;
229         decltype(a) A2 = a;
230         return A;
231       };
232     };
233   };
234   auto M_int = L(1);
235   auto N_int_int = M_int(2);
236   ASSERT_CLOSURE_SIZE_EXACT(L, sizeof(x));
237   // M_int captures both a & x
238   ASSERT_CLOSURE_SIZE_EXACT(M_int, sizeof(x) + sizeof(int));
239   // N_int_int captures both a & x
240   ASSERT_CLOSURE_SIZE_EXACT(N_int_int, sizeof(x) + sizeof(int));
241   auto M_double = L(3.14);
242   ASSERT_CLOSURE_SIZE(M_double, sizeof(x) + sizeof(double));
243 
244   return 0;
245 }
246 auto run = foo();
247 }
248 
249 }
250 namespace more_nested_captures_1 {
251 template<class T> struct Y {
fmore_nested_captures_1::Y252   static void f(int, double, ...) { }
253   template<class R>
fmore_nested_captures_1::Y254   static void f(const int&, R, ...) { }
255   template<class R>
foomore_nested_captures_1::Y256   void foo(R t) {
257     const int x = 10; //expected-note{{declared here}}
258     auto L = [](auto a) {
259        return [=](auto b) {
260         return [=](auto c) {
261           f(x, c, b, a);  //expected-error{{reference to local variable 'x'}}
262           return 0;
263         };
264       };
265     };
266     auto M = L(t);
267     auto N = M('b');
268     N(3.14);
269     N(5);  //expected-note{{in instantiation of}}
270   }
271 };
272 Y<int> yi;
273 int run = (yi.foo(3.14), 0); //expected-note{{in instantiation of}}
274 }
275 
276 
277 namespace more_nested_captures_1_1 {
278 template<class T> struct Y {
fmore_nested_captures_1_1::Y279   static void f(int, double, ...) { }
280   template<class R>
fmore_nested_captures_1_1::Y281   static void f(const int&, R, ...) { }
282   template<class R>
foomore_nested_captures_1_1::Y283   void foo(R t) {
284     const int x = 10; //expected-note{{declared here}}
285     auto L = [](auto a) {
286        return [=](char b) {
287         return [=](auto c) {
288           f(x, c, b, a);  //expected-error{{reference to local variable 'x'}}
289           return 0;
290         };
291       };
292     };
293     auto M = L(t);
294     auto N = M('b');
295     N(3.14);
296     N(5);  //expected-note{{in instantiation of}}
297   }
298 };
299 Y<int> yi;
300 int run = (yi.foo(3.14), 0); //expected-note{{in instantiation of}}
301 }
302 namespace more_nested_captures_1_2 {
303 template<class T> struct Y {
fmore_nested_captures_1_2::Y304   static void f(int, double, ...) { }
305   template<class R>
fmore_nested_captures_1_2::Y306   static void f(const int&, R, ...) { }
307   template<class R>
foomore_nested_captures_1_2::Y308   void foo(R t) {
309     const int x = 10;
310     auto L = [=](auto a) {
311        return [=](char b) {
312         return [=](auto c) {
313           f(x, c, b, a);
314           return 0;
315         };
316       };
317     };
318     auto M = L(t);
319     auto N = M('b');
320     N(3.14);
321     N(5);
322   }
323 };
324 Y<int> yi;
325 int run = (yi.foo(3.14), 0);
326 }
327 
328 namespace more_nested_captures_1_3 {
329 template<class T> struct Y {
fmore_nested_captures_1_3::Y330   static void f(int, double, ...) { }
331   template<class R>
fmore_nested_captures_1_3::Y332   static void f(const int&, R, ...) { }
333   template<class R>
foomore_nested_captures_1_3::Y334   void foo(R t) {
335     const int x = 10; //expected-note{{declared here}}
336     auto L = [=](auto a) {
337        return [](auto b) {
338         const int y = 0;
339         return [=](auto c) {
340           f(x, c, b);  //expected-error{{reference to local variable 'x'}}
341           f(y, b, c);
342           return 0;
343         };
344       };
345     };
346     auto M = L(t);
347     auto N = M('b');
348     N(3.14);
349     N(5);  //expected-note{{in instantiation of}}
350   }
351 };
352 Y<int> yi;
353 int run = (yi.foo(3.14), 0); //expected-note{{in instantiation of}}
354 }
355 
356 
357 namespace more_nested_captures_1_4 {
358 template<class T> struct Y {
fmore_nested_captures_1_4::Y359   static void f(int, double, ...) { }
360   template<class R>
fmore_nested_captures_1_4::Y361   static void f(const int&, R, ...) { }
362   template<class R>
foomore_nested_captures_1_4::Y363   void foo(R t) {
364     const int x = 10; //expected-note{{declared here}}
365     auto L = [=](auto a) {
366        T t2{t};
367        return [](auto b) {
368         const int y = 0; //expected-note{{declared here}}
369         return [](auto c) { //expected-note 2{{lambda expression begins here}}
370           f(x, c);  //expected-error{{variable 'x'}}
371           f(y, c);  //expected-error{{variable 'y'}}
372           return 0;
373         };
374       };
375     };
376     auto M = L(t);
377     auto N_char = M('b');
378     N_char(3.14);
379     auto N_double = M(3.14);
380     N_double(3.14);
381     N_char(3);  //expected-note{{in instantiation of}}
382   }
383 };
384 Y<int> yi;
385 int run = (yi.foo('a'), 0); //expected-note{{in instantiation of}}
386 }
387 
388 
389 namespace more_nested_captures_2 {
390 template<class T> struct Y {
fmore_nested_captures_2::Y391   static void f(int, double) { }
392   template<class R>
fmore_nested_captures_2::Y393   static void f(const int&, R) { }
394   template<class R>
foomore_nested_captures_2::Y395   void foo(R t) {
396     const int x = 10;
397     auto L = [=](auto a) {
398        return [=](auto b) {
399         return [=](auto c) {
400           f(x, c);
401           return 0;
402         };
403       };
404     };
405     auto M = L(t);
406     auto N = M('b');
407     N(3);
408     N(3.14);
409   }
410 };
411 Y<int> yi;
412 int run = (yi.foo(3.14), 0);
413 
414 }
415 
416 namespace more_nested_captures_3 {
417 template<class T> struct Y {
fmore_nested_captures_3::Y418   static void f(int, double) { }
419   template<class R>
fmore_nested_captures_3::Y420   static void f(const int&, R) { }
421   template<class R>
foomore_nested_captures_3::Y422   void foo(R t) {
423     const int x = 10; //expected-note{{declared here}}
424     auto L = [](auto a) {
425        return [=](auto b) {
426         return [=](auto c) {
427           f(x, c);   //expected-error{{reference to local variable 'x'}}
428           return 0;
429         };
430       };
431     };
432     auto M = L(t);
433     auto N = M('b');
434     N(3); //expected-note{{in instantiation of}}
435     N(3.14);
436   }
437 };
438 Y<int> yi;
439 int run = (yi.foo(3.14), 0); //expected-note{{in instantiation of}}
440 
441 }
442 
443 namespace more_nested_captures_4 {
444 template<class T> struct Y {
fmore_nested_captures_4::Y445   static void f(int, double) { }
446   template<class R>
fmore_nested_captures_4::Y447   static void f(const int&, R) { }
448   template<class R>
foomore_nested_captures_4::Y449   void foo(R t) {
450     const int x = 10;  //expected-note{{'x' declared here}}
451     auto L = [](auto a) {
452        return [=](char b) {
453         return [=](auto c) {
454           f(x, c);  //expected-error{{reference to local variable 'x'}}
455           return 0;
456         };
457       };
458     };
459     auto M = L(t);
460     auto N = M('b');
461     N(3); //expected-note{{in instantiation of}}
462     N(3.14);
463   }
464 };
465 Y<int> yi;
466 int run = (yi.foo(3.14), 0); //expected-note{{in instantiation of}}
467 
468 }
469 
470 namespace more_nested_captures_5 {
471 template<class T> struct Y {
fmore_nested_captures_5::Y472   static void f(int, double) { }
473   template<class R>
fmore_nested_captures_5::Y474   static void f(const int&, R) { }
475   template<class R>
foomore_nested_captures_5::Y476   void foo(R t) {
477     const int x = 10;
478     auto L = [=](auto a) {
479        return [=](char b) {
480         return [=](auto c) {
481           f(x, c);
482           return 0;
483         };
484       };
485     };
486     auto M = L(t);
487     auto N = M('b');
488     N(3);
489     N(3.14);
490   }
491 };
492 Y<int> yi;
493 int run = (yi.foo(3.14), 0);
494 
495 }
496 
497 namespace lambdas_in_NSDMIs {
498 template<class T>
499   struct L {
500       T t{};
__anon63b8ffb93902(auto b) 501       T t2 = ([](auto a) { return [](auto b) { return b; };})(t)(t);
__anon63b8ffb93a02lambdas_in_NSDMIs::L502       T t3 = ([](auto a) { return a; })(t);
503   };
504   L<int> l;
505   int run = l.t2;
506 }
507 namespace test_nested_decltypes_in_trailing_return_types {
foo()508 int foo() {
509   auto L = [](auto a) {
510       return [](auto b, decltype(a) b2) -> decltype(a) {
511         return decltype(a){};
512       };
513   };
514   auto M = L(3.14);
515   M('a', 6.26);
516   return 0;
517 }
518 }
519 
520 namespace more_this_capture_1 {
521 struct X {
fmore_this_capture_1::X522   void f(int) { }
fmore_this_capture_1::X523   static void f(double) { }
foomore_this_capture_1::X524   void foo() {
525     {
526       auto L = [=](auto a) {
527         f(a);
528       };
529       L(3);
530       L(3.13);
531     }
532     {
533       auto L = [](auto a) {
534         f(a); //expected-error{{this}}
535       };
536       L(3.13);
537       L(2); //expected-note{{in instantiation}}
538     }
539   }
540 
gmore_this_capture_1::X541   int g() {
542     auto L = [=](auto a) {
543       return [](int i) {
544         return [=](auto b) {
545           f(b);
546           int x = i;
547         };
548       };
549     };
550     auto M = L(0.0);
551     auto N = M(3);
552     N(5.32); // OK
553     return 0;
554   }
555 };
556 int run = X{}.g();
557 }
558 namespace more_this_capture_1_1 {
559 struct X {
fmore_this_capture_1_1::X560   void f(int) { }
fmore_this_capture_1_1::X561   static void f(double) { }
562 
gmore_this_capture_1_1::X563   int g() {
564     auto L = [=](auto a) {
565       return [](int i) {
566         return [=](auto b) {
567           f(decltype(a){}); //expected-error{{this}}
568           int x = i;
569         };
570       };
571     };
572     auto M = L(0.0);
573     auto N = M(3);
574     N(5.32); // OK
575     L(3); // expected-note{{instantiation}}
576     return 0;
577   }
578 };
579 int run = X{}.g();
580 }
581 
582 namespace more_this_capture_1_1_1 {
583 struct X {
fmore_this_capture_1_1_1::X584   void f(int) { }
fmore_this_capture_1_1_1::X585   static void f(double) { }
586 
gmore_this_capture_1_1_1::X587   int g() {
588     auto L = [=](auto a) {
589       return [](auto b) {
590         return [=](int i) {
591           f(b);
592           f(decltype(a){}); //expected-error{{this}}
593         };
594       };
595     };
596     auto M = L(0.0);  // OK
597     auto N = M(3.3); //OK
598     auto M_int = L(0); //expected-note{{instantiation}}
599     return 0;
600   }
601 };
602 int run = X{}.g();
603 }
604 
605 
606 namespace more_this_capture_1_1_1_1 {
607 struct X {
fmore_this_capture_1_1_1_1::X608   void f(int) { }
fmore_this_capture_1_1_1_1::X609   static void f(double) { }
610 
gmore_this_capture_1_1_1_1::X611   int g() {
612     auto L = [=](auto a) {
613       return [](auto b) {
614         return [=](int i) {
615           f(b); //expected-error{{this}}
616           f(decltype(a){});
617         };
618       };
619     };
620     auto M_double = L(0.0);  // OK
621     auto N = M_double(3); //expected-note{{instantiation}}
622 
623     return 0;
624   }
625 };
626 int run = X{}.g();
627 }
628 
629 namespace more_this_capture_2 {
630 struct X {
fmore_this_capture_2::X631   void f(int) { }
fmore_this_capture_2::X632   static void f(double) { }
633 
gmore_this_capture_2::X634   int g() {
635     auto L = [=](auto a) {
636       return [](int i) {
637         return [=](auto b) {
638           f(b); //expected-error{{'this' cannot}}
639           int x = i;
640         };
641       };
642     };
643     auto M = L(0.0);
644     auto N = M(3);
645     N(5); // NOT OK expected-note{{in instantiation of}}
646     return 0;
647   }
648 };
649 int run = X{}.g();
650 }
651 namespace diagnose_errors_early_in_generic_lambdas {
652 
foo()653 int foo()
654 {
655 
656   { // This variable is used and must be caught early, do not need instantiation
657     const int x = 0; //expected-note{{declared}}
658     auto L = [](auto a) { //expected-note{{begins}}
659       const int &r = x; //expected-error{{variable}}
660     };
661   }
662   { // This variable is not used
663     const int x = 0;
664     auto L = [](auto a) {
665       int i = x;
666     };
667   }
668   {
669 
670     const int x = 0; //expected-note{{declared}}
671     auto L = [=](auto a) { // <-- #A
672       const int y = 0;
673       return [](auto b) { //expected-note{{begins}}
674         int c[sizeof(b)];
675         f(x, c);
676         f(y, c);
677         int i = x;
678         // This use will always be an error regardless of instantiation
679         // so diagnose this early.
680         const int &r = x; //expected-error{{variable}}
681       };
682     };
683 
684   }
685   return 0;
686 }
687 
688 int run = foo();
689 }
690 
691 namespace generic_nongenerics_interleaved_1 {
foo()692 int foo() {
693   {
694     auto L = [](int a) {
695       int y = 10;
696       return [=](auto b) {
697         return a + y;
698       };
699     };
700     auto M = L(3);
701     M(5);
702   }
703   {
704     int x;
705     auto L = [](int a) {
706       int y = 10;
707       return [=](auto b) {
708         return a + y;
709       };
710     };
711     auto M = L(3);
712     M(5);
713   }
714   {
715     // FIXME: why are there 2 error messages here?
716     int x;
717     auto L = [](auto a) { //expected-note {{declared here}}
718       int y = 10; //expected-note {{declared here}}
719       return [](int b) { //expected-note 2{{expression begins here}}
720         return [=] (auto c) {
721           return a + y; //expected-error 2{{cannot be implicitly captured}}
722         };
723       };
724     };
725   }
726   {
727     int x;
728     auto L = [](auto a) {
729       int y = 10;
730       return [=](int b) {
731         return [=] (auto c) {
732           return a + y;
733         };
734       };
735     };
736   }
737   return 1;
738 }
739 
740 int run = foo();
741 }
742 namespace dont_capture_refs_if_initialized_with_constant_expressions {
743 
foo(int i)744 auto foo(int i) {
745   // This is surprisingly not odr-used within the lambda!
746   static int j;
747   j = i;
748   int &ref_j = j;
749   return [](auto a) { return ref_j; }; // ok
750 }
751 
752 template<class T>
foo2(T t)753 auto foo2(T t) {
754   // This is surprisingly not odr-used within the lambda!
755   static T j;
756   j = t;
757   T &ref_j = j;
758   return [](auto a) { return ref_j; }; // ok
759 }
760 
do_test()761 int do_test() {
762   auto L = foo(3);
763   auto L_int = L(3);
764   auto L_char = L('a');
765   auto L1 = foo2(3.14);
766   auto L1_int = L1(3);
767   auto L1_char = L1('a');
768   return 0;
769 }
770 
771 } // dont_capture_refs_if_initialized_with_constant_expressions
772 
773 namespace test_conversion_to_fptr {
774 
775 template<class T> struct X {
776 
__anon63b8ffb95d02test_conversion_to_fptr::X777   T (*fp)(T) = [](auto a) { return a; };
778 
779 };
780 
781 X<int> xi;
782 
783 template<class T>
__anon63b8ffb95e02(auto a) 784 void fooT(T t, T (*fp)(T) = [](auto a) { return a; }) {
785   fp(t);
786 }
787 
test()788 int test() {
789 {
790   auto L = [](auto a) { return a; };
791   int (*fp)(int) = L;
792   fp(5);
793   L(3);
794   char (*fc)(char) = L;
795   fc('b');
796   L('c');
797   double (*fd)(double) = L;
798   fd(3.14);
799   fd(6.26);
800   L(4.25);
801 }
802 {
803   auto L = [](auto a) ->int { return a; }; //expected-note 2{{candidate template ignored}}
804   int (*fp)(int) = L;
805   char (*fc)(char) = L; //expected-error{{no viable conversion}}
806   double (*fd)(double) = L; //expected-error{{no viable conversion}}
807 }
808 {
809   int x = 5;
810   auto L = [=](auto b, char c = 'x') {
811     int i = x;
812     return [](auto a) ->decltype(a) { return a; };
813   };
814   int (*fp)(int) = L(8);
815   fp(5);
816   L(3);
817   char (*fc)(char) = L('a');
818   fc('b');
819   L('c');
820   double (*fd)(double) = L(3.14);
821   fd(3.14);
822   fd(6.26);
823 
824 }
825 {
826  auto L = [=](auto b) {
827     return [](auto a) ->decltype(b)* { return (decltype(b)*)0; };
828   };
829   int* (*fp)(int) = L(8);
830   fp(5);
831   L(3);
832   char* (*fc)(char) = L('a');
833   fc('b');
834   L('c');
835   double* (*fd)(double) = L(3.14);
836   fd(3.14);
837   fd(6.26);
838 }
839 {
840  auto L = [=](auto b) {
841     return [](auto a) ->decltype(b)* { return (decltype(b)*)0; }; //expected-note{{candidate template ignored}}
842   };
843   char* (*fp)(int) = L('8');
844   fp(5);
845   char* (*fc)(char) = L('a');
846   fc('b');
847   double* (*fi)(int) = L(3.14);
848   fi(5);
849   int* (*fi2)(int) = L(3.14); //expected-error{{no viable conversion}}
850 }
851 
852 {
853  auto L = [=](auto b) {
854     return [](auto a) {
855       return [=](auto c) {
856         return [](auto d) ->decltype(a + b + c + d) { return d; };
857       };
858     };
859   };
860   int (*fp)(int) = L('8')(3)(short{});
861   double (*fs)(char) = L(3.14)(short{})('4');
862 }
863 
864   fooT(3);
865   fooT('a');
866   fooT(3.14);
867   fooT("abcdefg");
868   return 0;
869 }
870 int run2 = test();
871 
872 }
873 
874 
875 namespace this_capture {
f(char,int)876 void f(char, int) { }
877 template<class T>
f(T,const int &)878 void f(T, const int&) { }
879 
880 struct X {
881   int x = 0;
foothis_capture::X882   void foo() {
883     auto L = [=](auto a) {
884          return [=](auto b) {
885             //f(a, x++);
886             x++;
887          };
888     };
889     L('a')(5);
890     L('b')(4);
891     L(3.14)('3');
892 
893   }
894 
895 };
896 
897 int run = (X{}.foo(), 0);
898 
899 namespace this_capture_unresolvable {
900 struct X {
fthis_capture::this_capture_unresolvable::X901   void f(int) { }
fthis_capture::this_capture_unresolvable::X902   static void f(double) { }
903 
gthis_capture::this_capture_unresolvable::X904   int g() {
905     auto lam = [=](auto a) { f(a); }; // captures 'this'
906     lam(0); // ok.
907     lam(0.0); // ok.
908     return 0;
909   }
g2this_capture::this_capture_unresolvable::X910   int g2() {
911     auto lam = [](auto a) { f(a); }; // expected-error{{'this'}}
912     lam(0); // expected-note{{in instantiation of}}
913     lam(0.0); // ok.
914     return 0;
915   }
__anon63b8ffb96d02this_capture::this_capture_unresolvable::X916   double (*fd)(double) = [](auto a) { f(a); return a; };
917 
918 };
919 
920 int run = X{}.g();
921 
922 }
923 
924 namespace check_nsdmi_and_this_capture_of_member_functions {
925 
926 struct FunctorDouble {
FunctorDoublethis_capture::check_nsdmi_and_this_capture_of_member_functions::FunctorDouble927   template<class T> FunctorDouble(T t) { t(2.14); };
928 };
929 struct FunctorInt {
FunctorIntthis_capture::check_nsdmi_and_this_capture_of_member_functions::FunctorInt930   template<class T> FunctorInt(T t) { t(2); }; //expected-note{{in instantiation of}}
931 };
932 
933 template<class T> struct YUnresolvable {
fthis_capture::check_nsdmi_and_this_capture_of_member_functions::YUnresolvable934   void f(int) { }
fthis_capture::check_nsdmi_and_this_capture_of_member_functions::YUnresolvable935   static void f(double) { }
936 
__anon63b8ffb96e02this_capture::check_nsdmi_and_this_capture_of_member_functions::YUnresolvable937   T t = [](auto a) { f(a); return a; };
__anon63b8ffb96f02this_capture::check_nsdmi_and_this_capture_of_member_functions::YUnresolvable938   T t2 = [=](auto b) { f(b); return b; };
939 };
940 
941 template<class T> struct YUnresolvable2 {
fthis_capture::check_nsdmi_and_this_capture_of_member_functions::YUnresolvable2942   void f(int) { }
fthis_capture::check_nsdmi_and_this_capture_of_member_functions::YUnresolvable2943   static void f(double) { }
944 
__anon63b8ffb97002this_capture::check_nsdmi_and_this_capture_of_member_functions::YUnresolvable2945   T t = [](auto a) { f(a); return a; }; //expected-error{{'this'}} \
946                                         //expected-note{{in instantiation of}}
__anon63b8ffb97102this_capture::check_nsdmi_and_this_capture_of_member_functions::YUnresolvable2947   T t2 = [=](auto b) { f(b); return b; };
948 };
949 
950 
951 YUnresolvable<FunctorDouble> yud;
952 // This will cause an error since it call's with an int and calls a member function.
953 YUnresolvable2<FunctorInt> yui;
954 
955 
956 template<class T> struct YOnlyStatic {
fthis_capture::check_nsdmi_and_this_capture_of_member_functions::YOnlyStatic957   static void f(double) { }
958 
__anon63b8ffb97202this_capture::check_nsdmi_and_this_capture_of_member_functions::YOnlyStatic959   T t = [](auto a) { f(a); return a; };
960 };
961 YOnlyStatic<FunctorDouble> yos;
962 template<class T> struct YOnlyNonStatic {
fthis_capture::check_nsdmi_and_this_capture_of_member_functions::YOnlyNonStatic963   void f(int) { }
964 
__anon63b8ffb97302this_capture::check_nsdmi_and_this_capture_of_member_functions::YOnlyNonStatic965   T t = [](auto a) { f(a); return a; }; //expected-error{{'this'}}
966 };
967 
968 
969 }
970 
971 
972 namespace check_nsdmi_and_this_capture_of_data_members {
973 
974 struct FunctorDouble {
FunctorDoublethis_capture::check_nsdmi_and_this_capture_of_data_members::FunctorDouble975   template<class T> FunctorDouble(T t) { t(2.14); };
976 };
977 struct FunctorInt {
FunctorIntthis_capture::check_nsdmi_and_this_capture_of_data_members::FunctorInt978   template<class T> FunctorInt(T t) { t(2); };
979 };
980 
981 template<class T> struct YThisCapture {
982   const int x = 10;
983   static double d;
__anon63b8ffb97402this_capture::check_nsdmi_and_this_capture_of_data_members::YThisCapture984   T t = [](auto a) { return x; }; //expected-error{{'this'}}
__anon63b8ffb97502this_capture::check_nsdmi_and_this_capture_of_data_members::YThisCapture985   T t2 = [](auto b) {  return d; };
__anon63b8ffb97602this_capture::check_nsdmi_and_this_capture_of_data_members::YThisCapture986   T t3 = [this](auto a) {
987           return [=](auto b) {
988             return x;
989          };
990   };
__anon63b8ffb97802this_capture::check_nsdmi_and_this_capture_of_data_members::YThisCapture991   T t4 = [=](auto a) {
992           return [=](auto b) {
993             return x;
994          };
995   };
__anon63b8ffb97a02this_capture::check_nsdmi_and_this_capture_of_data_members::YThisCapture996   T t5 = [](auto a) {
997           return [=](auto b) {
998             return x;  //expected-error{{'this'}}
999          };
1000   };
1001 };
1002 
1003 template<class T> double YThisCapture<T>::d = 3.14;
1004 
1005 
1006 }
1007 
1008 
1009 #ifdef DELAYED_TEMPLATE_PARSING
foo_no_error(T t)1010 template<class T> void foo_no_error(T t) {
1011   auto L = []()
1012     { return t; };
1013 }
foo(T t)1014 template<class T> void foo(T t) { //expected-note 2{{declared here}}
1015   auto L = []()  //expected-note 2{{begins here}}
1016     { return t; }; //expected-error 2{{cannot be implicitly captured}}
1017 }
1018 template void foo(int); //expected-note{{in instantiation of}}
1019 
1020 #else
1021 
foo(T t)1022 template<class T> void foo(T t) { //expected-note{{declared here}}
1023   auto L = []()  //expected-note{{begins here}}
1024     { return t; }; //expected-error{{cannot be implicitly captured}}
1025 }
1026 
1027 #endif
1028 }
1029 
1030 namespace no_this_capture_for_static {
1031 
1032 struct X {
fno_this_capture_for_static::X1033   static void f(double) { }
1034 
gno_this_capture_for_static::X1035   int g() {
1036     auto lam = [=](auto a) { f(a); };
1037     lam(0); // ok.
1038     ASSERT_NO_CAPTURES(lam);
1039     return 0;
1040   }
1041 };
1042 
1043 int run = X{}.g();
1044 }
1045 
1046 namespace this_capture_for_non_static {
1047 
1048 struct X {
fthis_capture_for_non_static::X1049   void f(double) { }
1050 
gthis_capture_for_non_static::X1051   int g() {
1052     auto L = [=](auto a) { f(a); };
1053     L(0);
1054     auto L2 = [](auto a) { f(a); }; //expected-error {{cannot be implicitly captured}}
1055     return 0;
1056   }
1057 };
1058 
1059 int run = X{}.g();
1060 }
1061 
1062 namespace this_captures_with_num_args_disambiguation {
1063 
1064 struct X {
fthis_captures_with_num_args_disambiguation::X1065   void f(int) { }
fthis_captures_with_num_args_disambiguation::X1066   static void f(double, int i) { }
gthis_captures_with_num_args_disambiguation::X1067   int g() {
1068     auto lam = [](auto a) { f(a, a); };
1069     lam(0);
1070     return 0;
1071   }
1072 };
1073 
1074 int run = X{}.g();
1075 }
1076 namespace enclosing_function_is_template_this_capture {
1077 // Only error if the instantiation tries to use the member function.
1078 struct X {
fenclosing_function_is_template_this_capture::X1079   void f(int) { }
fenclosing_function_is_template_this_capture::X1080   static void f(double) { }
1081   template<class T>
genclosing_function_is_template_this_capture::X1082   int g(T t) {
1083     auto L = [](auto a) { f(a); }; //expected-error{{'this'}}
1084     L(t); // expected-note{{in instantiation of}}
1085     return 0;
1086   }
1087 };
1088 
1089 int run = X{}.g(0.0); // OK.
1090 int run2 = X{}.g(0);  // expected-note{{in instantiation of}}
1091 
1092 
1093 }
1094 
1095 namespace enclosing_function_is_template_this_capture_2 {
1096 // This should error, even if not instantiated, since
1097 // this would need to be captured.
1098 struct X {
fenclosing_function_is_template_this_capture_2::X1099   void f(int) { }
1100   template<class T>
genclosing_function_is_template_this_capture_2::X1101   int g(T t) {
1102     auto L = [](auto a) { f(a); }; //expected-error{{'this'}}
1103     L(t);
1104     return 0;
1105   }
1106 };
1107 
1108 }
1109 
1110 
1111 namespace enclosing_function_is_template_this_capture_3 {
1112 // This should not error, this does not need to be captured.
1113 struct X {
fenclosing_function_is_template_this_capture_3::X1114   static void f(int) { }
1115   template<class T>
genclosing_function_is_template_this_capture_3::X1116   int g(T t) {
1117     auto L = [](auto a) { f(a); };
1118     L(t);
1119     return 0;
1120   }
1121 };
1122 
1123 int run = X{}.g(0.0); // OK.
1124 int run2 = X{}.g(0);  // OK.
1125 
1126 }
1127 
1128 namespace nested_this_capture_1 {
1129 struct X {
fnested_this_capture_1::X1130   void f(int) { }
fnested_this_capture_1::X1131   static void f(double) { }
1132 
gnested_this_capture_1::X1133   int g() {
1134     auto L = [=](auto a) {
1135       return [this]() {
1136         return [=](auto b) {
1137           f(b);
1138         };
1139       };
1140     };
1141     auto M = L(0);
1142     auto N = M();
1143     N(5);
1144     return 0;
1145   }
1146 };
1147 
1148 int run = X{}.g();
1149 
1150 }
1151 
1152 
1153 namespace nested_this_capture_2 {
1154 struct X {
fnested_this_capture_2::X1155   void f(int) { }
fnested_this_capture_2::X1156   static void f(double) { }
1157 
gnested_this_capture_2::X1158   int g() {
1159     auto L = [=](auto a) {
1160       return [&]() {
1161         return [=](auto b) {
1162           f(b);
1163         };
1164       };
1165     };
1166     auto M = L(0);
1167     auto N = M();
1168     N(5);
1169     N(3.14);
1170     return 0;
1171   }
1172 };
1173 
1174 int run = X{}.g();
1175 
1176 }
1177 
1178 namespace nested_this_capture_3_1 {
1179 struct X {
1180   template<class T>
fnested_this_capture_3_1::X1181   void f(int, T t) { }
1182   template<class T>
fnested_this_capture_3_1::X1183   static void f(double, T t) { }
1184 
gnested_this_capture_3_1::X1185   int g() {
1186     auto L = [=](auto a) {
1187       return [&](auto c) {
1188         return [=](auto b) {
1189           f(b, c);
1190         };
1191       };
1192     };
1193     auto M = L(0);
1194     auto N = M('a');
1195     N(5);
1196     N(3.14);
1197     return 0;
1198   }
1199 };
1200 
1201 int run = X{}.g();
1202 
1203 }
1204 
1205 
1206 namespace nested_this_capture_3_2 {
1207 struct X {
fnested_this_capture_3_2::X1208   void f(int) { }
fnested_this_capture_3_2::X1209   static void f(double) { }
1210 
gnested_this_capture_3_2::X1211   int g() {
1212     auto L = [=](auto a) {
1213       return [](int i) {
1214         return [=](auto b) {
1215           f(b); //expected-error {{'this' cannot}}
1216           int x = i;
1217         };
1218       };
1219     };
1220     auto M = L(0.0);
1221     auto N = M(3);
1222     N(5); //expected-note {{in instantiation of}}
1223     N(3.14); // OK.
1224     return 0;
1225   }
1226 };
1227 
1228 int run = X{}.g();
1229 
1230 }
1231 
1232 namespace nested_this_capture_4 {
1233 struct X {
fnested_this_capture_4::X1234   void f(int) { }
fnested_this_capture_4::X1235   static void f(double) { }
1236 
gnested_this_capture_4::X1237   int g() {
1238     auto L = [](auto a) {
1239       return [=](auto i) {
1240         return [=](auto b) {
1241           f(b); //expected-error {{'this' cannot}}
1242           int x = i;
1243         };
1244       };
1245     };
1246     auto M = L(0.0);
1247     auto N = M(3);
1248     N(5); //expected-note {{in instantiation of}}
1249     N(3.14); // OK.
1250     return 0;
1251   }
1252 };
1253 
1254 int run = X{}.g();
1255 
1256 }
1257 namespace capture_enclosing_function_parameters {
1258 
1259 
foo(int x)1260 inline auto foo(int x) {
1261   int i = 10;
1262   auto lambda = [=](auto z) { return x + z; };
1263   return lambda;
1264 }
1265 
foo2()1266 int foo2() {
1267   auto L = foo(3);
1268   L(4);
1269   L('a');
1270   L(3.14);
1271   return 0;
1272 }
1273 
foo3(int x)1274 inline auto foo3(int x) {
1275   int local = 1;
1276   auto L = [=](auto a) {
1277         int i = a[local];
1278         return  [=](auto b) mutable {
1279           auto n = b;
1280           return [&, n](auto c) mutable {
1281             ++local;
1282             return ++x;
1283           };
1284         };
1285   };
1286   auto M = L("foo-abc");
1287   auto N = M("foo-def");
1288   auto O = N("foo-ghi");
1289 
1290   return L;
1291 }
1292 
main()1293 int main() {
1294   auto L3 = foo3(3);
1295   auto M3 = L3("L3-1");
1296   auto N3 = M3("M3-1");
1297   auto O3 = N3("N3-1");
1298   N3("N3-2");
1299   M3("M3-2");
1300   M3("M3-3");
1301   L3("L3-2");
1302 }
1303 } // end ns
1304 
1305 namespace capture_arrays {
1306 
sum_array(int n)1307 inline int sum_array(int n) {
1308   int array2[5] = { 1, 2, 3, 4, 5};
1309 
1310   auto L = [=](auto N) -> int {
1311     int sum = 0;
1312     int array[5] = { 1, 2, 3, 4, 5 };
1313     sum += array2[sum];
1314     sum += array2[N];
1315     return 0;
1316   };
1317   L(2);
1318   return L(n);
1319 }
1320 }
1321 
1322 namespace capture_non_odr_used_variable_because_named_in_instantiation_dependent_expressions {
1323 
1324 // even though 'x' is not odr-used, it should be captured.
1325 
test()1326 int test() {
1327   const int x = 10;
1328   auto L = [=](auto a) {
1329     (void) +x + a;
1330   };
1331   ASSERT_CLOSURE_SIZE_EXACT(L, sizeof(x));
1332 }
1333 
1334 } //end ns
1335 #ifdef MS_EXTENSIONS
1336 namespace explicit_spec {
1337 template<class R> struct X {
fooexplicit_spec::X1338   template<class T> int foo(T t) {
1339     auto L = [](auto a) { return a; };
1340     L(&t);
1341     return 0;
1342   }
1343 
fooexplicit_spec::X1344   template<> int foo<char>(char c) { //expected-warning{{explicit specialization}}
1345     const int x = 10;
1346     auto LC = [](auto a) { return a; };
1347     R r;
1348     LC(&r);
1349     auto L = [=](auto a) {
1350       return [=](auto b) {
1351         int d[sizeof(a)];
1352         f(x, d);
1353       };
1354     };
1355     auto M = L(1);
1356 
1357     ASSERT_NO_CAPTURES(M);
1358     return 0;
1359   }
1360 
1361 };
1362 
1363 int run_char = X<int>{}.foo('a');
1364 int run_int = X<double>{}.foo(4);
1365 }
1366 #endif // MS_EXTENSIONS
1367 
1368 namespace nsdmi_capturing_this {
1369 struct X {
1370   int m = 10;
__anon63b8ffb99f02nsdmi_capturing_this::X1371   int n = [this](auto) { return m; }(20);
1372 };
1373 
1374 template<class T>
1375 struct XT {
1376   T m = 10;
__anon63b8ffb9a002nsdmi_capturing_this::XT1377   T n = [this](auto) { return m; }(20);
1378 };
1379 
1380 XT<int> xt{};
1381 
1382 
1383 }
1384 
PR33318(int i)1385 void PR33318(int i) {
1386   [&](auto) { static_assert(&i != nullptr, ""); }(0); // expected-warning 2{{always true}} expected-note {{instantiation}}
1387 }
1388 
1389 // Check to make sure that we don't capture when member-calls are made to members that are not of 'this' class.
1390 namespace PR34266 {
1391 // https://bugs.llvm.org/show_bug.cgi?id=34266
1392 namespace ns1 {
1393 struct A {
barPR34266::ns1::A1394   static void bar(int) { }
barPR34266::ns1::A1395   static void bar(double) { }
1396 };
1397 
1398 struct B
1399 {
1400   template<class T>
fPR34266::ns1::B1401   auto f() {
1402     auto L =  [=] {
1403       T{}.bar(3.0);
1404       T::bar(3);
1405 
1406     };
1407     ASSERT_NO_CAPTURES(L);
1408     return L;
1409   };
1410 };
1411 
test()1412 void test() {
1413   B{}.f<A>();
1414 }
1415 } // end ns1
1416 
1417 namespace ns2 {
1418 struct A {
barPR34266::ns2::A1419   static void bar(int) { }
barPR34266::ns2::A1420   static void bar(double) { }
1421 };
1422 
1423 struct B
1424 {
1425   using T = A;
fPR34266::ns2::B1426   auto f() {
1427     auto L =  [=](auto a) {
1428       T{}.bar(a);
1429       T::bar(a);
1430 
1431     };
1432     ASSERT_NO_CAPTURES(L);
1433     return L;
1434   };
1435 };
1436 
test()1437 void test() {
1438   B{}.f()(3.0);
1439   B{}.f()(3);
1440 }
1441 } // end ns2
1442 
1443 namespace ns3 {
1444 struct A {
barPR34266::ns3::A1445   void bar(int) { }
barPR34266::ns3::A1446   static void bar(double) { }
1447 };
1448 
1449 struct B
1450 {
1451   using T = A;
fPR34266::ns3::B1452   auto f() {
1453     auto L =  [=](auto a) {
1454       T{}.bar(a);
1455       T::bar(a); // This call ignores the instance member function because the implicit object argument fails to convert.
1456 
1457     };
1458     ASSERT_NO_CAPTURES(L);
1459     return L;
1460   };
1461 };
1462 
test()1463 void test() {
1464   B{}.f()(3.0);
1465   B{}.f()(3);
1466 }
1467 
1468 } // end ns3
1469 
1470 
1471 namespace ns4 {
1472 struct A {
barPR34266::ns4::A1473   void bar(int) { }
barPR34266::ns4::A1474   static void bar(double) { }
1475 };
1476 
1477 struct B : A
1478 {
1479   using T = A;
fPR34266::ns4::B1480   auto f() {
1481     auto L =  [=](auto a) {
1482       T{}.bar(a);
1483       T::bar(a);
1484 
1485     };
1486     // just check to see if the size if >= 2 bytes (which should be the case if we capture anything)
1487     ASSERT_CLOSURE_SIZE(L, 2);
1488     return L;
1489   };
1490 };
1491 
test()1492 void test() {
1493   B{}.f()(3.0);
1494   B{}.f()(3);
1495 }
1496 
1497 } // end ns4
1498 
1499 namespace ns5 {
1500 struct A {
barPR34266::ns5::A1501   void bar(int) { }
barPR34266::ns5::A1502   static void bar(double) { }
1503 };
1504 
1505 struct B
1506 {
1507   template<class T>
fPR34266::ns5::B1508   auto f() {
1509     auto L =  [&](auto a) {
1510       T{}.bar(a);
1511       T::bar(a);
1512 
1513     };
1514 
1515     ASSERT_NO_CAPTURES(L);
1516     return L;
1517   };
1518 };
1519 
test()1520 void test() {
1521   B{}.f<A>()(3.0);
1522   B{}.f<A>()(3);
1523 }
1524 
1525 } // end ns5
1526 
1527 } // end PR34266
1528 
1529 namespace capture_pack {
1530 #if __cplusplus >= 201702L
1531   constexpr
1532 #endif
1533   auto v =
__anon63b8ffb9a702(auto ...a) 1534     [](auto ...a) {
1535       [&](auto ...b) {
1536         ((a = b), ...); // expected-warning 0-1{{extension}}
1537       }(100, 20, 3);
1538       return (a + ...); // expected-warning 0-1{{extension}}
1539     }(400, 50, 6);
1540 #if __cplusplus >= 201702L
1541   static_assert(v == 123);
1542 #endif
1543 }
1544