d: Fix assignment to anonymous union member corrupts sibling members in struct
authorIain Buclaw <ibuclaw@gdcproject.org>
Mon, 16 Mar 2020 22:53:20 +0000 (23:53 +0100)
committerIain Buclaw <ibuclaw@gdcproject.org>
Mon, 16 Mar 2020 22:56:38 +0000 (23:56 +0100)
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.

gcc/d/ChangeLog
gcc/d/types.cc
gcc/testsuite/ChangeLog
gcc/testsuite/gdc.dg/pr92309.d [new file with mode: 0644]

index d9c7657eaacab609924ebc829763796caf4c367d..ea43e3e82bd1ebcaa3a649bc15b94146544c5043 100644 (file)
@@ -1,3 +1,9 @@
+2020-03-16  Iain Buclaw  <ibuclaw@gdcproject.org>
+
+       PR d/92309
+       * types.cc (fixup_anonymous_offset): Don't set DECL_FIELD_OFFSET on
+       anonymous fields.
+
 2020-03-16  Iain Buclaw  <ibuclaw@gdcproject.org>
 
        PR d/92216
index 736f128422caffbe21b559cfa9da1e3ec3d9e64c..866da965b406a7af0da12d0207323fc4cec8ec9c 100644 (file)
@@ -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
        {
index a382072677232f482a0c32f63a46025b52837249..0146f8d1759a09abe702dbf6c34e2c301bd59d32 100644 (file)
@@ -1,3 +1,8 @@
+2020-03-16  Iain Buclaw  <ibuclaw@gdcproject.org>
+
+       PR d/92309
+       * gdc.dg/pr92309.d: New test.
+
 2020-03-16  Iain Buclaw  <ibuclaw@gdcproject.org>
 
        PR d/92216
diff --git a/gcc/testsuite/gdc.dg/pr92309.d b/gcc/testsuite/gdc.dg/pr92309.d
new file mode 100644 (file)
index 0000000..01ebc40
--- /dev/null
@@ -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);
+}