From 4bbb79124984c46f0124c2855abf69c61008c42e Mon Sep 17 00:00:00 2001 From: Jean Perier Date: Thu, 21 Sep 2023 08:56:03 -0700 Subject: [PATCH 1/2] [flang][runtime] Finalize polymorphic components using dynamic type Previous code was finalizing polymorphic components according to static type (calling the static type final routine, if any). There is no way (I think) to know from a Fortran::runtime::typeInfo::Component if an allocatable component is polymorphic or not. So this patch just always uses the dynamic type descriptor to check for derived type allocatable component finalization. --- flang/runtime/derived.cpp | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/flang/runtime/derived.cpp b/flang/runtime/derived.cpp index 5224c1426479a..55545b41da8d4 100644 --- a/flang/runtime/derived.cpp +++ b/flang/runtime/derived.cpp @@ -209,7 +209,25 @@ void Finalize(const Descriptor &descriptor, k < myComponents; ++k) { const auto &comp{ *componentDesc.ZeroBasedIndexedElement(k)}; - if (comp.genre() == typeInfo::Component::Genre::Allocatable || + if (comp.genre() == typeInfo::Component::Genre::Allocatable && + comp.category() == TypeCategory::Derived) { + // Component may be polymorphic or unlimited polymorphic. Need to use the + // dynamic type to check if finalization is needed. + for (std::size_t j{0}; j < elements; ++j) { + const Descriptor &compDesc{*descriptor.OffsetElement( + j * byteStride + comp.offset())}; + if (compDesc.IsAllocated()) { + if (const DescriptorAddendum * addendum{compDesc.Addendum()}) { + if (const typeInfo::DerivedType * + compDynamicType{addendum->derivedType()}) { + if (!compDynamicType->noFinalizationNeeded()) { + Finalize(compDesc, *compDynamicType, terminator); + } + } + } + } + } + } else if (comp.genre() == typeInfo::Component::Genre::Allocatable || comp.genre() == typeInfo::Component::Genre::Automatic) { if (const typeInfo::DerivedType * compType{comp.derivedType()}) { if (!compType->noFinalizationNeeded()) { From f63fe0f8dce518bcde029f58dfbc7aaf361d00ee Mon Sep 17 00:00:00 2001 From: Jean Perier Date: Thu, 21 Sep 2023 10:13:24 -0700 Subject: [PATCH 2/2] rephrase comment as suggested --- flang/runtime/derived.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flang/runtime/derived.cpp b/flang/runtime/derived.cpp index 55545b41da8d4..5edc47c4a9489 100644 --- a/flang/runtime/derived.cpp +++ b/flang/runtime/derived.cpp @@ -212,7 +212,7 @@ void Finalize(const Descriptor &descriptor, if (comp.genre() == typeInfo::Component::Genre::Allocatable && comp.category() == TypeCategory::Derived) { // Component may be polymorphic or unlimited polymorphic. Need to use the - // dynamic type to check if finalization is needed. + // dynamic type to check whether finalization is needed. for (std::size_t j{0}; j < elements; ++j) { const Descriptor &compDesc{*descriptor.OffsetElement( j * byteStride + comp.offset())};