diff --git a/flang/include/flang/Optimizer/Builder/BoxValue.h b/flang/include/flang/Optimizer/Builder/BoxValue.h --- a/flang/include/flang/Optimizer/Builder/BoxValue.h +++ b/flang/include/flang/Optimizer/Builder/BoxValue.h @@ -250,9 +250,12 @@ return fir::isRecordWithTypeParameters(getEleTy()); } - /// Is this a CLASS(*)/TYPE(*) ? + /// Is this a polymorphic entity? + bool isPolymorphic() const { return fir::isPolymorphicType(getBoxTy()); } + + /// Is this a CLASS(*)/TYPE(*)? bool isUnlimitedPolymorphic() const { - return fir::isUnlimitedPolymorphicType(getBaseTy()); + return fir::isUnlimitedPolymorphicType(getBoxTy()); } }; diff --git a/flang/lib/Lower/Allocatable.cpp b/flang/lib/Lower/Allocatable.cpp --- a/flang/lib/Lower/Allocatable.cpp +++ b/flang/lib/Lower/Allocatable.cpp @@ -511,7 +511,9 @@ const fir::MutableBoxValue &box, ErrorManager &errorManager) { // Deallocate intrinsic types inline. - if (!box.isDerived() && !errorManager.hasStatSpec() && !useAllocateRuntime) { + if (!box.isDerived() && !box.isPolymorphic() && + !box.isUnlimitedPolymorphic() && !errorManager.hasStatSpec() && + !useAllocateRuntime) { fir::factory::genInlinedDeallocate(builder, loc, box); return; } diff --git a/flang/test/Lower/polymorphic-types.f90 b/flang/test/Lower/polymorphic-types.f90 --- a/flang/test/Lower/polymorphic-types.f90 +++ b/flang/test/Lower/polymorphic-types.f90 @@ -98,6 +98,15 @@ ! CHECK-LABEL: func.func @_QMpolymorphic_typesPunlimited_polymorphic_pointer( ! CHECK-SAME: %{{.*}}: !fir.ref>> + subroutine unlimited_polymorphic_allocatable_intentout(p) + class(*), allocatable, intent(out) :: p + end subroutine + +! CHECK-LABEL: func.func @_QMpolymorphic_typesPunlimited_polymorphic_allocatable_intentout( +! CHECK-SAME: %[[ARG0:.*]]: !fir.ref>> +! CHECK: %[[BOX_NONE:.*]] = fir.convert %[[ARG0]] : (!fir.ref>>) -> !fir.ref> +! CHECK: %{{.*}} = fir.call @_FortranAAllocatableDeallocate(%[[BOX_NONE]], %{{.*}}, %{{.*}}, %{{.*}}, %{{.*}}) : (!fir.ref>, i1, !fir.box, !fir.ref, i32) -> i32 + ! ------------------------------------------------------------------------------ ! Test polymorphic function return types ! ------------------------------------------------------------------------------