]> gcc.gnu.org Git - gcc.git/commitdiff
c++: constexpr and designated initializer
authorJason Merrill <jason@redhat.com>
Fri, 22 Sep 2023 09:54:28 +0000 (10:54 +0100)
committerJason Merrill <jason@redhat.com>
Fri, 22 Sep 2023 13:22:26 +0000 (14:22 +0100)
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.

gcc/cp/constexpr.cc
gcc/testsuite/g++.dg/cpp2a/constexpr-union7.C [new file with mode: 0644]

index a673a6022f1755448d62e6f83615ccff4c635e99..2a6601c0cbca8faf4f282c8106e25c79cd9ef186 100644 (file)
@@ -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 (file)
index 0000000..230fa6e
--- /dev/null
@@ -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 } }
This page took 0.080826 seconds and 5 git commands to generate.