https://gcc.gnu.org/g:43522516e5a5c95807a1bf31c3d11014fb1ffb77

commit r13-9171-g43522516e5a5c95807a1bf31c3d11014fb1ffb77
Author: Paul Thomas <pa...@gcc.gnu.org>
Date:   Tue Nov 5 21:09:26 2024 +0000

    Fortran: Fix regressions with intent(out) class[PR115070, PR115348].
    
    2024-11-05  Paul Thomas  <pa...@gcc.gnu.org>
    
    gcc/fortran
            PR fortran/115070
            PR fortran/115348
            * trans-expr.cc (gfc_trans_class_init_assign): If all the
            components of the default initializer are null for a scalar,
            build an empty statement to prevent prior declarations from
            disappearing.
    
    gcc/testsuite/
            PR fortran/115070
            * gfortran.dg/ieee/pr115070.f90: New test.
    
            PR fortran/115348
            * gfortran.dg/pr115348.f90: New test.

Diff:
---
 gcc/fortran/trans-expr.cc                   |  6 ++++-
 gcc/testsuite/gfortran.dg/ieee/pr115070.f90 | 28 +++++++++++++++++++++++
 gcc/testsuite/gfortran.dg/pr115348.f90      | 35 +++++++++++++++++++++++++++++
 3 files changed, 68 insertions(+), 1 deletion(-)

diff --git a/gcc/fortran/trans-expr.cc b/gcc/fortran/trans-expr.cc
index 46348d7df456..59a7ff8d8d06 100644
--- a/gcc/fortran/trans-expr.cc
+++ b/gcc/fortran/trans-expr.cc
@@ -1722,7 +1722,11 @@ gfc_trans_class_init_assign (gfc_code *code)
          if (cmp->initializer)
            break;
          else if (!cmp->next)
-           return build_empty_stmt (input_location);
+           {
+             tmp = build_empty_stmt (input_location);
+             gfc_add_expr_to_block (&block, tmp);
+             return gfc_finish_block (&block);
+           }
        }
     }
 
diff --git a/gcc/testsuite/gfortran.dg/ieee/pr115070.f90 
b/gcc/testsuite/gfortran.dg/ieee/pr115070.f90
new file mode 100644
index 000000000000..9378f770e2c6
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/ieee/pr115070.f90
@@ -0,0 +1,28 @@
+! { dg-do compile }
+!
+! Test the fix for PR115070
+!
+! Contributed by Sebastien Bardeau  <bard...@iram.fr>
+!
+module my_mod
+  type my_type
+    integer :: a
+  contains
+    final :: myfinal
+  end type my_type
+contains
+  subroutine my_sub(obs)
+    use ieee_arithmetic
+    class(my_type), intent(out) :: obs
+  end subroutine my_sub
+  subroutine myfinal (arg)
+    type (my_type) :: arg
+    print *, arg%a
+  end
+end module my_mod
+
+  use my_mod
+  type (my_type) :: z
+  z%a = 42
+  call my_sub (z)
+end
diff --git a/gcc/testsuite/gfortran.dg/pr115348.f90 
b/gcc/testsuite/gfortran.dg/pr115348.f90
new file mode 100644
index 000000000000..bc644b2f1c0c
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/pr115348.f90
@@ -0,0 +1,35 @@
+! { dg-do compile }
+! { dg-options "-fcheck=recursion" }
+!
+! Test the fix for pr115348.
+!
+! Contributed by Maxime van den Bossche  <maxime.vandenboss...@kuleuven.be>
+!
+module mymodule
+    implicit none
+
+    type mytype
+        integer :: mynumber
+        contains
+        procedure :: myroutine
+    end type mytype
+
+    contains
+
+    subroutine myroutine(self)
+        class(mytype), intent(out) :: self
+
+        self%mynumber = 1
+    end subroutine myroutine
+end module mymodule
+
+
+program myprogram
+    use mymodule, only: mytype
+    implicit none
+
+    type(mytype) :: myobject
+
+    call myobject%myroutine()
+    print *, myobject%mynumber
+end program myprogram

Reply via email to