Skip to content

Commit

Permalink
Fortran: improve checking of character length specification [PR96025]
Browse files Browse the repository at this point in the history
gcc/fortran/ChangeLog:

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

gcc/testsuite/ChangeLog:

	PR fortran/96025
	* gfortran.dg/pr96025.f90: New test.
  • Loading branch information
harald-anlauf authored and ouuleilei-bot committed Feb 20, 2023
1 parent e869638 commit eea874a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 8 deletions.
23 changes: 16 additions & 7 deletions gcc/fortran/parse.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3974,21 +3974,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;
}


Expand Down Expand Up @@ -4097,8 +4106,8 @@ parse_spec (gfc_statement st)

if (verify_now)
{
check_function_result_typed ();
function_result_typed = true;
if (check_function_result_typed ())
function_result_typed = true;
}
}

Expand All @@ -4111,8 +4120,8 @@ parse_spec (gfc_statement st)
case ST_IMPLICIT:
if (!function_result_typed)
{
check_function_result_typed ();
function_result_typed = true;
if (check_function_result_typed ())
function_result_typed = true;
}
goto declSt;

Expand Down
4 changes: 3 additions & 1 deletion gcc/fortran/resolve.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17419,7 +17419,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);
}

Expand Down
11 changes: 11 additions & 0 deletions gcc/testsuite/gfortran.dg/pr96025.f90
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit eea874a

Please sign in to comment.