Skip to content

[flang] Fold character array constructor with unknown length #123983

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions flang/lib/Evaluate/fold-implementation.h
Original file line number Diff line number Diff line change
Expand Up @@ -1263,19 +1263,22 @@ template <typename T> class ArrayConstructorFolder {
explicit ArrayConstructorFolder(FoldingContext &c) : context_{c} {}

Expr<T> FoldArray(ArrayConstructor<T> &&array) {
if constexpr (T::category == TypeCategory::Character) {
if (const auto *len{array.LEN()}) {
charLength_ = ToInt64(Fold(context_, common::Clone(*len)));
knownCharLength_ = charLength_.has_value();
}
}
// Calls FoldArray(const ArrayConstructorValues<T> &) below
if (FoldArray(array)) {
auto n{static_cast<ConstantSubscript>(elements_.size())};
if constexpr (std::is_same_v<T, SomeDerived>) {
return Expr<T>{Constant<T>{array.GetType().GetDerivedTypeSpec(),
std::move(elements_), ConstantSubscripts{n}}};
} else if constexpr (T::category == TypeCategory::Character) {
if (const auto *len{array.LEN()}) {
auto length{Fold(context_, common::Clone(*len))};
if (std::optional<ConstantSubscript> lengthValue{ToInt64(length)}) {
return Expr<T>{Constant<T>{
*lengthValue, std::move(elements_), ConstantSubscripts{n}}};
}
if (charLength_) {
return Expr<T>{Constant<T>{
*charLength_, std::move(elements_), ConstantSubscripts{n}}};
}
} else {
return Expr<T>{
Expand All @@ -1296,6 +1299,11 @@ template <typename T> class ArrayConstructorFolder {
elements_.emplace_back(c->At(index));
} while (c->IncrementSubscripts(index));
}
if constexpr (T::category == TypeCategory::Character) {
if (!knownCharLength_) {
charLength_ = std::max(c->LEN(), charLength_.value_or(-1));
}
}
return true;
} else {
return false;
Expand Down Expand Up @@ -1345,6 +1353,8 @@ template <typename T> class ArrayConstructorFolder {

FoldingContext &context_;
std::vector<Scalar<T>> elements_;
std::optional<ConstantSubscript> charLength_;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would be the case, when knownCharLength_ is false, but charLengh_ has value?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

knownCharLength_ is set true if and only if the character array constructor has an explicit length (from an explicit type specifier); in this case, the known length is dispositive and the element lengths don't matter. Otherwise, charLength_ can change when longer elements are encountered.

bool knownCharLength_{false};
};

template <typename T>
Expand Down
5 changes: 5 additions & 0 deletions flang/test/Evaluate/bug123766.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
! RUN: %python %S/test_folding.py %s %flang_fc1
character(10), parameter :: a = '0123456789'
character(3), parameter :: arr(3) = [(a(1:i), i=1,3)]
logical, parameter :: test1 = all(arr == ["0", "01", "012"])
end
Loading