1 // RUN: %clang_cc1 -fsyntax-only -Wno-unused-value -verify -std=c++11 -Wno-c99-designator %s
2 // RUN: %clang_cc1 -fsyntax-only -Wno-unused-value -verify -std=c++2a -Wno-c99-designator %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     [] __attribute__((noreturn)) {}; // expected-error {{lambda requires '()' before attribute specifier}}
105     []() [[]]
106       mutable {}; // expected-error {{expected body of lambda expression}}
107 
108     []() [[]] {};
109     []() [[]] -> void {};
110     []() mutable [[]] -> void {};
111     []() mutable noexcept [[]] -> void {};
112 
113     // Testing GNU-style attributes on lambdas -- the attribute is specified
114     // before the mutable specifier instead of after (unlike C++11).
115     []() __attribute__((noreturn)) mutable { while(1); };
116     []() mutable
117       __attribute__((noreturn)) { while(1); }; // expected-error {{expected body of lambda expression}}
118 
119     // Testing support for P2173 on adding attributes to the declaration
120     // rather than the type.
121     [] [[]] () {}; // expected-warning {{an attribute specifier sequence in this position is a C++2b extension}}
122 #if __cplusplus > 201703L
123     [] <typename> [[]] () {};  // expected-warning {{an attribute specifier sequence in this position is a C++2b extension}}
124 #endif
125     [] [[]] {}; // expected-warning {{an attribute specifier sequence in this position is a C++2b extension}}
126   }
127 };
128 
129 template <typename>
130 void PR22122() {
131   [](int) -> {}; // expected-error {{expected a type}}
132 }
133 
134 template void PR22122<int>();
135 
136 namespace PR42778 {
137 struct A {
138   template <class F> A(F&&) {}
139 };
140 
141 struct S {
142   void mf() { A{[*this]{}}; }
143 #if __cplusplus < 201703L
144   // expected-warning@-2 {{C++17 extension}}
145 #endif
146 };
147 }
148 
149 struct S {
150   template <typename T>
151   void m (T x =[0); // expected-error{{expected variable name or 'this' in lambda capture list}}
152 } s;
153 
154 struct U {
155   template <typename T>
156   void m_fn1(T x = 0[0); // expected-error{{expected ']'}} expected-note{{to match this '['}}
157 } *U;
158