Fortran: ICE on recursive derived types with allocatable components [PR107872]
authorPaul Thomas <pault@gcc.gnu.org>
Fri, 9 Dec 2022 21:13:45 +0000 (22:13 +0100)
committerHarald Anlauf <anlauf@gmx.de>
Fri, 9 Dec 2022 21:19:26 +0000 (22:19 +0100)
gcc/fortran/ChangeLog:

PR fortran/107872
* resolve.cc (derived_inaccessible): Skip over allocatable components
to prevent an infinite loop.

gcc/testsuite/ChangeLog:

PR fortran/107872
* gfortran.dg/pr107872.f90: New test.

gcc/fortran/resolve.cc
gcc/testsuite/gfortran.dg/pr107872.f90 [new file with mode: 0644]

index 75dc4b59105f591238f20136d5a2e7e2ad53fac9..158bf08ec2676b2bac55c5edf3266739d174e750 100644 (file)
@@ -7536,7 +7536,8 @@ derived_inaccessible (gfc_symbol *sym)
   for (c = sym->components; c; c = c->next)
     {
        /* Prevent an infinite loop through this function.  */
-       if (c->ts.type == BT_DERIVED && c->attr.pointer
+       if (c->ts.type == BT_DERIVED
+           && (c->attr.pointer || c->attr.allocatable)
            && sym == c->ts.u.derived)
          continue;
 
diff --git a/gcc/testsuite/gfortran.dg/pr107872.f90 b/gcc/testsuite/gfortran.dg/pr107872.f90
new file mode 100644 (file)
index 0000000..0983847
--- /dev/null
@@ -0,0 +1,40 @@
+! { dg-do run }
+!
+! Test the fix for PR107872, where an ICE occurred in
+! resolve.cc(derived_inaccessible) because derived types with
+! recursive allocatable components were not catered for.
+!
+module mod1
+  type t
+     integer :: data
+     type(t), allocatable :: next
+   contains
+     procedure, private :: write_t
+     generic :: write(formatted) => write_t
+  end type
+contains
+  recursive subroutine write_t(this, unit, iotype, v_list, iostat, iomsg)
+    class(t), intent(in) :: this
+    integer, intent(in) :: unit
+    character(*), intent(in) :: iotype
+    integer, intent(in) :: v_list(:)
+    integer, intent(out) :: iostat
+    character(*), intent(inout) :: iomsg
+    if (ALLOCATED(this%next)) &
+         write (unit, '(dt)') this%next
+    write (unit, '(i2)') this%data
+  end subroutine
+end module
+
+  use mod1
+  type(t) :: a
+  character (8) :: buffer
+  a%data = 1
+  allocate (a%next)
+  a%next%data = 2
+  allocate (a%next%next)
+  a%next%next%data = 3
+  write (buffer, '(dt)')a
+  deallocate (a%next)
+  if (trim (buffer) .ne. ' 3 2 1') stop 1
+end