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.
Confirmed. See also PR 37336 comment 27. This is case (b).
Also still present in trunk.
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
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.
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