Douglas Gregor | 503384f | 2012-02-09 00:47:04 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -std=c++0x -Wno-unused-value -fsyntax-only -verify -fblocks %s |
Eli Friedman | e81d7e9 | 2012-01-07 01:08:17 +0000 | [diff] [blame] | 2 | |
Eli Friedman | 72899c3 | 2012-01-07 04:59:52 +0000 | [diff] [blame] | 3 | namespace std { class type_info; }; |
| 4 | |
Eli Friedman | e81d7e9 | 2012-01-07 01:08:17 +0000 | [diff] [blame] | 5 | namespace ExplicitCapture { |
Eli Friedman | e81d7e9 | 2012-01-07 01:08:17 +0000 | [diff] [blame] | 6 | class C { |
Eli Friedman | 72899c3 | 2012-01-07 04:59:52 +0000 | [diff] [blame] | 7 | int Member; |
Eli Friedman | e81d7e9 | 2012-01-07 01:08:17 +0000 | [diff] [blame] | 8 | |
Eli Friedman | 72899c3 | 2012-01-07 04:59:52 +0000 | [diff] [blame] | 9 | static void Overload(int); |
| 10 | void Overload(); |
| 11 | virtual C& Overload(float); |
| 12 | |
Eli Friedman | 72899c3 | 2012-01-07 04:59:52 +0000 | [diff] [blame] | 13 | void ImplicitThisCapture() { |
Douglas Gregor | b326ca8 | 2012-02-09 08:26:42 +0000 | [diff] [blame] | 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}} |
Eli Friedman | 72899c3 | 2012-01-07 04:59:52 +0000 | [diff] [blame] | 24 | } |
Eli Friedman | e81d7e9 | 2012-01-07 01:08:17 +0000 | [diff] [blame] | 25 | }; |
| 26 | |
| 27 | void f() { |
Douglas Gregor | b326ca8 | 2012-02-09 08:26:42 +0000 | [diff] [blame] | 28 | [this] () {}; // expected-error {{'this' cannot be captured in this context}} |
Eli Friedman | e81d7e9 | 2012-01-07 01:08:17 +0000 | [diff] [blame] | 29 | } |
| 30 | } |
Eli Friedman | 84b007f | 2012-01-26 03:00:14 +0000 | [diff] [blame] | 31 | |
| 32 | namespace ReturnDeduction { |
| 33 | void test() { |
Douglas Gregor | b326ca8 | 2012-02-09 08:26:42 +0000 | [diff] [blame] | 34 | [](){ return 1; }; |
| 35 | [](){ return 1; }; |
| 36 | [](){ return ({return 1; 1;}); }; |
Douglas Gregor | 54042f1 | 2012-02-09 10:18:50 +0000 | [diff] [blame] | 37 | [](){ return ({return 'c'; 1;}); }; // expected-error {{must match previous return type}} \ |
| 38 | // expected-warning{{omitted result type}} |
Douglas Gregor | b326ca8 | 2012-02-09 08:26:42 +0000 | [diff] [blame] | 39 | []()->int{ return 'c'; return 1; }; |
| 40 | [](){ return 'c'; return 1; }; // expected-error {{must match previous return type}} |
| 41 | []() { return; return (void)0; }; |
Douglas Gregor | 54042f1 | 2012-02-09 10:18:50 +0000 | [diff] [blame] | 42 | [](){ return 1; return 1; }; // expected-warning{{omitted result type}} |
Eli Friedman | 84b007f | 2012-01-26 03:00:14 +0000 | [diff] [blame] | 43 | } |
| 44 | } |
Eli Friedman | b942cb2 | 2012-02-03 22:47:37 +0000 | [diff] [blame] | 45 | |
| 46 | namespace ImplicitCapture { |
| 47 | void test() { |
Eli Friedman | cefc7b2 | 2012-02-03 23:06:43 +0000 | [diff] [blame] | 48 | int a = 0; // expected-note 5 {{declared}} |
Douglas Gregor | b326ca8 | 2012-02-09 08:26:42 +0000 | [diff] [blame] | 49 | []() { 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 Friedman | b942cb2 | 2012-02-03 22:47:37 +0000 | [diff] [blame] | 58 | |
| 59 | const int b = 2; |
Douglas Gregor | b326ca8 | 2012-02-09 08:26:42 +0000 | [diff] [blame] | 60 | []() { return b; }; |
Eli Friedman | b942cb2 | 2012-02-03 22:47:37 +0000 | [diff] [blame] | 61 | |
| 62 | union { // expected-note {{declared}} |
| 63 | int c; |
| 64 | float d; |
| 65 | }; |
| 66 | d = 3; |
Douglas Gregor | b326ca8 | 2012-02-09 08:26:42 +0000 | [diff] [blame] | 67 | [=]() { return c; }; // expected-error {{unnamed variable cannot be implicitly captured in a lambda expression}} |
Eli Friedman | b942cb2 | 2012-02-03 22:47:37 +0000 | [diff] [blame] | 68 | |
Eli Friedman | cefc7b2 | 2012-02-03 23:06:43 +0000 | [diff] [blame] | 69 | __block int e; // expected-note 3 {{declared}} |
Douglas Gregor | b326ca8 | 2012-02-09 08:26:42 +0000 | [diff] [blame] | 70 | [&]() { 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 Friedman | b942cb2 | 2012-02-03 22:47:37 +0000 | [diff] [blame] | 72 | |
| 73 | int f[10]; // expected-note {{declared}} |
Douglas Gregor | b326ca8 | 2012-02-09 08:26:42 +0000 | [diff] [blame] | 74 | [&]() { return f[2]; }; |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame^] | 75 | (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 Friedman | b942cb2 | 2012-02-03 22:47:37 +0000 | [diff] [blame] | 77 | |
| 78 | struct G { G(); G(G&); int a; }; // expected-note 6 {{not viable}} |
| 79 | G g; |
Douglas Gregor | 54042f1 | 2012-02-09 10:18:50 +0000 | [diff] [blame] | 80 | [=]() { const G* gg = &g; return gg->a; }; // expected-warning{{omitted result type}} |
| 81 | [=]() { return [=]{ const G* gg = &g; return gg->a; }(); }; // expected-error {{no matching constructor for initialization of 'const ImplicitCapture::G'}} \ |
| 82 | // 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 Friedman | 210386e | 2012-02-06 21:50:18 +0000 | [diff] [blame] | 85 | |
| 86 | const int h = a; // expected-note {{declared}} |
Douglas Gregor | b326ca8 | 2012-02-09 08:26:42 +0000 | [diff] [blame] | 87 | []() { return h; }; // expected-error {{variable 'h' cannot be implicitly captured in a lambda with no capture-default specified}} expected-note {{lambda expression begins here}} |
Eli Friedman | b942cb2 | 2012-02-03 22:47:37 +0000 | [diff] [blame] | 88 | } |
| 89 | } |