diff --git a/flang/lib/Optimizer/Builder/IntrinsicCall.cpp b/flang/lib/Optimizer/Builder/IntrinsicCall.cpp --- a/flang/lib/Optimizer/Builder/IntrinsicCall.cpp +++ b/flang/lib/Optimizer/Builder/IntrinsicCall.cpp @@ -5073,8 +5073,9 @@ // Create mutable fir.box to be passed to the runtime for the result. mlir::Type resultArrayType = builder.getVarLenSeqTy(resultType, maskRank); - fir::MutableBoxValue resultMutableBox = - fir::factory::createTempMutableBox(builder, loc, resultArrayType); + fir::MutableBoxValue resultMutableBox = fir::factory::createTempMutableBox( + builder, loc, resultArrayType, {}, + fir::isPolymorphicType(vector.getType()) ? vector : mlir::Value{}); mlir::Value resultIrBox = fir::factory::getMutableIRBox(builder, loc, resultMutableBox); diff --git a/flang/test/Lower/polymorphic-temp.f90 b/flang/test/Lower/polymorphic-temp.f90 --- a/flang/test/Lower/polymorphic-temp.f90 +++ b/flang/test/Lower/polymorphic-temp.f90 @@ -102,4 +102,23 @@ ! CHECK: %[[MASK_BOX_NONE:.*]] = fir.convert %[[EMBOXED_MASK]] : (!fir.box>>) -> !fir.box ! CHECK: %{{.*}} = fir.call @_FortranAPack(%[[RES_BOX_NONE]], %[[I_BOX_NONE]], %[[MASK_BOX_NONE]], %{{.*}}, %{{.*}}, %{{.*}}) {{.*}} : (!fir.ref>, !fir.box, !fir.box, !fir.box, !fir.ref, i32) -> none + subroutine check_unpack(r) + class(p1), intent(in) :: r(:,:) + end subroutine + + subroutine test_temp_from_unpack(v, m, f) + class(p1), intent(in) :: v(:), f(:,:) + logical, intent(in) :: m(:,:) + call check_unpack(unpack(v,m,f)) + end subroutine + +! CHECK-LABEL: func.func @_QMpoly_tmpPtest_temp_from_unpack( +! CHECK-SAME: %[[V:.*]]: !fir.class>> {fir.bindc_name = "v"}, %[[M:.*]]: !fir.box>> {fir.bindc_name = "m"}, %[[F:.*]]: !fir.class>> {fir.bindc_name = "f"}) { +! CHECK: %[[TMP_RES:.*]] = fir.alloca !fir.class>>> +! CHECK: %[[TMP_BOX_NONE:.*]] = fir.convert %[[TMP_RES]] : (!fir.ref>>>>) -> !fir.ref> +! CHECK: %[[V_BOX_NONE:.*]] = fir.convert %[[V]] : (!fir.class>>) -> !fir.box +! CHECK: %[[M_BOX_NONE:.*]] = fir.convert %[[M]] : (!fir.box>>) -> !fir.box +! CHECK: %[[F_BOX_NONE:.*]] = fir.convert %[[F]] : (!fir.class>>) -> !fir.box +! CHECK: %{{.*}} = fir.call @_FortranAUnpack(%[[TMP_BOX_NONE]], %[[V_BOX_NONE]], %[[M_BOX_NONE]], %[[F_BOX_NONE]], %{{.*}}, %{{.*}}) {{.*}} : (!fir.ref>, !fir.box, !fir.box, !fir.box, !fir.ref, i32) -> none + end module