Douglas Gregor | 7f99f43 | 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 | ae7902c | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 2 | |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 3 | enum E { e }; |
| 4 | |
Douglas Gregor | ae7902c | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 5 | class C { |
| 6 | |
| 7 | int f() { |
| 8 | int foo, bar; |
| 9 | |
| 10 | []; // expected-error {{expected body of lambda expression}} |
| 11 | [+] {}; // expected-error {{expected variable name or 'this' in lambda capture list}} |
| 12 | [foo+] {}; // expected-error {{expected ',' or ']' in lambda capture list}} |
| 13 | [foo,&this] {}; // expected-error {{'this' cannot be captured by reference}} |
| 14 | [&this] {}; // expected-error {{'this' cannot be captured by reference}} |
Richard Trieu | 2fe9b7f | 2011-12-15 00:38:15 +0000 | [diff] [blame] | 15 | [&,] {}; // expected-error {{expected variable name or 'this' in lambda capture list}} |
| 16 | [=,] {}; // expected-error {{expected variable name or 'this' in lambda capture list}} |
Douglas Gregor | b326ca8 | 2012-02-09 08:26:42 +0000 | [diff] [blame] | 17 | [] {}; |
| 18 | [=] (int i) {}; |
| 19 | [&] (int) mutable -> void {}; |
| 20 | [foo,bar] () { return 3; }; |
| 21 | [=,&foo] () {}; |
| 22 | [&,foo] () {}; |
| 23 | [this] () {}; |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 24 | [] () -> class C { return C(); }; |
| 25 | [] () -> enum E { return e; }; |
Douglas Gregor | ae7902c | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 26 | |
Douglas Gregor | c9ecec4 | 2012-02-16 21:53:36 +0000 | [diff] [blame] | 27 | [] -> int { return 0; }; // expected-error{{lambda requires '()' before return type}} |
| 28 | [] mutable -> int { return 0; }; // expected-error{{lambda requires '()' before 'mutable'}} |
Richard Smith | 3bc2226 | 2012-08-30 13:13:20 +0000 | [diff] [blame] | 29 | [](int) -> {}; // PR13652 expected-error {{expected a type}} |
Douglas Gregor | ae7902c | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 30 | return 1; |
| 31 | } |
| 32 | |
Douglas Gregor | b3f323d | 2012-02-17 03:49:44 +0000 | [diff] [blame] | 33 | void designator_or_lambda() { |
| 34 | typedef int T; |
| 35 | const int b = 0; |
| 36 | const int c = 1; |
Eli Friedman | 9dd686d | 2012-10-24 20:28:18 +0000 | [diff] [blame] | 37 | int a1[1] = {[b] (T()) {}}; // expected-error{{no viable conversion from '<lambda}} |
Douglas Gregor | b3f323d | 2012-02-17 03:49:44 +0000 | [diff] [blame] | 38 | int a2[1] = {[b] = 1 }; |
| 39 | int a3[1] = {[b,c] = 1 }; // expected-error{{expected body of lambda expression}} |
| 40 | int a4[1] = {[&b] = 1 }; // expected-error{{integral constant expression must have integral or unscoped enumeration type, not 'const int *'}} |
| 41 | int a5[3] = { []{return 0;}() }; |
| 42 | int a6[1] = {[this] = 1 }; // expected-error{{integral constant expression must have integral or unscoped enumeration type, not 'C *'}} |
| 43 | } |
Richard Smith | 950435c | 2012-08-09 19:01:51 +0000 | [diff] [blame] | 44 | |
| 45 | void delete_lambda(int *p) { |
| 46 | delete [] p; |
| 47 | delete [] (int*) { new int }; // ok, compound-literal, not lambda |
| 48 | delete [] { return new int; } (); // expected-error{{expected expression}} |
| 49 | delete [&] { return new int; } (); // ok, lambda |
| 50 | } |
Richard Smith | 0a664b8 | 2013-05-09 21:36:41 +0000 | [diff] [blame] | 51 | |
| 52 | // We support init-captures in C++11 as an extension. |
| 53 | int z; |
| 54 | void init_capture() { |
| 55 | // FIXME: These diagnostics should all disappear once semantic analysis |
| 56 | // for init-captures is complete. |
Richard Smith | 0d8e964 | 2013-05-16 06:20:58 +0000 | [diff] [blame] | 57 | [n(0)] () -> int { return ++n; }; // expected-error {{non-static data member}} |
| 58 | [n{0}] { return; }; // expected-error {{<initializer_list>}} |
| 59 | [n = 0] { return ++n; }; // expected-error {{non-static data member}} |
| 60 | [n = {0}] { return; }; // expected-error {{<initializer_list>}} |
| 61 | [a([&b = z]{})](){}; |
Richard Smith | 0a664b8 | 2013-05-09 21:36:41 +0000 | [diff] [blame] | 62 | |
Richard Smith | 0d8e964 | 2013-05-16 06:20:58 +0000 | [diff] [blame] | 63 | int x = 4; |
| 64 | auto y = [&r = x, x = x + 1]() -> int { |
| 65 | r += 2; // expected-error {{non-static data member}} |
| 66 | return x + 2; // expected-error {{non-static data member}} |
Richard Smith | 0a664b8 | 2013-05-09 21:36:41 +0000 | [diff] [blame] | 67 | } (); |
| 68 | } |
Douglas Gregor | ae7902c | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 69 | }; |