blob: d509177784277390ccdaab9622322a0334b0b2c2 [file] [log] [blame]
Richard Smith50309282019-08-30 22:52:55 +00001// 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
Douglas Gregordb0b9f12011-08-04 15:30:47 +00003
Richard Smithbfdb1082012-03-12 08:56:40 +00004enum E { e };
5
Richard Smith9e2f0a42014-04-13 04:31:48 +00006constexpr int id(int n) { return n; }
7
Douglas Gregordb0b9f12011-08-04 15:30:47 +00008class 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}}
Richard Trieu553b2b22011-12-15 00:38:15 +000018 [&,] {}; // expected-error {{expected variable name or 'this' in lambda capture list}}
19 [=,] {}; // expected-error {{expected variable name or 'this' in lambda capture list}}
Douglas Gregor656bc622012-02-09 08:26:42 +000020 [] {};
21 [=] (int i) {};
22 [&] (int) mutable -> void {};
23 [foo,bar] () { return 3; };
24 [=,&foo] () {};
25 [&,foo] () {};
26 [this] () {};
Richard Smithbfdb1082012-03-12 08:56:40 +000027 [] () -> class C { return C(); };
28 [] () -> enum E { return e; };
Douglas Gregordb0b9f12011-08-04 15:30:47 +000029
Douglas Gregor6746c5d2012-02-16 21:53:36 +000030 [] -> int { return 0; }; // expected-error{{lambda requires '()' before return type}}
31 [] mutable -> int { return 0; }; // expected-error{{lambda requires '()' before 'mutable'}}
Richard Smithb3afa6c2012-08-30 13:13:20 +000032 [](int) -> {}; // PR13652 expected-error {{expected a type}}
Douglas Gregordb0b9f12011-08-04 15:30:47 +000033 return 1;
34 }
35
Douglas Gregora80cae12012-02-17 03:49:44 +000036 void designator_or_lambda() {
37 typedef int T;
38 const int b = 0;
39 const int c = 1;
Richard Smith9e2f0a42014-04-13 04:31:48 +000040 int d;
David Blaikieabe1a392014-04-02 05:58:29 +000041 int a1[1] = {[b] (T()) {}}; // expected-error{{no viable conversion from '(lambda}}
Douglas Gregora80cae12012-02-17 03:49:44 +000042 int a2[1] = {[b] = 1 };
Richard Smith9e2f0a42014-04-13 04:31:48 +000043 int a3[1] = {[b,c] = 1 }; // expected-error{{expected ']'}} expected-note {{to match}}
Douglas Gregora80cae12012-02-17 03:49:44 +000044 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 *'}}
Nicolas Lesserf53d1722019-05-19 15:07:58 +000047 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
Richard Smith9e2f0a42014-04-13 04:31:48 +000055 int a9[1] = {[d = 0] = 1}; // expected-error{{is not an integral constant expression}}
Nicolas Lesserf53d1722019-05-19 15:07:58 +000056#if __cplusplus >= 201402L
57 // expected-note@-2{{constant expression cannot modify an object that is visible outside that expression}}
58#endif
Richard Smith9e2f0a42014-04-13 04:31:48 +000059 int a11[1] = {[id(0)] = 1};
Douglas Gregora80cae12012-02-17 03:49:44 +000060 }
Richard Smith10c60722012-08-09 19:01:51 +000061
62 void delete_lambda(int *p) {
63 delete [] p;
64 delete [] (int*) { new int }; // ok, compound-literal, not lambda
Nicolas Lesserf53d1722019-05-19 15:07:58 +000065 delete [] { return new int; } (); // expected-error {{'[]' after delete interpreted as 'delete[]'}}
Richard Smith10c60722012-08-09 19:01:51 +000066 delete [&] { return new int; } (); // ok, lambda
Nicolas Lesserf53d1722019-05-19 15:07:58 +000067
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
Richard Smith10c60722012-08-09 19:01:51 +000073 }
Richard Smith21b3ab42013-05-09 21:36:41 +000074
75 // We support init-captures in C++11 as an extension.
76 int z;
77 void init_capture() {
Nicolas Lesserf53d1722019-05-19 15:07:58 +000078 [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
Richard Smith21b3ab42013-05-09 21:36:41 +000091
Richard Smithba71c082013-05-16 06:20:58 +000092 int x = 4;
Nicolas Lesserf53d1722019-05-19 15:07:58 +000093 auto y = [&r = x, x = x + 1]() -> int {
94#if __cplusplus <= 201103L
95 // expected-warning@-2{{extension}}
96 // expected-warning@-3{{extension}}
97#endif
Richard Smithbb13c9a2013-09-28 04:02:39 +000098 r += 2;
99 return x + 2;
Richard Smith21b3ab42013-05-09 21:36:41 +0000100 } ();
101 }
Aaron Ballman7e1fd012014-03-11 20:32:35 +0000102
103 void attributes() {
104 [] [[]] {}; // expected-error {{lambda requires '()' before attribute specifier}}
Aaron Ballmane8d69b72014-03-12 00:01:07 +0000105 [] __attribute__((noreturn)) {}; // expected-error {{lambda requires '()' before attribute specifier}}
Aaron Ballman7e1fd012014-03-11 20:32:35 +0000106 []() [[]]
107 mutable {}; // expected-error {{expected body of lambda expression}}
108
109 []() [[]] {};
110 []() [[]] -> void {};
111 []() mutable [[]] -> void {};
112 []() mutable noexcept [[]] -> void {};
113
Aaron Ballmane8d69b72014-03-12 00:01:07 +0000114 // 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 }
Douglas Gregordb0b9f12011-08-04 15:30:47 +0000120};
David Majnemere01c4662015-01-09 05:10:55 +0000121
122template <typename>
123void PR22122() {
124 [](int) -> {}; // expected-error {{expected a type}}
125}
126
127template void PR22122<int>();
David Majnemer89296ee2015-01-12 02:28:16 +0000128
Erik Pilkingtonbe19c482019-07-30 19:21:20 +0000129namespace PR42778 {
130struct A {
131 template <class F> A(F&&) {}
132};
133
134struct S {
135 void mf() { A{[*this]{}}; }
136#if __cplusplus < 201703L
137 // expected-warning@-2 {{C++17 extension}}
138#endif
139};
140}
141
David Majnemer89296ee2015-01-12 02:28:16 +0000142struct S {
143 template <typename T>
144 void m (T x =[0); // expected-error{{expected variable name or 'this' in lambda capture list}}
145} s;
David Majnemera3aef352015-01-12 03:14:18 +0000146
147struct U {
148 template <typename T>
149 void m_fn1(T x = 0[0); // expected-error{{expected ']'}} expected-note{{to match this '['}}
150} *U;