blob: e1be75686ad0bf2eb19127861f471ad801fe138c [file] [log] [blame]
Douglas Gregor26f4b322012-02-09 01:02:27 +00001// RUN: %clang_cc1 -fsyntax-only -Wno-unused-value -verify -std=c++11 %s
Douglas Gregordb0b9f12011-08-04 15:30:47 +00002
Richard Smithbfdb1082012-03-12 08:56:40 +00003enum E { e };
4
Richard Smith9e2f0a42014-04-13 04:31:48 +00005constexpr int id(int n) { return n; }
6
Douglas Gregordb0b9f12011-08-04 15:30:47 +00007class C {
8
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}}
Richard Trieu553b2b22011-12-15 00:38:15 +000017 [&,] {}; // expected-error {{expected variable name or 'this' in lambda capture list}}
18 [=,] {}; // expected-error {{expected variable name or 'this' in lambda capture list}}
Douglas Gregor656bc622012-02-09 08:26:42 +000019 [] {};
20 [=] (int i) {};
21 [&] (int) mutable -> void {};
22 [foo,bar] () { return 3; };
23 [=,&foo] () {};
24 [&,foo] () {};
25 [this] () {};
Richard Smithbfdb1082012-03-12 08:56:40 +000026 [] () -> class C { return C(); };
27 [] () -> enum E { return e; };
Douglas Gregordb0b9f12011-08-04 15:30:47 +000028
Douglas Gregor6746c5d2012-02-16 21:53:36 +000029 [] -> int { return 0; }; // expected-error{{lambda requires '()' before return type}}
30 [] mutable -> int { return 0; }; // expected-error{{lambda requires '()' before 'mutable'}}
Richard Smithb3afa6c2012-08-30 13:13:20 +000031 [](int) -> {}; // PR13652 expected-error {{expected a type}}
Douglas Gregordb0b9f12011-08-04 15:30:47 +000032 return 1;
33 }
34
Douglas Gregora80cae12012-02-17 03:49:44 +000035 void designator_or_lambda() {
36 typedef int T;
37 const int b = 0;
38 const int c = 1;
Richard Smith9e2f0a42014-04-13 04:31:48 +000039 int d;
David Blaikieabe1a392014-04-02 05:58:29 +000040 int a1[1] = {[b] (T()) {}}; // expected-error{{no viable conversion from '(lambda}}
Douglas Gregora80cae12012-02-17 03:49:44 +000041 int a2[1] = {[b] = 1 };
Richard Smith9e2f0a42014-04-13 04:31:48 +000042 int a3[1] = {[b,c] = 1 }; // expected-error{{expected ']'}} expected-note {{to match}}
Douglas Gregora80cae12012-02-17 03:49:44 +000043 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 *'}}
Richard Smith9e2f0a42014-04-13 04:31:48 +000046 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};
Douglas Gregora80cae12012-02-17 03:49:44 +000051 }
Richard Smith10c60722012-08-09 19:01:51 +000052
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 }
Richard Smith21b3ab42013-05-09 21:36:41 +000059
60 // We support init-captures in C++11 as an extension.
61 int z;
62 void init_capture() {
Richard Smith5b013f52013-09-28 05:38:27 +000063 [n(0)] () mutable -> int { return ++n; }; // expected-warning{{extension}}
64 [n{0}] { return; }; // expected-error {{<initializer_list>}} 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}}
Richard Smith21b3ab42013-05-09 21:36:41 +000068
Richard Smithba71c082013-05-16 06:20:58 +000069 int x = 4;
Richard Smith5b013f52013-09-28 05:38:27 +000070 auto y = [&r = x, x = x + 1]() -> int { // expected-warning 2{{extension}}
Richard Smithbb13c9a2013-09-28 04:02:39 +000071 r += 2;
72 return x + 2;
Richard Smith21b3ab42013-05-09 21:36:41 +000073 } ();
74 }
Aaron Ballman7e1fd012014-03-11 20:32:35 +000075
76 void attributes() {
77 [] [[]] {}; // expected-error {{lambda requires '()' before attribute specifier}}
Aaron Ballmane8d69b72014-03-12 00:01:07 +000078 [] __attribute__((noreturn)) {}; // expected-error {{lambda requires '()' before attribute specifier}}
Aaron Ballman7e1fd012014-03-11 20:32:35 +000079 []() [[]]
80 mutable {}; // expected-error {{expected body of lambda expression}}
81
82 []() [[]] {};
83 []() [[]] -> void {};
84 []() mutable [[]] -> void {};
85 []() mutable noexcept [[]] -> void {};
86
Aaron Ballmane8d69b72014-03-12 00:01:07 +000087 // 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 }
Douglas Gregordb0b9f12011-08-04 15:30:47 +000093};
David Majnemere01c4662015-01-09 05:10:55 +000094
95template <typename>
96void PR22122() {
97 [](int) -> {}; // expected-error {{expected a type}}
98}
99
100template void PR22122<int>();
David Majnemer89296ee2015-01-12 02:28:16 +0000101
102struct S {
103 template <typename T>
104 void m (T x =[0); // expected-error{{expected variable name or 'this' in lambda capture list}}
105} s;