diff --git a/flang/lib/Lower/ConvertVariable.cpp b/flang/lib/Lower/ConvertVariable.cpp index ef1f68f7e0ebc..46a59b38ae6ab 100644 --- a/flang/lib/Lower/ConvertVariable.cpp +++ b/flang/lib/Lower/ConvertVariable.cpp @@ -1947,13 +1947,15 @@ void Fortran::lower::mapSymbolAttributes( if (ba.isChar()) { if (arg) { assert(!preAlloc && "dummy cannot be pre-allocated"); - if (arg.getType().isa()) { + if (arg.getType().isa()) std::tie(addr, len) = charHelp.createUnboxChar(arg); - // Ensure proper type is given to array/scalar that transited via - // fir.boxchar arg. - mlir::Type castTy = builder.getRefType(converter.genType(var)); - addr = builder.createConvert(loc, castTy, addr); - } + else if (!addr) + addr = arg; + // Ensure proper type is given to array/scalar that was transmitted as a + // fir.boxchar arg or is a statement function actual argument with + // a different length than the dummy. + mlir::Type castTy = builder.getRefType(converter.genType(var)); + addr = builder.createConvert(loc, castTy, addr); } if (std::optional cstLen = ba.getCharLenConst()) { // Static length diff --git a/flang/test/Lower/HLFIR/statement-functions.f90 b/flang/test/Lower/HLFIR/statement-functions.f90 index f66b285ed9452..d19b912e0fe21 100644 --- a/flang/test/Lower/HLFIR/statement-functions.f90 +++ b/flang/test/Lower/HLFIR/statement-functions.f90 @@ -33,3 +33,17 @@ subroutine char_test(c, n) ! CHECK: %[[VAL_18:.*]]:2 = hlfir.declare %[[VAL_13]]#0 typeparams %[[VAL_17]] {uniq_name = "_QFchar_testFstmt_funcEchar_stmt_func_dummy_arg"} : (!fir.ref>, i32) -> (!fir.boxchar<1>, !fir.ref>) ! CHECK: %[[VAL_19:.*]] = arith.constant 10 : i64 ! CHECK: %[[VAL_20:.*]] = hlfir.set_length %[[VAL_18]]#0 len %[[VAL_19]] : (!fir.boxchar<1>, i64) -> !hlfir.expr> + +subroutine char_test2(c) + character(10) :: c + character(5) :: c_stmt_func + character(*), parameter :: padding = "padding" + character(len(c_stmt_func)+len(padding)) :: stmt_func + stmt_func(c_stmt_func) = c_stmt_func // padding + call test(stmt_func(c)) +end subroutine +! CHECK-LABEL: func.func @_QPchar_test2( +! CHECK: %[[C:.*]]:2 = hlfir.declare %1 typeparams %c10 {uniq_name = "_QFchar_test2Ec"} : (!fir.ref>, index) -> (!fir.ref>, !fir.ref>) +! CHECK: %[[CAST:.*]] = fir.convert %[[C]]#0 : (!fir.ref>) -> !fir.ref> +! CHECK: %[[C_STMT_FUNC:.*]]:2 = hlfir.declare %[[CAST]] typeparams %c5{{.*}} {uniq_name = "_QFchar_test2Fstmt_funcEc_stmt_func"} : (!fir.ref>, index) -> (!fir.ref>, !fir.ref>) +! CHECK: hlfir.concat %[[C_STMT_FUNC]]#0, %{{.*}} len %{{.*}} : (!fir.ref>, !fir.ref>, index) -> !hlfir.expr>