Issue 171844
Summary [flang] rejection of valid code
Labels flang:frontend
Assignees
Reporter hmenke
    No overindexing is happening in the subroutine `test` at runtime, yet flang rejects this code. Gfortran warns but accepts the code.

```fortran
subroutine test()
  integer, parameter :: n = 1
  real :: r(n)
  r(1) = 1
  if (n .eq. 2) then
    r(2) = 2
  end if
end subroutine test
```

```console
$ flang -c test.F90 
error: Semantic errors in test.F90
./test.F90:6:5: error: subscript 2 is greater than upper bound 1 for dimension 1 of array
      r(2) = 2
 ^^^^
./test.F90:3:11: Declaration of 'r'
    real :: r(n)
 ^
```

Live demo https://godbolt.org/z/9qx69K14P

---

Source of the error message:

https://github.com/llvm/llvm-project/blob/16c0893f04c04faa8ac36495363344840f7c5db1/flang/lib/Semantics/_expression_.cpp#L344-L370

---

Proposed patch to downgrade the error to warning.

```patch
--- a/flang/lib/Semantics/_expression_.cpp
+++ b/flang/lib/Semantics/_expression_.cpp
@@ -349,17 +349,12 @@ static void ValidateSubscriptValue(parser::ContextualMessages &messages,
 std::optional<ConstantSubscript> bound;
   if (lb && val < *lb) {
     msg =
-        "%ssubscript %jd is less than lower %sbound %jd for %sdimension %d of array"_err_en_US;
+        "%ssubscript %jd is less than lower %sbound %jd for %sdimension %d of array"_warn_en_US;
     bound = *lb;
 } else if (ub && val > *ub) {
     msg =
-        "%ssubscript %jd is greater than upper %sbound %jd for %sdimension %d of array"_err_en_US;
+ "%ssubscript %jd is greater than upper %sbound %jd for %sdimension %d of array"_warn_en_US;
     bound = *ub;
-    if (dim + 1 == symbol.Rank() && IsDummy(symbol) && *bound == 1) {
-      // Old-school overindexing of a dummy array isn't fatal when
-      // it's on the last dimension and the extent is 1.
-      msg->set_severity(parser::Severity::Warning);
-    }
 }
   if (msg) {
     AttachDeclaration(
```
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to