Fortran: fix checking of coshape specification in ALLOCATE statement
authorHarald Anlauf <anlauf@gmx.de>
Wed, 6 Apr 2022 20:24:21 +0000 (22:24 +0200)
committerHarald Anlauf <anlauf@gmx.de>
Sun, 10 Apr 2022 18:20:53 +0000 (20:20 +0200)
gcc/fortran/ChangeLog:

PR fortran/105184
* array.cc (match_subscript): Reject assumed size coarray
specification with missing lower bound.
* resolve.cc (resolve_allocate_expr): Fix logic for checking
allocate-coshape-spec in ALLOCATE statement.

gcc/testsuite/ChangeLog:

PR fortran/105184
* gfortran.dg/coarray_44.f90: Adjust expected output.
* gfortran.dg/coarray_allocate_11.f90: Likewise.
* gfortran.dg/coarray_allocate_12.f90: New test.

gcc/fortran/array.cc
gcc/fortran/resolve.cc
gcc/testsuite/gfortran.dg/coarray_44.f90
gcc/testsuite/gfortran.dg/coarray_allocate_11.f90
gcc/testsuite/gfortran.dg/coarray_allocate_12.f90 [new file with mode: 0644]

index eb9ed85..90ea812 100644 (file)
@@ -134,6 +134,13 @@ end_element:
   if (m == MATCH_ERROR)
     return MATCH_ERROR;
 
+  if (star && ar->start[i] == NULL)
+    {
+      gfc_error ("Missing lower bound in assumed size "
+                "coarray specification at %C");
+      return MATCH_ERROR;
+    }
+
   /* See if we have an optional stride.  */
   if (gfc_match_char (':') == MATCH_YES)
     {
index 21c8797..05f8f1b 100644 (file)
@@ -8108,12 +8108,13 @@ resolve_allocate_expr (gfc_expr *e, gfc_code *code, bool *array_alloc_wo_spec)
            goto failure;
 
          case  DIMEN_RANGE:
-           if (ar->start[i] == 0 || ar->end[i] == 0)
+           /* F2018:R937:
+            * allocate-coshape-spec is [ lower-bound-expr : ] upper-bound-expr
+            */
+           if (ar->start[i] == 0 || ar->end[i] == 0 || ar->stride[i] != NULL)
              {
-               /* If ar->stride[i] is NULL, we issued a previous error.  */
-               if (ar->stride[i] == NULL)
-                 gfc_error ("Bad array specification in ALLOCATE statement "
-                            "at %L", &e->where);
+               gfc_error ("Bad coarray specification in ALLOCATE statement "
+                          "at %L", &e->where);
                goto failure;
              }
            else if (gfc_dep_compare_expr (ar->start[i], ar->end[i]) == 1)
index 15fb8c7..545b546 100644 (file)
@@ -10,3 +10,5 @@ program pr70071
   allocate (z(2)[1::2,*])  ! { dg-error "Bad array dimension" }
   allocate (z(1::2)[2,*])  ! { dg-error "Bad array specification in ALLOCATE" }
 end program pr70071
+
+! { dg-prune-output "Bad coarray specification in ALLOCATE statement" }
index 0e806f0..0e4f64e 100644 (file)
@@ -3,10 +3,10 @@
 program p
    integer, allocatable :: z[:,:]
    integer :: i
-   allocate (z[1:,*]) ! { dg-error "Bad array specification in ALLOCATE statement" }
-   allocate (z[:2,*]) ! { dg-error "Bad array specification in ALLOCATE statement" }
+   allocate (z[1:,*]) ! { dg-error "Bad coarray specification in ALLOCATE statement" }
+   allocate (z[:2,*]) ! { dg-error "Bad coarray specification in ALLOCATE statement" }
    allocate (z[2:1,*]) ! { dg-error "Upper cobound is less than lower cobound" }
-   allocate (z[:0,*]) ! { dg-error "Bad array specification in ALLOCATE statement" }
+   allocate (z[:0,*]) ! { dg-error "Bad coarray specification in ALLOCATE statement" }
    allocate (z[0,*]) ! { dg-error "Upper cobound is less than lower cobound" }
    allocate (z[1,*]) ! This is OK
    allocate (z[1:1,*]) ! This is OK
diff --git a/gcc/testsuite/gfortran.dg/coarray_allocate_12.f90 b/gcc/testsuite/gfortran.dg/coarray_allocate_12.f90
new file mode 100644 (file)
index 0000000..2169aa1
--- /dev/null
@@ -0,0 +1,19 @@
+! { dg-do compile }
+! { dg-options "-fcoarray=single" }
+!
+! PR fortran/105184
+! Based on testcases by Gerhard Steinmetz
+
+program p
+  real, allocatable :: x[:,:]
+  integer :: n = 2
+  allocate (x[  n, *])
+  allocate (x[1:n, *])
+  allocate (x[n:n, *])
+  allocate (x[n, 5:*])
+  allocate (x[ :n,   *]) ! { dg-error "Bad coarray specification" }
+  allocate (x[::n,   *]) ! { dg-error "Bad coarray specification" }
+  allocate (x[ :1:1, *]) ! { dg-error "Bad coarray specification" }
+  allocate (x[1:n:n, *]) ! { dg-error "Bad coarray specification" }
+  allocate (x[1,   : *]) ! { dg-error "Missing lower bound" }
+end