From e89a00db6d0596fe0263ca0a1052e5e6859744e9 Mon Sep 17 00:00:00 2001 From: Valentin Clement Date: Thu, 23 Feb 2023 17:26:52 +0100 Subject: [PATCH] [flang] Fix element indexing in Destroy component deallocation Derived type `Destroy` function does not take step into consideration when indexing the component element for deallocation. This leads to incorrect deallocation in case like: ``` module mod1 type :: t real, allocatable :: r(:) end type contains subroutine do_smth(c) class(t), intent(out) :: c(:) do i = 1, size(c) if (allocated(c(i)%r)) then print*, i, 'not deallocated' end if end do end subroutine end module program test use mod1 type(t) :: z(6) integer :: i do i = 1, 6 Allocate(z(i)%r(i)) end do call do_smth(z(::2)) end ``` Similar change was done in D142527 Reviewed By: klausler Differential Revision: https://reviews.llvm.org/D144553 --- flang/runtime/derived.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/flang/runtime/derived.cpp b/flang/runtime/derived.cpp index 05c5b52..5bfecdc 100644 --- a/flang/runtime/derived.cpp +++ b/flang/runtime/derived.cpp @@ -225,14 +225,18 @@ void Destroy(const Descriptor &descriptor, bool finalize, std::size_t myComponents{componentDesc.Elements()}; std::size_t elements{descriptor.Elements()}; std::size_t byteStride{descriptor.ElementBytes()}; + SubscriptValue at[maxRank]; + descriptor.GetLowerBounds(at); for (std::size_t k{0}; k < myComponents; ++k) { const auto &comp{ *componentDesc.ZeroBasedIndexedElement(k)}; if (comp.genre() == typeInfo::Component::Genre::Allocatable || comp.genre() == typeInfo::Component::Genre::Automatic) { for (std::size_t j{0}; j < elements; ++j) { - descriptor.OffsetElement(j * byteStride + comp.offset()) - ->Deallocate(); + Descriptor *d{reinterpret_cast( + descriptor.Element(at) + comp.offset())}; + d->Deallocate(); + descriptor.IncrementSubscripts(at); } } } -- 2.7.4