From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1873) id 68A5B3990C00; Wed, 28 Jul 2021 11:19:01 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 68A5B3990C00 MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Iain Buclaw To: gcc-cvs@gcc.gnu.org Subject: [gcc r12-2565] d: Wrong evaluation order of binary expressions (PR101640) X-Act-Checkin: gcc X-Git-Author: Iain Buclaw X-Git-Refname: refs/heads/master X-Git-Oldrev: c936c39f86c74b3bfc6831f694b3165296c99dc0 X-Git-Newrev: 54ec50bada94a8ff92edb04ee5216c27fa4bf942 Message-Id: <20210728111901.68A5B3990C00@sourceware.org> Date: Wed, 28 Jul 2021 11:19:01 +0000 (GMT) X-BeenThere: gcc-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 28 Jul 2021 11:19:01 -0000 https://gcc.gnu.org/g:54ec50bada94a8ff92edb04ee5216c27fa4bf942 commit r12-2565-g54ec50bada94a8ff92edb04ee5216c27fa4bf942 Author: Iain Buclaw Date: Tue Jul 27 13:24:34 2021 +0200 d: Wrong evaluation order of binary expressions (PR101640) The use of fold_build2 can in some cases swap the order of its operands if that is the more optimal thing to do. However this breaks semantic guarantee of left-to-right evaluation in D. PR d/101640 gcc/d/ChangeLog: * expr.cc (binary_op): Use build2 instead of fold_build2. gcc/testsuite/ChangeLog: * gdc.dg/pr96429.d: Update test. * gdc.dg/pr101640.d: New test. Diff: --- gcc/d/expr.cc | 2 +- gcc/testsuite/gdc.dg/pr101640.d | 11 +++++++++++ gcc/testsuite/gdc.dg/pr96429.d | 2 +- 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/gcc/d/expr.cc b/gcc/d/expr.cc index e76cae98f7e..b78778eb8ef 100644 --- a/gcc/d/expr.cc +++ b/gcc/d/expr.cc @@ -157,7 +157,7 @@ binary_op (tree_code code, tree type, tree arg0, tree arg1) eptype = type; } - ret = fold_build2 (code, eptype, arg0, arg1); + ret = build2 (code, eptype, arg0, arg1); } return d_convert (type, ret); diff --git a/gcc/testsuite/gdc.dg/pr101640.d b/gcc/testsuite/gdc.dg/pr101640.d new file mode 100644 index 00000000000..68de4088512 --- /dev/null +++ b/gcc/testsuite/gdc.dg/pr101640.d @@ -0,0 +1,11 @@ +// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101640 +// { dg-do compile } +// { dg-options "-fdump-tree-original" } + +int fun101640(ref int); + +int test101640(int val) +{ + // { dg-final { scan-tree-dump "= val \\\+ fun101640 \\\(\\\(int &\\\) &val\\\);" "original" } } + return val + fun101640(val); +} diff --git a/gcc/testsuite/gdc.dg/pr96429.d b/gcc/testsuite/gdc.dg/pr96429.d index af096e26b5a..9940a03e0ec 100644 --- a/gcc/testsuite/gdc.dg/pr96429.d +++ b/gcc/testsuite/gdc.dg/pr96429.d @@ -3,7 +3,7 @@ // { dg-options "-fdump-tree-original" } ptrdiff_t subbyte(byte* bp1, byte* bp2) { - // { dg-final { scan-tree-dump "bp1 - bp2;" "original" } } + // { dg-final { scan-tree-dump "\\\(bp1 - bp2\\\) /\\\[ex\\\] 1;" "original" } } return bp1 - bp2; }