]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
Fortran: fix bogus recursion with DT default initialization [PR118796]
authorHarald Anlauf <anlauf@gmx.de>
Wed, 26 Mar 2025 21:04:39 +0000 (22:04 +0100)
committerHarald Anlauf <anlauf@gmx.de>
Wed, 26 Mar 2025 21:52:37 +0000 (22:52 +0100)
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
gcc/testsuite/gfortran.dg/derived_result_4.f90 [new file with mode: 0644]

index cf9318ff763c5175bd5943d4100706f20a05857f..cb3658917efbdfea6ec2520c90165240ba788423 100644 (file)
@@ -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 (file)
index 0000000..12ab190
--- /dev/null
@@ -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