diff --git a/flang/lib/Optimizer/HLFIR/Transforms/BufferizeHLFIR.cpp b/flang/lib/Optimizer/HLFIR/Transforms/BufferizeHLFIR.cpp index 9109f2b331567..bff3c2ce528bd 100644 --- a/flang/lib/Optimizer/HLFIR/Transforms/BufferizeHLFIR.cpp +++ b/flang/lib/Optimizer/HLFIR/Transforms/BufferizeHLFIR.cpp @@ -445,8 +445,16 @@ struct AssociateOpConversion !mlir::isa(assocType)) || ((mlir::isa(sourceVar.getType()) && !mlir::isa(assocType)))) { - sourceVar = - fir::BoxAddrOp::create(builder, loc, assocType, sourceVar); + sourceVar = builder.create(loc, assocType, sourceVar); + } else if (mlir::isa(sourceVar.getType()) && + mlir::isa(assocType)) { + mlir::Value lenVal = associate.getTypeparams()[0]; + auto refTy = mlir::cast(sourceVar.getType()); + auto charTy = mlir::cast(refTy.getEleTy()); + auto boxCharType = + fir::BoxCharType::get(builder.getContext(), charTy.getFKind()); + sourceVar = builder.create(loc, boxCharType, + sourceVar, lenVal); } else { sourceVar = builder.createConvert(loc, assocType, sourceVar); } diff --git a/flang/test/Lower/adjustr.f90 b/flang/test/Lower/adjustr.f90 new file mode 100644 index 0000000000000..6eda4952ecca0 --- /dev/null +++ b/flang/test/Lower/adjustr.f90 @@ -0,0 +1,16 @@ +! RUN: %flang_fc1 -emit-fir -o - %s | FileCheck %s + +! Checking no reference to boxchar conversion is in the emitted fir. +! CHECK-NOT: (!fir.ref>) -> !fir.boxchar<1> +! CHECK: %[[Const4:.*]] = arith.constant 4 : index +! CHECK: fir.emboxchar + +program main + logical ,dimension(1):: mask = .true. + character(len=2),dimension(1):: d1 = "a " + character(len=4),dimension(1):: d4 + where (mask) + d4 = adjustr(d1 // d1) + end where + write(6,*) "d4 =", d4 +end