1 // RUN: %clang_cc1 -std=c++1y -verify -fsyntax-only -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
6 template<class F, class ...Rest> struct first_impl { typedef F type; };
7 template<class ...Args> using first = typename first_impl<Args...>::type;
9 namespace simple_explicit_capture {
12 auto L = [i](auto a) { return i + a; };
17 namespace explicit_call {
19 auto L = [](auto a) { return a; };
21 L.operator()<char>(3.14); //expected-warning{{implicit conversion}}
26 namespace test_conversion_to_fptr_2 {
28 template<class T> struct X {
30 T (*fp)(T) = [](auto a) { return a; };
37 void fooT(T t, T (*fp)(T) = [](auto a) { return a; }) {
43 auto L = [](auto a) { return a; };
50 double (*fd)(double) = L;
56 auto L = [](auto a) ->int { return a; }; //expected-note 2{{candidate template ignored}}
58 char (*fc)(char) = L; //expected-error{{no viable conversion}}
59 double (*fd)(double) = L; //expected-error{{no viable conversion}}
63 auto L = [=](auto b, char c = 'x') {
65 return [](auto a) ->decltype(a) { return a; };
67 int (*fp)(int) = L(8);
70 char (*fc)(char) = L('a');
73 double (*fd)(double) = L(3.14);
79 auto L = [=](auto b) {
80 return [](auto a) ->decltype(b)* { return (decltype(b)*)0; };
82 int* (*fp)(int) = L(8);
85 char* (*fc)(char) = L('a');
88 double* (*fd)(double) = L(3.14);
93 auto L = [=](auto b) {
94 return [](auto a) ->decltype(b)* { return (decltype(b)*)0; }; //expected-note{{candidate template ignored}}
96 char* (*fp)(int) = L('8');
98 char* (*fc)(char) = L('a');
100 double* (*fi)(int) = L(3.14);
102 int* (*fi2)(int) = L(3.14); //expected-error{{no viable conversion}}
106 auto L = [=](auto b) {
109 return [](auto d) ->decltype(a + b + c + d) { return d; };
113 int (*fp)(int) = L('8')(3)(short{});
114 double (*fs)(char) = L(3.14)(short{})('4');
128 namespace test_conversion_to_fptr {
130 void f1(int (*)(int)) { }
131 void f2(char (*)(int)) { } // expected-note{{candidate}}
132 void g(int (*)(int)) { } // #1 expected-note{{candidate}}
133 void g(char (*)(char)) { } // #2 expected-note{{candidate}}
134 void h(int (*)(int)) { } // #3
135 void h(char (*)(int)) { } // #4
139 auto glambda = [](auto a) { return a; };
142 f2(glambda); // expected-error{{no matching function}}
143 g(glambda); // expected-error{{call to 'g' is ambiguous}}
144 h(glambda); // OK: calls #3 since it is convertible from ID
146 int& (*fpi)(int*) = [](auto* a) -> auto& { return *a; }; // OK
151 auto L = [](auto a) { return a; };
155 char (*fc)(char) = L;
158 double (*fd)(double) = L;
164 auto L = [](auto a) ->int { return a; }; //expected-note 2{{candidate template ignored}}
166 char (*fc)(char) = L; //expected-error{{no viable conversion}}
167 double (*fd)(double) = L; //expected-error{{no viable conversion}}
170 int* (*fp)(int*) = [](auto *a) -> auto* { return a; };
175 namespace more_converion_to_ptr_to_function_tests {
180 int& (*fpi)(int*) = [](auto* a) -> auto& { return *a; }; // OK
181 int (*fp2)(int) = [](auto b) -> int { return b; };
182 int (*fp3)(char) = [](auto c) -> int { return c; };
183 char (*fp4)(int) = [](auto d) { return d; }; //expected-error{{no viable conversion}}\
184 //expected-note{{candidate template ignored}}
185 char (*fp5)(char) = [](auto e) -> int { return e; }; //expected-error{{no viable conversion}}\
186 //expected-note{{candidate template ignored}}
195 template<class ... Ts> void vfun(Ts ... ) { }
197 int variadic_test() {
199 int (*fp)(int, char, double) = [](auto ... a) -> int { vfun(a...); return 4; };
202 int (*fp2)(int, char, double) = [](auto ... a) { vfun(a...); return 4; };
209 namespace conversion_operator {
211 auto L = [](auto a) -> int { return a; };
213 int (&fp2)(int) = [](auto a) { return a; }; // expected-error{{non-const lvalue}}
214 int (&&fp3)(int) = [](auto a) { return a; }; // expected-error{{no viable conversion}}\
215 //expected-note{{candidate}}
220 namespace return_type_deduction_ok {
221 auto l = [](auto a) ->auto { return a; }(2);
222 auto l2 = [](auto a) ->decltype(auto) { return a; }(2);
223 auto l3 = [](auto a) { return a; }(2);
227 namespace generic_lambda_as_default_argument_ok {
228 void test(int i = [](auto a)->int { return a; }(3)) {
232 namespace nested_non_capturing_lambda_tests {
233 template<class ... Ts> void print(Ts ...) { }
236 auto L = [](auto a) {
245 int i = 10; //expected-note 3{{declared here}}
246 auto L = [](auto a) {
247 return [](auto b) { //expected-note 3{{begins here}}
248 i = b; //expected-error 3{{cannot be implicitly captured}}
252 auto M = L(3); //expected-note{{instantiation}}
253 M(4.15); //expected-note{{instantiation}}
257 auto L = [](auto a) {
265 auto L = [](auto a) {
266 print("a = ", a, "\n");
267 return [](auto b) ->decltype(a) {
268 print("b = ", b, "\n");
277 auto L = [](auto a) ->decltype(a) {
278 print("a = ", a, "\n");
279 return [](auto b) ->decltype(a) { //expected-error{{no viable conversion}}\
280 //expected-note{{candidate template ignored}}
281 print("b = ", b, "\n");
285 auto M = L(3); //expected-note{{in instantiation of}}
288 auto L = [](auto a) {
289 print("a = ", a, "\n");
290 return [](auto ... b) ->decltype(a) {
291 print("b = ", b ..., "\n");
300 auto L = [](auto a) {
301 print("a = ", a, "\n");
302 return [](auto ... b) ->decltype(a) {
303 print("b = ", b ..., "\n");
308 int (*fp)(double, int, const char*) = M;
313 auto L = [](auto a) {
314 print("a = ", a, "\n");
316 return [](auto ... c) ->decltype(b) {
317 print("c = ", c ..., "\n");
326 N("\n3 = ", 3, "\n6.14 = ", 6.14, "\n4'123'456 = ", 4'123'456);
327 char (*np)(const char*, int, const char*, double, const char*, int) = N;
328 np("\n3 = ", 3, "\n6.14 = ", 6.14, "\n4'123'456 = ", 4'123'456);
333 auto L = [](auto a) {
334 print("a = ", a, "\n");
335 return [](decltype(a) b) {
336 return [](auto ... c) ->decltype(b) {
337 print("c = ", c ..., "\n");
346 N("\n3 = ", 3, "\n6.14 = ", 6.14, "\n4'123'456 = ", 4'123'456);
347 char (*np)(const char*, int, const char*, double, const char*, int) = N;
348 np("\n3 = ", 3, "\n6.14 = ", 6.14, "\n4'123'456 = ", 4'123'456);
354 static void foo(double d) { }
356 auto L = [](auto a) {
357 print("a = ", a, "\n");
359 return [](decltype(a) b) {
361 foo(sizeof(a) + sizeof(b));
362 return [](auto ... c) ->decltype(b) {
363 print("c = ", c ..., "\n");
365 foo(sizeof(decltype(a)*) + sizeof(decltype(b)*));
374 N("\n3 = ", 3, "\n6.14 = ", 6.14, "\n4'123'456 = ", 4'123'456);
375 char (*np)(const char*, int, const char*, double, const char*, int) = N;
376 np("\n3 = ", 3, "\n6.14 = ", 6.14, "\n4'123'456 = ", 4'123'456);
382 // Make sure we can escape the function
385 static void foo(double d) { }
387 auto L = [](auto a) {
388 print("a = ", a, "\n");
390 return [](decltype(a) b) {
392 foo(sizeof(a) + sizeof(b));
393 return [](auto ... c) ->decltype(b) {
394 print("c = ", c ..., "\n");
396 foo(sizeof(decltype(a)*) + sizeof(decltype(b)*));
410 N("\n3 = ", 3, "\n6.14 = ", 6.14, "\n4'123'456 = ", 4'123'456);
411 char (*np)(const char*, int, const char*, double, const char*, int) = N;
412 np("\n3 = ", 3, "\n6.14 = ", 6.14, "\n4'123'456 = ", 4'123'456);
417 static void foo(double d) { }
419 auto L = [](auto a) {
420 print("a = ", a, "\n");
422 return [](decltype(a) b) {
424 foo(sizeof(a) + sizeof(b));
425 return [](auto ... c) {
426 print("c = ", c ..., "\n");
428 foo(sizeof(decltype(a)*) + sizeof(decltype(b)*));
429 return [](decltype(c) ... d) ->decltype(a) { //expected-note{{candidate}}
430 print("d = ", d ..., "\n");
432 foo(sizeof(decltype(a)*) + sizeof(decltype(b)*));
433 return decltype(a){};
447 auto O = N("\n3 = ", 3, "\n6.14 = ", 6.14, "\n4'123'456 = ", 4'123'456);
448 char (*np)(const char*, int, const char*, double, const char*, int) = O;
449 np("\n3 = ", 3, "\n6.14 = ", 6.14, "\n4'123'456 = ", 4'123'456);
450 int (*np2)(const char*, int, const char*, double, const char*, int) = O; // expected-error{{no viable conversion}}
455 namespace wrapped_within_templates {
457 namespace explicit_return {
458 template<class T> int fooT(T t) {
459 auto L = [](auto a) -> void {
460 auto M = [](char b) -> void {
461 auto N = [](auto c) -> void {
476 int run = fooT('a') + fooT(3.14);
478 } // end explicit_return
480 namespace implicit_return_deduction {
481 template<class T> auto fooT(T t) {
482 auto L = [](auto a) {
483 auto M = [](char b) {
484 auto N = [](auto c) {
499 int run = fooT('a') + fooT(3.14);
501 template<class ... Ts> void print(Ts ... ts) { }
503 template<class ... Ts> auto fooV(Ts ... ts) {
504 auto L = [](auto ... a) {
505 auto M = [](decltype(a) ... b) {
506 auto N = [](auto c) {
517 print("a = ", a..., "\n");
520 print("ts = ", ts..., "\n");
524 int run2 = fooV(3.14, " ", '4', 5) + fooV("BC", 3, 2.77, 'A', float{}, short{}, unsigned{});
526 } //implicit_return_deduction
529 } //wrapped_within_templates
531 namespace at_ns_scope {
532 void foo(double d) { }
534 auto L = [](auto a) {
535 print("a = ", a, "\n");
537 return [](decltype(a) b) {
539 foo(sizeof(a) + sizeof(b));
540 return [](auto ... c) {
541 print("c = ", c ..., "\n");
543 foo(sizeof(decltype(a)*) + sizeof(decltype(b)*));
544 return [](decltype(c) ... d) ->decltype(a) { //expected-note{{candidate}}
545 print("d = ", d ..., "\n");
547 foo(sizeof(decltype(a)*) + sizeof(decltype(b)*));
548 return decltype(a){};
556 auto L_test = L('4');
558 auto M_test = M('a');
560 auto O = N("\n3 = ", 3, "\n6.14 = ", 6.14, "\n4'123'456 = ", 4'123'456);
561 char (*np)(const char*, int, const char*, double, const char*, int) = O;
562 auto NP_result = np("\n3 = ", 3, "\n6.14 = ", 6.14, "\n4'123'456 = ", 4'123'456);
563 int (*np2)(const char*, int, const char*, double, const char*, int) = O; // expected-error{{no viable conversion}}
569 namespace variadic_tests_1 {
570 template<class ... Ts> void print(Ts ... ts) { }
572 template<class F, class ... Rest> F& FirstArg(F& f, Rest...) { return f; }
574 template<class ... Ts> int fooV(Ts ... ts) {
575 auto L = [](auto ... a) -> void {
576 auto M = [](decltype(a) ... b) -> void {
577 auto N = [](auto c) -> void {
588 print("a = ", a..., "\n");
591 print("ts = ", ts..., "\n");
595 int run2 = fooV(3.14, " ", '4', 5) + fooV("BC", 3, 2.77, 'A', float{}, short{}, unsigned{});
597 namespace more_variadic_1 {
599 template<class ... Ts> int fooV(Ts ... ts) {
600 auto L = [](auto ... a) {
601 auto M = [](decltype(a) ... b) -> void {
602 auto N = [](auto c) -> void {
615 auto M = L(L, ts...);
616 decltype(L(L, ts...)) (*fp)(decltype(L), decltype(ts) ...) = L;
617 void (*fp2)(decltype(L), decltype(ts) ...) = L(L, ts...);
620 auto L = [](auto ... a) {
621 auto M = [](decltype(a) ... b) {
622 auto N = [](auto c) -> void {
636 auto M = L(L, ts...);
637 decltype(L(L, ts...)) (*fp)(decltype(L), decltype(ts) ...) = L;
639 decltype(L(L, ts...)(L, ts...)) (*fp2)(decltype(L), decltype(ts) ...) = L(L, ts...);
641 void (*fp3)(char) = fp2(L, ts...);
647 int run2 = fooV(3.14, " ", '4', 5) + fooV("BC", 3, 2.77, 'A', float{}, short{}, unsigned{});
650 } //end ns more_variadic_1
652 } // end ns variadic_tests_1
654 namespace at_ns_scope_within_class_member {
656 static void foo(double d) { }
658 auto L = [](auto a) {
659 print("a = ", a, "\n");
661 return [](decltype(a) b) {
663 foo(sizeof(a) + sizeof(b));
664 return [](auto ... c) {
665 print("c = ", c ..., "\n");
667 foo(sizeof(decltype(a)*) + sizeof(decltype(b)*));
668 return [](decltype(c) ... d) ->decltype(a) { //expected-note{{candidate}}
669 print("d = ", d ..., "\n");
671 foo(sizeof(decltype(a)*) + sizeof(decltype(b)*));
672 return decltype(a){};
682 auto L_test = L('4');
684 auto M_test = M('a');
686 auto O = N("\n3 = ", 3, "\n6.14 = ", 6.14, "\n4'123'456 = ", 4'123'456);
687 char (*np)(const char*, int, const char*, double, const char*, int) = O;
688 auto NP_result = np("\n3 = ", 3, "\n6.14 = ", 6.14, "\n4'123'456 = ", 4'123'456);
689 int (*np2)(const char*, int, const char*, double, const char*, int) = O; // expected-error{{no viable conversion}}
691 } //end at_ns_scope_within_class_member
694 namespace at_ns_scope_within_class_template_member {
696 static void foo(double d) { }
697 template<class T = int>
699 auto L = [](auto a) {
700 print("a = ", a, "\n");
702 return [](decltype(a) b) {
704 foo(sizeof(a) + sizeof(b));
705 return [](auto ... c) {
706 print("c = ", c ..., "\n");
708 foo(sizeof(decltype(a)*) + sizeof(decltype(b)*));
709 return [](decltype(c) ... d) ->decltype(a) { //expected-note{{candidate}}
710 print("d = ", d ..., "\n");
712 foo(sizeof(decltype(a)*) + sizeof(decltype(b)*));
713 return decltype(a){};
724 auto L_test = L('4');
726 auto M_test = M('a');
728 auto O = N("\n3 = ", 3, "\n6.14 = ", 6.14, "\n4'123'456 = ", 4'123'456);
729 char (*np)(const char*, int, const char*, double, const char*, int) = O;
730 auto NP_result = np("\n3 = ", 3, "\n6.14 = ", 6.14, "\n4'123'456 = ", 4'123'456);
731 int (*np2)(const char*, int, const char*, double, const char*, int) = O; // expected-error{{no viable conversion}}
733 } //end at_ns_scope_within_class_member
736 namespace nested_generic_lambdas_123 {
738 auto L = [](auto a) -> int {
739 auto M = [](auto b, decltype(a) b2) -> int {
746 template<class T> void foo(T) {
747 auto L = [](auto a) { return a; };
749 template void foo(int);
750 } // end ns nested_generic_lambdas_123
752 namespace nested_fptr_235 {
755 auto L = [](auto b) {
756 return [](auto a) ->decltype(a) { return a; };
758 int (*fp)(int) = L(8);
761 char (*fc)(char) = L('a');
764 double (*fd)(double) = L(3.14);
773 namespace fptr_with_decltype_return_type {
774 template<class F, class ... Rest> F& FirstArg(F& f, Rest& ... r) { return f; };
775 template<class ... Ts> auto vfun(Ts&& ... ts) {
777 return FirstArg(ts...);
782 auto L = [](auto ... As) {
783 return [](auto b) ->decltype(b) {
784 vfun([](decltype(As) a) -> decltype(a) { return a; } ...)(first<decltype(As)...>{});
785 return decltype(b){};
788 auto LL = L(1, 'a', 3.14, "abc");
796 } // end ns nested_non_capturing_lambda_tests
800 string(const char *__s) { }
801 string &operator+=(const string &__str) { return *this; }
805 void finalizeDefaultAtomValues() {
806 auto startEnd = [](const char * sym) -> void {
810 startEnd("preinit_array");
813 void f() { finalizeDefaultAtomValues<char>(); }
817 namespace PR17476_variant {
819 string(const char *__s) { }
820 string &operator+=(const string &__str) { return *this; }
824 void finalizeDefaultAtomValues() {
825 auto startEnd = [](const T *sym) -> void {
829 startEnd("preinit_array");
832 void f() { finalizeDefaultAtomValues<char>(); }
836 namespace PR17877_lambda_declcontext_and_get_cur_lambda_disconnect {
839 template<class T> struct U {
845 U<T> size() const { return U<T>{}; }
856 namespace inclass_lambdas_within_nested_classes {
861 enum { E = [](auto i) { return i; }(3) }; //expected-error{{inside of a constant expression}}\
862 //expected-error{{not an integral constant}}\
863 //expected-note{{non-literal type}}
864 int L = ([] (int i) { return i; })(2);
865 void foo(int i = ([] (int i) { return i; })(2)) { }
866 int B : ([](int i) { return i; })(3); //expected-error{{inside of a constant expression}}\
867 //expected-error{{not an integral constant}}\
868 //expected-note{{non-literal type}}
869 int arr[([](int i) { return i; })(3)]; //expected-error{{inside of a constant expression}}\
870 //expected-error{{must have a constant size}}
871 int (*fp)(int) = [](int i) { return i; };
872 void fooptr(int (*fp)(char) = [](char c) { return 0; }) { }
873 int L2 = ([](auto i) { return i; })(2);
874 void fooG(int i = ([] (auto i) { return i; })(2)) { }
875 int BG : ([](auto i) { return i; })(3); //expected-error{{inside of a constant expression}} \
876 //expected-error{{not an integral constant}}\
877 //expected-note{{non-literal type}}
878 int arrG[([](auto i) { return i; })(3)]; //expected-error{{inside of a constant expression}}\
879 //expected-error{{must have a constant size}}
880 int (*fpG)(int) = [](auto i) { return i; };
881 void fooptrG(int (*fp)(char) = [](auto c) { return 0; }) { }
890 int L = ([] (T i) { return i; })(2);
891 void foo(int i = ([] (int i) { return i; })(2)) { }
892 int B : ([](T i) { return i; })(3); //expected-error{{inside of a constant expression}}\
893 //expected-error{{not an integral constant}}\
894 //expected-note{{non-literal type}}
895 int arr[([](T i) { return i; })(3)]; //expected-error{{inside of a constant expression}}\
896 //expected-error{{must have a constant size}}
897 int (*fp)(T) = [](T i) { return i; };
898 void fooptr(T (*fp)(char) = [](char c) { return 0; }) { }
899 int L2 = ([](auto i) { return i; })(2);
900 void fooG(T i = ([] (auto i) { return i; })(2)) { }
901 int BG : ([](auto i) { return i; })(3); //expected-error{{not an integral constant}}\
902 //expected-note{{non-literal type}}\
903 //expected-error{{inside of a constant expression}}
904 int arrG[([](auto i) { return i; })(3)]; //expected-error{{must have a constant size}} \
905 //expected-error{{inside of a constant expression}}
906 int (*fpG)(T) = [](auto i) { return i; };
907 void fooptrG(T (*fp)(char) = [](auto c) { return 0; }) { }
908 template<class U = char> int fooG2(T (*fp)(U) = [](auto a) { return 0; }) { return 0; }
909 template<class U = char> int fooG3(T (*fp)(U) = [](auto a) { return 0; });
914 int X1::X2<T>::fooG3(T (*fp)(U)) { return 0; }
915 X1::X2<int> x2; //expected-note 3{{in instantiation of}}
916 int run1 = x2.fooG2();
917 int run2 = x2.fooG3();
922 } //end ns inclass_lambdas_within_nested_classes
924 namespace pr21684_disambiguate_auto_followed_by_ellipsis_no_id {
925 int a = [](auto ...) { return 0; }();
930 return [](auto... run_args) {
931 using T = int(decltype(run_args)...);
940 auto g = [](auto&&... args) {
941 auto h = [args...]() -> int {
954 void Method(char c = []()->char {
955 int d = [](auto x)->int {
957 int Method() { return 0; }
966 void Method(char c = [](auto x)->char {
969 int Method() { return 0; }
978 void Method(char c = [](auto x)->char {
979 int d = [](auto y)->int {
981 int Method() { return 0; }