See attached patch. A dummy function whose result is CHARACTER(n), with
n one of its own dummy arguments, caused an ICE in gfc_get_symbol_decl.
gfc_trans_deferred_vars treated the dummy function like a CHARACTER
dummy variable and tried to evaluate n in the caller, where n has no
declaration. gfc_conv_procedure_call already evaluates the result length
at each call, so the patch skips that setup for a dummy function. I also
checked at run time that calls through CHARACTER(*), PROCEDURE() and
procedure pointer dummy functions still return the right length.

The fix is in gfc_trans_deferred_vars in trans-decl.cc; the rest is the
new test.

Regression tested on x86_64.

I will commit as simple and obvious.

Regards,

Jerry

---

fortran: [PR107144] ICE for dummy function returning CHARACTER(n)

gfc_trans_deferred_vars set up the result length of a CHARACTER dummy
function as it does the length of a CHARACTER dummy variable.  When that
length is an argument of the dummy function itself, the argument is
never declared in the caller, so gfc_get_symbol_decl failed an
assertion.  The result length is evaluated at each call, so skip the
setup for a dummy function.

Assisted-by: Claude Opus 5.5

        PR fortran/107144

gcc/fortran/ChangeLog:

        * trans-decl.cc (gfc_trans_deferred_vars): Do not set up the result
        length of a CHARACTER dummy function.

gcc/testsuite/ChangeLog:

        * gfortran.dg/pr107144.f90: New test.
>From 94a187d3e00f1c14c60f70d02b30b08d570bee2e Mon Sep 17 00:00:00 2001
From: Jerry DeLisle <[email protected]>
Date: Sat, 14 Nov 2026 17:58:13 -0800
Subject: [PATCH] fortran: [PR107144] ICE for dummy function returning
 CHARACTER(n)

gfc_trans_deferred_vars set up the result length of a CHARACTER dummy
function as it does the length of a CHARACTER dummy variable.  When that
length is an argument of the dummy function itself, the argument is
never declared in the caller, so gfc_get_symbol_decl failed an
assertion.  The result length is evaluated at each call, so skip the
setup for a dummy function.

Assisted-by: Claude Opus 5.5

	PR fortran/107144

gcc/fortran/ChangeLog:

	* trans-decl.cc (gfc_trans_deferred_vars): Do not set up the result
	length of a CHARACTER dummy function.

gcc/testsuite/ChangeLog:

	* gfortran.dg/pr107144.f90: New test.
---
 gcc/fortran/trans-decl.cc              |  5 +++++
 gcc/testsuite/gfortran.dg/pr107144.f90 | 14 ++++++++++++++
 2 files changed, 19 insertions(+)
 create mode 100644 gcc/testsuite/gfortran.dg/pr107144.f90

diff --git a/gcc/fortran/trans-decl.cc b/gcc/fortran/trans-decl.cc
index 0de93be7829..5129637165a 100644
--- a/gcc/fortran/trans-decl.cc
+++ b/gcc/fortran/trans-decl.cc
@@ -5661,6 +5661,11 @@ gfc_trans_deferred_vars (gfc_symbol * proc_sym, gfc_wrapped_block * block)
 	gfc_fatal_error ("Deferred type parameter not yet supported");
       else if (alloc_comp_or_fini)
 	gfc_trans_deferred_array (sym, block);
+      else if (sym->ts.type == BT_CHARACTER && sym->attr.dummy
+	       && sym->attr.function)
+	{
+	  /* A dummy function's result length is evaluated at each call.  */
+	}
       else if (sym->ts.type == BT_CHARACTER)
 	{
 	  loc = input_location;
diff --git a/gcc/testsuite/gfortran.dg/pr107144.f90 b/gcc/testsuite/gfortran.dg/pr107144.f90
new file mode 100644
index 00000000000..085394a44a1
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/pr107144.f90
@@ -0,0 +1,14 @@
+! { dg-do compile }
+!
+! PR 107144
+! ICE for a dummy function whose result length is its own dummy argument.
+
+subroutine s (fn)
+  interface
+    function fn (n)
+      integer :: n
+      character(n) :: fn
+    end function
+  end interface
+  if (fn (3) /= 'abc') stop 1
+end subroutine
-- 
2.56.0

Reply via email to