Skip to content

[flang] Fix spurious error due to bad expression shape calculation #124323

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
25 changes: 17 additions & 8 deletions flang/include/flang/Evaluate/shape.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ template <typename A>
std::optional<Shape> GetShape(
FoldingContext &, const A &, bool invariantOnly = true);
template <typename A>
std::optional<Shape> GetShape(
FoldingContext *, const A &, bool invariantOnly = true);
template <typename A>
std::optional<Shape> GetShape(const A &, bool invariantOnly = true);

// The dimension argument to these inquiries is zero-based,
Expand Down Expand Up @@ -149,6 +152,8 @@ inline MaybeExtentExpr GetSize(const std::optional<Shape> &maybeShape) {
// Utility predicate: does an expression reference any implied DO index?
bool ContainsAnyImpliedDoIndex(const ExtentExpr &);

// GetShape()
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this comment intentional?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes. I like to have a comment like this appear before the implementation of a library function when that implementation is necessarily preceded by screens worth of helper functions and classes, so that it's clear that the code after the comment is leading up to something. I'm not always consistent about this, though.


class GetShapeHelper
: public AnyTraverse<GetShapeHelper, std::optional<Shape>> {
public:
Expand Down Expand Up @@ -261,23 +266,27 @@ class GetShapeHelper

template <typename A>
std::optional<Shape> GetShape(
FoldingContext &context, const A &x, bool invariantOnly) {
if (auto shape{GetShapeHelper{&context, invariantOnly}(x)}) {
return Fold(context, std::move(shape));
FoldingContext *context, const A &x, bool invariantOnly) {
if (auto shape{GetShapeHelper{context, invariantOnly}(x)}) {
if (context) {
return Fold(*context, std::move(shape));
} else {
return shape;
}
} else {
return std::nullopt;
}
}

template <typename A>
std::optional<Shape> GetShape(const A &x, bool invariantOnly) {
return GetShapeHelper{/*context=*/nullptr, invariantOnly}(x);
std::optional<Shape> GetShape(
FoldingContext &context, const A &x, bool invariantOnly) {
return GetShape(&context, x, invariantOnly);
}

template <typename A>
std::optional<Shape> GetShape(
FoldingContext *context, const A &x, bool invariantOnly = true) {
return GetShapeHelper{context, invariantOnly}(x);
std::optional<Shape> GetShape(const A &x, bool invariantOnly) {
return GetShape(/*context=*/nullptr, x, invariantOnly);
}

template <typename A>
Expand Down
13 changes: 10 additions & 3 deletions flang/lib/Evaluate/shape.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,18 @@
#include "flang/Evaluate/tools.h"
#include "flang/Evaluate/type.h"
#include "flang/Parser/message.h"
#include "flang/Semantics/semantics.h"
#include "flang/Semantics/symbol.h"
#include <functional>

using namespace std::placeholders; // _1, _2, &c. for std::bind()

namespace Fortran::evaluate {

FoldingContext &GetFoldingContextFrom(const Symbol &symbol) {
return symbol.owner().context().foldingContext();
}

bool IsImpliedShape(const Symbol &original) {
const Symbol &symbol{ResolveAssociations(original)};
const auto *details{symbol.detailsIf<semantics::ObjectEntityDetails>()};
Expand Down Expand Up @@ -483,7 +488,7 @@ static MaybeExtentExpr GetAssociatedExtent(
const Symbol &symbol, int dimension) {
if (const auto *assoc{symbol.detailsIf<semantics::AssocEntityDetails>()};
assoc && !assoc->rank()) { // not SELECT RANK case
if (auto shape{GetShape(assoc->expr())};
if (auto shape{GetShape(GetFoldingContextFrom(symbol), assoc->expr())};
shape && dimension < static_cast<int>(shape->size())) {
if (auto &extent{shape->at(dimension)};
// Don't return a non-constant extent, as the variables that
Expand Down Expand Up @@ -519,7 +524,8 @@ MaybeExtentExpr GetExtent(
}
if (const auto *details{symbol.detailsIf<semantics::ObjectEntityDetails>()}) {
if (IsImpliedShape(symbol) && details->init()) {
if (auto shape{GetShape(symbol, invariantOnly)}) {
if (auto shape{
GetShape(GetFoldingContextFrom(symbol), symbol, invariantOnly)}) {
if (dimension < static_cast<int>(shape->size())) {
return std::move(shape->at(dimension));
}
Expand Down Expand Up @@ -568,7 +574,8 @@ MaybeExtentExpr GetExtent(const Subscript &subscript, const NamedEntity &base,
MaybeExtentExpr{triplet.stride()});
},
[&](const IndirectSubscriptIntegerExpr &subs) -> MaybeExtentExpr {
if (auto shape{GetShape(subs.value())};
if (auto shape{GetShape(
GetFoldingContextFrom(base.GetLastSymbol()), subs.value())};
shape && GetRank(*shape) == 1) {
// vector-valued subscript
return std::move(shape->at(0));
Expand Down
6 changes: 6 additions & 0 deletions flang/test/Evaluate/bug124191.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
! RUN: %flang_fc1 -fsyntax-only -pedantic %s 2>&1 | FileCheck --allow-empty %s
! CHECK-NOT: error:
! Regression test for https://github.com/llvm/llvm-project/issues/124191
character(3) :: arr(5) = ['aa.', 'bb.', 'cc.', 'dd.', 'ee.']
arr([(mod(iachar(arr(i:i-1:-1)(1:1)),5)+1, i=2,5,3)]) = arr(5:2:-1)
end
Loading