]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
Fortran: Ensure deep copy of allocatable components in cylic types [PR114612]
authorAndre Vehreschild <vehre@gcc.gnu.org>
Fri, 13 Dec 2024 11:07:01 +0000 (12:07 +0100)
committerAndre Vehreschild <vehre@gcc.gnu.org>
Tue, 7 Jan 2025 11:53:52 +0000 (12:53 +0100)
gcc/fortran/ChangeLog:

PR fortran/114612

* trans-array.cc (structure_alloc_comps): Ensure deep copy is
also done for types having cycles.

gcc/testsuite/ChangeLog:

* gfortran.dg/alloc_comp_deep_copy_4.f03: New test.

gcc/fortran/trans-array.cc
gcc/testsuite/gfortran.dg/alloc_comp_deep_copy_4.f03 [new file with mode: 0644]

index 057f6a63fdf5dd263c4d0b75888b7e8f52c868db..44b091af2c6900603fafba552ef323405cbc67a0 100644 (file)
@@ -10584,10 +10584,9 @@ structure_alloc_comps (gfc_symbol * der_type, tree decl, tree dest,
                                           false, false, NULL_TREE, NULL_TREE);
              gfc_add_expr_to_block (&fnblock, tmp);
            }
-         else if ((c->attr.allocatable)
-                   && !c->attr.proc_pointer && !same_type
-                   && (!(cmp_has_alloc_comps && c->as) || c->attr.codimension
-                       || caf_in_coarray (caf_mode)))
+         else if (c->attr.allocatable && !c->attr.proc_pointer
+                  && (!(cmp_has_alloc_comps && c->as) || c->attr.codimension
+                      || caf_in_coarray (caf_mode)))
            {
              rank = c->as ? c->as->rank : 0;
              if (c->attr.codimension)
diff --git a/gcc/testsuite/gfortran.dg/alloc_comp_deep_copy_4.f03 b/gcc/testsuite/gfortran.dg/alloc_comp_deep_copy_4.f03
new file mode 100644 (file)
index 0000000..3c445be
--- /dev/null
@@ -0,0 +1,29 @@
+!{ dg-do run }
+!
+! Contributed Vladimir Terzi  <vterzi1996@gmail.com>
+! Check that deep-copy for b=a works.
+
+program pr114672
+    type node
+        integer::val
+        type(node),allocatable::next
+    end type
+
+    type(node)::a,b
+
+    allocate(a%next)
+    a%val=1
+    a%next%val=2
+!    print*,a%val,a%next%val
+    b=a
+    b%val=3
+    b%next%val=4
+    if (loc(b) == loc(a)) stop 1
+    if (loc(b%next) == loc(a%next)) stop 2
+!    print*,a%val,a%next%val
+    deallocate(b%next)
+    if (.NOT. allocated(a%next)) stop 3
+!    print*,a%val,a%next%val
+    deallocate(a%next)
+end
+