diff --git a/flang/runtime/allocatable.cpp b/flang/runtime/allocatable.cpp --- a/flang/runtime/allocatable.cpp +++ b/flang/runtime/allocatable.cpp @@ -151,8 +151,15 @@ descriptor, hasStat, errMsg, sourceFile, sourceLine)}; if (stat == StatOk) { DescriptorAddendum *addendum{descriptor.Addendum()}; - INTERNAL_CHECK(addendum != nullptr); - addendum->set_derivedType(derivedType); + if (addendum) { // Unlimited polymorphic allocated from intrinsic type spec + // does not have + addendum->set_derivedType(derivedType); + } else { + // Unlimited polymorphic descriptors initialized with + // AllocatableInitIntrinsic do not have an addendum. Make sure the + // derivedType is null in that case. + INTERNAL_CHECK(!derivedType); + } } return stat; } diff --git a/flang/runtime/pointer.cpp b/flang/runtime/pointer.cpp --- a/flang/runtime/pointer.cpp +++ b/flang/runtime/pointer.cpp @@ -168,8 +168,14 @@ pointer, hasStat, errMsg, sourceFile, sourceLine)}; if (stat == StatOk) { DescriptorAddendum *addendum{pointer.Addendum()}; - INTERNAL_CHECK(addendum != nullptr); - addendum->set_derivedType(derivedType); + if (addendum) { + addendum->set_derivedType(derivedType); + } else { + // Unlimited polymorphic descriptors initialized with + // PointerNullifyIntrinsic do not have an addendum. Make sure the + // derivedType is null in that case. + INTERNAL_CHECK(!derivedType); + } } return stat; } diff --git a/flang/unittests/Runtime/Pointer.cpp b/flang/unittests/Runtime/Pointer.cpp --- a/flang/unittests/Runtime/Pointer.cpp +++ b/flang/unittests/Runtime/Pointer.cpp @@ -51,3 +51,15 @@ EXPECT_EQ(p->ElementBytes(), m->ElementBytes()); EXPECT_EQ(p->type(), m->type()); } + +TEST(Pointer, DeallocatePolymorphic) { + // CLASS(*) :: p + // ALLOCATE(integer::p) + auto p{Descriptor::Create(TypeCode{Fortran::common::TypeCategory::Integer, 4}, + 4, nullptr, 0, nullptr, CFI_attribute_pointer)}; + RTNAME(PointerAllocate) + (*p, /*hasStat=*/false, /*errMsg=*/nullptr, __FILE__, __LINE__); + // DEALLOCATE(p) + RTNAME(PointerDeallocatePolymorphic) + (*p, nullptr, /*hasStat=*/false, /*errMsg=*/nullptr, __FILE__, __LINE__); +}