[PATCH] D85672: [flang] Avoid cascading error in subscript triplet

Tim Keith via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 10 11:00:52 PDT 2020


tskeith created this revision.
tskeith added reviewers: klausler, sscalpone.
tskeith added a project: Flang.
Herald added a reviewer: jdoerfert.
Herald added a reviewer: DavidTruby.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.
tskeith requested review of this revision.

If a bound of a subscript triplet is present but fails to analyze
due to an error, return nullopt rather than returning a Triplet with
that bound missing. This is so we can distinguish an absent bound from
an erroneous one and avoid spurious errors.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D85672

Files:
  flang/lib/Semantics/expression.cpp
  flang/test/Semantics/assign04.f90


Index: flang/test/Semantics/assign04.f90
===================================================================
--- flang/test/Semantics/assign04.f90
+++ flang/test/Semantics/assign04.f90
@@ -125,3 +125,10 @@
   !ERROR: Assignment to subprogram 'f9' is not allowed
   f9 = 1.0
 end
+
+!ERROR: No explicit type declared for 'n'
+subroutine s10(a, n)
+  implicit none
+  real a(n)
+  a(1:n) = 0.0  ! should not get a second error here
+end
Index: flang/lib/Semantics/expression.cpp
===================================================================
--- flang/lib/Semantics/expression.cpp
+++ flang/lib/Semantics/expression.cpp
@@ -871,21 +871,28 @@
 
 std::optional<Subscript> ExpressionAnalyzer::AnalyzeSectionSubscript(
     const parser::SectionSubscript &ss) {
-  return std::visit(common::visitors{
-                        [&](const parser::SubscriptTriplet &t) {
-                          return std::make_optional<Subscript>(
-                              Triplet{TripletPart(std::get<0>(t.t)),
-                                  TripletPart(std::get<1>(t.t)),
-                                  TripletPart(std::get<2>(t.t))});
-                        },
-                        [&](const auto &s) -> std::optional<Subscript> {
-                          if (auto subscriptExpr{AsSubscript(Analyze(s))}) {
-                            return Subscript{std::move(*subscriptExpr)};
-                          } else {
-                            return std::nullopt;
-                          }
-                        },
-                    },
+  return std::visit(
+      common::visitors{
+          [&](const parser::SubscriptTriplet &t) -> std::optional<Subscript> {
+            const auto &lower{std::get<0>(t.t)};
+            const auto &upper{std::get<1>(t.t)};
+            const auto &stride{std::get<2>(t.t)};
+            auto result{Triplet{
+                TripletPart(lower), TripletPart(upper), TripletPart(stride)}};
+            if ((lower && !result.lower()) || (upper && !result.upper())) {
+              return std::nullopt;
+            } else {
+              return std::make_optional<Subscript>(result);
+            }
+          },
+          [&](const auto &s) -> std::optional<Subscript> {
+            if (auto subscriptExpr{AsSubscript(Analyze(s))}) {
+              return Subscript{std::move(*subscriptExpr)};
+            } else {
+              return std::nullopt;
+            }
+          },
+      },
       ss.u);
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D85672.284450.patch
Type: text/x-patch
Size: 2473 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200810/f499ecda/attachment-0001.bin>


More information about the llvm-commits mailing list