]> git.ipfire.org Git - thirdparty/gcc.git/commit
c++: constexpr uninitialized union [PR120577]
authorJason Merrill <jason@redhat.com>
Fri, 25 Jul 2025 19:57:26 +0000 (15:57 -0400)
committerJason Merrill <jason@redhat.com>
Fri, 25 Jul 2025 19:57:26 +0000 (15:57 -0400)
commit6f0c68450871de6ea211748294c63c73e3e82748
treeaddfeb015143e31c88f6a9153a915aca7382200d
parent2fb7db82783c51009d57deffd405ef501ef9aa9a
c++: constexpr uninitialized union [PR120577]

This was failing for two reasons:

1) We were wrongly treating the basic_string constructor as
zero-initializing the object, which it doesn't.
2) Given that, when we went to look for a value for the anonymous union,
we concluded that it was value-initialized, and trying to evaluate that
broke because we weren't setting ctx->ctor for it.

This patch fixes both issues, #1 by setting CONSTRUCTOR_NO_CLEARING and #2
by inserting a new CONSTRUCTOR for the member rather than evaluate it out of
context, which is consistent with cxx_eval_store_expression.

PR c++/120577

gcc/cp/ChangeLog:

* constexpr.cc (cxx_eval_call_expression): Set
CONSTRUCTOR_NO_CLEARING on initial value for ctor.
(cxx_eval_component_reference): Make value-initialization
of an aggregate member explicit.

gcc/testsuite/ChangeLog:

* g++.dg/cpp2a/constexpr-union9.C: New test.

(cherry picked from commit f23b5df56e237df9f66b615ca4babc564d5f75de)
gcc/cp/constexpr.cc
gcc/testsuite/g++.dg/cpp2a/constexpr-union9.C [new file with mode: 0644]