]> gcc.gnu.org Git - gcc.git/commitdiff
Fortran: improve checking of character length specification [PR96025]
authorHarald Anlauf <anlauf@gmx.de>
Mon, 20 Feb 2023 20:28:09 +0000 (21:28 +0100)
committerHarald Anlauf <anlauf@gmx.de>
Sun, 5 Mar 2023 19:35:11 +0000 (20:35 +0100)
gcc/fortran/ChangeLog:

PR fortran/96025
* parse.c (check_function_result_typed): Improve type check of
specification expression for character length and return status.
(parse_spec): Use status from above.
* resolve.c (resolve_fntype): Prevent use of invalid specification
expression for character length.

gcc/testsuite/ChangeLog:

PR fortran/96025
* gfortran.dg/pr96025.f90: New test.

(cherry picked from commit 6c1b825b3d6499dfeacf7c79dcf4b56a393ac204)

gcc/fortran/parse.c
gcc/fortran/resolve.c
gcc/testsuite/gfortran.dg/pr96025.f90 [new file with mode: 0644]

index 04501f44ef8497a2a2f52e644051a949ca7a7a02..23602446b61f7e10c8b30fd3cdd8a1ed885da36d 100644 (file)
@@ -3747,21 +3747,30 @@ match_deferred_characteristics (gfc_typespec * ts)
    For return types specified in a FUNCTION prefix, the IMPLICIT rules of the
    scope are not yet parsed so this has to be delayed up to parse_spec.  */
 
-static void
+static bool
 check_function_result_typed (void)
 {
   gfc_typespec ts;
 
   gcc_assert (gfc_current_state () == COMP_FUNCTION);
 
-  if (!gfc_current_ns->proc_name->result) return;
+  if (!gfc_current_ns->proc_name->result)
+    return true;
 
   ts = gfc_current_ns->proc_name->result->ts;
 
   /* Check type-parameters, at the moment only CHARACTER lengths possible.  */
   /* TODO:  Extend when KIND type parameters are implemented.  */
   if (ts.type == BT_CHARACTER && ts.u.cl && ts.u.cl->length)
-    gfc_expr_check_typed (ts.u.cl->length, gfc_current_ns, true);
+    {
+      /* Reject invalid type of specification expression for length.  */
+      if (ts.u.cl->length->ts.type != BT_INTEGER)
+         return false;
+
+      gfc_expr_check_typed (ts.u.cl->length, gfc_current_ns, true);
+    }
+
+  return true;
 }
 
 
@@ -3869,10 +3878,7 @@ loop:
        }
 
       if (verify_now)
-       {
-         check_function_result_typed ();
-         function_result_typed = true;
-       }
+       function_result_typed = check_function_result_typed ();
     }
 
   switch (st)
@@ -3883,10 +3889,7 @@ loop:
     case ST_IMPLICIT_NONE:
     case ST_IMPLICIT:
       if (!function_result_typed)
-       {
-         check_function_result_typed ();
-         function_result_typed = true;
-       }
+       function_result_typed = check_function_result_typed ();
       goto declSt;
 
     case ST_FORMAT:
index 3005619c4639f4456195770a9391bac7d3aa95bf..29125694e1e4b8b266b4783d18e4349db5beb3d1 100644 (file)
@@ -17260,7 +17260,9 @@ resolve_fntype (gfc_namespace *ns)
          }
       }
 
-  if (sym->ts.type == BT_CHARACTER)
+  if (sym->ts.type == BT_CHARACTER
+      && sym->ts.u.cl->length
+      && sym->ts.u.cl->length->ts.type == BT_INTEGER)
     gfc_traverse_expr (sym->ts.u.cl->length, sym, flag_fn_result_spec, 0);
 }
 
diff --git a/gcc/testsuite/gfortran.dg/pr96025.f90 b/gcc/testsuite/gfortran.dg/pr96025.f90
new file mode 100644 (file)
index 0000000..ce292bd
--- /dev/null
@@ -0,0 +1,11 @@
+! { dg-do compile }
+! PR fortran/96025 - ICE in expr_check_typed_help
+! Contributed by G.Steinmetz
+
+program p
+  print *, f()
+contains
+  character(char(1)) function f() ! { dg-error "must be of INTEGER type" }
+    f = 'f'
+  end
+end
This page took 0.081005 seconds and 5 git commands to generate.