diff --git a/flang/lib/Evaluate/fold-implementation.h b/flang/lib/Evaluate/fold-implementation.h --- a/flang/lib/Evaluate/fold-implementation.h +++ b/flang/lib/Evaluate/fold-implementation.h @@ -1349,6 +1349,7 @@ template std::optional> MapOperation(FoldingContext &context, std::function(Expr &&)> &&f, const Shape &shape, + [[maybe_unused]] std::optional> &&length, Expr &&values) { ArrayConstructor result{values}; if constexpr (common::HasMember) { @@ -1369,6 +1370,11 @@ result.Push(Fold(context, f(std::move(scalar)))); } } + if constexpr (RESULT::category == TypeCategory::Character) { + if (length) { + result.set_LEN(std::move(*length)); + } + } return FromArrayConstructor(context, std::move(result), shape); } @@ -1506,9 +1512,9 @@ return FromArrayConstructor(context, std::move(result), shape); } -template +template std::optional> ComputeResultLength( - Operation &operation) { + Operation &operation) { if constexpr (RESULT::category == TypeCategory::Character) { return Expr{operation.derived()}.LEN(); } @@ -1529,7 +1535,8 @@ if (expr.Rank() > 0) { if (std::optional shape{GetShape(context, expr)}) { if (auto values{AsFlatArrayConstructor(expr)}) { - return MapOperation(context, std::move(f), *shape, std::move(*values)); + return MapOperation(context, std::move(f), *shape, + ComputeResultLength(operation), std::move(*values)); } } } diff --git a/flang/test/Evaluate/folding31.f90 b/flang/test/Evaluate/folding31.f90 new file mode 100644 --- /dev/null +++ b/flang/test/Evaluate/folding31.f90 @@ -0,0 +1,6 @@ +! RUN: %python %S/test_folding.py %s %flang_fc1 +! Test folding of character array conversion. +module m + character(*,4), parameter :: str4arr(1) = ['a'] + logical, parameter :: test = str4arr(1) == 4_'a' +end