From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1928) id 0E75A3858C78; Fri, 29 Sep 2023 13:13:54 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 0E75A3858C78 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1695993234; bh=i9cE06zwkPCzEHwanrfgTN3eGFRM7XnacFbfDWEMAWs=; h=From:To:Subject:Date:From; b=xpf2OClirRMdNrxJ/iltNAcb8AhQcbl4ew3+xwUFHHCSM1Ecq7zj5yQo7TgadZwUX pa/0hSoH8MrPjvm6dfFsy1ZGbSNdmGR05jXIQw0LiAUd1Jo1I7HdHqW1sEFHzKaOMa Vy1AcRS2xH5QvV+oYVV9wyw8Khypi0agspNSzijo= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Andre Vehreschild To: gcc-cvs@gcc.gnu.org Subject: [gcc r13-7923] Fortran: Free alloc. comp. in allocated coarrays only. X-Act-Checkin: gcc X-Git-Author: Andre Vehreschild X-Git-Refname: refs/heads/releases/gcc-13 X-Git-Oldrev: b5b98a2d055d967d1fc92859827839c83c9368d7 X-Git-Newrev: d9b3269bdccac2db9200303494c4e82f2aeb7bbc Message-Id: <20230929131354.0E75A3858C78@sourceware.org> Date: Fri, 29 Sep 2023 13:13:54 +0000 (GMT) List-Id: https://gcc.gnu.org/g:d9b3269bdccac2db9200303494c4e82f2aeb7bbc commit r13-7923-gd9b3269bdccac2db9200303494c4e82f2aeb7bbc Author: Andre Vehreschild Date: Thu Sep 28 09:30:12 2023 +0200 Fortran: Free alloc. comp. in allocated coarrays only. When freeing allocatable components of an allocatable coarray, add a check that the coarray is still allocated, before accessing the components. This patch adds to PR fortran/37336, but does not fix it completely. gcc/fortran/ChangeLog: PR fortran/37336 * trans-array.cc (structure_alloc_comps): Deref coarray. (gfc_trans_deferred_array): Add freeing of components after check for allocated coarray. gcc/testsuite/ChangeLog: PR fortran/37336 * gfortran.dg/coarray/alloc_comp_6.f90: New test. * gfortran.dg/coarray/alloc_comp_7.f90: New test. (cherry picked from commit 9a63a62dfd73e159f1956e9b04b555c445de4e78) Diff: --- gcc/fortran/trans-array.cc | 16 ++++++- gcc/testsuite/gfortran.dg/coarray/alloc_comp_6.f90 | 29 +++++++++++++ gcc/testsuite/gfortran.dg/coarray/alloc_comp_7.f90 | 49 ++++++++++++++++++++++ 3 files changed, 92 insertions(+), 2 deletions(-) diff --git a/gcc/fortran/trans-array.cc b/gcc/fortran/trans-array.cc index e70af03d16d..35fc77e7541 100644 --- a/gcc/fortran/trans-array.cc +++ b/gcc/fortran/trans-array.cc @@ -9200,6 +9200,12 @@ structure_alloc_comps (gfc_symbol * der_type, tree decl, tree dest, gfc_add_expr_to_block (&fnblock, tmp); } + /* Still having a descriptor array of rank == 0 here, indicates an + allocatable coarrays. Dereference it correctly. */ + if (GFC_DESCRIPTOR_TYPE_P (decl_type)) + { + decl = build_fold_indirect_ref (gfc_conv_array_data (decl)); + } /* Otherwise, act on the components or recursively call self to act on a chain of components. */ for (c = der_type->components; c; c = c->next) @@ -11387,7 +11393,11 @@ gfc_trans_deferred_array (gfc_symbol * sym, gfc_wrapped_block * block) { int rank; rank = sym->as ? sym->as->rank : 0; - tmp = gfc_deallocate_alloc_comp (sym->ts.u.derived, descriptor, rank); + tmp = gfc_deallocate_alloc_comp (sym->ts.u.derived, descriptor, rank, + (sym->attr.codimension + && flag_coarray == GFC_FCOARRAY_LIB) + ? GFC_STRUCTURE_CAF_MODE_IN_COARRAY + : 0); gfc_add_expr_to_block (&cleanup, tmp); } @@ -11401,9 +11411,11 @@ gfc_trans_deferred_array (gfc_symbol * sym, gfc_wrapped_block * block) NULL_TREE, NULL_TREE, true, e, sym->attr.codimension ? GFC_CAF_COARRAY_DEREGISTER - : GFC_CAF_COARRAY_NOCOARRAY); + : GFC_CAF_COARRAY_NOCOARRAY, + NULL_TREE, gfc_finish_block (&cleanup)); if (e) gfc_free_expr (e); + gfc_init_block (&cleanup); gfc_add_expr_to_block (&cleanup, tmp); } diff --git a/gcc/testsuite/gfortran.dg/coarray/alloc_comp_6.f90 b/gcc/testsuite/gfortran.dg/coarray/alloc_comp_6.f90 new file mode 100644 index 00000000000..e8a74db2c18 --- /dev/null +++ b/gcc/testsuite/gfortran.dg/coarray/alloc_comp_6.f90 @@ -0,0 +1,29 @@ +! { dg-do run } + +program alloc_comp_6 + + implicit none + + type :: foo + real :: x + integer, allocatable :: y(:) + end type + + call check() + +contains + + subroutine check() + block + type(foo), allocatable :: example[:] ! needs to be a coarray + + allocate(example[*]) + allocate(example%y(10)) + example%x = 3.4 + example%y = 4 + + deallocate(example) + end block ! example%y shall not be accessed here by the finalizer, + ! because example is already deallocated + end subroutine check +end program alloc_comp_6 diff --git a/gcc/testsuite/gfortran.dg/coarray/alloc_comp_7.f90 b/gcc/testsuite/gfortran.dg/coarray/alloc_comp_7.f90 new file mode 100644 index 00000000000..5ebd31f3df7 --- /dev/null +++ b/gcc/testsuite/gfortran.dg/coarray/alloc_comp_7.f90 @@ -0,0 +1,49 @@ +! { dg-do run } + +module alloc_comp_module_7 + + public :: check + + type :: foo + real :: x + integer, allocatable :: y(:) + contains + final :: foo_final + end type + +contains + + subroutine foo_final(f) + type(foo), intent(inout) :: f + + if (allocated(f%y)) then + f%y = -1 + end if + end subroutine foo_final + + subroutine check() + block + type(foo), allocatable :: example[:] ! needs to be a coarray + + allocate(example[*]) + allocate(example%y(10)) + example%x = 3.4 + example%y = 4 + + deallocate(example%y) + deallocate(example) + end block ! example%y shall not be accessed here by the finalizer, + ! because example is already deallocated + end subroutine check +end module alloc_comp_module_7 + +program alloc_comp_7 + + use alloc_comp_module_7, only: check + + implicit none + + call check() + +end program alloc_comp_7 +