Richard Smith | 5030928 | 2019-08-30 22:52:55 +0000 | [diff] [blame] | 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 |
Douglas Gregor | db0b9f1 | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 3 | |
Richard Smith | bfdb108 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 4 | enum E { e }; |
| 5 | |
Richard Smith | 9e2f0a4 | 2014-04-13 04:31:48 +0000 | [diff] [blame] | 6 | constexpr int id(int n) { return n; } |
| 7 | |
Douglas Gregor | db0b9f1 | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 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}} |
Richard Trieu | 553b2b2 | 2011-12-15 00:38:15 +0000 | [diff] [blame] | 18 | [&,] {}; // expected-error {{expected variable name or 'this' in lambda capture list}} |
| 19 | [=,] {}; // expected-error {{expected variable name or 'this' in lambda capture list}} |
Douglas Gregor | 656bc62 | 2012-02-09 08:26:42 +0000 | [diff] [blame] | 20 | [] {}; |
| 21 | [=] (int i) {}; |
| 22 | [&] (int) mutable -> void {}; |
| 23 | [foo,bar] () { return 3; }; |
| 24 | [=,&foo] () {}; |
| 25 | [&,foo] () {}; |
| 26 | [this] () {}; |
Richard Smith | bfdb108 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 27 | [] () -> class C { return C(); }; |
| 28 | [] () -> enum E { return e; }; |
Douglas Gregor | db0b9f1 | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 29 | |
Douglas Gregor | 6746c5d | 2012-02-16 21:53:36 +0000 | [diff] [blame] | 30 | [] -> int { return 0; }; // expected-error{{lambda requires '()' before return type}} |
| 31 | [] mutable -> int { return 0; }; // expected-error{{lambda requires '()' before 'mutable'}} |
Richard Smith | b3afa6c | 2012-08-30 13:13:20 +0000 | [diff] [blame] | 32 | [](int) -> {}; // PR13652 expected-error {{expected a type}} |
Douglas Gregor | db0b9f1 | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 33 | return 1; |
| 34 | } |
| 35 | |
Douglas Gregor | a80cae1 | 2012-02-17 03:49:44 +0000 | [diff] [blame] | 36 | void designator_or_lambda() { |
| 37 | typedef int T; |
| 38 | const int b = 0; |
| 39 | const int c = 1; |
Richard Smith | 9e2f0a4 | 2014-04-13 04:31:48 +0000 | [diff] [blame] | 40 | int d; |
David Blaikie | abe1a39 | 2014-04-02 05:58:29 +0000 | [diff] [blame] | 41 | int a1[1] = {[b] (T()) {}}; // expected-error{{no viable conversion from '(lambda}} |
Douglas Gregor | a80cae1 | 2012-02-17 03:49:44 +0000 | [diff] [blame] | 42 | int a2[1] = {[b] = 1 }; |
Richard Smith | 9e2f0a4 | 2014-04-13 04:31:48 +0000 | [diff] [blame] | 43 | 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] | 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 *'}} |
Nicolas Lesser | f53d172 | 2019-05-19 15:07:58 +0000 | [diff] [blame] | 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 |
Richard Smith | 9e2f0a4 | 2014-04-13 04:31:48 +0000 | [diff] [blame] | 55 | int a9[1] = {[d = 0] = 1}; // expected-error{{is not an integral constant expression}} |
Nicolas Lesser | f53d172 | 2019-05-19 15:07:58 +0000 | [diff] [blame] | 56 | #if __cplusplus >= 201402L |
| 57 | // expected-note@-2{{constant expression cannot modify an object that is visible outside that expression}} |
| 58 | #endif |
Richard Smith | 9e2f0a4 | 2014-04-13 04:31:48 +0000 | [diff] [blame] | 59 | int a11[1] = {[id(0)] = 1}; |
Douglas Gregor | a80cae1 | 2012-02-17 03:49:44 +0000 | [diff] [blame] | 60 | } |
Richard Smith | 10c6072 | 2012-08-09 19:01:51 +0000 | [diff] [blame] | 61 | |
| 62 | void delete_lambda(int *p) { |
| 63 | delete [] p; |
| 64 | delete [] (int*) { new int }; // ok, compound-literal, not lambda |
Nicolas Lesser | f53d172 | 2019-05-19 15:07:58 +0000 | [diff] [blame] | 65 | delete [] { return new int; } (); // expected-error {{'[]' after delete interpreted as 'delete[]'}} |
Richard Smith | 10c6072 | 2012-08-09 19:01:51 +0000 | [diff] [blame] | 66 | delete [&] { return new int; } (); // ok, lambda |
Nicolas Lesser | f53d172 | 2019-05-19 15:07:58 +0000 | [diff] [blame] | 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 |
Richard Smith | 10c6072 | 2012-08-09 19:01:51 +0000 | [diff] [blame] | 73 | } |
Richard Smith | 21b3ab4 | 2013-05-09 21:36:41 +0000 | [diff] [blame] | 74 | |
| 75 | // We support init-captures in C++11 as an extension. |
| 76 | int z; |
| 77 | void init_capture() { |
Nicolas Lesser | f53d172 | 2019-05-19 15:07:58 +0000 | [diff] [blame] | 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 |
Richard Smith | 21b3ab4 | 2013-05-09 21:36:41 +0000 | [diff] [blame] | 91 | |
Richard Smith | ba71c08 | 2013-05-16 06:20:58 +0000 | [diff] [blame] | 92 | int x = 4; |
Nicolas Lesser | f53d172 | 2019-05-19 15:07:58 +0000 | [diff] [blame] | 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 |
Richard Smith | bb13c9a | 2013-09-28 04:02:39 +0000 | [diff] [blame] | 98 | r += 2; |
| 99 | return x + 2; |
Richard Smith | 21b3ab4 | 2013-05-09 21:36:41 +0000 | [diff] [blame] | 100 | } (); |
| 101 | } |
Aaron Ballman | 7e1fd01 | 2014-03-11 20:32:35 +0000 | [diff] [blame] | 102 | |
| 103 | void attributes() { |
| 104 | [] [[]] {}; // expected-error {{lambda requires '()' before attribute specifier}} |
Aaron Ballman | e8d69b7 | 2014-03-12 00:01:07 +0000 | [diff] [blame] | 105 | [] __attribute__((noreturn)) {}; // expected-error {{lambda requires '()' before attribute specifier}} |
Aaron Ballman | 7e1fd01 | 2014-03-11 20:32:35 +0000 | [diff] [blame] | 106 | []() [[]] |
| 107 | mutable {}; // expected-error {{expected body of lambda expression}} |
| 108 | |
| 109 | []() [[]] {}; |
| 110 | []() [[]] -> void {}; |
| 111 | []() mutable [[]] -> void {}; |
| 112 | []() mutable noexcept [[]] -> void {}; |
| 113 | |
Aaron Ballman | e8d69b7 | 2014-03-12 00:01:07 +0000 | [diff] [blame] | 114 | // 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 Gregor | db0b9f1 | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 120 | }; |
David Majnemer | e01c466 | 2015-01-09 05:10:55 +0000 | [diff] [blame] | 121 | |
| 122 | template <typename> |
| 123 | void PR22122() { |
| 124 | [](int) -> {}; // expected-error {{expected a type}} |
| 125 | } |
| 126 | |
| 127 | template void PR22122<int>(); |
David Majnemer | 89296ee | 2015-01-12 02:28:16 +0000 | [diff] [blame] | 128 | |
Erik Pilkington | be19c48 | 2019-07-30 19:21:20 +0000 | [diff] [blame] | 129 | namespace PR42778 { |
| 130 | struct A { |
| 131 | template <class F> A(F&&) {} |
| 132 | }; |
| 133 | |
| 134 | struct S { |
| 135 | void mf() { A{[*this]{}}; } |
| 136 | #if __cplusplus < 201703L |
| 137 | // expected-warning@-2 {{C++17 extension}} |
| 138 | #endif |
| 139 | }; |
| 140 | } |
| 141 | |
David Majnemer | 89296ee | 2015-01-12 02:28:16 +0000 | [diff] [blame] | 142 | struct 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 Majnemer | a3aef35 | 2015-01-12 03:14:18 +0000 | [diff] [blame] | 146 | |
| 147 | struct U { |
| 148 | template <typename T> |
| 149 | void m_fn1(T x = 0[0); // expected-error{{expected ']'}} expected-note{{to match this '['}} |
| 150 | } *U; |