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 namespace explicit_call {
8 auto L = [](auto a) { return a; };
10 L.operator()<char>(3.14); //expected-warning{{implicit conversion}}
15 namespace test_conversion_to_fptr_2 {
17 template<class T> struct X {
19 T (*fp)(T) = [](auto a) { return a; };
26 void fooT(T t, T (*fp)(T) = [](auto a) { return a; }) {
32 auto L = [](auto a) { return a; };
39 double (*fd)(double) = L;
45 auto L = [](auto a) ->int { return a; }; //expected-note 2{{candidate template ignored}}
47 char (*fc)(char) = L; //expected-error{{no viable conversion}}
48 double (*fd)(double) = L; //expected-error{{no viable conversion}}
52 auto L = [=](auto b, char c = 'x') {
54 return [](auto a) ->decltype(a) { return a; };
56 int (*fp)(int) = L(8);
59 char (*fc)(char) = L('a');
62 double (*fd)(double) = L(3.14);
68 auto L = [=](auto b) {
69 return [](auto a) ->decltype(b)* { return (decltype(b)*)0; };
71 int* (*fp)(int) = L(8);
74 char* (*fc)(char) = L('a');
77 double* (*fd)(double) = L(3.14);
82 auto L = [=](auto b) {
83 return [](auto a) ->decltype(b)* { return (decltype(b)*)0; }; //expected-note{{candidate template ignored}}
85 char* (*fp)(int) = L('8');
87 char* (*fc)(char) = L('a');
89 double* (*fi)(int) = L(3.14);
91 int* (*fi2)(int) = L(3.14); //expected-error{{no viable conversion}}
95 auto L = [=](auto b) {
98 return [](auto d) ->decltype(a + b + c + d) { return d; };
102 int (*fp)(int) = L('8')(3)(short{});
103 double (*fs)(char) = L(3.14)(short{})('4');
117 namespace test_conversion_to_fptr {
119 void f1(int (*)(int)) { }
120 void f2(char (*)(int)) { } // expected-note{{candidate}}
121 void g(int (*)(int)) { } // #1 expected-note{{candidate}}
122 void g(char (*)(char)) { } // #2 expected-note{{candidate}}
123 void h(int (*)(int)) { } // #3
124 void h(char (*)(int)) { } // #4
128 auto glambda = [](auto a) { return a; };
131 f2(glambda); // expected-error{{no matching function}}
132 g(glambda); // expected-error{{call to 'g' is ambiguous}}
133 h(glambda); // OK: calls #3 since it is convertible from ID
135 int& (*fpi)(int*) = [](auto* a) -> auto& { return *a; }; // OK
140 auto L = [](auto a) { return a; };
144 char (*fc)(char) = L;
147 double (*fd)(double) = L;
153 auto L = [](auto a) ->int { return a; }; //expected-note 2{{candidate template ignored}}
155 char (*fc)(char) = L; //expected-error{{no viable conversion}}
156 double (*fd)(double) = L; //expected-error{{no viable conversion}}
159 int* (*fp)(int*) = [](auto *a) -> auto* { return a; };
164 namespace more_converion_to_ptr_to_function_tests {
169 int& (*fpi)(int*) = [](auto* a) -> auto& { return *a; }; // OK
170 int (*fp2)(int) = [](auto b) -> int { return b; };
171 int (*fp3)(char) = [](auto c) -> int { return c; };
172 char (*fp4)(int) = [](auto d) { return d; }; //expected-error{{no viable conversion}}\
173 //expected-note{{candidate template ignored}}
174 char (*fp5)(char) = [](auto e) -> int { return e; }; //expected-error{{no viable conversion}}\
175 //expected-note{{candidate template ignored}}
184 template<class ... Ts> void vfun(Ts ... ) { }
186 int variadic_test() {
188 int (*fp)(int, char, double) = [](auto ... a) -> int { vfun(a...); return 4; };
191 int (*fp2)(int, char, double) = [](auto ... a) { vfun(a...); return 4; };
198 namespace conversion_operator {
200 auto L = [](auto a) -> int { return a; };
202 int (&fp2)(int) = [](auto a) { return a; }; // expected-error{{non-const lvalue}}
203 int (&&fp3)(int) = [](auto a) { return a; }; // expected-error{{no viable conversion}}\
204 //expected-note{{candidate}}
209 namespace return_type_deduction_ok {
210 auto l = [](auto a) ->auto { return a; }(2);
211 auto l2 = [](auto a) ->decltype(auto) { return a; }(2);
212 auto l3 = [](auto a) { return a; }(2);
216 namespace generic_lambda_as_default_argument_ok {
217 void test(int i = [](auto a)->int { return a; }(3)) {
221 namespace nested_non_capturing_lambda_tests {
222 template<class ... Ts> void print(Ts ...) { }
225 auto L = [](auto a) {
234 int i = 10; //expected-note 3{{declared here}}
235 auto L = [](auto a) {
236 return [](auto b) { //expected-note 3{{begins here}}
237 i = b; //expected-error 3{{cannot be implicitly captured}}
241 auto M = L(3); //expected-note{{instantiation}}
242 M(4.15); //expected-note{{instantiation}}
246 auto L = [](auto a) {
254 auto L = [](auto a) {
255 print("a = ", a, "\n");
256 return [](auto b) ->decltype(a) {
257 print("b = ", b, "\n");
266 auto L = [](auto a) ->decltype(a) {
267 print("a = ", a, "\n");
268 return [](auto b) ->decltype(a) { //expected-error{{no viable conversion}}\
269 //expected-note{{candidate template ignored}}
270 print("b = ", b, "\n");
274 auto M = L(3); //expected-note{{in instantiation of}}
277 auto L = [](auto a) {
278 print("a = ", a, "\n");
279 return [](auto ... b) ->decltype(a) {
280 print("b = ", b ..., "\n");
289 auto L = [](auto a) {
290 print("a = ", a, "\n");
291 return [](auto ... b) ->decltype(a) {
292 print("b = ", b ..., "\n");
297 int (*fp)(double, int, const char*) = M;
302 auto L = [](auto a) {
303 print("a = ", a, "\n");
305 return [](auto ... c) ->decltype(b) {
306 print("c = ", c ..., "\n");
315 N("\n3 = ", 3, "\n6.14 = ", 6.14, "\n4'123'456 = ", 4'123'456);
316 char (*np)(const char*, int, const char*, double, const char*, int) = N;
317 np("\n3 = ", 3, "\n6.14 = ", 6.14, "\n4'123'456 = ", 4'123'456);
322 auto L = [](auto a) {
323 print("a = ", a, "\n");
324 return [](decltype(a) b) {
325 return [](auto ... c) ->decltype(b) {
326 print("c = ", c ..., "\n");
335 N("\n3 = ", 3, "\n6.14 = ", 6.14, "\n4'123'456 = ", 4'123'456);
336 char (*np)(const char*, int, const char*, double, const char*, int) = N;
337 np("\n3 = ", 3, "\n6.14 = ", 6.14, "\n4'123'456 = ", 4'123'456);
343 static void foo(double d) { }
345 auto L = [](auto a) {
346 print("a = ", a, "\n");
348 return [](decltype(a) b) {
350 foo(sizeof(a) + sizeof(b));
351 return [](auto ... c) ->decltype(b) {
352 print("c = ", c ..., "\n");
354 foo(sizeof(decltype(a)*) + sizeof(decltype(b)*));
363 N("\n3 = ", 3, "\n6.14 = ", 6.14, "\n4'123'456 = ", 4'123'456);
364 char (*np)(const char*, int, const char*, double, const char*, int) = N;
365 np("\n3 = ", 3, "\n6.14 = ", 6.14, "\n4'123'456 = ", 4'123'456);
371 // Make sure we can escape the function
374 static void foo(double d) { }
376 auto L = [](auto a) {
377 print("a = ", a, "\n");
379 return [](decltype(a) b) {
381 foo(sizeof(a) + sizeof(b));
382 return [](auto ... c) ->decltype(b) {
383 print("c = ", c ..., "\n");
385 foo(sizeof(decltype(a)*) + sizeof(decltype(b)*));
399 N("\n3 = ", 3, "\n6.14 = ", 6.14, "\n4'123'456 = ", 4'123'456);
400 char (*np)(const char*, int, const char*, double, const char*, int) = N;
401 np("\n3 = ", 3, "\n6.14 = ", 6.14, "\n4'123'456 = ", 4'123'456);
406 static void foo(double d) { }
408 auto L = [](auto a) {
409 print("a = ", a, "\n");
411 return [](decltype(a) b) {
413 foo(sizeof(a) + sizeof(b));
414 return [](auto ... c) {
415 print("c = ", c ..., "\n");
417 foo(sizeof(decltype(a)*) + sizeof(decltype(b)*));
418 return [](decltype(c) ... d) ->decltype(a) { //expected-note{{candidate}}
419 print("d = ", d ..., "\n");
421 foo(sizeof(decltype(a)*) + sizeof(decltype(b)*));
422 return decltype(a){};
436 auto O = N("\n3 = ", 3, "\n6.14 = ", 6.14, "\n4'123'456 = ", 4'123'456);
437 char (*np)(const char*, int, const char*, double, const char*, int) = O;
438 np("\n3 = ", 3, "\n6.14 = ", 6.14, "\n4'123'456 = ", 4'123'456);
439 int (*np2)(const char*, int, const char*, double, const char*, int) = O; // expected-error{{no viable conversion}}
444 namespace wrapped_within_templates {
446 namespace explicit_return {
447 template<class T> int fooT(T t) {
448 auto L = [](auto a) -> void {
449 auto M = [](char b) -> void {
450 auto N = [](auto c) -> void {
465 int run = fooT('a') + fooT(3.14);
467 } // end explicit_return
469 namespace implicit_return_deduction {
470 template<class T> auto fooT(T t) {
471 auto L = [](auto a) {
472 auto M = [](char b) {
473 auto N = [](auto c) {
488 int run = fooT('a') + fooT(3.14);
490 template<class ... Ts> void print(Ts ... ts) { }
492 template<class F, class ... Rest> using first = F;
494 template<class ... Ts> auto fooV(Ts ... ts) {
495 auto L = [](auto ... a) {
496 auto M = [](decltype(a) ... b) {
497 auto N = [](auto c) {
508 print("a = ", a..., "\n");
511 print("ts = ", ts..., "\n");
515 int run2 = fooV(3.14, " ", '4', 5) + fooV("BC", 3, 2.77, 'A', float{}, short{}, unsigned{});
517 } //implicit_return_deduction
520 } //wrapped_within_templates
522 namespace at_ns_scope {
523 void foo(double d) { }
525 auto L = [](auto a) {
526 print("a = ", a, "\n");
528 return [](decltype(a) b) {
530 foo(sizeof(a) + sizeof(b));
531 return [](auto ... c) {
532 print("c = ", c ..., "\n");
534 foo(sizeof(decltype(a)*) + sizeof(decltype(b)*));
535 return [](decltype(c) ... d) ->decltype(a) { //expected-note{{candidate}}
536 print("d = ", d ..., "\n");
538 foo(sizeof(decltype(a)*) + sizeof(decltype(b)*));
539 return decltype(a){};
547 auto L_test = L('4');
549 auto M_test = M('a');
551 auto O = N("\n3 = ", 3, "\n6.14 = ", 6.14, "\n4'123'456 = ", 4'123'456);
552 char (*np)(const char*, int, const char*, double, const char*, int) = O;
553 auto NP_result = np("\n3 = ", 3, "\n6.14 = ", 6.14, "\n4'123'456 = ", 4'123'456);
554 int (*np2)(const char*, int, const char*, double, const char*, int) = O; // expected-error{{no viable conversion}}
560 namespace variadic_tests_1 {
561 template<class ... Ts> void print(Ts ... ts) { }
563 template<class F, class ... Rest> using FirstType = F;
564 template<class F, class ... Rest> F& FirstArg(F& f, Rest...) { return f; }
566 template<class ... Ts> int fooV(Ts ... ts) {
567 auto L = [](auto ... a) -> void {
568 auto M = [](decltype(a) ... b) -> void {
569 auto N = [](auto c) -> void {
577 N(FirstType<Ts...>{});
580 print("a = ", a..., "\n");
583 print("ts = ", ts..., "\n");
587 int run2 = fooV(3.14, " ", '4', 5) + fooV("BC", 3, 2.77, 'A', float{}, short{}, unsigned{});
589 namespace more_variadic_1 {
591 template<class ... Ts> int fooV(Ts ... ts) {
592 auto L = [](auto ... a) {
593 auto M = [](decltype(a) ... b) -> void {
594 auto N = [](auto c) -> void {
602 N(FirstType<Ts...>{});
607 auto M = L(L, ts...);
608 decltype(L(L, ts...)) (*fp)(decltype(L), decltype(ts) ...) = L;
609 void (*fp2)(decltype(L), decltype(ts) ...) = L(L, ts...);
612 auto L = [](auto ... a) {
613 auto M = [](decltype(a) ... b) {
614 auto N = [](auto c) -> void {
622 N(FirstType<Ts...>{});
628 auto M = L(L, ts...);
629 decltype(L(L, ts...)) (*fp)(decltype(L), decltype(ts) ...) = L;
631 decltype(L(L, ts...)(L, ts...)) (*fp2)(decltype(L), decltype(ts) ...) = L(L, ts...);
633 void (*fp3)(char) = fp2(L, ts...);
639 int run2 = fooV(3.14, " ", '4', 5) + fooV("BC", 3, 2.77, 'A', float{}, short{}, unsigned{});
642 } //end ns more_variadic_1
644 } // end ns variadic_tests_1
646 namespace at_ns_scope_within_class_member {
648 static void foo(double d) { }
650 auto L = [](auto a) {
651 print("a = ", a, "\n");
653 return [](decltype(a) b) {
655 foo(sizeof(a) + sizeof(b));
656 return [](auto ... c) {
657 print("c = ", c ..., "\n");
659 foo(sizeof(decltype(a)*) + sizeof(decltype(b)*));
660 return [](decltype(c) ... d) ->decltype(a) { //expected-note{{candidate}}
661 print("d = ", d ..., "\n");
663 foo(sizeof(decltype(a)*) + sizeof(decltype(b)*));
664 return decltype(a){};
674 auto L_test = L('4');
676 auto M_test = M('a');
678 auto O = N("\n3 = ", 3, "\n6.14 = ", 6.14, "\n4'123'456 = ", 4'123'456);
679 char (*np)(const char*, int, const char*, double, const char*, int) = O;
680 auto NP_result = np("\n3 = ", 3, "\n6.14 = ", 6.14, "\n4'123'456 = ", 4'123'456);
681 int (*np2)(const char*, int, const char*, double, const char*, int) = O; // expected-error{{no viable conversion}}
683 } //end at_ns_scope_within_class_member
686 namespace at_ns_scope_within_class_template_member {
688 static void foo(double d) { }
689 template<class T = int>
691 auto L = [](auto a) {
692 print("a = ", a, "\n");
694 return [](decltype(a) b) {
696 foo(sizeof(a) + sizeof(b));
697 return [](auto ... c) {
698 print("c = ", c ..., "\n");
700 foo(sizeof(decltype(a)*) + sizeof(decltype(b)*));
701 return [](decltype(c) ... d) ->decltype(a) { //expected-note{{candidate}}
702 print("d = ", d ..., "\n");
704 foo(sizeof(decltype(a)*) + sizeof(decltype(b)*));
705 return decltype(a){};
716 auto L_test = L('4');
718 auto M_test = M('a');
720 auto O = N("\n3 = ", 3, "\n6.14 = ", 6.14, "\n4'123'456 = ", 4'123'456);
721 char (*np)(const char*, int, const char*, double, const char*, int) = O;
722 auto NP_result = np("\n3 = ", 3, "\n6.14 = ", 6.14, "\n4'123'456 = ", 4'123'456);
723 int (*np2)(const char*, int, const char*, double, const char*, int) = O; // expected-error{{no viable conversion}}
725 } //end at_ns_scope_within_class_member
728 namespace nested_generic_lambdas_123 {
730 auto L = [](auto a) -> int {
731 auto M = [](auto b, decltype(a) b2) -> int {
738 template<class T> void foo(T) {
739 auto L = [](auto a) { return a; };
741 template void foo(int);
742 } // end ns nested_generic_lambdas_123
744 namespace nested_fptr_235 {
747 auto L = [](auto b) {
748 return [](auto a) ->decltype(a) { return a; };
750 int (*fp)(int) = L(8);
753 char (*fc)(char) = L('a');
756 double (*fd)(double) = L(3.14);
765 namespace fptr_with_decltype_return_type {
766 template<class F, class ... Ts> using FirstType = F;
767 template<class F, class ... Rest> F& FirstArg(F& f, Rest& ... r) { return f; };
768 template<class ... Ts> auto vfun(Ts&& ... ts) {
770 return FirstArg(ts...);
775 auto L = [](auto ... As) {
776 return [](auto b) ->decltype(b) {
777 vfun([](decltype(As) a) -> decltype(a) { return a; } ...)(FirstType<decltype(As)...>{});
778 return decltype(b){};
781 auto LL = L(1, 'a', 3.14, "abc");
789 } // end ns nested_non_capturing_lambda_tests
793 string(const char *__s) { }
794 string &operator+=(const string &__str) { return *this; }
798 void finalizeDefaultAtomValues() {
799 auto startEnd = [](const char * sym) -> void {
803 startEnd("preinit_array");
806 void f() { finalizeDefaultAtomValues<char>(); }
810 namespace PR17476_variant {
812 string(const char *__s) { }
813 string &operator+=(const string &__str) { return *this; }
817 void finalizeDefaultAtomValues() {
818 auto startEnd = [](const T *sym) -> void {
822 startEnd("preinit_array");
825 void f() { finalizeDefaultAtomValues<char>(); }
829 namespace PR17877_lambda_declcontext_and_get_cur_lambda_disconnect {
832 template<class T> struct U {
838 U<T> size() const { return U<T>{}; }
849 namespace inclass_lambdas_within_nested_classes {
854 enum { E = [](auto i) { return i; }(3) }; //expected-error{{inside of a constant expression}}\
855 //expected-error{{not an integral constant}}
856 int L = ([] (int i) { return i; })(2);
857 void foo(int i = ([] (int i) { return i; })(2)) { }
858 int B : ([](int i) { return i; })(3); //expected-error{{inside of a constant expression}}\
859 //expected-error{{not an integral constant}}
860 int arr[([](int i) { return i; })(3)]; //expected-error{{inside of a constant expression}}\
861 //expected-error{{must have a constant size}}
862 int (*fp)(int) = [](int i) { return i; };
863 void fooptr(int (*fp)(char) = [](char c) { return 0; }) { }
864 int L2 = ([](auto i) { return i; })(2);
865 void fooG(int i = ([] (auto i) { return i; })(2)) { }
866 int BG : ([](auto i) { return i; })(3); //expected-error{{inside of a constant expression}} \
867 //expected-error{{not an integral constant}}
868 int arrG[([](auto i) { return i; })(3)]; //expected-error{{inside of a constant expression}}\
869 //expected-error{{must have a constant size}}
870 int (*fpG)(int) = [](auto i) { return i; };
871 void fooptrG(int (*fp)(char) = [](auto c) { return 0; }) { }
880 int L = ([] (T i) { return i; })(2);
881 void foo(int i = ([] (int i) { return i; })(2)) { }
882 int B : ([](T i) { return i; })(3); //expected-error{{inside of a constant expression}}\
883 //expected-error{{not an integral constant}}
884 int arr[([](T i) { return i; })(3)]; //expected-error{{inside of a constant expression}}\
885 //expected-error{{must have a constant size}}
886 int (*fp)(T) = [](T i) { return i; };
887 void fooptr(T (*fp)(char) = [](char c) { return 0; }) { }
888 int L2 = ([](auto i) { return i; })(2);
889 void fooG(T i = ([] (auto i) { return i; })(2)) { }
890 int BG : ([](auto i) { return i; })(3); //expected-error{{not an integral constant}}
891 int arrG[([](auto i) { return i; })(3)]; //expected-error{{must have a constant size}}
892 int (*fpG)(T) = [](auto i) { return i; };
893 void fooptrG(T (*fp)(char) = [](auto c) { return 0; }) { }
894 template<class U = char> int fooG2(T (*fp)(U) = [](auto a) { return 0; }) { return 0; }
895 template<class U = char> int fooG3(T (*fp)(U) = [](auto a) { return 0; });
900 int X1::X2<T>::fooG3(T (*fp)(U)) { return 0; }
901 X1::X2<int> x2; //expected-note 3{{in instantiation of}}
902 int run1 = x2.fooG2();
903 int run2 = x2.fooG3();
908 } //end ns inclass_lambdas_within_nested_classes