]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
Fortran: fix diagnostics for SELECT RANK [PR100607]
authorSteve Kargl <kargl@gcc.gnu.org>
Fri, 2 Jun 2023 17:44:11 +0000 (19:44 +0200)
committerHarald Anlauf <anlauf@gmx.de>
Fri, 2 Jun 2023 17:47:01 +0000 (19:47 +0200)
gcc/fortran/ChangeLog:

PR fortran/100607
* resolve.cc (resolve_select_rank): Remove duplicate error.
(resolve_fl_var_and_proc): Prevent NULL pointer dereference and
suppress error message for temporary.

gcc/testsuite/ChangeLog:

PR fortran/100607
* gfortran.dg/select_rank_6.f90: New test.

gcc/fortran/resolve.cc
gcc/testsuite/gfortran.dg/select_rank_6.f90 [new file with mode: 0644]

index 2ba3101f1fe95dac4e05d4e9cda4fded746a7015..fd059dddf051a216ddfa0e7b4db41f833a5fc369 100644 (file)
@@ -10020,11 +10020,6 @@ resolve_select_rank (gfc_code *code, gfc_namespace *old_ns)
                               || gfc_expr_attr (code->expr1).pointer))
        gfc_error ("RANK (*) at %L cannot be used with the pointer or "
                   "allocatable selector at %L", &c->where, &code->expr1->where);
-
-      if (case_value == -1 && (gfc_expr_attr (code->expr1).allocatable
-                              || gfc_expr_attr (code->expr1).pointer))
-       gfc_error ("RANK (*) at %L cannot be used with the pointer or "
-                  "allocatable selector at %L", &c->where, &code->expr1->where);
     }
 
   /* Add EXEC_SELECT to switch on rank.  */
@@ -13262,7 +13257,10 @@ resolve_fl_var_and_proc (gfc_symbol *sym, int mp_flag)
 
       if (allocatable)
        {
-         if (dimension && as->type != AS_ASSUMED_RANK)
+         if (dimension
+             && as
+             && as->type != AS_ASSUMED_RANK
+             && !sym->attr.select_rank_temporary)
            {
              gfc_error ("Allocatable array %qs at %L must have a deferred "
                         "shape or assumed rank", sym->name, &sym->declared_at);
diff --git a/gcc/testsuite/gfortran.dg/select_rank_6.f90 b/gcc/testsuite/gfortran.dg/select_rank_6.f90
new file mode 100644 (file)
index 0000000..d012177
--- /dev/null
@@ -0,0 +1,48 @@
+! { dg-do compile }
+! PR fortran/100607 - fix diagnostics for SELECT RANK
+! Contributed by T.Burnus
+
+program p
+  implicit none
+  integer, allocatable :: A(:,:,:)
+
+  allocate(a(5:6,-2:2, 99:100))
+  call foo(a)
+  call bar(a)
+
+contains
+
+  subroutine foo(x)
+    integer, allocatable :: x(..)
+    if (rank(x) /= 3) stop 1
+    if (any (lbound(x) /= [5, -2, 99])) stop 2
+
+    select rank (x)
+    rank(3)
+      if (any (lbound(x) /= [5, -2, 99])) stop 3
+    end select
+
+    select rank (x) ! { dg-error "pointer or allocatable selector at .2." }
+    rank(*)         ! { dg-error "pointer or allocatable selector at .2." }
+      if (rank(x) /= 1) stop 4
+      if (lbound(x, 1) /= 1) stop 5
+    end select
+  end
+
+  subroutine bar(x)
+    integer :: x(..)
+    if (rank(x) /= 3) stop 6
+    if (any (lbound(x) /= 1)) stop 7
+
+    select rank (x)
+    rank(3)
+      if (any (lbound(x) /= 1)) stop 8
+    end select
+
+    select rank (x)
+    rank(*)
+      if (rank(x) /= 1) stop 9
+      if (lbound(x, 1) /= 1) stop 10
+    end select
+  end
+end