Home | History | Annotate | Download | only in SemaCXX
      1 // RUN: %clang_cc1 -std=c++11 -Wno-unused-value -fsyntax-only -verify -fblocks %s
      2 // RUN: %clang_cc1 -std=c++1y -Wno-unused-value -fsyntax-only -verify -fblocks %s
      3 
      4 namespace std { class type_info; };
      5 
      6 namespace ExplicitCapture {
      7   class C {
      8     int Member;
      9 
     10     static void Overload(int);
     11     void Overload();
     12     virtual C& Overload(float);
     13 
     14     void ImplicitThisCapture() {
     15       [](){(void)Member;}; // expected-error {{'this' cannot be implicitly captured in this context}}
     16       [&](){(void)Member;};
     17 
     18       [this](){(void)Member;};
     19       [this]{[this]{};};
     20       []{[this]{};};// expected-error {{'this' cannot be implicitly captured in this context}}
     21       []{Overload(3);};
     22       []{Overload();}; // expected-error {{'this' cannot be implicitly captured in this context}}
     23       []{(void)typeid(Overload());};
     24       []{(void)typeid(Overload(.5f));};// expected-error {{'this' cannot be implicitly captured in this context}}
     25     }
     26   };
     27 
     28   void f() {
     29     [this] () {}; // expected-error {{'this' cannot be captured in this context}}
     30   }
     31 }
     32 
     33 namespace ReturnDeduction {
     34   void test() {
     35     [](){ return 1; };
     36     [](){ return 1; };
     37     [](){ return ({return 1; 1;}); };
     38     [](){ return ({return 'c'; 1;}); }; // expected-error {{must match previous return type}}
     39     []()->int{ return 'c'; return 1; };
     40     [](){ return 'c'; return 1; };  // expected-error {{must match previous return type}}
     41     []() { return; return (void)0; };
     42     [](){ return 1; return 1; };
     43   }
     44 }
     45 
     46 namespace ImplicitCapture {
     47   void test() {
     48     int a = 0; // expected-note 5 {{declared}}
     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; }; }; //
     58 
     59     const int b = 2;
     60     []() { return b; };
     61 
     62     union { // expected-note {{declared}}
     63       int c;
     64       float d;
     65     };
     66     d = 3;
     67     [=]() { return c; }; // expected-error {{unnamed variable cannot be implicitly captured in a lambda expression}}
     68 
     69     __block int e; // expected-note 3 {{declared}}
     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}}
     72 
     73     int f[10]; // expected-note {{declared}}
     74     [&]() { return f[2]; };
     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}}
     77 
     78     struct G { G(); G(G&); int a; }; // expected-note 6 {{not viable}}
     79     G g;
     80     [=]() { const G* gg = &g; return gg->a; };
     81     [=]() { return [=]{ const G* gg = &g; return gg->a; }(); }; // expected-error {{no matching constructor for initialization of 'G'}}
     82     (void)^{ return [=]{ const G* gg = &g; return gg->a; }(); }; // expected-error 2 {{no matching constructor for initialization of 'const G'}}
     83 
     84     const int h = a; // expected-note {{declared}}
     85     []() { return h; }; // expected-error {{variable 'h' cannot be implicitly captured in a lambda with no capture-default specified}} expected-note {{lambda expression begins here}}
     86 
     87     // References can appear in constant expressions if they are initialized by
     88     // reference constant expressions.
     89     int i;
     90     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     static int j;
     94     int &ref_j = j;
     95     [] { return ref_j; }; // ok
     96   }
     97 }
     98 
     99 namespace PR12031 {
    100   struct X {
    101     template<typename T>
    102     X(const T&);
    103     ~X();
    104   };
    105 
    106   void f(int i, X x);
    107   void g() {
    108     const int v = 10;
    109     f(v, [](){});
    110   }
    111 }
    112 
    113 namespace Array {
    114   int &f(int *p);
    115   char &f(...);
    116   void g() {
    117     int n = -1;
    118     [=] {
    119       int arr[n]; // VLA
    120     } ();
    121 
    122     const int m = -1;
    123     [] {
    124       int arr[m]; // expected-error{{negative size}}
    125     } ();
    126 
    127     [&] {
    128       int arr[m]; // expected-error{{negative size}}
    129     } ();
    130 
    131     [=] {
    132       int arr[m]; // expected-error{{negative size}}
    133     } ();
    134 
    135     [m] {
    136       int arr[m]; // expected-error{{negative size}}
    137     } ();
    138   }
    139 }
    140 
    141 void PR12248()
    142 {
    143   unsigned int result = 0;
    144   auto l = [&]() { ++result; };
    145 }
    146 
    147 namespace ModifyingCapture {
    148   void test() {
    149     int n = 0;
    150     [=] {
    151       n = 1; // expected-error {{cannot assign to a variable captured by copy in a non-mutable lambda}}
    152     };
    153   }
    154 }
    155 
    156 namespace VariadicPackExpansion {
    157   template<typename T, typename U> using Fst = T;
    158   template<typename...Ts> bool g(Fst<bool, Ts> ...bools);
    159   template<typename...Ts> bool f(Ts &&...ts) {
    160     return g<Ts...>([&ts] {
    161       if (!ts)
    162         return false;
    163       --ts;
    164       return true;
    165     } () ...);
    166   }
    167   void h() {
    168     int a = 5, b = 2, c = 3;
    169     while (f(a, b, c)) {
    170     }
    171   }
    172 
    173   struct sink {
    174     template<typename...Ts> sink(Ts &&...) {}
    175   };
    176 
    177   template<typename...Ts> void local_class() {
    178     sink {
    179       [] (Ts t) {
    180         struct S : Ts {
    181           void f(Ts t) {
    182             Ts &that = *this;
    183             that = t;
    184           }
    185           Ts g() { return *this; };
    186         };
    187         S s;
    188         s.f(t);
    189         return s;
    190       } (Ts()).g() ...
    191     };
    192   };
    193   struct X {}; struct Y {};
    194   template void local_class<X, Y>();
    195 
    196   template<typename...Ts> void nested(Ts ...ts) {
    197     f(
    198       // Each expansion of this lambda implicitly captures all of 'ts', because
    199       // the inner lambda also expands 'ts'.
    200       [&] {
    201         return ts + [&] { return f(ts...); } ();
    202       } () ...
    203     );
    204   }
    205   template void nested(int, int, int);
    206 
    207   template<typename...Ts> void nested2(Ts ...ts) { // expected-note 2{{here}}
    208     // Capture all 'ts', use only one.
    209     f([&ts...] { return ts; } ()...);
    210     // Capture each 'ts', use it.
    211     f([&ts] { return ts; } ()...);
    212     // Capture all 'ts', use all of them.
    213     f([&ts...] { return (int)f(ts...); } ());
    214     // Capture each 'ts', use all of them. Ill-formed. In more detail:
    215     //
    216     // We instantiate two lambdas here; the first captures ts$0, the second
    217     // captures ts$1. Both of them reference both ts parameters, so both are
    218     // ill-formed because ts can't be implicitly captured.
    219     //
    220     // FIXME: This diagnostic does not explain what's happening. We should
    221     // specify which 'ts' we're referring to in its diagnostic name. We should
    222     // also say which slice of the pack expansion is being performed in the
    223     // instantiation backtrace.
    224     f([&ts] { return (int)f(ts...); } ()...); // \
    225     // expected-error 2{{'ts' cannot be implicitly captured}} \
    226     // expected-note 2{{lambda expression begins here}}
    227   }
    228   template void nested2(int); // ok
    229   template void nested2(int, int); // expected-note {{in instantiation of}}
    230 }
    231 
    232 namespace PR13860 {
    233   void foo() {
    234     auto x = PR13860UndeclaredIdentifier(); // expected-error {{use of undeclared identifier 'PR13860UndeclaredIdentifier'}}
    235     auto y = [x]() { };
    236     static_assert(sizeof(y), "");
    237   }
    238 }
    239 
    240 namespace PR13854 {
    241   auto l = [](void){};
    242 }
    243 
    244 namespace PR14518 {
    245   auto f = [](void) { return __func__; }; // no-warning
    246 }
    247 
    248 namespace PR16708 {
    249   auto L = []() {
    250     auto ret = 0;
    251     return ret;
    252     return 0;
    253   };
    254 }
    255 
    256 namespace TypeDeduction {
    257   struct S {};
    258   void f() {
    259     const S s {};
    260     S &&t = [&] { return s; } ();
    261 #if __cplusplus <= 201103L
    262     // expected-error@-2 {{drops qualifiers}}
    263 #else
    264     S &&u = [&] () -> auto { return s; } ();
    265 #endif
    266   }
    267 }
    268 
    269 
    270 namespace lambdas_in_NSDMIs {
    271   template<class T>
    272   struct L {
    273       T t{};
    274       T t2 = ([](int a) { return [](int b) { return b; };})(t)(t);
    275   };
    276   L<int> l;
    277 
    278   namespace non_template {
    279     struct L {
    280       int t = 0;
    281       int t2 = ([](int a) { return [](int b) { return b; };})(t)(t);
    282     };
    283     L l;
    284   }
    285 }
    286 
    287 // PR18477: don't try to capture 'this' from an NSDMI encountered while parsing
    288 // a lambda.
    289 namespace NSDMIs_in_lambdas {
    290   template<typename T> struct S { int a = 0; int b = a; };
    291   void f() { []() { S<int> s; }; }
    292 
    293   auto x = []{ struct S { int n, m = n; }; };
    294   auto y = [&]{ struct S { int n, m = n; }; }; // expected-error {{non-local lambda expression cannot have a capture-default}}
    295   void g() { auto z = [&]{ struct S { int n, m = n; }; }; }
    296 }
    297 
    298 namespace CaptureIncomplete {
    299   struct Incomplete; // expected-note 2{{forward decl}}
    300   void g(const Incomplete &a);
    301   void f(Incomplete &a) {
    302     (void) [a] {}; // expected-error {{incomplete}}
    303     (void) [&a] {};
    304 
    305     (void) [=] { g(a); }; // expected-error {{incomplete}}
    306     (void) [&] { f(a); };
    307   }
    308 }
    309 
    310 namespace CaptureAbstract {
    311   struct S {
    312     virtual void f() = 0; // expected-note {{unimplemented}}
    313     int n = 0;
    314   };
    315   struct T : S {
    316     constexpr T() {}
    317     void f();
    318   };
    319   void f() {
    320     constexpr T t = T();
    321     S &s = const_cast<T&>(t);
    322     // FIXME: Once we properly compute odr-use per DR712, this should be
    323     // accepted (and should not capture 's').
    324     [=] { return s.n; }; // expected-error {{abstract}}
    325   }
    326 }
    327 
    328 namespace PR18128 {
    329   auto l = [=]{}; // expected-error {{non-local lambda expression cannot have a capture-default}}
    330 
    331   struct S {
    332     int n;
    333     int (*f())[true ? 1 : ([=]{ return n; }(), 0)];
    334     // expected-error@-1 {{non-local lambda expression cannot have a capture-default}}
    335     // expected-error@-2 {{invalid use of non-static data member 'n'}}
    336     // expected-error@-3 {{a lambda expression may not appear inside of a constant expression}}
    337     int g(int k = ([=]{ return n; }(), 0));
    338     // expected-error@-1 {{non-local lambda expression cannot have a capture-default}}
    339     // expected-error@-2 {{invalid use of non-static data member 'n'}}
    340 
    341     int a = [=]{ return n; }(); // ok
    342     int b = [=]{ return [=]{ return n; }(); }(); // ok
    343     int c = []{ int k = 0; return [=]{ return k; }(); }(); // ok
    344     int d = []{ return [=]{ return n; }(); }(); // expected-error {{'this' cannot be implicitly captured in this context}}
    345   };
    346 }
    347 
    348 namespace PR18473 {
    349   template<typename T> void f() {
    350     T t(0);
    351     (void) [=]{ int n = t; }; // expected-error {{deleted}}
    352   }
    353 
    354   template void f<int>();
    355   struct NoCopy {
    356     NoCopy(int);
    357     NoCopy(const NoCopy &) = delete; // expected-note {{deleted}}
    358     operator int() const;
    359   };
    360   template void f<NoCopy>(); // expected-note {{instantiation}}
    361 }
    362 
    363 void PR19249() {
    364   auto x = [&x]{}; // expected-error {{cannot appear in its own init}}
    365 }
    366