[gcc r14-4230] c++: constexpr and designated initializer
Jason Merrill
jason@gcc.gnu.org
Fri Sep 22 13:23:26 GMT 2023
https://gcc.gnu.org/g:22cda0ca5fb406f22925bbf51ab152a958e3319d
commit r14-4230-g22cda0ca5fb406f22925bbf51ab152a958e3319d
Author: Jason Merrill <jason@redhat.com>
Date: Fri Sep 22 10:54:28 2023 +0100
c++: constexpr and designated initializer
The change of active member being non-constant (before C++20) results in a
CONSTRUCTOR with a null value for the first field, don't crash.
gcc/cp/ChangeLog:
* constexpr.cc (free_constructor): Handle null ce->value.
gcc/testsuite/ChangeLog:
* g++.dg/cpp2a/constexpr-union7.C: New test.
Diff:
---
gcc/cp/constexpr.cc | 2 +-
gcc/testsuite/g++.dg/cpp2a/constexpr-union7.C | 6 ++++++
2 files changed, 7 insertions(+), 1 deletion(-)
diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc
index a673a6022f1..2a6601c0cbc 100644
--- a/gcc/cp/constexpr.cc
+++ b/gcc/cp/constexpr.cc
@@ -1753,7 +1753,7 @@ free_constructor (tree t)
{
constructor_elt *ce;
for (HOST_WIDE_INT i = 0; vec_safe_iterate (elts, i, &ce); ++i)
- if (TREE_CODE (ce->value) == CONSTRUCTOR)
+ if (ce->value && TREE_CODE (ce->value) == CONSTRUCTOR)
vec_safe_push (ctors, ce->value);
ggc_free (elts);
}
diff --git a/gcc/testsuite/g++.dg/cpp2a/constexpr-union7.C b/gcc/testsuite/g++.dg/cpp2a/constexpr-union7.C
new file mode 100644
index 00000000000..230fa6e7d06
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp2a/constexpr-union7.C
@@ -0,0 +1,6 @@
+// { dg-do compile { target c++14 } }
+// { dg-options "" }
+
+union U { int i; float f; };
+constexpr auto g (U u) { return (u.i = 42); } // { dg-error "active member" "" { target c++17_down } }
+static_assert (g({.f = 3.14}) == 42); // { dg-error "non-constant" "" { target c++17_down } }
More information about the Gcc-cvs
mailing list