@@ -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=202211L");
cpp_define (pfile, "__cpp_named_character_escapes=202207L");
cpp_define (pfile, "__cpp_static_call_operator=202207L");
@@ -7098,7 +7098,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)
+ && !decl_constant_var_p (r))
{
if (!ctx->quiet)
{
@@ -9586,7 +9587,10 @@ potential_constant_expression_1 (tree t,
case DECL_EXPR:
tmp = DECL_EXPR_DECL (t);
- if (VAR_P (tmp) && !DECL_ARTIFICIAL (tmp))
+ if (VAR_P (tmp) && !DECL_ARTIFICIAL (tmp)
+ && (processing_template_decl
+ ? !decl_maybe_constant_var_p (tmp)
+ : !decl_constant_var_p (tmp)))
{
if (CP_DECL_THREAD_LOCAL_P (tmp) && !DECL_REALLY_EXTERN (tmp))
{
@@ -5600,6 +5600,57 @@ groktypename (cp_decl_specifier_seq *typ
return type;
}
+/* For C++20 and older diagnose static or thread_local decls in constexpr
+ or consteval functions. If they are usable in constant expressions,
+ just pedwarn on them. */
+
+static void
+diagnose_static_in_constexpr (tree decl)
+{
+ if (cxx_dialect >= cxx23)
+ return;
+ if (current_function_decl
+ && VAR_P (decl)
+ && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
+ {
+ bool ok = false;
+ if (processing_template_decl
+ ? decl_maybe_constant_var_p (decl)
+ : decl_constant_var_p (decl))
+ {
+ if (CP_DECL_THREAD_LOCAL_P (decl) && !DECL_REALLY_EXTERN (decl))
+ pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
+ "%qD defined %<thread_local%> in %qs function only "
+ "available with %<-std=c++2b%> or %<-std=gnu++2b%>", decl,
+ DECL_IMMEDIATE_FUNCTION_P (current_function_decl)
+ ? "consteval" : "constexpr");
+ else if (TREE_STATIC (decl))
+ pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
+ "%qD defined %<static%> in %qs function only available "
+ "with %<-std=c++2b%> or %<-std=gnu++2b%>", decl,
+ DECL_IMMEDIATE_FUNCTION_P (current_function_decl)
+ ? "consteval" : "constexpr");
+ ok = true;
+ }
+ else if (CP_DECL_THREAD_LOCAL_P (decl) && !DECL_REALLY_EXTERN (decl))
+ error_at (DECL_SOURCE_LOCATION (decl),
+ "%qD defined %<thread_local%> in %qs function only "
+ "available with %<-std=c++2b%> or %<-std=gnu++2b%>", decl,
+ DECL_IMMEDIATE_FUNCTION_P (current_function_decl)
+ ? "consteval" : "constexpr");
+ else if (TREE_STATIC (decl))
+ error_at (DECL_SOURCE_LOCATION (decl),
+ "%qD defined %<static%> in %qs function only available "
+ "with %<-std=c++2b%> or %<-std=gnu++2b%>", decl,
+ DECL_IMMEDIATE_FUNCTION_P (current_function_decl)
+ ? "consteval" : "constexpr");
+ else
+ ok = true;
+ if (!ok)
+ cp_function_chain->invalid_constexpr = true;
+ }
+}
+
/* Process a DECLARATOR for a function-scope or namespace-scope
variable or function declaration.
(Function definitions go through start_function; class member
@@ -5860,29 +5911,6 @@ start_decl (const cp_declarator *declara
DECL_THIS_STATIC (decl) = 1;
}
- if (current_function_decl && VAR_P (decl)
- && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
- && cxx_dialect < cxx23)
- {
- bool ok = false;
- if (CP_DECL_THREAD_LOCAL_P (decl) && !DECL_REALLY_EXTERN (decl))
- error_at (DECL_SOURCE_LOCATION (decl),
- "%qD defined %<thread_local%> in %qs function only "
- "available with %<-std=c++2b%> or %<-std=gnu++2b%>", decl,
- DECL_IMMEDIATE_FUNCTION_P (current_function_decl)
- ? "consteval" : "constexpr");
- else if (TREE_STATIC (decl))
- error_at (DECL_SOURCE_LOCATION (decl),
- "%qD defined %<static%> in %qs function only available "
- "with %<-std=c++2b%> or %<-std=gnu++2b%>", decl,
- DECL_IMMEDIATE_FUNCTION_P (current_function_decl)
- ? "consteval" : "constexpr");
- else
- ok = true;
- if (!ok)
- cp_function_chain->invalid_constexpr = true;
- }
-
if (!processing_template_decl && VAR_P (decl))
start_decl_1 (decl, initialized);
@@ -8424,6 +8452,9 @@ cp_finish_decl (tree decl, tree init, bo
set_user_assembler_name (decl, asmspec);
DECL_HARD_REGISTER (decl) = 1;
}
+
+ diagnose_static_in_constexpr (decl);
+
return;
}
@@ -8749,6 +8780,8 @@ cp_finish_decl (tree decl, tree init, bo
&& !DECL_HARD_REGISTER (decl))
targetm.lower_local_decl_alignment (decl);
+ diagnose_static_in_constexpr (decl);
+
invoke_plugin_callbacks (PLUGIN_FINISH_DECL, decl);
}
@@ -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 () == ' ');
@@ -0,0 +1,49 @@
+// P2647R1 - Permitting static constexpr variables in constexpr functions
+// { dg-do compile { target c++14 } }
+
+constexpr int
+f1 (int x)
+{
+ if (x)
+ throw 1;
+ return 0;
+}
+
+constexpr int
+f2 ()
+{
+ static const int a = f1 (1); // { dg-error "'a' defined 'static' in 'constexpr' function only available with" "" { target c++20_down } }
+ return 0;
+}
+
+constexpr int
+f3 ()
+{
+ static const int a = 5; // { dg-error "'a' defined 'static' in 'constexpr' function only available with" "" { target c++20_down } }
+ return 0;
+}
+
+constexpr int
+f4 () // { dg-message "declared here" "" { target c++20_down } }
+{ // { dg-message "is not usable as a 'constexpr' function because:" "" { target c++23 } .-1 }
+ static const int a = f1 (1); // { dg-error "'a' defined 'static' in 'constexpr' function only available with" "" { target c++20_down } }
+ return 0; // { dg-error "'a' defined 'static' in 'constexpr' context" "" { target c++23 } .-1 }
+}
+
+constexpr int a4 = f4 (); // { dg-error "called in a constant expression" }
+
+constexpr int
+f5 ()
+{
+ static const int a = f1 (0); // { dg-error "'a' defined 'static' in 'constexpr' function only available with" "" { target c++20_down } }
+ return 0;
+}
+
+constexpr int
+f6 ()
+{
+ static const int a = f1 (0); // { dg-error "'a' defined 'static' in 'constexpr' function only available with" "" { target c++20_down } }
+ return 0;
+}
+
+constexpr int a6 = f6 ();
@@ -0,0 +1,50 @@
+// P2647R1 - Permitting static constexpr variables in constexpr functions
+// { dg-do compile { target c++14 } }
+// { dg-options "-pedantic" }
+
+constexpr int
+f1 (int x)
+{
+ if (x)
+ throw 1;
+ return 0;
+}
+
+constexpr int
+f2 ()
+{
+ static const int a = f1 (1); // { dg-error "'a' defined 'static' in 'constexpr' function only available with" "" { target c++20_down } }
+ return 0;
+}
+
+constexpr int
+f3 ()
+{
+ static const int a = 5; // { dg-warning "'a' defined 'static' in 'constexpr' function only available with" "" { target c++20_down } }
+ return 0;
+}
+
+constexpr int
+f4 () // { dg-message "declared here" "" { target c++20_down } }
+{ // { dg-message "is not usable as a 'constexpr' function because:" "" { target c++23 } .-1 }
+ static const int a = f1 (1); // { dg-error "'a' defined 'static' in 'constexpr' function only available with" "" { target c++20_down } }
+ return 0; // { dg-error "'a' defined 'static' in 'constexpr' context" "" { target c++23 } .-1 }
+}
+
+constexpr int a4 = f4 (); // { dg-error "called in a constant expression" }
+
+constexpr int
+f5 ()
+{
+ static const int a = f1 (0); // { dg-warning "'a' defined 'static' in 'constexpr' function only available with" "" { target c++20_down } }
+ return 0;
+}
+
+constexpr int
+f6 ()
+{
+ static const int a = f1 (0); // { dg-warning "'a' defined 'static' in 'constexpr' function only available with" "" { target c++20_down } }
+ return 0;
+}
+
+constexpr int a6 = f6 ();
@@ -0,0 +1,50 @@
+// P2647R1 - Permitting static constexpr variables in constexpr functions
+// { dg-do compile { target c++14 } }
+// { dg-options "-Wno-pedantic" }
+
+constexpr int
+f1 (int x)
+{
+ if (x)
+ throw 1;
+ return 0;
+}
+
+constexpr int
+f2 ()
+{
+ static const int a = f1 (1); // { dg-error "'a' defined 'static' in 'constexpr' function only available with" "" { target c++20_down } }
+ return 0;
+}
+
+constexpr int
+f3 ()
+{
+ static const int a = 5;
+ return 0;
+}
+
+constexpr int
+f4 () // { dg-message "declared here" "" { target c++20_down } }
+{ // { dg-message "is not usable as a 'constexpr' function because:" "" { target c++23 } .-1 }
+ static const int a = f1 (1); // { dg-error "'a' defined 'static' in 'constexpr' function only available with" "" { target c++20_down } }
+ return 0; // { dg-error "'a' defined 'static' in 'constexpr' context" "" { target c++23 } .-1 }
+}
+
+constexpr int a4 = f4 (); // { dg-error "called in a constant expression" }
+
+constexpr int
+f5 ()
+{
+ static const int a = f1 (0);
+ return 0;
+}
+
+constexpr int
+f6 ()
+{
+ static const int a = f1 (0);
+ return 0;
+}
+
+constexpr int a6 = f6 ();
@@ -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
@@ -8,7 +8,7 @@ const test* setup()
{
static constexpr test atest =
{
- ({ static const int inner = 123; &inner; }) // { dg-error "static" }
+ ({ static const int inner = 123; &inner; })
};
return &atest;
@@ -0,0 +1,17 @@
+// PR c++/81073
+// { dg-options "" }
+// { dg-do compile { target c++11 } }
+
+struct test { const int *addr; };
+
+const test* setup()
+{
+ static constexpr test atest =
+ {
+ ({ static const int inner = (throw 1, 1); &inner; }) // { dg-error "static" "" }
+ };
+
+ return &atest;
+}
+
+int main(){}