1 // RUN: %clang_cc1 -fsyntax-only -Wno-unused-value -verify -std=c++11 %s 2 // RUN: %clang_cc1 -fsyntax-only -Wno-unused-value -verify -std=c++2a %s 3 4 enum E { e }; 5 6 constexpr int id(int n) { return n; } 7 8 class C { 9 10 int f() { 11 int foo, bar; 12 13 []; // expected-error {{expected body of lambda expression}} 14 [+] {}; // expected-error {{expected variable name or 'this' in lambda capture list}} 15 [foo+] {}; // expected-error {{expected ',' or ']' in lambda capture list}} 16 [foo,&this] {}; // expected-error {{'this' cannot be captured by reference}} 17 [&this] {}; // expected-error {{'this' cannot be captured by reference}} 18 [&,] {}; // expected-error {{expected variable name or 'this' in lambda capture list}} 19 [=,] {}; // expected-error {{expected variable name or 'this' in lambda capture list}} 20 [] {}; 21 [=] (int i) {}; 22 [&] (int) mutable -> void {}; 23 [foo,bar] () { return 3; }; 24 [=,&foo] () {}; 25 [&,foo] () {}; 26 [this] () {}; 27 [] () -> class C { return C(); }; 28 [] () -> enum E { return e; }; 29 30 [] -> int { return 0; }; // expected-error{{lambda requires '()' before return type}} 31 [] mutable -> int { return 0; }; // expected-error{{lambda requires '()' before 'mutable'}} 32 [](int) -> {}; // PR13652 expected-error {{expected a type}} 33 return 1; 34 } 35 36 void designator_or_lambda() { 37 typedef int T; 38 const int b = 0; 39 const int c = 1; 40 int d; 41 int a1[1] = {[b] (T()) {}}; // expected-error{{no viable conversion from '(lambda}} 42 int a2[1] = {[b] = 1 }; 43 int a3[1] = {[b,c] = 1 }; // expected-error{{expected ']'}} expected-note {{to match}} 44 int a4[1] = {[&b] = 1 }; // expected-error{{integral constant expression must have integral or unscoped enumeration type, not 'const int *'}} 45 int a5[3] = { []{return 0;}() }; 46 int a6[1] = {[this] = 1 }; // expected-error{{integral constant expression must have integral or unscoped enumeration type, not 'C *'}} 47 int a7[1] = {[d(0)] { return d; } ()}; 48 int a8[1] = {[d = 0] { return d; } ()}; 49 int a10[1] = {[id(0)] { return id; } ()}; 50 #if __cplusplus <= 201103L 51 // expected-warning@-4{{extension}} 52 // expected-warning@-4{{extension}} 53 // expected-warning@-4{{extension}} 54 #endif 55 int a9[1] = {[d = 0] = 1}; // expected-error{{is not an integral constant expression}} 56 #if __cplusplus >= 201402L 57 // expected-note@-2{{constant expression cannot modify an object that is visible outside that expression}} 58 #endif 59 int a11[1] = {[id(0)] = 1}; 60 } 61 62 void delete_lambda(int *p) { 63 delete [] p; 64 delete [] (int*) { new int }; // ok, compound-literal, not lambda 65 delete [] { return new int; } (); // expected-error {{'[]' after delete interpreted as 'delete[]'}} 66 delete [&] { return new int; } (); // ok, lambda 67 68 delete []() { return new int; }(); // expected-error{{'[]' after delete interpreted as 'delete[]'}} 69 delete [](E Enum) { return new int((int)Enum); }(e); // expected-error{{'[]' after delete interpreted as 'delete[]'}} 70 #if __cplusplus > 201703L 71 delete []<int = 0>() { return new int; }(); // expected-error{{'[]' after delete interpreted as 'delete[]'}} 72 #endif 73 } 74 75 // We support init-captures in C++11 as an extension. 76 int z; 77 void init_capture() { 78 [n(0)] () mutable -> int { return ++n; }; 79 [n{0}] { return; }; 80 [a([&b = z]{})](){}; 81 [n = 0] { return ++n; }; // expected-error {{captured by copy in a non-mutable}} 82 [n = {0}] { return; }; // expected-error {{<initializer_list>}} 83 #if __cplusplus <= 201103L 84 // expected-warning@-6{{extension}} 85 // expected-warning@-6{{extension}} 86 // expected-warning@-6{{extension}} 87 // expected-warning@-7{{extension}} 88 // expected-warning@-7{{extension}} 89 // expected-warning@-7{{extension}} 90 #endif 91 92 int x = 4; 93 auto y = [&r = x, x = x + 1]() -> int { 94 #if __cplusplus <= 201103L 95 // expected-warning@-2{{extension}} 96 // expected-warning@-3{{extension}} 97 #endif 98 r += 2; 99 return x + 2; 100 } (); 101 } 102 103 void attributes() { 104 [] [[]] {}; // expected-error {{lambda requires '()' before attribute specifier}} 105 [] __attribute__((noreturn)) {}; // expected-error {{lambda requires '()' before attribute specifier}} 106 []() [[]] 107 mutable {}; // expected-error {{expected body of lambda expression}} 108 109 []() [[]] {}; 110 []() [[]] -> void {}; 111 []() mutable [[]] -> void {}; 112 []() mutable noexcept [[]] -> void {}; 113 114 // Testing GNU-style attributes on lambdas -- the attribute is specified 115 // before the mutable specifier instead of after (unlike C++11). 116 []() __attribute__((noreturn)) mutable { while(1); }; 117 []() mutable 118 __attribute__((noreturn)) { while(1); }; // expected-error {{expected body of lambda expression}} 119 } 120 }; 121 122 template <typename> 123 void PR22122() { 124 [](int) -> {}; // expected-error {{expected a type}} 125 } 126 127 template void PR22122<int>(); 128 129 namespace PR42778 { 130 struct A { 131 template <class F> A(F&&) {} 132 }; 133 134 struct S { 135 void mf() { A{[*this]{}}; } 136 #if __cplusplus < 201703L 137 // expected-warning@-2 {{C++17 extension}} 138 #endif 139 }; 140 } 141 142 struct S { 143 template <typename T> 144 void m (T x =[0); // expected-error{{expected variable name or 'this' in lambda capture list}} 145 } s; 146 147 struct U { 148 template <typename T> 149 void m_fn1(T x = 0[0); // expected-error{{expected ']'}} expected-note{{to match this '['}} 150 } *U; 151