xref: /llvm-project/clang/test/SemaCXX/cxx1y-generic-lambdas.cpp (revision 8a26c6d9d4ca18b9906f3ec13d52fc0b2952a5f7)
1 // RUN: %clang_cc1 -std=c++1y -verify -fblocks -emit-llvm-only %s
2 // RUN: %clang_cc1 -std=c++1y -verify -fsyntax-only -fblocks -fdelayed-template-parsing %s -DDELAYED_TEMPLATE_PARSING
3 // RUN: %clang_cc1 -std=c++1y -verify -fsyntax-only -fblocks -fms-extensions %s -DMS_EXTENSIONS
4 // RUN: %clang_cc1 -std=c++1y -verify -fsyntax-only -fblocks -fdelayed-template-parsing -fms-extensions %s -DMS_EXTENSIONS -DDELAYED_TEMPLATE_PARSING
5 // RUN: %clang_cc1 -std=c++1y -verify -fblocks -triple i386-windows-pc -emit-llvm-only %s
6 // RUN: %clang_cc1 -std=c++1y -verify -fsyntax-only -fblocks -triple i386-windows-pc -fdelayed-template-parsing %s -DDELAYED_TEMPLATE_PARSING
7 // RUN: %clang_cc1 -std=c++1y -verify -fsyntax-only -fblocks -triple i386-windows-pc -fms-extensions %s -DMS_EXTENSIONS
8 // RUN: %clang_cc1 -std=c++1y -verify -fsyntax-only -fblocks -triple i386-windows-pc -fdelayed-template-parsing -fms-extensions %s -DMS_EXTENSIONS -DDELAYED_TEMPLATE_PARSING
9 
10 template<class F, class ...Rest> struct first_impl { typedef F type; };
11 template<class ...Args> using first = typename first_impl<Args...>::type;
12 
13 namespace simple_explicit_capture {
14   void test() {
15     int i;
16     auto L = [i](auto a) { return i + a; };
17     L(3.14);
18   }
19 }
20 
21 namespace explicit_call {
22 int test() {
23   auto L = [](auto a) { return a; };
24   L.operator()(3);
25   L.operator()<char>(3.14); //expected-warning{{implicit conversion}}
26   return 0;
27 }
28 } //end ns
29 
30 namespace test_conversion_to_fptr_2 {
31 
32 template<class T> struct X {
33 
34   T (*fp)(T) = [](auto a) { return a; };
35 
36 };
37 
38 X<int> xi;
39 
40 template<class T>
41 void fooT(T t, T (*fp)(T) = [](auto a) { return a; }) {
42   fp(t);
43 }
44 
45 int test() {
46 {
47   auto L = [](auto a) { return a; };
48   int (*fp)(int) = L;
49   fp(5);
50   L(3);
51   char (*fc)(char) = L;
52   fc('b');
53   L('c');
54   double (*fd)(double) = L;
55   fd(3.14);
56   fd(6.26);
57   L(4.25);
58 }
59 {
60   auto L = [](auto a) ->int { return a; }; //expected-note 2{{candidate template ignored}}
61   int (*fp)(int) = L;
62   char (*fc)(char) = L; //expected-error{{no viable conversion}}
63   double (*fd)(double) = L; //expected-error{{no viable conversion}}
64 }
65 {
66   int x = 5;
67   auto L = [=](auto b, char c = 'x') {
68     int i = x;
69     return [](auto a) ->decltype(a) { return a; };
70   };
71   int (*fp)(int) = L(8);
72   fp(5);
73   L(3);
74   char (*fc)(char) = L('a');
75   fc('b');
76   L('c');
77   double (*fd)(double) = L(3.14);
78   fd(3.14);
79   fd(6.26);
80 
81 }
82 {
83  auto L = [=](auto b) {
84     return [](auto a) ->decltype(b)* { return (decltype(b)*)0; };
85   };
86   int* (*fp)(int) = L(8);
87   fp(5);
88   L(3);
89   char* (*fc)(char) = L('a');
90   fc('b');
91   L('c');
92   double* (*fd)(double) = L(3.14);
93   fd(3.14);
94   fd(6.26);
95 }
96 {
97  auto L = [=](auto b) {
98     return [](auto a) ->decltype(b)* { return (decltype(b)*)0; }; //expected-note{{candidate template ignored}}
99   };
100   char* (*fp)(int) = L('8');
101   fp(5);
102   char* (*fc)(char) = L('a');
103   fc('b');
104   double* (*fi)(int) = L(3.14);
105   fi(5);
106   int* (*fi2)(int) = L(3.14); //expected-error{{no viable conversion}}
107 }
108 
109 {
110  auto L = [=](auto b) {
111     return [](auto a) {
112       return [=](auto c) {
113         return [](auto d) ->decltype(a + b + c + d) { return d; };
114       };
115     };
116   };
117   int (*fp)(int) = L('8')(3)(short{});
118   double (*fs)(char) = L(3.14)(short{})('4');
119 }
120 
121   fooT(3);
122   fooT('a');
123   fooT(3.14);
124   fooT("abcdefg");
125   return 0;
126 }
127 int run2 = test();
128 
129 }
130 
131 
132 namespace test_conversion_to_fptr {
133 
134 void f1(int (*)(int)) { }
135 void f2(char (*)(int)) { } // expected-note{{candidate}}
136 void g(int (*)(int)) { } // #1 expected-note{{candidate}}
137 void g(char (*)(char)) { } // #2 expected-note{{candidate}}
138 void h(int (*)(int)) { } // #3
139 void h(char (*)(int)) { } // #4
140 
141 int test() {
142 {
143   auto glambda = [](auto a) { return a; };
144   glambda(1);
145   f1(glambda); // OK
146   f2(glambda); // expected-error{{no matching function}}
147   g(glambda); // expected-error{{call to 'g' is ambiguous}}
148   h(glambda); // OK: calls #3 since it is convertible from ID
149 
150   int& (*fpi)(int*) = [](auto* a) -> auto& { return *a; }; // OK
151 
152 }
153 {
154 
155   auto L = [](auto a) { return a; };
156   int (*fp)(int) = L;
157   fp(5);
158   L(3);
159   char (*fc)(char) = L;
160   fc('b');
161   L('c');
162   double (*fd)(double) = L;
163   fd(3.14);
164   fd(6.26);
165   L(4.25);
166 }
167 {
168   auto L = [](auto a) ->int { return a; }; //expected-note 2{{candidate template ignored}}
169   int (*fp)(int) = L;
170   char (*fc)(char) = L; //expected-error{{no viable conversion}}
171   double (*fd)(double) = L; //expected-error{{no viable conversion}}
172 }
173 {
174   int* (*fp)(int*) = [](auto *a) -> auto* { return a; };
175   fp(0);
176 }
177 }
178 
179 namespace more_converion_to_ptr_to_function_tests {
180 
181 
182 int test() {
183   {
184     int& (*fpi)(int*) = [](auto* a) -> auto& { return *a; }; // OK
185     int (*fp2)(int) = [](auto b) -> int {  return b; };
186     int (*fp3)(char) = [](auto c) -> int { return c; };
187     char (*fp4)(int) = [](auto d) { return d; }; //expected-error{{no viable conversion}}\
188                                                  //expected-note{{candidate function [with d:auto = int]}}
189     char (*fp5)(char) = [](auto e) -> int { return e; }; //expected-error{{no viable conversion}}\
190                                                  //expected-note{{candidate template ignored}}
191 
192     fp2(3);
193     fp3('\n');
194     fp3('a');
195     return 0;
196   }
197 } // end test()
198 
199 template<class ... Ts> void vfun(Ts ... ) { }
200 
201 int variadic_test() {
202 
203  int (*fp)(int, char, double) = [](auto ... a) -> int { vfun(a...); return 4; };
204  fp(3, '4', 3.14);
205 
206  int (*fp2)(int, char, double) = [](auto ... a) { vfun(a...); return 4; };
207  fp(3, '4', 3.14);
208  return 2;
209 }
210 
211 } // end ns
212 
213 namespace conversion_operator {
214   void test() {
215     auto L = [](auto a) -> int { return a; }; // expected-error {{cannot initialize}}
216     int (*fp)(int) = L;
217     int (&fp2)(int) = [](auto a) { return a; };  // expected-error{{non-const lvalue}}
218     int (&&fp3)(int) = [](auto a) { return a; };
219     // expected-error@-1   {{no viable conversion}}
220     // expected-note-re@-2 {{candidate template ignored: could not match 'auto (*)(auto){{.*}}' against 'int (int)'}}
221 
222     using F = int(int);
223     using G = int(void*);
224     L.operator F*();
225     L.operator G*(); // expected-note-re {{instantiation of function template specialization '{{.*}}::operator()<void *>'}}
226 
227     // Here, the conversion function is named 'operator auto (*)(int)', and
228     // there is no way to write that name in valid C++.
229     auto M = [](auto a) -> auto { return a; };
230     M.operator F*(); // expected-error {{no member named 'operator int (*)(int)'}}
231   }
232 }
233 }
234 
235 namespace return_type_deduction_ok {
236  auto l = [](auto a) ->auto { return a; }(2);
237  auto l2 = [](auto a) ->decltype(auto) { return a; }(2);
238  auto l3 = [](auto a) { return a; }(2);
239 
240 }
241 
242 namespace generic_lambda_as_default_argument_ok {
243   void test(int i = [](auto a)->int { return a; }(3)) {
244   }
245 }
246 
247 namespace nested_non_capturing_lambda_tests {
248 template<class ... Ts> void print(Ts ...) { }
249 int test() {
250 {
251   auto L = [](auto a) {
252     return [](auto b) {
253       return b;
254     };
255   };
256   auto M = L(3);
257   M(4.15);
258  }
259 {
260   int i = 10; //expected-note 3{{declared here}}
261   auto L = [](auto a) {
262     return [](auto b) { //expected-note 3{{begins here}} expected-note 6 {{capture 'i' by}} expected-note 6 {{default capture by}} expected-note {{while substituting into a lambda}}
263       i = b;  //expected-error 3{{cannot be implicitly captured}}
264       return b;
265     };
266   };
267   auto M = L(3); //expected-note{{instantiation}}
268   M(4.15); //expected-note{{instantiation}}
269  }
270  {
271   int i = 10;
272   auto L = [](auto a) {
273     return [](auto b) {
274       b = sizeof(i);  //ok
275       return b;
276     };
277   };
278  }
279  {
280   auto L = [](auto a) {
281     print("a = ", a, "\n");
282     return [](auto b) ->decltype(a) {
283       print("b = ", b, "\n");
284       return b;
285     };
286   };
287   auto M = L(3);
288   M(4.15);
289  }
290 
291 {
292   auto L = [](auto a) ->decltype(a) {
293     print("a = ", a, "\n");
294     return [](auto b) ->decltype(a) {
295       // expected-error@-1   {{no viable conversion}}
296       // expected-note-re@-2 {{candidate template ignored: could not match 'auto (*)(auto){{.*}}' ({{.*}}) against 'decltype(a)' (aka 'int')}}
297       print("b = ", b, "\n");
298       return b;
299     };
300   };
301   auto M = L(3); //expected-note{{in instantiation of}}
302  }
303 {
304   auto L = [](auto a) {
305     print("a = ", a, "\n");
306     return [](auto ... b) ->decltype(a) {
307       print("b = ", b ..., "\n");
308       return 4;
309     };
310   };
311   auto M = L(3);
312   M(4.15, 3, "fv");
313 }
314 
315 {
316   auto L = [](auto a) {
317     print("a = ", a, "\n");
318     return [](auto ... b) ->decltype(a) {
319       print("b = ", b ..., "\n");
320       return 4;
321     };
322   };
323   auto M = L(3);
324   int (*fp)(double, int, const char*) = M;
325   fp(4.15, 3, "fv");
326 }
327 
328 {
329   auto L = [](auto a) {
330     print("a = ", a, "\n");
331     return [](char b) {
332       return [](auto ... c) ->decltype(b) {
333         print("c = ", c ..., "\n");
334         return 42;
335       };
336     };
337   };
338   L(4);
339   auto M = L(3);
340   M('a');
341   auto N = M('x');
342   N("\n3 = ", 3, "\n6.14 = ", 6.14, "\n4'123'456 = ", 4'123'456);
343   char (*np)(const char*, int, const char*, double, const char*, int) = N;
344   np("\n3 = ", 3, "\n6.14 = ", 6.14, "\n4'123'456 = ", 4'123'456);
345 }
346 
347 
348 {
349   auto L = [](auto a) {
350     print("a = ", a, "\n");
351     return [](decltype(a) b) {
352       return [](auto ... c) ->decltype(b) {
353         print("c = ", c ..., "\n");
354         return 42;
355       };
356     };
357   };
358   L('4');
359   auto M = L('3');
360   M('a');
361   auto N = M('x');
362   N("\n3 = ", 3, "\n6.14 = ", 6.14, "\n4'123'456 = ", 4'123'456);
363   char (*np)(const char*, int, const char*, double, const char*, int) = N;
364   np("\n3 = ", 3, "\n6.14 = ", 6.14, "\n4'123'456 = ", 4'123'456);
365 }
366 
367 
368 {
369  struct X {
370   static void foo(double d) { }
371   void test() {
372     auto L = [](auto a) {
373       print("a = ", a, "\n");
374       foo(a);
375       return [](decltype(a) b) {
376         foo(b);
377         foo(sizeof(a) + sizeof(b));
378         return [](auto ... c) ->decltype(b) {
379           print("c = ", c ..., "\n");
380           foo(decltype(b){});
381           foo(sizeof(decltype(a)*) + sizeof(decltype(b)*));
382           return 42;
383         };
384       };
385     };
386     L('4');
387     auto M = L('3');
388     M('a');
389     auto N = M('x');
390     N("\n3 = ", 3, "\n6.14 = ", 6.14, "\n4'123'456 = ", 4'123'456);
391     char (*np)(const char*, int, const char*, double, const char*, int) = N;
392     np("\n3 = ", 3, "\n6.14 = ", 6.14, "\n4'123'456 = ", 4'123'456);
393   }
394 };
395 X x;
396 x.test();
397 }
398 // Make sure we can escape the function
399 {
400  struct X {
401   static void foo(double d) { }
402   auto test() {
403     auto L = [](auto a) {
404       print("a = ", a, "\n");
405       foo(a);
406       return [](decltype(a) b) {
407         foo(b);
408         foo(sizeof(a) + sizeof(b));
409         return [](auto ... c) ->decltype(b) {
410           print("c = ", c ..., "\n");
411           foo(decltype(b){});
412           foo(sizeof(decltype(a)*) + sizeof(decltype(b)*));
413           return 42;
414         };
415       };
416     };
417     return L;
418   }
419 };
420   X x;
421   auto L = x.test();
422   L('4');
423   auto M = L('3');
424   M('a');
425   auto N = M('x');
426   N("\n3 = ", 3, "\n6.14 = ", 6.14, "\n4'123'456 = ", 4'123'456);
427   char (*np)(const char*, int, const char*, double, const char*, int) = N;
428   np("\n3 = ", 3, "\n6.14 = ", 6.14, "\n4'123'456 = ", 4'123'456);
429 }
430 
431 {
432  struct X {
433   static void foo(double d) { }
434   auto test() {
435     auto L = [](auto a) {
436       print("a = ", a, "\n");
437       foo(a);
438       return [](decltype(a) b) {
439         foo(b);
440         foo(sizeof(a) + sizeof(b));
441         return [](auto ... c) {
442           print("c = ", c ..., "\n");
443           foo(decltype(b){});
444           foo(sizeof(decltype(a)*) + sizeof(decltype(b)*));
445           return [](decltype(c) ... d) ->decltype(a) { //expected-note{{candidate}}
446             print("d = ", d ..., "\n");
447             foo(decltype(b){});
448             foo(sizeof(decltype(a)*) + sizeof(decltype(b)*));
449             return decltype(a){};
450           };
451         };
452       };
453     };
454     return L;
455   }
456 };
457   X x;
458   auto L = x.test();
459   L('4');
460   auto M = L('3');
461   M('a');
462   auto N = M('x');
463   auto O = N("\n3 = ", 3, "\n6.14 = ", 6.14, "\n4'123'456 = ", 4'123'456);
464   char (*np)(const char*, int, const char*, double, const char*, int) = O;
465   np("\n3 = ", 3, "\n6.14 = ", 6.14, "\n4'123'456 = ", 4'123'456);
466   int (*np2)(const char*, int, const char*, double, const char*, int) = O; // expected-error{{no viable conversion}}
467 
468 }
469 } // end test()
470 
471 namespace wrapped_within_templates {
472 
473 namespace explicit_return {
474 template<class T> int fooT(T t) {
475   auto L = [](auto a) -> void {
476     auto M = [](char b) -> void {
477       auto N = [](auto c) -> void {
478         int x = 0;
479         x = sizeof(a);
480         x = sizeof(b);
481         x = sizeof(c);
482       };
483       N('a');
484       N(decltype(a){});
485     };
486   };
487   L(t);
488   L(3.14);
489   return 0;
490 }
491 
492 int run = fooT('a') + fooT(3.14);
493 
494 } // end explicit_return
495 
496 namespace implicit_return_deduction {
497 template<class T> auto fooT(T t) {
498   auto L = [](auto a)  {
499     auto M = [](char b)  {
500       auto N = [](auto c)  {
501         int x = 0;
502         x = sizeof(a);
503         x = sizeof(b);
504         x = sizeof(c);
505       };
506       N('a');
507       N(decltype(a){});
508     };
509   };
510   L(t);
511   L(3.14);
512   return 0;
513 }
514 
515 int run = fooT('a') + fooT(3.14);
516 
517 template<class ... Ts> void print(Ts ... ts) { }
518 
519 template<class ... Ts> auto fooV(Ts ... ts) {
520   auto L = [](auto ... a) {
521     auto M = [](decltype(a) ... b) {
522       auto N = [](auto c) {
523         int x = 0;
524         x = sizeof...(a);
525         x = sizeof...(b);
526         x = sizeof(c);
527       };
528       N('a');
529       N(N);
530       N(first<Ts...>{});
531     };
532     M(a...);
533     print("a = ", a..., "\n");
534   };
535   L(L, ts...);
536   print("ts = ", ts..., "\n");
537   return 0;
538 }
539 
540 int run2 = fooV(3.14, " ", '4', 5) + fooV("BC", 3, 2.77, 'A', float{}, short{}, unsigned{});
541 
542 } //implicit_return_deduction
543 
544 
545 } //wrapped_within_templates
546 
547 namespace at_ns_scope {
548   void foo(double d) { }
549   auto test() {
550     auto L = [](auto a) {
551       print("a = ", a, "\n");
552       foo(a);
553       return [](decltype(a) b) {
554         foo(b);
555         foo(sizeof(a) + sizeof(b));
556         return [](auto ... c) {
557           print("c = ", c ..., "\n");
558           foo(decltype(b){});
559           foo(sizeof(decltype(a)*) + sizeof(decltype(b)*));
560           return [](decltype(c) ... d) ->decltype(a) { //expected-note{{candidate}}
561             print("d = ", d ..., "\n");
562             foo(decltype(b){});
563             foo(sizeof(decltype(a)*) + sizeof(decltype(b)*));
564             return decltype(a){};
565           };
566         };
567       };
568     };
569     return L;
570   }
571 auto L = test();
572 auto L_test = L('4');
573 auto M = L('3');
574 auto M_test = M('a');
575 auto N = M('x');
576 auto O = N("\n3 = ", 3, "\n6.14 = ", 6.14, "\n4'123'456 = ", 4'123'456);
577 char (*np)(const char*, int, const char*, double, const char*, int) = O;
578 auto NP_result = np("\n3 = ", 3, "\n6.14 = ", 6.14, "\n4'123'456 = ", 4'123'456);
579 int (*np2)(const char*, int, const char*, double, const char*, int) = O; // expected-error{{no viable conversion}}
580 
581 
582 
583 }
584 
585 namespace variadic_tests_1 {
586 template<class ... Ts> void print(Ts ... ts) { }
587 
588 template<class F, class ... Rest> F& FirstArg(F& f, Rest...) { return f; }
589 
590 template<class ... Ts> int fooV(Ts ... ts) {
591   auto L = [](auto ... a) -> void {
592     auto M = [](decltype(a) ... b) -> void {
593       auto N = [](auto c) -> void {
594         int x = 0;
595         x = sizeof...(a);
596         x = sizeof...(b);
597         x = sizeof(c);
598       };
599       N('a');
600       N(N);
601       N(first<Ts...>{});
602     };
603     M(a...);
604     print("a = ", a..., "\n");
605   };
606   L(L, ts...);
607   print("ts = ", ts..., "\n");
608   return 0;
609 }
610 
611 int run2 = fooV(3.14, " ", '4', 5) + fooV("BC", 3, 2.77, 'A', float{}, short{}, unsigned{});
612 
613 namespace more_variadic_1 {
614 
615 template<class ... Ts> int fooV(Ts ... ts) {
616   auto L = [](auto ... a) {
617     auto M = [](decltype(a) ... b) -> void {
618       auto N = [](auto c) -> void {
619         int x = 0;
620         x = sizeof...(a);
621         x = sizeof...(b);
622         x = sizeof(c);
623       };
624       N('a');
625       N(N);
626       N(first<Ts...>{});
627     };
628     M(a...);
629     return M;
630   };
631   auto M = L(L, ts...);
632   decltype(L(L, ts...)) (*fp)(decltype(L), decltype(ts) ...) = L;
633   void (*fp2)(decltype(L), decltype(ts) ...) = L(L, ts...);
634 
635   {
636     auto L = [](auto ... a) {
637       auto M = [](decltype(a) ... b) {
638         auto N = [](auto c) -> void {
639           int x = 0;
640           x = sizeof...(a);
641           x = sizeof...(b);
642           x = sizeof(c);
643         };
644         N('a');
645         N(N);
646         N(first<Ts...>{});
647         return N;
648       };
649       M(a...);
650       return M;
651     };
652     auto M = L(L, ts...);
653     decltype(L(L, ts...)) (*fp)(decltype(L), decltype(ts) ...) = L;
654     fp(L, ts...);
655     decltype(L(L, ts...)(L, ts...)) (*fp2)(decltype(L), decltype(ts) ...) = L(L, ts...);
656     fp2 = fp(L, ts...);
657     void (*fp3)(char) = fp2(L, ts...);
658     fp3('a');
659   }
660   return 0;
661 }
662 
663 int run2 = fooV(3.14, " ", '4', 5) + fooV("BC", 3, 2.77, 'A', float{}, short{}, unsigned{});
664 
665 
666 } //end ns more_variadic_1
667 
668 } // end ns variadic_tests_1
669 
670 namespace at_ns_scope_within_class_member {
671  struct X {
672   static void foo(double d) { }
673   auto test() {
674     auto L = [](auto a) {
675       print("a = ", a, "\n");
676       foo(a);
677       return [](decltype(a) b) {
678         foo(b);
679         foo(sizeof(a) + sizeof(b));
680         return [](auto ... c) {
681           print("c = ", c ..., "\n");
682           foo(decltype(b){});
683           foo(sizeof(decltype(a)*) + sizeof(decltype(b)*));
684           return [](decltype(c) ... d) ->decltype(a) { //expected-note{{candidate}}
685             print("d = ", d ..., "\n");
686             foo(decltype(b){});
687             foo(sizeof(decltype(a)*) + sizeof(decltype(b)*));
688             return decltype(a){};
689           };
690         };
691       };
692     };
693     return L;
694   }
695 };
696 X x;
697 auto L = x.test();
698 auto L_test = L('4');
699 auto M = L('3');
700 auto M_test = M('a');
701 auto N = M('x');
702 auto O = N("\n3 = ", 3, "\n6.14 = ", 6.14, "\n4'123'456 = ", 4'123'456);
703 char (*np)(const char*, int, const char*, double, const char*, int) = O;
704 auto NP_result = np("\n3 = ", 3, "\n6.14 = ", 6.14, "\n4'123'456 = ", 4'123'456);
705 int (*np2)(const char*, int, const char*, double, const char*, int) = O; // expected-error{{no viable conversion}}
706 
707 } //end at_ns_scope_within_class_member
708 
709 
710 namespace at_ns_scope_within_class_template_member {
711  struct X {
712   static void foo(double d) { }
713   template<class T = int>
714   auto test(T = T{}) {
715     auto L = [](auto a) {
716       print("a = ", a, "\n");
717       foo(a);
718       return [](decltype(a) b) {
719         foo(b);
720         foo(sizeof(a) + sizeof(b));
721         return [](auto ... c) {
722           print("c = ", c ..., "\n");
723           foo(decltype(b){});
724           foo(sizeof(decltype(a)*) + sizeof(decltype(b)*));
725           return [](decltype(c) ... d) ->decltype(a) { //expected-note{{candidate}}
726             print("d = ", d ..., "\n");
727             foo(decltype(b){});
728             foo(sizeof(decltype(a)*) + sizeof(decltype(b)*));
729             return decltype(a){};
730           };
731         };
732       };
733     };
734     return L;
735   }
736 
737 };
738 X x;
739 auto L = x.test();
740 auto L_test = L('4');
741 auto M = L('3');
742 auto M_test = M('a');
743 auto N = M('x');
744 auto O = N("\n3 = ", 3, "\n6.14 = ", 6.14, "\n4'123'456 = ", 4'123'456);
745 char (*np)(const char*, int, const char*, double, const char*, int) = O;
746 auto NP_result = np("\n3 = ", 3, "\n6.14 = ", 6.14, "\n4'123'456 = ", 4'123'456);
747 int (*np2)(const char*, int, const char*, double, const char*, int) = O; // expected-error{{no viable conversion}}
748 
749 } //end at_ns_scope_within_class_member
750 
751 
752 namespace nested_generic_lambdas_123 {
753 void test() {
754   auto L = [](auto a) -> int {
755     auto M = [](auto b, decltype(a) b2) -> int {
756       return 1;
757     };
758     M(a, a);
759   };
760   L(3);
761 }
762 template<class T> void foo(T) {
763  auto L = [](auto a) { return a; };
764 }
765 template void foo(int);
766 } // end ns nested_generic_lambdas_123
767 
768 namespace nested_fptr_235 {
769 int test()
770 {
771   auto L = [](auto b) {
772     return [](auto a) ->decltype(a) { return a; };
773   };
774   int (*fp)(int) = L(8);
775   fp(5);
776   L(3);
777   char (*fc)(char) = L('a');
778   fc('b');
779   L('c');
780   double (*fd)(double) = L(3.14);
781   fd(3.14);
782   fd(6.26);
783   return 0;
784 }
785 int run = test();
786 }
787 
788 
789 namespace fptr_with_decltype_return_type {
790 template<class F, class ... Rest> F& FirstArg(F& f, Rest& ... r) { return f; };
791 template<class ... Ts> auto vfun(Ts&& ... ts) {
792   print(ts...);
793   return FirstArg(ts...);
794 }
795 int test()
796 {
797  {
798    auto L = [](auto ... As) {
799     return [](auto b) ->decltype(b) {
800       vfun([](decltype(As) a) -> decltype(a) { return a; } ...)(first<decltype(As)...>{});
801       return decltype(b){};
802     };
803    };
804    auto LL = L(1, 'a', 3.14, "abc");
805    LL("dim");
806  }
807   return 0;
808 }
809 int run = test();
810 }
811 
812 } // end ns nested_non_capturing_lambda_tests
813 
814 namespace PR17476 {
815 struct string {
816   string(const char *__s) { }
817   string &operator+=(const string &__str) { return *this; }
818 };
819 
820 template <class T>
821 void finalizeDefaultAtomValues() {
822   auto startEnd = [](const char * sym) -> void {
823     string start("__");
824     start += sym;
825   };
826   startEnd("preinit_array");
827 }
828 
829 void f() { finalizeDefaultAtomValues<char>(); }
830 
831 }
832 
833 namespace PR17476_variant {
834 struct string {
835   string(const char *__s) { }
836   string &operator+=(const string &__str) { return *this; }
837 };
838 
839 template <class T>
840 void finalizeDefaultAtomValues() {
841   auto startEnd = [](const T *sym) -> void {
842     string start("__");
843     start += sym;
844   };
845   startEnd("preinit_array");
846 }
847 
848 void f() { finalizeDefaultAtomValues<char>(); }
849 
850 }
851 
852 namespace PR17877_lambda_declcontext_and_get_cur_lambda_disconnect {
853 
854 
855 template<class T> struct U {
856   int t = 0;
857 };
858 
859 template<class T>
860 struct V {
861   U<T> size() const { return U<T>{}; }
862 };
863 
864 template<typename T>
865 void Do() {
866   V<int> v{};
867   [=] { v.size(); };
868 }
869 
870 }
871 
872 namespace inclass_lambdas_within_nested_classes {
873 namespace ns1 {
874 
875 struct X1 {
876   struct X2 {
877     enum { E = [](auto i) { return i; }(3) }; //expected-error{{inside of a constant expression}}\
878                                           //expected-error{{constant}}\
879                                           //expected-note{{non-literal type}}
880     int L = ([] (int i) { return i; })(2);
881     void foo(int i = ([] (int i) { return i; })(2)) { }
882     int B : ([](int i) { return i; })(3); //expected-error{{inside of a constant expression}}\
883                                           //expected-error{{not an integral constant}}\
884                                           //expected-note{{non-literal type}}
885     int arr[([](int i) { return i; })(3)]; //expected-error{{inside of a constant expression}}\
886                                            //expected-error{{must have a constant size}}\
887                                            //expected-warning{{variable length arrays in C++ are a Clang extension}}\
888                                            //expected-note-re{{non-literal type '{{.*}}' cannot be used in a constant expression}}
889     int (*fp)(int) = [](int i) { return i; };
890     void fooptr(int (*fp)(char) = [](char c) { return 0; }) { }
891     int L2 = ([](auto i) { return i; })(2);
892     void fooG(int i = ([] (auto i) { return i; })(2)) { }
893     int BG : ([](auto i) { return i; })(3); //expected-error{{inside of a constant expression}}  \
894                                             //expected-error{{not an integral constant}}\
895                                             //expected-note{{non-literal type}}
896     int arrG[([](auto i) { return i; })(3)]; //expected-error{{inside of a constant expression}}\
897                                              //expected-error{{must have a constant size}}\
898                                              //expected-warning{{variable length arrays in C++ are a Clang extension}}\
899                                              //expected-note-re{{non-literal type '{{.*}}' cannot be used in a constant expression}}
900 
901     int (*fpG)(int) = [](auto i) { return i; };
902     void fooptrG(int (*fp)(char) = [](auto c) { return 0; }) { }
903   };
904 };
905 } //end ns
906 
907 namespace ns2 {
908 struct X1 {
909   template<class T>
910   struct X2 {
911     int L = ([] (T i) { return i; })(2);
912     void foo(int i = ([] (int i) { return i; })(2)) { }
913     int B : ([](T i) { return i; })(3); //expected-error{{inside of a constant expression}}\
914                                         //expected-error{{not an integral constant}}\
915                                         //expected-note{{non-literal type}}
916     int arr[([](T i) { return i; })(3)]; //expected-error{{inside of a constant expression}}\
917                                          //expected-error{{must have a constant size}}\
918                                          //expected-warning{{variable length arrays in C++ are a Clang extension}}\
919                                          //expected-note-re{{non-literal type '{{.*}}' cannot be used in a constant expression}}
920 
921     int (*fp)(T) = [](T i) { return i; };
922     void fooptr(T (*fp)(char) = [](char c) { return 0; }) { }
923     int L2 = ([](auto i) { return i; })(2);
924     void fooG(T i = ([] (auto i) { return i; })(2)) { }
925     int BG : ([](auto i) { return i; })(3); //expected-error{{not an integral constant}}\
926                                             //expected-note{{non-literal type}}\
927                                             //expected-error{{inside of a constant expression}}
928     int arrG[([](auto i) { return i; })(3)]; //expected-error{{must have a constant size}} \
929                                              //expected-error{{inside of a constant expression}}\
930                                              //expected-warning{{variable length arrays in C++ are a Clang extension}}\
931                                              //expected-note-re{{non-literal type '{{.*}}' cannot be used in a constant expression}}
932 
933     int (*fpG)(T) = [](auto i) { return i; };
934     void fooptrG(T (*fp)(char) = [](auto c) { return 0; }) { }
935     template<class U = char> int fooG2(T (*fp)(U) = [](auto a) { return 0; }) { return 0; }
936     template<class U = char> int fooG3(T (*fp)(U) = [](auto a) { return 0; });
937   };
938 };
939 template<class T>
940 template<class U>
941 int X1::X2<T>::fooG3(T (*fp)(U)) { return 0; }
942 X1::X2<int> x2; //expected-note {{in instantiation of}}
943 int run1 = x2.fooG2();
944 int run2 = x2.fooG3();
945 } // end ns
946 
947 
948 
949 } //end ns inclass_lambdas_within_nested_classes
950 
951 namespace pr21684_disambiguate_auto_followed_by_ellipsis_no_id {
952 int a = [](auto ...) { return 0; }();
953 }
954 
955 namespace PR22117 {
956   int x = [](auto) {
957     return [](auto... run_args) {
958       using T = int(decltype(run_args)...);
959       return 0;
960     };
961   }(0)(0);
962 }
963 
964 namespace PR41139 {
965   int y = [](auto outer) {
966     return [](auto inner) {
967       using T = int(decltype(outer), decltype(inner));
968       return 0;
969     };
970   }(0)(0);
971 }
972 
973 namespace PR23716 {
974 template<typename T>
975 auto f(T x) {
976   auto g = [](auto&&... args) {
977     auto h = [args...]() -> int {
978       return 0;
979     };
980     return h;
981   };
982   return g;
983 }
984 
985 auto x = f(0)();
986 }
987 
988 namespace PR13987 {
989 class Enclosing {
990   void Method(char c = []()->char {
991     int d = [](auto x)->int {
992         struct LocalClass {
993           int Method() { return 0; }
994         };
995       return 0;
996     }(0);
997     return d; }()
998   );
999 };
1000 
1001 class Enclosing2 {
1002   void Method(char c = [](auto x)->char {
1003     int d = []()->int {
1004         struct LocalClass {
1005           int Method() { return 0; }
1006         };
1007       return 0;
1008     }();
1009     return d; }(0)
1010   );
1011 };
1012 
1013 class Enclosing3 {
1014   void Method(char c = [](auto x)->char {
1015     int d = [](auto y)->int {
1016         struct LocalClass {
1017           int Method() { return 0; }
1018         };
1019       return 0;
1020     }(0);
1021     return d; }(0)
1022   );
1023 };
1024 }
1025 
1026 namespace PR32638 {
1027  //https://bugs.llvm.org/show_bug.cgi?id=32638
1028  void test() {
1029     [](auto x) noexcept(noexcept(x)) { } (0);
1030  }
1031 }
1032 
1033 namespace PR46637 {
1034   auto x = [](auto (*p)()) { return p(); };
1035   auto y = [](auto (*p)() -> auto) { return p(); };
1036   int f();
1037   void *v = x(f); // expected-error {{cannot initialize a variable of type 'void *' with an rvalue of type 'int'}}
1038   void *w = y(f); // expected-error {{cannot initialize a variable of type 'void *' with an rvalue of type 'int'}}
1039 }
1040 
1041 namespace GH37792 {
1042 struct A { int x; };
1043 
1044 void f() {
1045   [](auto t) -> decltype(decltype(t)::x) { return 0; }(A());
1046 }
1047 }
1048