blob: 76c1e0e7cec43a2887594666c11ccb0c6dd7ed4c [file] [log] [blame]
Douglas Gregor7f99f432012-02-09 01:02:27 +00001// RUN: %clang_cc1 -fsyntax-only -Wno-unused-value -verify -std=c++11 %s
Douglas Gregorae7902c2011-08-04 15:30:47 +00002
Richard Smith7796eb52012-03-12 08:56:40 +00003enum E { e };
4
Douglas Gregorae7902c2011-08-04 15:30:47 +00005class 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 Trieu2fe9b7f2011-12-15 00:38:15 +000015 [&,] {}; // expected-error {{expected variable name or 'this' in lambda capture list}}
16 [=,] {}; // expected-error {{expected variable name or 'this' in lambda capture list}}
Douglas Gregorb326ca82012-02-09 08:26:42 +000017 [] {};
18 [=] (int i) {};
19 [&] (int) mutable -> void {};
20 [foo,bar] () { return 3; };
21 [=,&foo] () {};
22 [&,foo] () {};
23 [this] () {};
Richard Smith7796eb52012-03-12 08:56:40 +000024 [] () -> class C { return C(); };
25 [] () -> enum E { return e; };
Douglas Gregorae7902c2011-08-04 15:30:47 +000026
Douglas Gregorc9ecec42012-02-16 21:53:36 +000027 [] -> int { return 0; }; // expected-error{{lambda requires '()' before return type}}
28 [] mutable -> int { return 0; }; // expected-error{{lambda requires '()' before 'mutable'}}
Richard Smith3bc22262012-08-30 13:13:20 +000029 [](int) -> {}; // PR13652 expected-error {{expected a type}}
Douglas Gregorae7902c2011-08-04 15:30:47 +000030 return 1;
31 }
32
Douglas Gregorb3f323d2012-02-17 03:49:44 +000033 void designator_or_lambda() {
34 typedef int T;
35 const int b = 0;
36 const int c = 1;
Eli Friedman9dd686d2012-10-24 20:28:18 +000037 int a1[1] = {[b] (T()) {}}; // expected-error{{no viable conversion from '<lambda}}
Douglas Gregorb3f323d2012-02-17 03:49:44 +000038 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 Smith950435c2012-08-09 19:01:51 +000044
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 Smith0a664b82013-05-09 21:36:41 +000051
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 Smith0d8e9642013-05-16 06:20:58 +000057 [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 Smith0a664b82013-05-09 21:36:41 +000062
Richard Smith0d8e9642013-05-16 06:20:58 +000063 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 Smith0a664b82013-05-09 21:36:41 +000067 } ();
68 }
Douglas Gregorae7902c2011-08-04 15:30:47 +000069};