-
Notifications
You must be signed in to change notification settings - Fork 13.6k
[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
Conversation
GetShape() needed to be called with a FoldingContext in order to properly construct an extent expression for the shape of an array constructor whose elements (nested in an implied DO loop) were not scalars. Fixes llvm#124191.
@llvm/pr-subscribers-flang-semantics Author: Peter Klausler (klausler) ChangesGetShape() needed to be called with a FoldingContext in order to properly construct an extent expression for the shape of an array constructor whose elements (nested in an implied DO loop) were not scalars. Fixes #124191. Full diff: https://github.com/llvm/llvm-project/pull/124323.diff 3 Files Affected:
diff --git a/flang/include/flang/Evaluate/shape.h b/flang/include/flang/Evaluate/shape.h
index 3e42ec691158bc..f0505cfcdf2d7f 100644
--- a/flang/include/flang/Evaluate/shape.h
+++ b/flang/include/flang/Evaluate/shape.h
@@ -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,
@@ -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()
+
class GetShapeHelper
: public AnyTraverse<GetShapeHelper, std::optional<Shape>> {
public:
@@ -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>
diff --git a/flang/lib/Evaluate/shape.cpp b/flang/lib/Evaluate/shape.cpp
index 58b824d9b8e644..fa957cfc084951 100644
--- a/flang/lib/Evaluate/shape.cpp
+++ b/flang/lib/Evaluate/shape.cpp
@@ -16,6 +16,7 @@
#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>
@@ -23,6 +24,10 @@ 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>()};
@@ -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
@@ -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));
}
@@ -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));
diff --git a/flang/test/Evaluate/bug124191.f90 b/flang/test/Evaluate/bug124191.f90
new file mode 100644
index 00000000000000..27d08032efa2f0
--- /dev/null
+++ b/flang/test/Evaluate/bug124191.f90
@@ -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
|
@@ -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() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this comment intentional?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM.
Thanks for the quick fix!
GetShape() needed to be called with a FoldingContext in order to properly construct an extent expression for the shape of an array constructor whose elements (nested in an implied DO loop) were not scalars.
Fixes #124191.