]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: ICE with NSDMIs and fn arguments [PR116015]
authorMarek Polacek <polacek@redhat.com>
Fri, 9 Aug 2024 20:14:18 +0000 (16:14 -0400)
committerMarek Polacek <polacek@redhat.com>
Wed, 14 Aug 2024 19:32:38 +0000 (15:32 -0400)
The problem in this PR is that we ended up with

  {.rows=(&<PLACEHOLDER_EXPR struct Widget>)->n,
   .outer_stride=(&<PLACEHOLDER_EXPR struct MatrixLayout>)->rows}

that is, two PLACEHOLDER_EXPRs for different types on the same level
in one { }.  That should not happen; we may, for instance, neglect to
replace a PLACEHOLDER_EXPR due to CONSTRUCTOR_PLACEHOLDER_BOUNDARY on
the constructor.

The same problem happened in PR100252, which I fixed by introducing
replace_placeholders_for_class_temp_r.  That didn't work here, though,
because r_p_for_c_t_r only works for non-eliding TARGET_EXPRs: replacing
a PLACEHOLDER_EXPR with a temporary that is going to be elided will
result in a crash in gimplify_var_or_parm_decl when it encounters such
a loose decl.

But leaving the PLACEHOLDER_EXPRs in is also bad because then we end
up with this PR.

TARGET_EXPRs for function arguments are elided in gimplify_arg.  The
argument will get a real temporary only in get_formal_tmp_var.  One
idea was to use the temporary that is going to be elided anyway, and
then replace_decl it with the real object once we get it.  But that
didn't work out: one problem is that we elide the TARGET_EXPR for an
argument before we create the real temporary for the argument, and
when we get it, the context that this was a TARGET_EXPR for an argument
has been lost.  We're also in the middle end territory now, even though
this is a C++-specific problem.

A solution is to simply stop eliding TARGET_EXPRs whose initializer is
a CONSTRUCTOR.  Such copies can't be (at the moment) elided anyway.  But
not eliding all TARGET_EXPRs would be a pessimization.

PR c++/116015

gcc/cp/ChangeLog:

* call.cc (convert_for_arg_passing): Don't set_target_expr_eliding
when the TARGET_EXPR initializer is a CONSTRUCTOR.

gcc/ChangeLog:

* gimplify.cc (gimplify_arg): Do not strip a TARGET_EXPR whose
initializer is a CONSTRUCTOR.

gcc/testsuite/ChangeLog:

* g++.dg/cpp1y/nsdmi-aggr23.C: New test.

gcc/cp/call.cc
gcc/gimplify.cc
gcc/testsuite/g++.dg/cpp1y/nsdmi-aggr23.C [new file with mode: 0644]

index 94015db4e6500c77ca09472881bec78b1712dde6..0fe679aae9fe21942a57a21e35f631ce6b4fe6fe 100644 (file)
@@ -9468,8 +9468,11 @@ convert_for_arg_passing (tree type, tree val, tsubst_flags_t complain)
   if (complain & tf_warning)
     warn_for_address_of_packed_member (type, val);
 
-  /* gimplify_arg elides TARGET_EXPRs that initialize a function argument.  */
-  if (SIMPLE_TARGET_EXPR_P (val))
+  /* gimplify_arg elides TARGET_EXPRs that initialize a function argument,
+     unless the initializer is a CONSTRUCTOR.  In that case, we fail to
+     elide the copy anyway.  See that function for more information.  */
+  if (SIMPLE_TARGET_EXPR_P (val)
+      && TREE_CODE (TARGET_EXPR_INITIAL (val)) != CONSTRUCTOR)
     set_target_expr_eliding (val);
 
   return val;
index 71cc6c38d807daa7cd038975b448a963a279929c..26a216e151d6fe3f8d78e3eeef837ba930c8f804 100644 (file)
@@ -3760,7 +3760,22 @@ gimplify_arg (tree *arg_p, gimple_seq *pre_p, location_t call_location,
        {
          tree init = TARGET_EXPR_INITIAL (*arg_p);
          if (init
-             && !VOID_TYPE_P (TREE_TYPE (init)))
+             && !VOID_TYPE_P (TREE_TYPE (init))
+             /* Currently, due to c++/116015, it is not desirable to
+                strip a TARGET_EXPR whose initializer is a {}.  The
+                problem is that if we do elide it, we also have to
+                replace all the occurrences of the slot temporary in the
+                initializer with the temporary created for the argument.
+                But we do not have that temporary yet so the replacement
+                would be quite awkward and it might be needed to resort
+                back to a PLACEHOLDER_EXPR.  Note that stripping the
+                TARGET_EXPR wouldn't help anyway, as gimplify_expr would
+                just allocate a temporary to store the CONSTRUCTOR into.
+                (FIXME PR116375.)
+
+                See convert_for_arg_passing for the C++ code that marks
+                the TARGET_EXPR as eliding or not.  */
+             && TREE_CODE (init) != CONSTRUCTOR)
            *arg_p = init;
        }
     }
diff --git a/gcc/testsuite/g++.dg/cpp1y/nsdmi-aggr23.C b/gcc/testsuite/g++.dg/cpp1y/nsdmi-aggr23.C
new file mode 100644 (file)
index 0000000..2f5b8ca
--- /dev/null
@@ -0,0 +1,26 @@
+// PR c++/116015
+// { dg-do compile { target c++14 } }
+// { dg-additional-options "-Wno-c++20-extensions" }
+
+struct MatrixLayout {
+    int rows         = 0;
+    int outer_stride = rows;
+};
+struct Matrix {
+    Matrix(MatrixLayout m) {}
+};
+struct Widget {
+    int n = 5;
+    Matrix A0{{}};
+    Matrix A1{{n}};
+    Matrix A1_{{.rows = n}};
+    Matrix A2{{n, n}};
+};
+
+int
+main ()
+{
+ Widget w{};
+ Widget w1;
+ Widget w2 = {};
+}