aboutsummaryrefslogtreecommitdiff
path: root/gcc/cp
diff options
context:
space:
mode:
authorPatrick Palka <ppalka@redhat.com>2023-03-02 14:03:21 -0500
committerPatrick Palka <ppalka@redhat.com>2023-03-02 14:03:21 -0500
commitcbaa1d9c218d9c0b5e34e510a462ff4e299a0f3f (patch)
treefdea0865f0d6bb42ef6b39e6c06d943f9064fd32 /gcc/cp
parent20bd258d0fa09837b3a93478ef92d8789cbcd442 (diff)
downloadgcc-cbaa1d9c218d9c0b5e34e510a462ff4e299a0f3f.zip
gcc-cbaa1d9c218d9c0b5e34e510a462ff4e299a0f3f.tar.gz
gcc-cbaa1d9c218d9c0b5e34e510a462ff4e299a0f3f.tar.bz2
c++: constant non-copy-init is manifestly constant [PR108243]
According to [basic.start.static]/2 and [expr.const]/2, a variable with static storage duration initialized with a constant initializer has constant initialization, and such an initializer is manifestly constant-evaluated. For copy initialization, we're already getting this right because in that case check_initializer would consistently call store_init_value, which for TREE_STATIC variables calls fold_non_dependent_init with m_c_e=true. But for direct (or default) initialization, check_initializer doesn't always call store_init_value. We instead however always call maybe_constant_init from expand_default_init[1], albeit with m_c_e=false which means we don't get the "manifestly constant-evaluated" part right for non-copy-init. This patch fixes this by setting m_c_e=true in maybe_constant_init for static storage duration variables, mainly for benefit of the call to maybe_constant_init from expand_default_init. [1]: this maybe_constant_init call isn't reached in the copy-init case because there init is a CONSTRUCTOR rather than a TREE_LIST, and so we exit early from expand_default_init, returning an INIT_EXPR. This INIT_EXPR is ultimately what causes us to consistently hit the store_init_value code path from check_initializer in the copy-init case. PR c++/108243 gcc/cp/ChangeLog: * constexpr.cc (maybe_constant_init_1): Override manifestly_const_eval to true if is_static. gcc/testsuite/ChangeLog: * g++.dg/cpp2a/is-constant-evaluated14.C: New test.
Diffstat (limited to 'gcc/cp')
-rw-r--r--gcc/cp/constexpr.cc2
1 files changed, 2 insertions, 0 deletions
diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc
index 0770cdc..acf9847 100644
--- a/gcc/cp/constexpr.cc
+++ b/gcc/cp/constexpr.cc
@@ -8770,6 +8770,8 @@ maybe_constant_init_1 (tree t, tree decl, bool allow_non_constant,
shouldn't bend the rules the same way for automatic variables. */
bool is_static = (decl && DECL_P (decl)
&& (TREE_STATIC (decl) || DECL_EXTERNAL (decl)));
+ if (is_static)
+ manifestly_const_eval = true;
t = cxx_eval_outermost_constant_expr (t, allow_non_constant, !is_static,
mce_value (manifestly_const_eval),
false, decl);