@@ -4730,14 +4730,23 @@ class FirConverter : public Fortran::lower::AbstractConverter {
47304730 if (Fortran::evaluate::UnwrapExpr<Fortran::evaluate::NullPointer>(
47314731 assign.rhs ))
47324732 return fir::factory::createUnallocatedBox (*builder, loc, lhsBoxType, {});
4733- hlfir::Entity rhs{ fir::getBase ( genExprBox (loc, assign. rhs , rhsContext))};
4734- auto rhsBoxType =
4735- llvm::cast<fir::BaseBoxType>( fir::unwrapRefType ( rhs.getType ()) );
4733+ hlfir::Entity rhs = Fortran::lower::convertExprToHLFIR (
4734+ loc, * this , assign. rhs , localSymbols, rhsContext);
4735+ auto rhsBoxType = rhs.getBoxType ( );
47364736 // Create pointer descriptor value from the RHS.
47374737 if (rhs.isMutableBox ())
47384738 rhs = hlfir::Entity{fir::LoadOp::create (*builder, loc, rhs)};
4739- mlir::Value rhsBox = hlfir::genVariableBox (
4740- loc, *builder, rhs, rhsBoxType.getBoxTypeWithNewShape (rhs.getRank ()));
4739+
4740+ // Use LHS type if LHS is not polymorphic.
4741+ fir::BaseBoxType targetBoxType;
4742+ if (assign.lhs .GetType ()->IsPolymorphic ())
4743+ targetBoxType = rhsBoxType.getBoxTypeWithNewAttr (
4744+ fir::BaseBoxType::Attribute::Pointer);
4745+ else
4746+ targetBoxType = lhsBoxType.getBoxTypeWithNewShape (rhs.getRank ());
4747+ mlir::Value rhsBox =
4748+ hlfir::genVariableBox (loc, *builder, rhs, targetBoxType);
4749+
47414750 // Apply lower bounds or reshaping if any.
47424751 if (const auto *lbExprs =
47434752 std::get_if<Fortran::evaluate::Assignment::BoundsSpec>(&assign.u );
0 commit comments