brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.4 KiB · a5e4b6a Raw
73 lines · cpp
1// RUN: %clang_cc1 -std=c++23 -fsyntax-only -verify=expected,cxx23    %s2// RUN: %clang_cc1 -std=c++20 -fsyntax-only -verify=expected,cxx14_20 %s3// RUN: %clang_cc1 -std=c++14 -fsyntax-only -verify=expected,cxx14_20 %s4 5int a;6int &b = [] (int &r) -> decltype(auto) { return r; } (a);7int &c = [] (int &r) -> decltype(auto) { return (r); } (a);8int &d = [] (int &r) -> auto & { return r; } (a);9int &e = [] (int &r) -> auto { return r; } (a); // expected-error {{cannot bind to a temporary}}10int &f = [] (int r) -> decltype(auto) { return r; } (a); // expected-error {{non-const lvalue reference to type 'int' cannot bind to a temporary of type 'int'}}11int &g = [] (int r) -> decltype(auto) { return (r); } (a); // expected-warning {{reference to stack}}12// cxx23-error@-1 {{non-const lvalue reference to type 'int' cannot bind to a temporary of type 'int'}}13 14int test_explicit_auto_return()15{16    struct X {};17    auto L = [](auto F, auto a) { return F(a); };18    auto M = [](auto a) -> auto { return a; }; // OK19    auto MRef = [](auto b) -> auto & { return b; }; //cxx14_20-warning{{reference to stack}}20    // cxx23-error@-1 {{non-const lvalue reference to type 'X' cannot bind to a temporary of type 'X'}}21    auto MPtr = [](auto c) -> auto* { return &c; }; //expected-warning{{address of stack}}22    auto MDeclType = [](auto&& d) -> decltype(auto) { return static_cast<decltype(d)>(d); }; //OK23    M(3);24 25    auto &&x = MDeclType(X{});26    auto &&x1 = M(X{});27    auto &&x2 = MRef(X{});//expected-note{{in instantiation of}}28    auto &&x3 = MPtr(X{}); //expected-note{{in instantiation of}}29    return 0;30}31 32int test_implicit_auto_return()33{34  {35    auto M = [](auto a) { return a; };36    struct X {};37    X x = M(X{});38 39  }40}41 42int test_multiple_returns()  {43    auto M = [](auto a) {44      bool k;45      if (k)46        return a;47      else48        return 5; //expected-error{{deduced as 'int' here}}49    };50    M(3); // OK51    M('a'); //expected-note{{in instantiation of}}52  return 0;53}54int test_no_parameter_list()55{56  static int si = 0;57    auto M = [] { return 5; }; // OK58    auto M2 = [] -> auto && { return si; };59#if __cplusplus <= 202002L60      // expected-warning@-2{{is a C++23 extension}}61#endif62    M();63}64 65int test_conditional_in_return() {66  auto Fac = [](auto f, auto n) {67    return n <= 0 ? n : f(f, n - 1) * n;68  };69  // FIXME: this test causes a recursive limit - need to error more gracefully.70  //Fac(Fac, 3);71 72}73