c++: Implement C++23 P2647R1 - Permitting static constexpr variables in constexpr functions

Message ID Y26BNiMCgUMaOpW5@tucnak
State Unresolved
Headers
Series c++: Implement C++23 P2647R1 - Permitting static constexpr variables in constexpr functions |

Checks

Context Check Description
snail/gcc-patch-check warning Git am fail log

Commit Message

Jakub Jelinek Nov. 11, 2022, 5:07 p.m. UTC
  Hi!

The following patch on top of Marek's P2448 PR106649 patch
(mainly because that patch implements the previous __cpp_constexpr
feature test macro bump so this can't go in earlier; OT,
P2280R4 doesn't have any feature test macro?) implements this
simple paper.

Ok for trunk if it passes bootstrap/regtest and is voted into C++23?

2022-11-11  Jakub Jelinek  <jakub@redhat.com>

gcc/c-family/
	* c-cppbuiltin.cc (c_cpp_builtins): Bump __cpp_constexpr
	value from 202207L to 202211L.
gcc/cp/
	* constexpr.cc (cxx_eval_constant_expression): Implement C++23
	P2647R1 - Permitting static constexpr variables in constexpr functions.
	Allow decl_maybe_constant_var_p static or thread_local vars for
	C++23.
	(potential_constant_expression_1): Likewise.
gcc/testsuite/
	* g++.dg/cpp23/constexpr-nonlit17.C: New test.
	* g++.dg/cpp23/feat-cxx2b.C: Adjust expected __cpp_constexpr
	value.


	Jakub
  

Patch

--- gcc/c-family/c-cppbuiltin.cc.jj	2022-11-11 17:14:52.021613271 +0100
+++ gcc/c-family/c-cppbuiltin.cc	2022-11-11 17:17:45.065265246 +0100
@@ -1074,7 +1074,7 @@  c_cpp_builtins (cpp_reader *pfile)
 	  /* Set feature test macros for C++23.  */
 	  cpp_define (pfile, "__cpp_size_t_suffix=202011L");
 	  cpp_define (pfile, "__cpp_if_consteval=202106L");
-	  cpp_define (pfile, "__cpp_constexpr=202207L");
+	  cpp_define (pfile, "__cpp_constexpr=202211L");
 	  cpp_define (pfile, "__cpp_multidimensional_subscript=202110L");
 	  cpp_define (pfile, "__cpp_named_character_escapes=202207L");
 	  cpp_define (pfile, "__cpp_static_call_operator=202207L");
--- gcc/cp/constexpr.cc.jj	2022-11-11 17:14:52.024613231 +0100
+++ gcc/cp/constexpr.cc	2022-11-11 17:16:54.384952917 +0100
@@ -7085,7 +7085,8 @@  cxx_eval_constant_expression (const cons
 	    && (TREE_STATIC (r)
 		|| (CP_DECL_THREAD_LOCAL_P (r) && !DECL_REALLY_EXTERN (r)))
 	    /* Allow __FUNCTION__ etc.  */
-	    && !DECL_ARTIFICIAL (r))
+	    && !DECL_ARTIFICIAL (r)
+	    && (cxx_dialect < cxx23 || !decl_maybe_constant_var_p (r)))
 	  {
 	    if (!ctx->quiet)
 	      {
@@ -9577,7 +9578,10 @@  potential_constant_expression_1 (tree t,
       tmp = DECL_EXPR_DECL (t);
       if (VAR_P (tmp) && !DECL_ARTIFICIAL (tmp))
 	{
-	  if (CP_DECL_THREAD_LOCAL_P (tmp) && !DECL_REALLY_EXTERN (tmp))
+	  if (CP_DECL_THREAD_LOCAL_P (tmp)
+	      && !DECL_REALLY_EXTERN (tmp)
+	      && (cxx_dialect < cxx23
+		  || !decl_maybe_constant_var_p (tmp)))
 	    {
 	      if (flags & tf_error)
 		constexpr_error (DECL_SOURCE_LOCATION (tmp), fundef_p,
@@ -9585,7 +9589,9 @@  potential_constant_expression_1 (tree t,
 				 "%<constexpr%> context", tmp);
 	      return false;
 	    }
-	  else if (TREE_STATIC (tmp))
+	  else if (TREE_STATIC (tmp)
+		   && (cxx_dialect < cxx23
+		       || !decl_maybe_constant_var_p (tmp)))
 	    {
 	      if (flags & tf_error)
 		constexpr_error (DECL_SOURCE_LOCATION (tmp), fundef_p,
--- gcc/testsuite/g++.dg/cpp23/constexpr-nonlit17.C.jj	2022-11-11 17:59:59.972852793 +0100
+++ gcc/testsuite/g++.dg/cpp23/constexpr-nonlit17.C	2022-11-11 17:59:38.725141231 +0100
@@ -0,0 +1,12 @@ 
+// P2647R1 - Permitting static constexpr variables in constexpr functions
+// { dg-do compile { target c++23 } }
+
+constexpr char
+test ()
+{
+  static const int x = 5;
+  static constexpr char c[] = "Hello World";
+  return *(c + x);
+}
+
+static_assert (test () == ' ');
--- gcc/testsuite/g++.dg/cpp23/feat-cxx2b.C.jj	2022-11-11 17:14:52.194610922 +0100
+++ gcc/testsuite/g++.dg/cpp23/feat-cxx2b.C	2022-11-11 17:48:56.038865825 +0100
@@ -134,8 +134,8 @@ 
 
 #ifndef __cpp_constexpr
 #  error "__cpp_constexpr"
-#elif __cpp_constexpr != 202207
-#  error "__cpp_constexpr != 202207"
+#elif __cpp_constexpr != 202211
+#  error "__cpp_constexpr != 202211"
 #endif
 
 #ifndef __cpp_decltype_auto