From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1059) id 236C83877008; Thu, 19 Mar 2020 12:45:47 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 236C83877008 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1584621947; bh=Z2uWPbrLz3ch1BXqz3fwscNPiMDNHNx9D6Uy2rOjEU4=; h=From:To:Subject:Date:From; b=dmelB+TVEpUcEUeqUKnbJG1MbiM4BAYYHn0nT7sjCLr7jOYn9m4rvGScvAMxguOsK jzJaIxAYraIoWodakY3lWrL3JPol6nCa+kSoQQpFirq6De3R+OYczZo6OPZSQvAQHn da4+hvQxOA90pzFRQ6/eQxyMHxc5R2uCKU/AcZ70= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Nathan Sidwell To: gcc-cvs@gcc.gnu.org Subject: [gcc/devel/c++-modules] d: Fix assignment to anonymous union member corrupts sibling members in struct X-Act-Checkin: gcc X-Git-Author: Iain Buclaw X-Git-Refname: refs/heads/devel/c++-modules X-Git-Oldrev: c62f5e6e1f457462b1cea74792833821bbea64bb X-Git-Newrev: 2691ffe6dbaffb704593dd6220178c28848b3855 Message-Id: <20200319124547.236C83877008@sourceware.org> Date: Thu, 19 Mar 2020 12:45:47 +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: Thu, 19 Mar 2020 12:45:47 -0000 https://gcc.gnu.org/g:2691ffe6dbaffb704593dd6220178c28848b3855 commit 2691ffe6dbaffb704593dd6220178c28848b3855 Author: Iain Buclaw Date: Mon Mar 16 23:53:20 2020 +0100 d: Fix assignment to anonymous union member corrupts sibling members in struct gcc/d/ChangeLog: PR d/92309 * types.cc (fixup_anonymous_offset): Don't set DECL_FIELD_OFFSET on anonymous fields. gcc/testsuite/ChangeLog: PR d/92309 * gdc.dg/pr92309.d: New test. Diff: --- gcc/d/ChangeLog | 6 ++++++ gcc/d/types.cc | 10 +++++++--- gcc/testsuite/ChangeLog | 5 +++++ gcc/testsuite/gdc.dg/pr92309.d | 25 +++++++++++++++++++++++++ 4 files changed, 43 insertions(+), 3 deletions(-) diff --git a/gcc/d/ChangeLog b/gcc/d/ChangeLog index d9c7657eaac..ea43e3e82bd 100644 --- a/gcc/d/ChangeLog +++ b/gcc/d/ChangeLog @@ -1,3 +1,9 @@ +2020-03-16 Iain Buclaw + + PR d/92309 + * types.cc (fixup_anonymous_offset): Don't set DECL_FIELD_OFFSET on + anonymous fields. + 2020-03-16 Iain Buclaw PR d/92216 diff --git a/gcc/d/types.cc b/gcc/d/types.cc index 736f128422c..866da965b40 100644 --- a/gcc/d/types.cc +++ b/gcc/d/types.cc @@ -234,16 +234,20 @@ insert_aggregate_field (tree type, tree field, size_t offset) static void fixup_anonymous_offset (tree fields, tree offset) { + /* No adjustment in field offset required. */ + if (integer_zerop (offset)) + return; + while (fields != NULL_TREE) { - /* Traverse all nested anonymous aggregates to update their offset. - Set the anonymous decl offset to its first member. */ + /* Traverse all nested anonymous aggregates to update the offset of their + fields. Note that the anonymous field itself is not adjusted, as it + already has an offset relative to its outer aggregate. */ tree ftype = TREE_TYPE (fields); if (TYPE_NAME (ftype) && IDENTIFIER_ANON_P (TYPE_IDENTIFIER (ftype))) { tree vfields = TYPE_FIELDS (ftype); fixup_anonymous_offset (vfields, offset); - DECL_FIELD_OFFSET (fields) = DECL_FIELD_OFFSET (vfields); } else { diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index a3820726772..0146f8d1759 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2020-03-16 Iain Buclaw + + PR d/92309 + * gdc.dg/pr92309.d: New test. + 2020-03-16 Iain Buclaw PR d/92216 diff --git a/gcc/testsuite/gdc.dg/pr92309.d b/gcc/testsuite/gdc.dg/pr92309.d new file mode 100644 index 00000000000..01ebc40d336 --- /dev/null +++ b/gcc/testsuite/gdc.dg/pr92309.d @@ -0,0 +1,25 @@ +// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92309 +// { dg-do run { target hw } } +// { dg-skip-if "needs gcc/config.d" { ! d_runtime } } + +union U +{ + struct + { + size_t a; + size_t b; + union + { + size_t c; + size_t d; + } + } +} + +void main() +{ + U u; + assert(u.a == 0); + u.d = 1; + assert(u.a == 0); +}