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.