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