Skip to content

Commit

Permalink
[flang] Fix element indexing in Destroy component deallocation
Browse files Browse the repository at this point in the history
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
  • Loading branch information
clementval committed Feb 23, 2023
1 parent e340e9e commit e89a00d
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions flang/runtime/derived.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeInfo::Component>(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<Descriptor>(j * byteStride + comp.offset())
->Deallocate();
Descriptor *d{reinterpret_cast<Descriptor *>(
descriptor.Element<char>(at) + comp.offset())};
d->Deallocate();
descriptor.IncrementSubscripts(at);
}
}
}
Expand Down

0 comments on commit e89a00d

Please sign in to comment.