llvm-project/clang/test/SemaCXX/cxx1z-constexpr-lambdas.cpp
Faisal Vali a734ab9808 [Cxx1z-constexpr-lambda-P0170R1] Support parsing of constexpr specifier (and its inference) on lambda expressions
Support the constexpr specifier on lambda expressions - and support its inference from the lambda call operator's body.

i.e.
  auto L = [] () constexpr { return 5; };
  static_assert(L() == 5); // OK
  auto Implicit = [] (auto a) { return a; };
  static_assert(Implicit(5) == 5); 

We do not support evaluation of lambda's within constant expressions just yet.

Implementation Strategy:
  - teach ParseLambdaExpressionAfterIntroducer to expect a constexpr specifier and mark the invented function call operator's declarator's decl-specifier with it; Have it emit fixits for multiple decl-specifiers (mutable or constexpr) in this location.
  - for cases where constexpr is not explicitly specified, have buildLambdaExpr check whether the invented function call operator satisfies the requirements of a constexpr function, by calling CheckConstexprFunctionDecl/Body.

Much obliged to Richard Smith for his patience and his care, in ensuring the code is clang-worthy.

llvm-svn: 264513
2016-03-26 16:11:37 +00:00

36 lines
1.2 KiB
C++

// RUN: %clang_cc1 -std=c++1z -verify -fsyntax-only -fblocks %s
// RUN: %clang_cc1 -std=c++1z -verify -fsyntax-only -fblocks -fdelayed-template-parsing %s
// RUN: %clang_cc1 -std=c++1z -verify -fsyntax-only -fblocks -fms-extensions %s
// RUN: %clang_cc1 -std=c++1z -verify -fsyntax-only -fblocks -fdelayed-template-parsing -fms-extensions %s
namespace test_constexpr_checking {
namespace ns1 {
struct NonLit { ~NonLit(); }; //expected-note{{not literal}}
auto L = [](NonLit NL) constexpr { }; //expected-error{{not a literal type}}
} // end ns1
namespace ns2 {
auto L = [](int I) constexpr { asm("non-constexpr"); }; //expected-error{{not allowed in constexpr function}}
} // end ns1
} // end ns test_constexpr_checking
namespace test_constexpr_call {
namespace ns1 {
auto L = [](int I) { return I; };
static_assert(L(3) == 3);
} // end ns1
namespace ns2 {
auto L = [](auto a) { return a; };
static_assert(L(3) == 3);
static_assert(L(3.14) == 3.14);
}
namespace ns3 {
auto L = [](auto a) { asm("non-constexpr"); return a; }; //expected-note{{declared here}}
constexpr int I = //expected-error{{must be initialized by a constant expression}}
L(3); //expected-note{{non-constexpr function}}
}
} // end ns test_constexpr_call