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.
We're already getting this right with copy initialization 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, we don'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 always get the "manifestly constant-evaluated" part right for copy-init. This patch fixes this by simply passing m_c_e=true to this call to maybe_constant_init for static storage duration variables, mirroring what store_init_value basically does. [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 so expand_default_init exits early 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. Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for trunk? Would it be suitable to backport this to the 12 branch since it should only affect C++20 code? PR c++/108243 gcc/cp/ChangeLog: * init.cc (expand_default_init): Pass m_c_e=true instead of =false to maybe_constant_init when initializing a variable with static storage duration. gcc/testsuite/ChangeLog: * g++.dg/cpp2a/is-constant-evaluated14.C: New test. --- gcc/cp/init.cc | 5 +- .../g++.dg/cpp2a/is-constant-evaluated14.C | 140 ++++++++++++++++++ 2 files changed, 144 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/g++.dg/cpp2a/is-constant-evaluated14.C diff --git a/gcc/cp/init.cc b/gcc/cp/init.cc index 52e96fbe590..705a5b3bdb6 100644 --- a/gcc/cp/init.cc +++ b/gcc/cp/init.cc @@ -2203,7 +2203,10 @@ expand_default_init (tree binfo, tree true_exp, tree exp, tree init, int flags, tree fn = get_callee_fndecl (rval); if (fn && DECL_DECLARED_CONSTEXPR_P (fn)) { - tree e = maybe_constant_init (rval, exp); + bool manifestly_const_eval = false; + if (VAR_P (exp) && TREE_STATIC (exp)) + manifestly_const_eval = true; + tree e = maybe_constant_init (rval, exp, manifestly_const_eval); if (TREE_CONSTANT (e)) rval = cp_build_init_expr (exp, e); } diff --git a/gcc/testsuite/g++.dg/cpp2a/is-constant-evaluated14.C b/gcc/testsuite/g++.dg/cpp2a/is-constant-evaluated14.C new file mode 100644 index 00000000000..365bca3fd9a --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/is-constant-evaluated14.C @@ -0,0 +1,140 @@ +// PR c++/108243 +// Verify a variable with static storage duration initialized with a +// constant initializer has constant initialization, and the initializer +// is manifestly constant-evaluated. +// { dg-do run { target c++11 } } +// { dg-additional-options "-fdump-tree-original" } + +#include <initializer_list> + +struct A { + constexpr A(int n) : n(n), m(__builtin_is_constant_evaluated()) { } + constexpr A() : A(42) { } + void verify_mce() const { + if (m != 1) __builtin_abort(); + } + int n; + int m; +}; + +A a1 = {42}; +A a2{42}; +A a3(42); +A a4; +A a5{}; + +void f() { + static A a1 = {42}; + static A a2{42}; + static A a3(42); + static A a4; + static A a5{}; + for (auto& a : {a1, a2, a3, a4, a5}) + a.verify_mce(); +} + +template<int... N> +void g() { + static A a1 = {42}; + static A a2{42}; + static A a3(42); + static A a4; + static A a5{}; + static A a6 = {N...}; + static A a7{N...}; + static A a8(N...); + for (auto& a : {a1, a2, a3, a4, a5, a6, a7, a8}) + a.verify_mce(); +} + +struct B { + static A a1; + static A a2; + static A a3; + static A a4; + static A a5; + static void verify_mce() { + for (auto& a : {a1, a2, a3, a4, a5}) + a.verify_mce(); + } +}; + +A B::a1 = {42}; +A B::a2{42}; +A B::a3(42); +A B::a4; +A B::a5{}; + +template<int... N> +struct BT { + static A a1; + static A a2; + static A a3; + static A a4; + static A a5; + static A a6; + static A a7; + static A a8; + static void verify_mce() { + for (auto& a : {a1, a2, a3, a4, a5}) + a.verify_mce(); + } +}; + +template<int... N> A BT<N...>::a1 = {42}; +template<int... N> A BT<N...>::a2{42}; +template<int... N> A BT<N...>::a3(42); +template<int... N> A BT<N...>::a4; +template<int... N> A BT<N...>::a5{}; +template<int... N> A BT<N...>::a6 = {N...}; +template<int... N> A BT<N...>::a7{N...}; +template<int... N> A BT<N...>::a8(N...); + +#if __cpp_inline_variables +struct BI { + static inline A a1 = {42}; + static inline A a2{42}; + static inline A a3; + static inline A a4{}; + static void verify_mce() { + for (auto& a : {a1, a2, a3, a4}) + a.verify_mce(); + } +}; + +template<int... N> +struct BIT { + static inline A a1 = {42}; + static inline A a2{42}; + static inline A a3; + static inline A a4{}; + static inline A a5 = {N...}; + static inline A a6{N...}; + static void verify_mce() { + for (auto& a : {a1, a2, a3, a4, a5, a6}) + a.verify_mce(); + } +}; +#endif + +int main() { + for (auto& a : {a1, a2, a3, a4, a5}) + a.verify_mce(); + + f(); + g<42>(); + g<>(); + + B::verify_mce(); + BT<42>::verify_mce(); + BT<>::verify_mce(); + +#if __cpp_inline_variables + BI::verify_mce(); + BIT<42>::verify_mce(); + BIT<>::verify_mce(); +#endif +} + +// { dg-final { scan-tree-dump-not "static initializers for" "original" } } +// { dg-final { scan-tree-dump-not "cxa_guard_acquire" "original" } } -- 2.39.2.501.gd9d677b2d8