Bug 65347 - [F03] Final subroutine not called for function result
Summary: [F03] Final subroutine not called for function result
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: fortran (show other bugs)
Version: 5.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: wrong-code
Depends on:
Blocks: Finalization
  Show dependency treegraph
 
Reported: 2015-03-08 13:31 UTC by eddyg_61-bugzilla
Modified: 2023-04-14 13:59 UTC (History)
4 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2015-03-20 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description eddyg_61-bugzilla 2015-03-08 13:31:52 UTC
In the following program the final subroutine are not called
module testfin_mod
implicit none

type tfin
   integer :: t = -1
contains
   final :: del_tfin
   final :: del_tfinv
end type

contains

   function Ctfin(s) result(this)
     integer,intent(in) :: s
	 type(tfin) :: this
	 this % t = s
   end function
   subroutine del_tfin(this)
      type(tfin), intent(inout) :: this
      print *,"Finalized", this % t      
   end subroutine
   subroutine del_tfinv(this)
      type(tfin), intent(inout) :: this(:)
      print *,"Finalized vector", this % t      
   end subroutine
   
   subroutine printsomev(a)
      type(tfin) :: a(:)
      print *,'PRV:', a
   end subroutine
   
   subroutine printsomes(a)
      type(tfin) :: a
      print *,'PRS:', a%t
   end subroutine
   
   subroutine pluto
      type(tfin),save :: a(3)

      call printsomev([a(2), a(1), a(3)])
      call printsomev([a(2), Ctfin(10), a(3)])
      call printsomes(Ctfin(11))
   end subroutine
end module

program test1
use testfin_mod
implicit none

   call pluto

end program

The standard (as far as I know) requires the temporary variables corresponding to Ctfin(10) and Ctfin(11) to be finalized after the end of the subroutines printsomev and printsomev.

But no finalization take places.

The compiler ifort 13.0.1 correctly calls the final procedures.
Comment 1 janus 2015-03-20 08:29:28 UTC
Confirmed. See also PR 37336 comment 27. This is case (b).
Comment 2 Jürgen Reuter 2018-09-28 06:27:34 UTC
Also still present in trunk.
Comment 3 Damian Rouson 2022-04-02 00:47:50 UTC
I suspect the code below is a simpler reproducer (25 lines) of this bug

% cat missing-finalization.f90 
module object_m
  implicit none

  type object_t
  contains
    final :: count_finalizations
  end type

contains
  subroutine count_finalizations(self)
    type(object_t) self
    print *,"finalization"
  end subroutine

  function new_object() 
    type(object_t) new_object
  end function
end module

  use object_m
  implicit none
  type(object_t) object

 object = new_object()
end

% gfortran missing-finalization.f90 

% ./a.out

% gfortran --version
GNU Fortran (Homebrew GCC 11.2.0_3) 11.2.0
Comment 4 Paul Thomas 2023-03-18 15:06:52 UTC
This is partially fixed on mainline and behaves in the same way as nagfor. Finalization of array and stucture constructors is removed in F2018 and so are only activated by -std=f2003/8 in gfortran. ifort activates both be default. I seem to have overlooked finalization of structure constructors or functions within array constructors. I'll have a look to see why it is not working.
Comment 5 Paul Thomas 2023-04-14 13:59:52 UTC
I am closing this as fixed.

The original testcase output now agrees with nagfor. The testcase of comment #3 produces the same output as nagfor and ifort. ifort finalizes the function call Ctfin(10) in the original testcase, which is certainly not required by the F2018 standard and was explicitly deleted in a revision to the F2008 standard.

Finalization of structure and array constructors is all over the place with the different compilers. I therefore am taking the view that complying with F2018 is the best that can be hoped for.

Sorry this has taken so long to address.

Paul