Douglas Gregor | 26f4b32 | 2012-02-09 01:02:27 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -fsyntax-only -Wno-unused-value -verify -std=c++11 %s |
Douglas Gregor | db0b9f1 | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 2 | |
Richard Smith | bfdb108 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 3 | enum E { e }; |
| 4 | |
Richard Smith | 9e2f0a4 | 2014-04-13 04:31:48 +0000 | [diff] [blame] | 5 | constexpr int id(int n) { return n; } |
| 6 | |
Douglas Gregor | db0b9f1 | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 7 | class 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 Trieu | 553b2b2 | 2011-12-15 00:38:15 +0000 | [diff] [blame] | 17 | [&,] {}; // expected-error {{expected variable name or 'this' in lambda capture list}} |
| 18 | [=,] {}; // expected-error {{expected variable name or 'this' in lambda capture list}} |
Douglas Gregor | 656bc62 | 2012-02-09 08:26:42 +0000 | [diff] [blame] | 19 | [] {}; |
| 20 | [=] (int i) {}; |
| 21 | [&] (int) mutable -> void {}; |
| 22 | [foo,bar] () { return 3; }; |
| 23 | [=,&foo] () {}; |
| 24 | [&,foo] () {}; |
| 25 | [this] () {}; |
Richard Smith | bfdb108 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 26 | [] () -> class C { return C(); }; |
| 27 | [] () -> enum E { return e; }; |
Douglas Gregor | db0b9f1 | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 28 | |
Douglas Gregor | 6746c5d | 2012-02-16 21:53:36 +0000 | [diff] [blame] | 29 | [] -> int { return 0; }; // expected-error{{lambda requires '()' before return type}} |
| 30 | [] mutable -> int { return 0; }; // expected-error{{lambda requires '()' before 'mutable'}} |
Richard Smith | b3afa6c | 2012-08-30 13:13:20 +0000 | [diff] [blame] | 31 | [](int) -> {}; // PR13652 expected-error {{expected a type}} |
Douglas Gregor | db0b9f1 | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 32 | return 1; |
| 33 | } |
| 34 | |
Douglas Gregor | a80cae1 | 2012-02-17 03:49:44 +0000 | [diff] [blame] | 35 | void designator_or_lambda() { |
| 36 | typedef int T; |
| 37 | const int b = 0; |
| 38 | const int c = 1; |
Richard Smith | 9e2f0a4 | 2014-04-13 04:31:48 +0000 | [diff] [blame] | 39 | int d; |
David Blaikie | abe1a39 | 2014-04-02 05:58:29 +0000 | [diff] [blame] | 40 | int a1[1] = {[b] (T()) {}}; // expected-error{{no viable conversion from '(lambda}} |
Douglas Gregor | a80cae1 | 2012-02-17 03:49:44 +0000 | [diff] [blame] | 41 | int a2[1] = {[b] = 1 }; |
Richard Smith | 9e2f0a4 | 2014-04-13 04:31:48 +0000 | [diff] [blame] | 42 | int a3[1] = {[b,c] = 1 }; // expected-error{{expected ']'}} expected-note {{to match}} |
Douglas Gregor | a80cae1 | 2012-02-17 03:49:44 +0000 | [diff] [blame] | 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 *'}} |
Richard Smith | 9e2f0a4 | 2014-04-13 04:31:48 +0000 | [diff] [blame] | 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}; |
Douglas Gregor | a80cae1 | 2012-02-17 03:49:44 +0000 | [diff] [blame] | 51 | } |
Richard Smith | 10c6072 | 2012-08-09 19:01:51 +0000 | [diff] [blame] | 52 | |
| 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 Smith | 21b3ab4 | 2013-05-09 21:36:41 +0000 | [diff] [blame] | 59 | |
| 60 | // We support init-captures in C++11 as an extension. |
| 61 | int z; |
| 62 | void init_capture() { |
Richard Smith | 5b013f5 | 2013-09-28 05:38:27 +0000 | [diff] [blame] | 63 | [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 Smith | 21b3ab4 | 2013-05-09 21:36:41 +0000 | [diff] [blame] | 68 | |
Richard Smith | ba71c08 | 2013-05-16 06:20:58 +0000 | [diff] [blame] | 69 | int x = 4; |
Richard Smith | 5b013f5 | 2013-09-28 05:38:27 +0000 | [diff] [blame] | 70 | auto y = [&r = x, x = x + 1]() -> int { // expected-warning 2{{extension}} |
Richard Smith | bb13c9a | 2013-09-28 04:02:39 +0000 | [diff] [blame] | 71 | r += 2; |
| 72 | return x + 2; |
Richard Smith | 21b3ab4 | 2013-05-09 21:36:41 +0000 | [diff] [blame] | 73 | } (); |
| 74 | } |
Aaron Ballman | 7e1fd01 | 2014-03-11 20:32:35 +0000 | [diff] [blame] | 75 | |
| 76 | void attributes() { |
| 77 | [] [[]] {}; // expected-error {{lambda requires '()' before attribute specifier}} |
Aaron Ballman | e8d69b7 | 2014-03-12 00:01:07 +0000 | [diff] [blame] | 78 | [] __attribute__((noreturn)) {}; // expected-error {{lambda requires '()' before attribute specifier}} |
Aaron Ballman | 7e1fd01 | 2014-03-11 20:32:35 +0000 | [diff] [blame] | 79 | []() [[]] |
| 80 | mutable {}; // expected-error {{expected body of lambda expression}} |
| 81 | |
| 82 | []() [[]] {}; |
| 83 | []() [[]] -> void {}; |
| 84 | []() mutable [[]] -> void {}; |
| 85 | []() mutable noexcept [[]] -> void {}; |
| 86 | |
Aaron Ballman | e8d69b7 | 2014-03-12 00:01:07 +0000 | [diff] [blame] | 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 | } |
Douglas Gregor | db0b9f1 | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 93 | }; |
David Majnemer | e01c466 | 2015-01-09 05:10:55 +0000 | [diff] [blame] | 94 | |
| 95 | template <typename> |
| 96 | void PR22122() { |
| 97 | [](int) -> {}; // expected-error {{expected a type}} |
| 98 | } |
| 99 | |
| 100 | template void PR22122<int>(); |
David Majnemer | 89296ee | 2015-01-12 02:28:16 +0000 | [diff] [blame^] | 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; |