[flang] Substrings with lower bound greater than upper bound
authorPeter Steinfeld <psteinfeld@nvidia.com>
Wed, 16 Sep 2020 21:42:30 +0000 (14:42 -0700)
committerPeter Steinfeld <psteinfeld@nvidia.com>
Wed, 16 Sep 2020 21:56:23 +0000 (14:56 -0700)
According to section 9.4.1, paragraph 3,
 If the starting point is greater than the ending point, the substring has
 length zero

But the compilers code for substring processing was failing a call to `CHECK()`
in this case.  I fixed this by just setting the number of items in the
resulting string to 0 for this situation.

Differential Revision: https://reviews.llvm.org/D87799

flang/lib/Evaluate/variable.cpp
flang/test/Semantics/resolve49.f90

index d87c716..c81f2b1 100644 (file)
@@ -204,9 +204,11 @@ std::optional<Expr<SomeCharacter>> Substring::Fold(FoldingContext &context) {
       *ubi = *length;
     }
     if (lbi && literal) {
-      CHECK(*ubi >= *lbi);
       auto newStaticData{StaticDataObject::Create()};
-      auto items{*ubi - *lbi + 1};
+      auto items{0}; // If the lower bound is greater, the length is 0
+      if (*ubi >= *lbi) {
+        items = *ubi - *lbi + 1;
+      }
       auto width{(*literal)->itemBytes()};
       auto bytes{items * width};
       auto startByte{(*lbi - 1) * width};
index b0bca05..5ead078 100644 (file)
@@ -17,6 +17,7 @@ program p2
   end type
   character :: a(10)
   character :: b(5)
+  character :: c(0)
   integer :: n
   n = 3
   b = a(n:7)
@@ -26,6 +27,7 @@ program p2
   a(n+3:) = b
   a(:n+2) = b
   n = iachar(1_'ABCDEFGHIJ'(1:1))
+  c = 'ABCDEFGHIJ'(1:0)
 end
 
 ! Test pointer assignment with bounds