https://gcc.gnu.org/g:3003d404b436c2dbd5a3a282c25b797d887237d9

commit r14-11346-g3003d404b436c2dbd5a3a282c25b797d887237d9
Author: Jason Merrill <ja...@redhat.com>
Date:   Tue Jan 28 13:11:50 2025 -0500

    c++: constexpr VEC_INIT_EXPR [PR118285]
    
    cxx_eval_vec_init_1 was doing the wrong thing for an array of
    self-referential class type; just evaluating the TARGET_EXPR initializer
    creates a new object that refers to the TARGET_EXPR_SLOT, if we want it to
    refer properly to the initialization target we need to provide it.
    
            PR c++/118285
    
    gcc/cp/ChangeLog:
    
            * constexpr.cc (cxx_eval_vec_init_1): Build INIT_EXPR for
            initializing a class.
    
    gcc/testsuite/ChangeLog:
    
            * g++.dg/cpp0x/initlist-opt7.C: New test.

Diff:
---
 gcc/cp/constexpr.cc                        |  9 ++++++-
 gcc/testsuite/g++.dg/cpp0x/initlist-opt7.C | 41 ++++++++++++++++++++++++++++++
 2 files changed, 49 insertions(+), 1 deletion(-)

diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc
index 83a9175b265d..4b10763950d2 100644
--- a/gcc/cp/constexpr.cc
+++ b/gcc/cp/constexpr.cc
@@ -5522,7 +5522,11 @@ cxx_eval_vec_init_1 (const constexpr_ctx *ctx, tree 
atype, tree init,
          if (init == void_node)
            /* Trivial default-init, don't do anything to the CONSTRUCTOR.  */
            return ctx->ctor;
-         eltinit = cxx_eval_constant_expression (&new_ctx, init, lval,
+         eltinit = init;
+         if (CLASS_TYPE_P (elttype) && new_ctx.object)
+           /* Clarify what object is being initialized (118285).  */
+           eltinit = build2 (INIT_EXPR, elttype, new_ctx.object, eltinit);
+         eltinit = cxx_eval_constant_expression (&new_ctx, eltinit, lval,
                                                  non_constant_p, overflow_p);
          reuse = i == 0;
        }
@@ -5535,6 +5539,9 @@ cxx_eval_vec_init_1 (const constexpr_ctx *ctx, tree 
atype, tree init,
          eltinit = (perform_implicit_conversion_flags
                     (elttype, eltinit, complain,
                      LOOKUP_IMPLICIT|LOOKUP_NO_NARROWING));
+         if (CLASS_TYPE_P (elttype) && new_ctx.object)
+           /* Clarify what object is being initialized (118285).  */
+           eltinit = build2 (INIT_EXPR, elttype, new_ctx.object, eltinit);
          eltinit = cxx_eval_constant_expression (&new_ctx, eltinit, lval,
                                                  non_constant_p, overflow_p);
        }
diff --git a/gcc/testsuite/g++.dg/cpp0x/initlist-opt7.C 
b/gcc/testsuite/g++.dg/cpp0x/initlist-opt7.C
new file mode 100644
index 000000000000..f55ef5b77c5b
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/initlist-opt7.C
@@ -0,0 +1,41 @@
+// PR c++/118285
+// { dg-do compile { target c++20 } }
+
+#include <initializer_list>
+
+struct A {
+  char *a;
+  union { char b[8]; long c; };
+  constexpr A (const char *x) : a(b)
+  {
+    for (int i = 0; i < 8; ++i)
+      b[i] = 0;
+  }
+  constexpr ~A ()
+  {
+    if (!foo ())
+      bar (c);
+  }
+  constexpr bool foo ()
+  {
+    char *x = a;
+    if (x == b)
+      return true;
+    return false;
+  }
+  constexpr void bar (long) {}
+};
+
+constexpr void
+baz (std::initializer_list<A>)
+{
+}
+
+constexpr bool
+qux ()
+{
+  baz ({""});
+  return true;
+}
+
+static_assert (qux (), "");

Reply via email to