[Clang] Permit static constexpr variables in constexpr functions
authorCorentin Jabot <corentinjabot@gmail.com>
Mon, 28 Nov 2022 20:05:07 +0000 (21:05 +0100)
committerCorentin Jabot <corentinjabot@gmail.com>
Mon, 28 Nov 2022 20:38:31 +0000 (21:38 +0100)
This implement the C++23 paper P2647R1 (adopted in Kona)

Reviewed By: #clang-language-wg, erichkeane

Differential Revision: https://reviews.llvm.org/D138851

clang/docs/ReleaseNotes.rst
clang/lib/AST/ExprConstant.cpp
clang/lib/Frontend/InitPreprocessor.cpp
clang/test/Lexer/cxx-features.cpp
clang/test/SemaCXX/constant-expression-cxx2b.cpp
clang/www/cxx_status.html

index 3dcd65e..3f99bc2 100644 (file)
@@ -662,7 +662,8 @@ C++2b Feature Support
 - Support label at end of compound statement (`P2324 <https://wg21.link/p2324r2>`_).
 - Implemented `P1169R4: static operator() <https://wg21.link/P1169R4>`_.
 - Implemented "char8_t Compatibility and Portability Fix" (`P2513R3 <https://wg21.link/P2513R3>`_).
-  This Change was applied to C++20 as a Defect Report.
+  This change was applied to C++20 as a Defect Report.
+- Implemented "Permitting static constexpr variables in constexpr functions" (`P2647R1 <https://wg21.link/P2647R1>_`).
 
 CUDA/HIP Language Changes in Clang
 ----------------------------------
