blob: 80ba0a1d035ba92fe391b181776893cd0fed340c [file] [log] [blame]
Douglas Gregor503384f2012-02-09 00:47:04 +00001// RUN: %clang_cc1 -std=c++0x -Wno-unused-value -fsyntax-only -verify -fblocks %s
Eli Friedmane81d7e92012-01-07 01:08:17 +00002
Eli Friedman72899c32012-01-07 04:59:52 +00003namespace std { class type_info; };
4
Eli Friedmane81d7e92012-01-07 01:08:17 +00005namespace ExplicitCapture {
Eli Friedmane81d7e92012-01-07 01:08:17 +00006 class C {
Eli Friedman72899c32012-01-07 04:59:52 +00007 int Member;
Eli Friedmane81d7e92012-01-07 01:08:17 +00008
Eli Friedman72899c32012-01-07 04:59:52 +00009 static void Overload(int);
10 void Overload();
11 virtual C& Overload(float);
12
Eli Friedman72899c32012-01-07 04:59:52 +000013 void ImplicitThisCapture() {
Douglas Gregorb326ca82012-02-09 08:26:42 +000014 [](){(void)Member;}; // expected-error {{'this' cannot be implicitly captured in this context}}
15 [&](){(void)Member;};
16
17 [this](){(void)Member;};
18 [this]{[this]{};};
19 []{[this]{};};// expected-error {{'this' cannot be implicitly captured in this context}}
20 []{Overload(3);};
21 []{Overload();}; // expected-error {{'this' cannot be implicitly captured in this context}}
22 []{(void)typeid(Overload());};
23 []{(void)typeid(Overload(.5f));};// expected-error {{'this' cannot be implicitly captured in this context}}
Eli Friedman72899c32012-01-07 04:59:52 +000024 }
Eli Friedmane81d7e92012-01-07 01:08:17 +000025 };
26
27 void f() {
Douglas Gregorb326ca82012-02-09 08:26:42 +000028 [this] () {}; // expected-error {{'this' cannot be captured in this context}}
Eli Friedmane81d7e92012-01-07 01:08:17 +000029 }
30}
Eli Friedman84b007f2012-01-26 03:00:14 +000031
32namespace ReturnDeduction {
33 void test() {
Douglas Gregorb326ca82012-02-09 08:26:42 +000034 [](){ return 1; };
35 [](){ return 1; };
36 [](){ return ({return 1; 1;}); };
Douglas Gregor54042f12012-02-09 10:18:50 +000037 [](){ return ({return 'c'; 1;}); }; // expected-error {{must match previous return type}} \
38 // expected-warning{{omitted result type}}
Douglas Gregorb326ca82012-02-09 08:26:42 +000039 []()->int{ return 'c'; return 1; };
40 [](){ return 'c'; return 1; }; // expected-error {{must match previous return type}}
41 []() { return; return (void)0; };
Douglas Gregor54042f12012-02-09 10:18:50 +000042 [](){ return 1; return 1; }; // expected-warning{{omitted result type}}
Eli Friedman84b007f2012-01-26 03:00:14 +000043 }
44}
Eli Friedmanb942cb22012-02-03 22:47:37 +000045
46namespace ImplicitCapture {
47 void test() {
Eli Friedmancefc7b22012-02-03 23:06:43 +000048 int a = 0; // expected-note 5 {{declared}}
Douglas Gregorb326ca82012-02-09 08:26:42 +000049 []() { return a; }; // expected-error {{variable 'a' cannot be implicitly captured in a lambda with no capture-default specified}} expected-note {{begins here}}
50 [&]() { return a; };
51 [=]() { return a; };
52 [=]() { int* b = &a; }; // expected-error {{cannot initialize a variable of type 'int *' with an rvalue of type 'const int *'}}
53 [=]() { return [&]() { return a; }; };
54 []() { return [&]() { return a; }; }; // expected-error {{variable 'a' cannot be implicitly captured in a lambda with no capture-default specified}} expected-note {{lambda expression begins here}}
55 []() { return ^{ return a; }; };// expected-error {{variable 'a' cannot be implicitly captured in a lambda with no capture-default specified}} expected-note {{lambda expression begins here}}
56 []() { return [&a] { return a; }; }; // expected-error 2 {{variable 'a' cannot be implicitly captured in a lambda with no capture-default specified}} expected-note 2 {{lambda expression begins here}}
57 [=]() { return [&a] { return a; }; }; //
Eli Friedmanb942cb22012-02-03 22:47:37 +000058
59 const int b = 2;
Douglas Gregorb326ca82012-02-09 08:26:42 +000060 []() { return b; };
Eli Friedmanb942cb22012-02-03 22:47:37 +000061
62 union { // expected-note {{declared}}
63 int c;
64 float d;
65 };
66 d = 3;
Douglas Gregorb326ca82012-02-09 08:26:42 +000067 [=]() { return c; }; // expected-error {{unnamed variable cannot be implicitly captured in a lambda expression}}
Eli Friedmanb942cb22012-02-03 22:47:37 +000068
Eli Friedmancefc7b22012-02-03 23:06:43 +000069 __block int e; // expected-note 3 {{declared}}
Douglas Gregorb326ca82012-02-09 08:26:42 +000070 [&]() { return e; }; // expected-error {{__block variable 'e' cannot be captured in a lambda expression}}
71 [&e]() { return e; }; // expected-error 2 {{__block variable 'e' cannot be captured in a lambda expression}}
Eli Friedmanb942cb22012-02-03 22:47:37 +000072
73 int f[10]; // expected-note {{declared}}
Douglas Gregorb326ca82012-02-09 08:26:42 +000074 [&]() { return f[2]; };
Douglas Gregorf8af9822012-02-12 18:42:33 +000075 (void) ^{ return []() { return f[2]; }; }; // expected-error {{variable 'f' cannot be implicitly captured in a lambda with no capture-default specified}} \
76 // expected-note{{lambda expression begins here}}
Eli Friedmanb942cb22012-02-03 22:47:37 +000077
78 struct G { G(); G(G&); int a; }; // expected-note 6 {{not viable}}
79 G g;
Douglas Gregor54042f12012-02-09 10:18:50 +000080 [=]() { const G* gg = &g; return gg->a; }; // expected-warning{{omitted result type}}
Douglas Gregor999713e2012-02-18 09:37:24 +000081 [=]() { return [=]{ const G* gg = &g; return gg->a; }(); }; // expected-error {{no matching constructor for initialization of 'ImplicitCapture::G'}} \
Douglas Gregor54042f12012-02-09 10:18:50 +000082 // expected-warning{{omitted result type}}
83 (void)^{ return [=]{ const G* gg = &g; return gg->a; }(); }; // expected-error 2 {{no matching constructor for initialization of 'const ImplicitCapture::G'}} \
84 // expected-warning{{omitted result type}}
Eli Friedman210386e2012-02-06 21:50:18 +000085
86 const int h = a; // expected-note {{declared}}
Douglas Gregorb326ca82012-02-09 08:26:42 +000087 []() { return h; }; // expected-error {{variable 'h' cannot be implicitly captured in a lambda with no capture-default specified}} expected-note {{lambda expression begins here}}
Richard Smith16581332012-03-02 04:14:40 +000088
89 // The exemption for variables which can appear in constant expressions
90 // applies only to objects (and not to references).
91 // FIXME: This might be a bug in the standard.
92 static int i;
93 constexpr int &ref_i = i; // expected-note {{declared}}
94 [] { return ref_i; }; // expected-error {{variable 'ref_i' cannot be implicitly captured in a lambda with no capture-default specified}} expected-note {{lambda expression begins here}}
Eli Friedmanb942cb22012-02-03 22:47:37 +000095 }
96}
Douglas Gregorb09ab8c2012-02-21 20:05:31 +000097
98namespace PR12031 {
99 struct X {
100 template<typename T>
101 X(const T&);
102 ~X();
103 };
104
105 void f(int i, X x);
106 void g() {
107 const int v = 10;
108 f(v, [](){});
109 }
110}
Richard Smith359c89d2012-02-24 22:12:32 +0000111
112namespace NullPtr {
113 int &f(int *p);
114 char &f(...);
115 void g() {
116 int n = 0;
117 [=] {
118 char &k = f(n); // not a null pointer constant
119 } ();
120
121 const int m = 0;
122 [=] {
123 int &k = f(m); // a null pointer constant
124 } ();
125
Richard Smith359c89d2012-02-24 22:12:32 +0000126 [=] () -> bool {
Richard Smith61dab362012-02-24 23:21:24 +0000127 int &k = f(m); // a null pointer constant
128 return &m == 0;
Richard Smith359c89d2012-02-24 22:12:32 +0000129 } ();
130
131 [m] {
Richard Smith61dab362012-02-24 23:21:24 +0000132 int &k = f(m); // a null pointer constant
Richard Smith359c89d2012-02-24 22:12:32 +0000133 } ();
134 }
135}
Eli Friedman71930e02012-03-12 20:57:19 +0000136
137void PR12248()
138{
139 unsigned int result = 0;
140 auto l = [&]() { ++result; };
141}
John McCall78dae242012-03-13 00:37:01 +0000142
143namespace ModifyingCapture {
144 void test() {
145 int n = 0;
146 [=] {
147 n = 1; // expected-error {{variable is not assignable (captured by copy)}}
148 };
149 }
150}