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