public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] d: Fix LHS of array concatentation evaluated before the RHS.
@ 2020-11-18  9:25 Iain Buclaw
  0 siblings, 0 replies; only message in thread
From: Iain Buclaw @ 2020-11-18  9:25 UTC (permalink / raw)
  To: gcc-patches

Hi,

In an array append expression:

    array ~= fun(array);

The array in the left hand side of the expression was extended before
evaluating the result of the right hand side, which resulted in the
newly uninitialized array index being used before set.

This fixes that so that the result of the right hand side is always
saved in a reusable temporary before assigning to the destination.

Bootstrapped and regression tested on x86_64-linux-gnu, committed to
mainline, and backported to the release/gcc-10 branch, as it is a
regression from gcc-9.

Regards
Iain.

---
gcc/d/ChangeLog:

	PR d/97843
	* d-codegen.cc (build_assign): Evaluate TARGET_EXPR before use in
	the right hand side of an assignment.
	* expr.cc (ExprVisitor::visit (CatAssignExp *)): Force a TARGET_EXPR
	on the element to append if it is a CALL_EXPR.

gcc/testsuite/ChangeLog:

	PR d/97843
	* gdc.dg/torture/pr97843.d: New test.
---
 gcc/d/d-codegen.cc                     |  5 +++-
 gcc/d/expr.cc                          |  3 +++
 gcc/testsuite/gdc.dg/torture/pr97843.d | 37 ++++++++++++++++++++++++++
 3 files changed, 44 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/gdc.dg/torture/pr97843.d

diff --git a/gcc/d/d-codegen.cc b/gcc/d/d-codegen.cc
index 1f2d65c4ae2..4c16f6a822b 100644
--- a/gcc/d/d-codegen.cc
+++ b/gcc/d/d-codegen.cc
@@ -1343,7 +1343,10 @@ build_assign (tree_code code, tree lhs, tree rhs)
 	 since that would cause the LHS to be constructed twice.
 	 So we force the TARGET_EXPR to be expanded without a target.  */
       if (code != INIT_EXPR)
-	rhs = compound_expr (rhs, TARGET_EXPR_SLOT (rhs));
+	{
+	  init = compound_expr (init, rhs);
+	  rhs = TARGET_EXPR_SLOT (rhs);
+	}
       else
 	{
 	  d_mark_addressable (lhs);
diff --git a/gcc/d/expr.cc b/gcc/d/expr.cc
index 79f212c3a08..ef2bf5f2e36 100644
--- a/gcc/d/expr.cc
+++ b/gcc/d/expr.cc
@@ -884,6 +884,9 @@ public:
 	    tree t2 = build_expr (e->e2);
 	    tree expr = stabilize_expr (&t2);
 
+	    if (TREE_CODE (t2) == CALL_EXPR)
+	      t2 = force_target_expr (t2);
+
 	    result = modify_expr (build_deref (ptrexp), t2);
 
 	    this->result_ = compound_expr (expr, result);
diff --git a/gcc/testsuite/gdc.dg/torture/pr97843.d b/gcc/testsuite/gdc.dg/torture/pr97843.d
new file mode 100644
index 00000000000..9a775f2b650
--- /dev/null
+++ b/gcc/testsuite/gdc.dg/torture/pr97843.d
@@ -0,0 +1,37 @@
+// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90601
+// { dg-additional-options "-fmain -funittest" }
+// { dg-do run }
+// { dg-skip-if "needs gcc/config.d" { ! d_runtime } }
+
+struct Sdtor
+{
+    int value;
+    ~this() { }
+}
+
+Sdtor sum(Sdtor[] sdtors)
+{
+    int result;
+    foreach (s; sdtors)
+        result += s.value;
+    return Sdtor(result);
+}
+
+uint sum(uint[] ints)
+{
+    uint result;
+    foreach(i; ints)
+        result += i;
+    return result;
+}
+
+unittest
+{
+    Sdtor[] sdtors = [Sdtor(0), Sdtor(1)];
+    sdtors ~= sum(sdtors);
+    assert(sdtors == [Sdtor(0), Sdtor(1), Sdtor(1)]);
+
+    uint[] ints = [0, 1];
+    ints ~= ints.sum;
+    assert(ints == [0, 1, 1]);
+}
-- 
2.27.0


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

only message in thread, other threads:[~2020-11-18  9:25 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-11-18  9:25 [PATCH] d: Fix LHS of array concatentation evaluated before the RHS Iain Buclaw

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