From patchwork Wed Nov 18 09:25:43 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Iain Buclaw X-Patchwork-Id: 1402139 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=gcc-patches-bounces@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=gcc.gnu.org Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=M6OA8G1i; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4Cbcqp5t4zz9sTc for ; Wed, 18 Nov 2020 20:25:58 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 99B773982C73; Wed, 18 Nov 2020 09:25:56 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 99B773982C73 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1605691556; bh=LEAn1kMFju3uk6RjVFD+F1YVVBREotpbTxitXx7M7WE=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=M6OA8G1iAqsKcczS6GEuCUrqrHPCbZddzlFxzD77PIcIIh0UlJ1bbQ6muqAFo/okx T2YBdeGzY2WhprDQy9KTrYAMZY8l/VbHU7f/N8mpmSosLYIztSwfU6rqWq2Tu9onYE qjlqSnN2j8nYYEce977htPSvKfXy8MPH5D4nUHls= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mout-p-201.mailbox.org (mout-p-201.mailbox.org [IPv6:2001:67c:2050::465:201]) by sourceware.org (Postfix) with ESMTPS id 2C033385800A for ; Wed, 18 Nov 2020 09:25:53 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 2C033385800A Received: from smtp2.mailbox.org (smtp2.mailbox.org [80.241.60.241]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-384) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-p-201.mailbox.org (Postfix) with ESMTPS id 4Cbcqg6bdfzQkSN; Wed, 18 Nov 2020 10:25:51 +0100 (CET) X-Virus-Scanned: amavisd-new at heinlein-support.de Received: from smtp2.mailbox.org ([80.241.60.241]) by gerste.heinlein-support.de (gerste.heinlein-support.de [91.198.250.173]) (amavisd-new, port 10030) with ESMTP id SUuIrWydVlsp; Wed, 18 Nov 2020 10:25:48 +0100 (CET) To: gcc-patches@gcc.gnu.org Subject: [PATCH] d: Fix LHS of array concatentation evaluated before the RHS. Date: Wed, 18 Nov 2020 10:25:43 +0100 Message-Id: <20201118092543.3907703-1-ibuclaw@gdcproject.org> MIME-Version: 1.0 X-MBO-SPAM-Probability: **** X-Rspamd-Score: 5.64 / 15.00 / 15.00 X-Rspamd-Queue-Id: A904516FD X-Rspamd-UID: 667a67 X-Spam-Status: No, score=-15.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, URIBL_ABUSE_SURBL autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Iain Buclaw via Gcc-patches From: Iain Buclaw Reply-To: Iain Buclaw Errors-To: gcc-patches-bounces@gcc.gnu.org Sender: "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]); +}