1 // RUN: %clang_cc1 -std=c++0x -Wno-unused-value -fsyntax-only -verify -fblocks %s
2 
3 namespace std { class type_info; };
4 
5 namespace ExplicitCapture {
6   class C {
7     int Member;
8 
9     static void Overload(int);
10     void Overload();
11     virtual C& Overload(float);
12 
13     void ImplicitThisCapture() {
14       [](){(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}}
24     }
25   };
26 
27   void f() {
28     [this] () {}; // expected-error {{'this' cannot be captured in this context}}
29   }
30 }
31 
32 namespace ReturnDeduction {
33   void test() {
34     [](){ return 1; };
35     [](){ return 1; };
36     [](){ return ({return 1; 1;}); };
37     [](){ return ({return 'c'; 1;}); }; // expected-error {{must match previous return type}}
38     []()->int{ return 'c'; return 1; };
39     [](){ return 'c'; return 1; };  // expected-error {{must match previous return type}}
40     []() { return; return (void)0; };
41     [](){ return 1; return 1; };
42   }
43 }
44 
45 namespace ImplicitCapture {
46   void test() {
47     int a = 0; // expected-note 5 {{declared}}
48     []() { return a; }; // expected-error {{variable 'a' cannot be implicitly captured in a lambda with no capture-default specified}} expected-note {{begins here}}
49     [&]() { return a; };
50     [=]() { return a; };
51     [=]() { int* b = &a; }; // expected-error {{cannot initialize a variable of type 'int *' with an rvalue of type 'const int *'}}
52     [=]() { return [&]() { return a; }; };
53     []() { 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}}
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 [&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}}
56     [=]() { return [&a] { return a; }; }; //
57 
58     const int b = 2;
59     []() { return b; };
60 
61     union { // expected-note {{declared}}
62       int c;
63       float d;
64     };
65     d = 3;
66     [=]() { return c; }; // expected-error {{unnamed variable cannot be implicitly captured in a lambda expression}}
67 
68     __block int e; // expected-note 3 {{declared}}
69     [&]() { return e; }; // expected-error {{__block variable 'e' cannot be captured in a lambda expression}}
70     [&e]() { return e; }; // expected-error 2 {{__block variable 'e' cannot be captured in a lambda expression}}
71 
72     int f[10]; // expected-note {{declared}}
73     [&]() { return f[2]; };
74     (void) ^{ return []() { return f[2]; }; }; // expected-error {{variable 'f' cannot be implicitly captured in a lambda with no capture-default specified}} \
75     // expected-note{{lambda expression begins here}}
76 
77     struct G { G(); G(G&); int a; }; // expected-note 6 {{not viable}}
78     G g;
79     [=]() { const G* gg = &g; return gg->a; };
80     [=]() { return [=]{ const G* gg = &g; return gg->a; }(); }; // expected-error {{no matching constructor for initialization of 'ImplicitCapture::G'}}
81     (void)^{ return [=]{ const G* gg = &g; return gg->a; }(); }; // expected-error 2 {{no matching constructor for initialization of 'const ImplicitCapture::G'}}
82 
83     const int h = a; // expected-note {{declared}}
84     []() { return h; }; // expected-error {{variable 'h' cannot be implicitly captured in a lambda with no capture-default specified}} expected-note {{lambda expression begins here}}
85 
86     // The exemption for variables which can appear in constant expressions
87     // applies only to objects (and not to references).
88     // FIXME: This might be a bug in the standard.
89     static int i;
90     constexpr int &ref_i = i; // expected-note {{declared}}
91     [] { 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}}
92   }
93 }
94 
95 namespace PR12031 {
96   struct X {
97     template<typename T>
98     X(const T&);
99     ~X();
100   };
101 
102   void f(int i, X x);
103   void g() {
104     const int v = 10;
105     f(v, [](){});
106   }
107 }
108 
109 namespace NullPtr {
110   int &f(int *p);
111   char &f(...);
112   void g() {
113     int n = 0;
114     [=] {
115       char &k = f(n); // not a null pointer constant
116     } ();
117 
118     const int m = 0;
119     [=] {
120       int &k = f(m); // a null pointer constant
121     } ();
122 
123     [=] () -> bool {
124       int &k = f(m); // a null pointer constant
125       return &m == 0;
126     } ();
127 
128     [m] {
129       int &k = f(m); // a null pointer constant
130     } ();
131   }
132 }
133 
134 void PR12248()
135 {
136   unsigned int result = 0;
137   auto l = [&]() { ++result; };
138 }
139 
140 namespace ModifyingCapture {
141   void test() {
142     int n = 0;
143     [=] {
144       n = 1; // expected-error {{cannot assign to a variable captured by copy in a non-mutable lambda}}
145     };
146   }
147 }
148