]> git.ipfire.org Git - thirdparty/gcc.git/commit
c++: constexpr uninitialized union [PR120577]
authorJason Merrill <jason@redhat.com>
Sat, 12 Jul 2025 09:15:01 +0000 (05:15 -0400)
committerJason Merrill <jason@redhat.com>
Tue, 15 Jul 2025 15:34:39 +0000 (11:34 -0400)
commitf23b5df56e237df9f66b615ca4babc564d5f75de
treee1978c1a9a4f1c3f05e86a275d0c568f09a64416
parent9956dc37cb3d71c1fc7a89b45cc291645c11817b
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.
gcc/cp/constexpr.cc
gcc/testsuite/g++.dg/cpp2a/constexpr-union9.C [new file with mode: 0644]