diff --git a/flang/lib/Semantics/compute-offsets.cpp b/flang/lib/Semantics/compute-offsets.cpp index 8789f212feacf..c44660925622b 100644 --- a/flang/lib/Semantics/compute-offsets.cpp +++ b/flang/lib/Semantics/compute-offsets.cpp @@ -321,11 +321,12 @@ auto ComputeOffsetsHelper::GetSizeAndAlignment( const Symbol &symbol, bool entire) -> SizeAndAlignment { auto &targetCharacteristics{context_.targetCharacteristics()}; if (IsDescriptor(symbol)) { - const auto *derived{ - evaluate::GetDerivedTypeSpec(evaluate::DynamicType::From(symbol))}; + auto dyType{evaluate::DynamicType::From(symbol)}; + const auto *derived{evaluate::GetDerivedTypeSpec(dyType)}; int lenParams{derived ? CountLenParameters(*derived) : 0}; + bool needAddendum{derived || (dyType && dyType->IsUnlimitedPolymorphic())}; std::size_t size{runtime::Descriptor::SizeInBytes( - symbol.Rank(), derived != nullptr, lenParams)}; + symbol.Rank(), needAddendum, lenParams)}; return {size, targetCharacteristics.descriptorAlignment()}; } if (IsProcedurePointer(symbol)) { diff --git a/flang/runtime/derived.cpp b/flang/runtime/derived.cpp index 354de1baddd99..981ddb2a6e9d4 100644 --- a/flang/runtime/derived.cpp +++ b/flang/runtime/derived.cpp @@ -20,8 +20,8 @@ int Initialize(const Descriptor &instance, const typeInfo::DerivedType &derived, std::size_t elements{instance.Elements()}; std::size_t byteStride{instance.ElementBytes()}; int stat{StatOk}; - // Initialize data components in each element; the per-element iteration - // constitutes the inner loops, not outer + // Initialize data components in each element; the per-element iterations + // constitute the inner loops, not the outer ones std::size_t myComponents{componentDesc.Elements()}; for (std::size_t k{0}; k < myComponents; ++k) { const auto &comp{ @@ -36,7 +36,14 @@ int Initialize(const Descriptor &instance, const typeInfo::DerivedType &derived, if (comp.genre() == typeInfo::Component::Genre::Automatic) { stat = ReturnError(terminator, allocDesc.Allocate(), errMsg, hasStat); if (stat == StatOk) { - stat = Initialize(allocDesc, derived, terminator, hasStat, errMsg); + if (const DescriptorAddendum * addendum{allocDesc.Addendum()}) { + if (const auto *derived{addendum->derivedType()}) { + if (!derived->noInitializationNeeded()) { + stat = Initialize( + allocDesc, *derived, terminator, hasStat, errMsg); + } + } + } } if (stat != StatOk) { break;