c++: array new initialized from a call [PR99643]
authorJason Merrill <jason@redhat.com>
Sat, 3 Apr 2021 20:17:29 +0000 (16:17 -0400)
committerJason Merrill <jason@redhat.com>
Sun, 4 Apr 2021 00:59:00 +0000 (20:59 -0400)
Here the get_foo() call results in a TARGET_EXPR, which we strip in
massage_init_elt, but then when build_vec_init tries to use it to initialize
the array element we crash because build_aggr_init expects a class rvalue to
have a TARGET_EXPR.  So don't strip it.

The stripping was added in r206639 for PR59659, so I checked that removing
it didn't significantly increase compile time or memory usage for that
testcase; compile time was unaffected, memory usage increased by 0.00004%.

gcc/cp/ChangeLog:

PR c++/99643
* typeck2.c (massage_init_elt): Don't strip TARGET_EXPR.

gcc/testsuite/ChangeLog:

PR c++/99643
* g++.dg/cpp0x/initlist-new5.C: New test.

gcc/cp/typeck2.c
gcc/testsuite/g++.dg/cpp0x/initlist-new5.C [new file with mode: 0644]

index bde305b..a58d397 100644 (file)
@@ -1320,9 +1320,6 @@ massage_init_elt (tree type, tree init, int nested, int flags,
   if (flags & LOOKUP_AGGREGATE_PAREN_INIT)
     new_flags |= LOOKUP_AGGREGATE_PAREN_INIT;
   init = digest_init_r (type, init, nested ? 2 : 1, new_flags, complain);
-  /* Strip a simple TARGET_EXPR when we know this is an initializer.  */
-  if (SIMPLE_TARGET_EXPR_P (init))
-    init = TARGET_EXPR_INITIAL (init);
   /* When we defer constant folding within a statement, we may want to
      defer this folding as well.  */
   tree t = fold_non_dependent_init (init, complain);
diff --git a/gcc/testsuite/g++.dg/cpp0x/initlist-new5.C b/gcc/testsuite/g++.dg/cpp0x/initlist-new5.C
new file mode 100644 (file)
index 0000000..da54d89
--- /dev/null
@@ -0,0 +1,9 @@
+// PR c++/99643
+// { dg-do compile { target c++11 } }
+
+struct Foo {};
+Foo get_foo();
+
+int main() {
+    new Foo[1]{get_foo()};
+}