Home | History | Annotate | Download | only in expr.prim.lambda
      1 // RUN: %clang_cc1 -fsyntax-only -std=c++1y %s -verify
      2 
      3 int a;
      4 int &b = [] (int &r) -> decltype(auto) { return r; } (a);
      5 int &c = [] (int &r) -> decltype(auto) { return (r); } (a);
      6 int &d = [] (int &r) -> auto & { return r; } (a);
      7 int &e = [] (int &r) -> auto { return r; } (a); // expected-error {{cannot bind to a temporary}}
      8 int &f = [] (int r) -> decltype(auto) { return r; } (a); // expected-error {{cannot bind to a temporary}}
      9 int &g = [] (int r) -> decltype(auto) { return (r); } (a); // expected-warning {{reference to stack}}
     10 
     11 
     12 int test_explicit_auto_return()
     13 {
     14     struct X {};
     15     auto L = [](auto F, auto a) { return F(a); };
     16     auto M = [](auto a) -> auto { return a; }; // OK
     17     auto MRef = [](auto b) -> auto& { return b; }; //expected-warning{{reference to stack}}
     18     auto MPtr = [](auto c) -> auto* { return &c; }; //expected-warning{{address of stack}}
     19     auto MDeclType = [](auto&& d) -> decltype(auto) { return static_cast<decltype(d)>(d); }; //OK
     20     M(3);
     21 
     22     auto &&x = MDeclType(X{});
     23     auto &&x1 = M(X{});
     24     auto &&x2 = MRef(X{});//expected-note{{in instantiation of}}
     25     auto &&x3 = MPtr(X{}); //expected-note{{in instantiation of}}
     26     return 0;
     27 }
     28 
     29 int test_implicit_auto_return()
     30 {
     31   {
     32     auto M = [](auto a) { return a; };
     33     struct X {};
     34     X x = M(X{});
     35 
     36   }
     37 }
     38 
     39 int test_multiple_returns()  {
     40     auto M = [](auto a) {
     41       bool k;
     42       if (k)
     43         return a;
     44       else
     45         return 5; //expected-error{{deduced as 'int' here}}
     46     };
     47     M(3); // OK
     48     M('a'); //expected-note{{in instantiation of}}
     49   return 0;
     50 }
     51 int test_no_parameter_list()
     52 {
     53   static int si = 0;
     54     auto M = [] { return 5; }; // OK
     55     auto M2 = [] -> auto&& { return si; }; // expected-error{{lambda requires '()'}}
     56     M();
     57 }
     58 
     59 int test_conditional_in_return() {
     60   auto Fac = [](auto f, auto n) {
     61     return n <= 0 ? n : f(f, n - 1) * n;
     62   };
     63   // FIXME: this test causes a recursive limit - need to error more gracefully.
     64   //Fac(Fac, 3);
     65 
     66 }