index df8b420..08130e0 100644 (file)
@@ -5049,8 +5049,10 @@ static EvalStmtResult EvaluateSwitch(StmtResult &Result, EvalInfo &Info,
 static bool CheckLocalVariableDeclaration(EvalInfo &Info, const VarDecl *VD) {
   // An expression E is a core constant expression unless the evaluation of E
   // would evaluate one of the following: [C++2b] - a control flow that passes
-  // through a declaration of a variable with static or thread storage duration.
-  if (VD->isLocalVarDecl() && VD->isStaticLocal()) {
+  // through a declaration of a variable with static or thread storage duration
+  // unless that variable is usable in constant expressions.
+  if (VD->isLocalVarDecl() && VD->isStaticLocal() &&
+      !VD->isUsableInConstantExpressions(Info.Ctx)) {
     Info.CCEDiag(VD->getLocation(), diag::note_constexpr_static_local)
         << (VD->getTSCSpec() == TSCS_unspecified ? 0 : 1) << VD;
     return false;
index bdff0bd..53a5f13 100644 (file)
@@ -606,7 +606,7 @@ static void InitializeCPlusPlusFeatureTestMacros(const LangOptions &LangOpts,
     Builder.defineMacro("__cpp_unicode_literals", "200710L");
     Builder.defineMacro("__cpp_user_defined_literals", "200809L");
     Builder.defineMacro("__cpp_lambdas", "200907L");
-    Builder.defineMacro("__cpp_constexpr", LangOpts.CPlusPlus2b   ? "202110L"
+    Builder.defineMacro("__cpp_constexpr", LangOpts.CPlusPlus2b   ? "202211L"
                                            : LangOpts.CPlusPlus20 ? "201907L"
                                            : LangOpts.CPlusPlus17 ? "201603L"
                                            : LangOpts.CPlusPlus14 ? "201304L"
index c12f2d2..e577801 100644 (file)
 #error "wrong value for __cpp_lambdas"
 #endif
 
-#if check(constexpr, 0, 200704, 201304, 201603, 201907, 202110)
+#if check(constexpr, 0, 200704, 201304, 201603, 201907, 202211)
 #error "wrong value for __cpp_constexpr"
 #endif
 
index 5a5fe6c..e52dc14 100644 (file)
@@ -6,6 +6,8 @@ struct NonLiteral { // cxx2a-note {{'NonLiteral' is not literal}} \
   NonLiteral() {}
 };
 
+struct Constexpr{};
+
 #if __cplusplus > 202002L
 
 constexpr int f(int n) {  // expected-error {{constexpr function never produces a constant expression}}
@@ -82,15 +84,15 @@ constexpr int k_evaluated(int n) {
 constexpr int ke = k_evaluated(1); // expected-error {{constexpr variable 'ke' must be initialized by a constant expression}} \
                                    // expected-note {{in call}}
 
-constexpr int static_constexpr() { // expected-error {{constexpr function never produces a constant expression}}
-  static constexpr int m = 42;     // expected-note {{control flows through the definition of a static variable}} \
-                                   // cxx2b-warning {{definition of a static variable in a constexpr function is incompatible with C++ standards before C++2b}}
+constexpr int static_constexpr() {
+  static constexpr int m = 42;     // cxx2b-warning {{definition of a static variable in a constexpr function is incompatible with C++ standards before C++2b}}
+  static constexpr Constexpr foo; // cxx2b-warning {{definition of a static variable in a constexpr function is incompatible with C++ standards before C++2b}}
   return m;
 }
 
-constexpr int thread_local_constexpr() { // expected-error {{constexpr function never produces a constant expression}}
-  thread_local constexpr int m = 42;     // expected-note {{control flows through the definition of a thread_local variable}} \
-                                         // cxx2b-warning {{definition of a thread_local variable in a constexpr function is incompatible with C++ standards before C++2b}}
+constexpr int thread_local_constexpr() {
+  thread_local constexpr int m = 42; // cxx2b-warning {{definition of a thread_local variable in a constexpr function is incompatible with C++ standards before C++2b}}
+  thread_local constexpr Constexpr foo; // cxx2b-warning {{definition of a thread_local variable in a constexpr function is incompatible with C++ standards before C++2b}}
   return m;
 }
 
@@ -135,9 +137,8 @@ constexpr int d = label();
 // Test that explicitly constexpr lambdas behave correctly,
 // This is to be contrasted with the test for implicitly constexpr lambdas below.
 int test_in_lambdas() {
-  auto a = []() constexpr {  // expected-error{{constexpr function never produces a constant expression}}
-    static const int m = 32; // expected-note {{control flows through the definition of a static variable}} \
-                             // cxx2b-warning {{definition of a static variable in a constexpr function is incompatible with C++ standards before C++2b}}
+  auto a = []() constexpr {
+    static const int m = 32; // cxx2b-warning {{definition of a static variable in a constexpr function is incompatible with C++ standards before C++2b}}
     return m;
   };
 
@@ -240,3 +241,16 @@ constexpr auto dependent_var_def_lambda() {
 constexpr auto non_literal_valid_in_cxx2b = dependent_var_def_lambda<NonLiteral>()(true); // \
     // cxx2a-error {{constexpr variable 'non_literal_valid_in_cxx2b' must be initialized by a constant expression}} \
     // cxx2a-note {{non-constexpr function}}
+
+
+constexpr double evaluate_static_constexpr() {
+  struct Constexpr{
+    constexpr double f() const {
+      return 42;
+    }
+  };
+  thread_local constexpr Constexpr t; // cxx2b-warning {{before C++2b}}
+  static constexpr Constexpr s; // cxx2b-warning {{before C++2b}}
+  return t.f() + s.f();
+}
+static_assert(evaluate_static_constexpr() == 84);
index e3b7565..e296d5d 100755 (executable)
@@ -1523,7 +1523,7 @@ C++20, informally referred to as C++2b.</p>
     <tr>
       <td>Permitting static constexpr variables in constexpr functions (DR)</td>
       <td><a href="https://wg21.link/P2647R1">P2647R1</a></td>
-      <td class="none" align="center">No</td>
+      <td class="unreleased" align="center">Clang 16</td>
     </tr>
     <tr>
       <td>consteval needs to propagate up (DR)</td>