Skip to content

Commit 73f9034

Browse files
authored
[flang] Fix failure to fold character array (#123418)
When a character component reference is applied to a constant array of derived type, ensure that the length of the resulting character array is properly defined. Fixes #123362.
1 parent 4e23101 commit 73f9034

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

flang/lib/Evaluate/fold-implementation.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,9 @@ std::optional<Constant<T>> Folder<T>::ApplyComponent(
290290
auto *typedExpr{UnwrapExpr<Expr<T>>(expr.value())};
291291
CHECK(typedExpr);
292292
array = std::make_unique<ArrayConstructor<T>>(*typedExpr);
293+
if constexpr (T::category == TypeCategory::Character) {
294+
array->set_LEN(Expr<SubscriptInteger>{value->LEN()});
295+
}
293296
}
294297
if (subscripts) {
295298
if (auto element{ApplySubscripts(*value, *subscripts)}) {
@@ -407,6 +410,7 @@ template <typename T> Expr<T> Folder<T>::Folding(Designator<T> &&designator) {
407410
template <typename T>
408411
Constant<T> *Folder<T>::Folding(std::optional<ActualArgument> &arg) {
409412
if (auto *expr{UnwrapExpr<Expr<SomeType>>(arg)}) {
413+
*expr = Fold(context_, std::move(*expr));
410414
if constexpr (T::category != TypeCategory::Derived) {
411415
if (!UnwrapExpr<Expr<T>>(*expr)) {
412416
if (const Symbol *
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
! RUN: %python %S/test_folding.py %s %flang_fc1
2+
! Ensure that array-valued component references have lengths
3+
! (see https://github.com/llvm/llvm-project/issues/123362)
4+
module m
5+
type cdt
6+
character(7) :: a = "ibm704", b = "cdc6600"
7+
end type
8+
type(cdt), parameter :: arr(2) = cdt()
9+
integer, parameter :: check(*) = scan(arr%a, arr%b)
10+
logical, parameter :: test1 = all(check == 5) ! the '0'
11+
end

0 commit comments

Comments
 (0)