diff --git a/flang/lib/Optimizer/Builder/FIRBuilder.cpp b/flang/lib/Optimizer/Builder/FIRBuilder.cpp --- a/flang/lib/Optimizer/Builder/FIRBuilder.cpp +++ b/flang/lib/Optimizer/Builder/FIRBuilder.cpp @@ -337,12 +337,12 @@ return create(loc, toTy, val); } - if (fir::isPolymorphicType(fromTy) && - (fir::isAllocatableType(fromTy) || fir::isPointerType(fromTy)) && - fir::isPolymorphicType(toTy)) { + if ((fir::isPolymorphicType(fromTy) && + (fir::isAllocatableType(fromTy) || fir::isPointerType(fromTy)) && + fir::isPolymorphicType(toTy)) || + (fir::isPolymorphicType(fromTy) && toTy.isa())) return create(loc, toTy, val, mlir::Value{}, /*slice=*/mlir::Value{}); - } return createConvert(loc, toTy, val); } diff --git a/flang/test/Lower/polymorphic.f90 b/flang/test/Lower/polymorphic.f90 --- a/flang/test/Lower/polymorphic.f90 +++ b/flang/test/Lower/polymorphic.f90 @@ -165,4 +165,21 @@ ! CHECK-SAME: %[[ARG0:.*]]: !fir.class}>> {fir.bindc_name = "c"}) { ! CHECK: %{{.*}} = fir.dispatch "get_tmp"(%[[ARG0]] : !fir.class}>>) (%{{.*}}, %{{.*}}, %[[ARG0]] : !fir.ref>, index, !fir.class}>>) -> !fir.boxchar<1> {pass_arg_pos = 2 : i32} + subroutine sub_with_type_array(a) + type(p1) :: a(:) + end subroutine + +! CHECK-LABEL: func.func @_QMpolymorphic_testPsub_with_type_array(%{{.*}}: !fir.box>> {fir.bindc_name = "a"}) + + subroutine call_sub_with_type_array(p) + class(p1), pointer :: p(:) + call sub_with_type_array(p) + end subroutine + +! CHECK-LABEL: func.func @_QMpolymorphic_testPcall_sub_with_type_array( +! CHECK-SAME: %[[ARG0:.*]]: !fir.ref>>>> {fir.bindc_name = "p"}) { +! CHECK: %[[CLASS:.*]] = fir.load %[[ARG0]] : !fir.ref>>>> +! CHECK: %[[REBOX:.*]] = fir.rebox %[[CLASS]] : (!fir.class>>>) -> !fir.box>> +! CHECK: fir.call @_QMpolymorphic_testPsub_with_type_array(%[[REBOX]]) {{.*}} : (!fir.box>>) -> () + end module