Skip to content

Commit e89a00d

Browse files
committed
[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
1 parent e340e9e commit e89a00d

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

flang/runtime/derived.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,14 +225,18 @@ void Destroy(const Descriptor &descriptor, bool finalize,
225225
std::size_t myComponents{componentDesc.Elements()};
226226
std::size_t elements{descriptor.Elements()};
227227
std::size_t byteStride{descriptor.ElementBytes()};
228+
SubscriptValue at[maxRank];
229+
descriptor.GetLowerBounds(at);
228230
for (std::size_t k{0}; k < myComponents; ++k) {
229231
const auto &comp{
230232
*componentDesc.ZeroBasedIndexedElement<typeInfo::Component>(k)};
231233
if (comp.genre() == typeInfo::Component::Genre::Allocatable ||
232234
comp.genre() == typeInfo::Component::Genre::Automatic) {
233235
for (std::size_t j{0}; j < elements; ++j) {
234-
descriptor.OffsetElement<Descriptor>(j * byteStride + comp.offset())
235-
->Deallocate();
236+
Descriptor *d{reinterpret_cast<Descriptor *>(
237+
descriptor.Element<char>(at) + comp.offset())};
238+
d->Deallocate();
239+
descriptor.IncrementSubscripts(at);
236240
}
237241
}
238242
}

0 commit comments

Comments
 (0)