public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Jason Merrill <jason@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc r13-1901] c++: constexpr, empty base after non-empty [PR106369]
Date: Sun, 31 Jul 2022 06:05:11 +0000 (GMT)	[thread overview]
Message-ID: <20220731060511.773D03858436@sourceware.org> (raw)

https://gcc.gnu.org/g:9efe4e153d994974afcbba09c3c683f5f4a19c63

commit r13-1901-g9efe4e153d994974afcbba09c3c683f5f4a19c63
Author: Jason Merrill <jason@redhat.com>
Date:   Tue Jul 26 11:02:21 2022 -0400

    c++: constexpr, empty base after non-empty [PR106369]
    
    Here the CONSTRUCTOR we were providing for D{} had an entry for the B base
    subobject at offset 0 following the entry for the C base, causing
    output_constructor_regular_field to ICE due to going backwards.  It might be
    nice for that function to be more tolerant of empty fields, but it also
    seems reasonable for the front end to prune the useless entry.
    
            PR c++/106369
    
    gcc/cp/ChangeLog:
    
            * constexpr.cc (reduced_constant_expression_p): Return false
            if a CONSTRUCTOR initializes an empty field.
    
    gcc/testsuite/ChangeLog:
    
            * g++.dg/cpp1z/constexpr-lambda27.C: New test.

Diff:
---
 gcc/cp/constexpr.cc                             |  8 +++++++-
 gcc/testsuite/g++.dg/cpp1z/constexpr-lambda27.C | 26 +++++++++++++++++++++++++
 2 files changed, 33 insertions(+), 1 deletion(-)

diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc
index 5f7fc6f8f0c..5e0d3399172 100644
--- a/gcc/cp/constexpr.cc
+++ b/gcc/cp/constexpr.cc
@@ -3081,7 +3081,13 @@ reduced_constant_expression_p (tree t)
 	     element.  */
 	  if (!reduced_constant_expression_p (e.value))
 	    return false;
-	  /* Empty class field may or may not have an initializer.  */
+	  /* We want to remove initializers for empty fields in a struct to
+	     avoid confusing output_constructor.  */
+	  if (is_empty_field (e.index)
+	      && TREE_CODE (TREE_TYPE (t)) == RECORD_TYPE)
+	    return false;
+	  /* Check for non-empty fields between initialized fields when
+	     CONSTRUCTOR_NO_CLEARING.  */
 	  for (; field && e.index != field;
 	       field = next_subobject_field (DECL_CHAIN (field)))
 	    if (!is_really_empty_class (TREE_TYPE (field),
diff --git a/gcc/testsuite/g++.dg/cpp1z/constexpr-lambda27.C b/gcc/testsuite/g++.dg/cpp1z/constexpr-lambda27.C
new file mode 100644
index 00000000000..24e2e9b6c98
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp1z/constexpr-lambda27.C
@@ -0,0 +1,26 @@
+// PR c++/106369
+// { dg-do compile { target c++17 } }
+
+struct A {
+  int a[256];
+  constexpr int &operator[] (int n) noexcept { return a[n]; }
+  constexpr const int &operator[] (int n) const noexcept { return a[n]; }
+};
+struct B {};
+template <typename T>
+struct C {
+  constexpr T &foo (const char x) noexcept { c = T::d[x]; return static_cast<T &>(*this); }
+  int c;
+};
+struct D : public C<D>, public B
+{
+  D () noexcept = default;
+  static constexpr char e[9] { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I' };
+  static constexpr A d = [] () constexpr {
+    A f {};
+    for (int i = 0; i < 9; ++i)
+      f[e[i]] = 1;
+    return f;
+  } ();
+};
+constexpr auto g = D{}.foo ('E');


                 reply	other threads:[~2022-07-31  6:05 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=20220731060511.773D03858436@sourceware.org \
    --to=jason@gcc.gnu.org \
    --cc=gcc-cvs@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).