Dear all, it seems that my patch for default-initialization of function results of derived type could trigger a weird issue if a type-bound function of a type that is use-associated from a different module had the same name, even if the declared function had a different type, but only if the result clause is present.
While I had hoped that resolution should not lead to this situation, checking whether the function was use-associated solved the issue. Regtested on x86_64-pc-linux-gnu. OK for mainline? Thanks, Harald
From 0cfeedf5865cd781478b4652477009ca05f4de77 Mon Sep 17 00:00:00 2001 From: Harald Anlauf <anl...@gmx.de> Date: Wed, 26 Mar 2025 22:04:39 +0100 Subject: [PATCH] Fortran: fix bogus recursion with DT default initialization [PR118796] PR fortran/118796 gcc/fortran/ChangeLog: * resolve.cc: Do not apply default initialization to a derived-type function result if the resolved function is use-associated. gcc/testsuite/ChangeLog: * gfortran.dg/derived_result_4.f90: New test. --- gcc/fortran/resolve.cc | 3 +- .../gfortran.dg/derived_result_4.f90 | 38 +++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/gfortran.dg/derived_result_4.f90 diff --git a/gcc/fortran/resolve.cc b/gcc/fortran/resolve.cc index cf9318ff763..cb3658917ef 100644 --- a/gcc/fortran/resolve.cc +++ b/gcc/fortran/resolve.cc @@ -17946,7 +17946,8 @@ skip_interfaces: /* Mark the result symbol to be referenced, when it has allocatable components. */ sym->result->attr.referenced = 1; - else if (a->function && !a->pointer && !a->allocatable && sym->result) + else if (a->function && !a->pointer && !a->allocatable && !a->use_assoc + && sym->result) /* Default initialization for function results. */ apply_default_init (sym->result); } diff --git a/gcc/testsuite/gfortran.dg/derived_result_4.f90 b/gcc/testsuite/gfortran.dg/derived_result_4.f90 new file mode 100644 index 00000000000..12ab1909492 --- /dev/null +++ b/gcc/testsuite/gfortran.dg/derived_result_4.f90 @@ -0,0 +1,38 @@ +! { dg-do compile } +! { dg-additional-options "-Wall -Wno-return-type -Wno-unused-variable" } +! +! PR fortran/118796 - bogus recursion with DT default initialization + +module m1 + implicit none + + type :: t1 + type(integer) :: f1 = 0 + end type t1 + + TYPE :: c1 + contains + procedure, public :: z + END TYPE c1 + +contains + ! type-bound procedure z has a default initialization + function z( this ) + type(t1) :: z + class(c1), intent(in) :: this + end function z +end module m1 + +module m2 + use m1, only : c1 +contains + function z() result(field) + end function z +end module m2 + +module m3 + use m1, only : c1 +contains + function z() + end function z +end module m3 -- 2.43.0