From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2071) id 071C4384E20A; Fri, 9 Dec 2022 22:09:28 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 071C4384E20A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1670623768; bh=ry41m1ZdWfVJJSVpD+yXK1BO1fDCIvOqnKOgQ5f4H/8=; h=From:To:Subject:Date:From; b=JOoKvItTKMJHi7Te2k/wxeJ7sNo6s9y6TcD+xn4AFWM/wxaCyoMrXNwReE+6n0lx3 9/021vU2T4rTY/o6SLVRY+aJnuyYlkNs3ELdt9x2n2u02iYj4r50bHH+rJ8XAToa13 dgKBHofVAuKaXQuS/uZOu1EpiscmTt5SntqdizYs= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Harald Anlauf To: gcc-cvs@gcc.gnu.org Subject: [gcc r13-4585] Fortran: ICE on recursive derived types with allocatable components [PR107872] X-Act-Checkin: gcc X-Git-Author: Paul Thomas X-Git-Refname: refs/heads/master X-Git-Oldrev: b2e1c49b4a4592f9e96ae9ece8af7d0e6527b194 X-Git-Newrev: 01254aa2eb766c7584fd047568d7277d4d65d067 Message-Id: <20221209220928.071C4384E20A@sourceware.org> Date: Fri, 9 Dec 2022 22:09:28 +0000 (GMT) List-Id: https://gcc.gnu.org/g:01254aa2eb766c7584fd047568d7277d4d65d067 commit r13-4585-g01254aa2eb766c7584fd047568d7277d4d65d067 Author: Paul Thomas Date: Fri Dec 9 22:13:45 2022 +0100 Fortran: ICE on recursive derived types with allocatable components [PR107872] 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. Diff: --- gcc/fortran/resolve.cc | 3 ++- gcc/testsuite/gfortran.dg/pr107872.f90 | 40 ++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/gcc/fortran/resolve.cc b/gcc/fortran/resolve.cc index 75dc4b59105..158bf08ec26 100644 --- a/gcc/fortran/resolve.cc +++ b/gcc/fortran/resolve.cc @@ -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 index 00000000000..09838479e92 --- /dev/null +++ b/gcc/testsuite/gfortran.dg/pr107872.f90 @@ -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