From: Jason Merrill <jason@redhat.com>
To: gcc-patches@gcc.gnu.org
Subject: [pushed] c++: constexpr and designated initializer
Date: Fri, 22 Sep 2023 14:23:04 +0100 [thread overview]
Message-ID: <20230922132304.263323-1-jason@redhat.com> (raw)
Tested x86_64-pc-linux-gnu, applying to trunk.
-- 8< --
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 | 2 +-
gcc/testsuite/g++.dg/cpp2a/constexpr-union7.C | 6 ++++++
2 files changed, 7 insertions(+), 1 deletion(-)
create mode 100644 gcc/testsuite/g++.dg/cpp2a/constexpr-union7.C
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 } }
base-commit: 9c62af101e11e1cce573c2b3d2e18b403412dbc8
--
2.39.3
reply other threads:[~2023-09-22 13:23 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230922132304.263323-1-jason@redhat.com \
--to=jason@redhat.com \
--cc=gcc-patches@gcc.gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).