aboutsummaryrefslogtreecommitdiff
path: root/gcc/cp/constexpr.c
diff options
context:
space:
mode:
authorJakub Jelinek <jakub@redhat.com>2021-10-06 10:28:31 +0200
committerJakub Jelinek <jakub@redhat.com>2021-10-06 10:28:31 +0200
commit8892d532d66910e518bc135a851a104322385ca2 (patch)
tree4de92e23189314aa0778d012923888b88a9c4a06 /gcc/cp/constexpr.c
parentf43eb7707c06e8824d07d5c87ed2019d796fa8a0 (diff)
downloadgcc-8892d532d66910e518bc135a851a104322385ca2.zip
gcc-8892d532d66910e518bc135a851a104322385ca2.tar.gz
gcc-8892d532d66910e518bc135a851a104322385ca2.tar.bz2
c++: Implement C++23 P2242R3 - Non-literal variables (and labels and gotos) in constexpr functions [PR102612]
The following patch implements C++23 P2242R3 - Non-literal variables (and labels and gotos) in constexpr functions. I think it is mostly straightforward, don't diagnose certain statements/declarations just because of their presence in constexpr/consteval functions, but (except for the non-literal type var declarations which ought to be caught by e.g. constructor or destructor call during evaluation not being constexpr and for labels which are now always allowed) diagnose it during constexpr evaluation. 2021-10-06 Jakub Jelinek <jakub@redhat.com> PR c++/102612 gcc/c-family/ * c-cppbuiltin.c (c_cpp_builtins): For -std=c++23 predefine __cpp_constexpr to 202110L rather than 201907L. gcc/cp/ * parser.c (cp_parser_jump_statement): Implement C++23 P2242R3. Allow goto expressions in constexpr function bodies for C++23. Adjust error message for older standards to mention it. * decl.c (start_decl): Allow static and thread_local declarations in constexpr function bodies for C++23. Adjust error message for older standards to mention it. * constexpr.c (ensure_literal_type_for_constexpr_object): Allow declarations of variables with non-literal type in constexpr function bodies for C++23. Adjust error message for older standards to mention it. (cxx_eval_constant_expression) <case DECL_EXPR>: Diagnose declarations of initialization of static or thread_local vars. (cxx_eval_constant_expression) <case GOTO_EXPR>: Diagnose goto statements for C++23. (potential_constant_expression_1) <case DECL_EXPR>: Swap the CP_DECL_THREAD_LOCAL_P and TREE_STATIC checks. (potential_constant_expression_1) <case LABEL_EXPR>: Allow labels for C++23. Adjust error message for older standards to mention it. gcc/testsuite/ * g++.dg/cpp23/feat-cxx2b.C: Expect __cpp_constexpr 202110L rather than 201907L. * g++.dg/cpp23/constexpr-nonlit1.C: New test. * g++.dg/cpp23/constexpr-nonlit2.C: New test. * g++.dg/cpp23/constexpr-nonlit3.C: New test. * g++.dg/cpp23/constexpr-nonlit4.C: New test. * g++.dg/cpp23/constexpr-nonlit5.C: New test. * g++.dg/cpp23/constexpr-nonlit6.C: New test. * g++.dg/diagnostic/constexpr1.C: Only expect some diagnostics for c++20_down. * g++.dg/cpp1y/constexpr-label.C: Likewise. * g++.dg/cpp1y/constexpr-neg1.C: Likewise. * g++.dg/cpp2a/constexpr-try5.C: Likewise. Adjust some expected wording. * g++.dg/cpp2a/constexpr-dtor3.C: Likewise. * g++.dg/cpp2a/consteval3.C: Likewise. Add effective target c++20 and remove dg-options.
Diffstat (limited to 'gcc/cp/constexpr.c')
-rw-r--r--gcc/cp/constexpr.c54
1 files changed, 42 insertions, 12 deletions
diff --git a/gcc/cp/constexpr.c b/gcc/cp/constexpr.c
index e95ff00..66d5221 100644
--- a/gcc/cp/constexpr.c
+++ b/gcc/cp/constexpr.c
@@ -109,14 +109,15 @@ ensure_literal_type_for_constexpr_object (tree decl)
explain_non_literal_class (type);
decl = error_mark_node;
}
- else
+ else if (cxx_dialect < cxx23)
{
if (!is_instantiation_of_constexpr (current_function_decl))
{
auto_diagnostic_group d;
error_at (DECL_SOURCE_LOCATION (decl),
"variable %qD of non-literal type %qT in "
- "%<constexpr%> function", decl, type);
+ "%<constexpr%> function only available with "
+ "%<-std=c++2b%> or %<-std=gnu++2b%>", decl, type);
explain_non_literal_class (type);
decl = error_mark_node;
}
@@ -6345,6 +6346,26 @@ cxx_eval_constant_expression (const constexpr_ctx *ctx, tree t,
r = void_node;
break;
}
+
+ if (VAR_P (r)
+ && (TREE_STATIC (r) || CP_DECL_THREAD_LOCAL_P (r))
+ /* Allow __FUNCTION__ etc. */
+ && !DECL_ARTIFICIAL (r))
+ {
+ gcc_assert (cxx_dialect >= cxx23);
+ if (!ctx->quiet)
+ {
+ if (CP_DECL_THREAD_LOCAL_P (r))
+ error_at (loc, "control passes through declaration of %qD "
+ "with thread storage duration", r);
+ else
+ error_at (loc, "control passes through declaration of %qD "
+ "with static storage duration", r);
+ }
+ *non_constant_p = true;
+ break;
+ }
+
if (AGGREGATE_TYPE_P (TREE_TYPE (r))
|| VECTOR_TYPE_P (TREE_TYPE (r)))
{
@@ -7049,10 +7070,18 @@ cxx_eval_constant_expression (const constexpr_ctx *ctx, tree t,
break;
case GOTO_EXPR:
- *jump_target = TREE_OPERAND (t, 0);
- gcc_assert (breaks (jump_target) || continues (jump_target)
- /* Allow for jumping to a cdtor_label. */
- || returns (jump_target));
+ if (breaks (&TREE_OPERAND (t, 0))
+ || continues (&TREE_OPERAND (t, 0))
+ /* Allow for jumping to a cdtor_label. */
+ || returns (&TREE_OPERAND (t, 0)))
+ *jump_target = TREE_OPERAND (t, 0);
+ else
+ {
+ gcc_assert (cxx_dialect >= cxx23);
+ if (!ctx->quiet)
+ error_at (loc, "%<goto%> is not a constant expression");
+ *non_constant_p = true;
+ }
break;
case LOOP_EXPR:
@@ -8736,18 +8765,18 @@ potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now,
tmp = DECL_EXPR_DECL (t);
if (VAR_P (tmp) && !DECL_ARTIFICIAL (tmp))
{
- if (TREE_STATIC (tmp))
+ if (CP_DECL_THREAD_LOCAL_P (tmp))
{
if (flags & tf_error)
error_at (DECL_SOURCE_LOCATION (tmp), "%qD declared "
- "%<static%> in %<constexpr%> context", tmp);
+ "%<thread_local%> in %<constexpr%> context", tmp);
return false;
}
- else if (CP_DECL_THREAD_LOCAL_P (tmp))
+ else if (TREE_STATIC (tmp))
{
if (flags & tf_error)
error_at (DECL_SOURCE_LOCATION (tmp), "%qD declared "
- "%<thread_local%> in %<constexpr%> context", tmp);
+ "%<static%> in %<constexpr%> context", tmp);
return false;
}
else if (!check_for_uninitialized_const_var
@@ -9025,10 +9054,11 @@ potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now,
case LABEL_EXPR:
t = LABEL_EXPR_LABEL (t);
- if (DECL_ARTIFICIAL (t))
+ if (DECL_ARTIFICIAL (t) || cxx_dialect >= cxx23)
return true;
else if (flags & tf_error)
- error_at (loc, "label definition is not a constant expression");
+ error_at (loc, "label definition in %<constexpr%> function only "
+ "available with %<-std=c++2b%> or %<-std=gnu++2b%>");
return false;
case ANNOTATE_EXPR: