Skip to content

Commit

Permalink
[flang] Fix failure to fold character array
Browse files Browse the repository at this point in the history
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 llvm#123362.
  • Loading branch information
klausler committed Jan 17, 2025
1 parent 93fd72c commit eb26904
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
4 changes: 4 additions & 0 deletions flang/lib/Evaluate/fold-implementation.h
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,9 @@ std::optional<Constant<T>> Folder<T>::ApplyComponent(
auto *typedExpr{UnwrapExpr<Expr<T>>(expr.value())};
CHECK(typedExpr);
array = std::make_unique<ArrayConstructor<T>>(*typedExpr);
if constexpr (T::category == TypeCategory::Character) {
array->set_LEN(Expr<SubscriptInteger>{value->LEN()});
}
}
if (subscripts) {
if (auto element{ApplySubscripts(*value, *subscripts)}) {
Expand Down Expand Up @@ -407,6 +410,7 @@ template <typename T> Expr<T> Folder<T>::Folding(Designator<T> &&designator) {
template <typename T>
Constant<T> *Folder<T>::Folding(std::optional<ActualArgument> &arg) {
if (auto *expr{UnwrapExpr<Expr<SomeType>>(arg)}) {
*expr = Fold(context_, std::move(*expr));
if constexpr (T::category != TypeCategory::Derived) {
if (!UnwrapExpr<Expr<T>>(*expr)) {
if (const Symbol *
Expand Down
11 changes: 11 additions & 0 deletions flang/test/Evaluate/fold-arr-char-component.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
! RUN: %python %S/test_folding.py %s %flang_fc1
! Ensure that array-valued component references have lengths
! (see https://github.com/llvm/llvm-project/issues/123362)
module m
type cdt
character(7) :: a = "ibm704", b = "cdc6600"
end type
type(cdt), parameter :: arr(2) = cdt()
integer, parameter :: check(*) = scan(arr%a, arr%b)
logical, parameter :: test1 = all(check == 5) ! the '0'
end

0 comments on commit eb26904

Please sign in to comment.