public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r11-10046] c++: constexpr empty aggr [PR105795]
@ 2022-06-03  4:38 Jason Merrill
  0 siblings, 0 replies; only message in thread
From: Jason Merrill @ 2022-06-03  4:38 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:340c53bc6352d223f543faac71996a3fa87d9119

commit r11-10046-g340c53bc6352d223f543faac71996a3fa87d9119
Author: Jason Merrill <jason@redhat.com>
Date:   Wed Jun 1 16:13:48 2022 -0400

    c++: constexpr empty aggr [PR105795]
    
    In this testcase, leaving ctx->ctor pointing to the enclosing object meant
    that evaluating the initializer for the subobject clobbered previous
    initializers for the enclosing object.  So do update ctx->ctor, just don't
    add it to the enclosing object ctor.
    
            PR c++/105795
    
    gcc/cp/ChangeLog:
    
            * constexpr.c (cxx_eval_bare_aggregate): Always call
            init_subob_ctx.
    
    gcc/testsuite/ChangeLog:
    
            * g++.dg/cpp1z/constexpr-aggr-base1.C: New test.

Diff:
---
 gcc/cp/constexpr.c                                |  7 ++----
 gcc/testsuite/g++.dg/cpp1z/constexpr-aggr-base1.C | 27 +++++++++++++++++++++++
 2 files changed, 29 insertions(+), 5 deletions(-)

diff --git a/gcc/cp/constexpr.c b/gcc/cp/constexpr.c
index 5a1db17a14d..7be5f25b10f 100644
--- a/gcc/cp/constexpr.c
+++ b/gcc/cp/constexpr.c
@@ -4467,12 +4467,9 @@ cxx_eval_bare_aggregate (const constexpr_ctx *ctx, tree t,
       tree orig_value = value;
       /* Like in cxx_eval_store_expression, omit entries for empty fields.  */
       bool no_slot = TREE_CODE (type) == RECORD_TYPE && is_empty_field (index);
-      if (no_slot)
-	new_ctx = *ctx;
-      else
-	init_subob_ctx (ctx, new_ctx, index, value);
+      init_subob_ctx (ctx, new_ctx, index, value);
       int pos_hint = -1;
-      if (new_ctx.ctor != ctx->ctor)
+      if (new_ctx.ctor != ctx->ctor && !no_slot)
 	{
 	  /* If we built a new CONSTRUCTOR, attach it now so that other
 	     initializers can refer to it.  */
diff --git a/gcc/testsuite/g++.dg/cpp1z/constexpr-aggr-base1.C b/gcc/testsuite/g++.dg/cpp1z/constexpr-aggr-base1.C
new file mode 100644
index 00000000000..06acb4a9f78
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp1z/constexpr-aggr-base1.C
@@ -0,0 +1,27 @@
+// PR c++/105795
+// { dg-do compile { target c++17 } }
+
+struct empty
+{};
+
+template <typename T>
+struct tuple_holder
+{
+    [[no_unique_address]] T value;
+};
+
+struct tuple : tuple_holder<int>, tuple_holder<empty>
+{};
+
+constexpr auto make_tuple(int&& i, empty&& e)
+{
+    return tuple{i, e};
+}
+
+constexpr int foo()
+{
+    auto tuple = make_tuple(1, empty{});
+    return static_cast<const tuple_holder<int>&>(tuple).value;
+}
+
+static_assert (foo() == 1);


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-06-03  4:38 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-03  4:38 [gcc r11-10046] c++: constexpr empty aggr [PR105795] Jason Merrill

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).