Consider the following program:
module associate_example
implicit none
type :: sparse
integer :: colidx
integer, allocatable :: rows(:)
real, allocatable :: values(:)
contains
final :: finalize
end type
contains
subroutine finalize(this)
type(sparse) :: this
print *, "Finalizing column ", this%colidx
end subroutine
end module associate_example
program associate_test
use associate_example
implicit none
associate(s => sparse(3,[1,2,3],[5.,3.,8.]))
print *, "In associate:"
print *, s%colidx
print *, s%rows
print *, s%values
end associate
block
type(sparse) :: s
print *, "In block:"
s = sparse(3,[1,2,3],[5.,3.,8.])
print *, s%colidx
print *, s%rows
print *, s%values
end block
end program
Both gfortran (v10.3) and ifort (v2021.4) compile the program without warnings. The output of gfortran is:
$ gfortran -Wall associate_test.f90
$ ./a.out
munmap_chunk(): invalid pointer
Program received signal SIGABRT: Process abort signal.
The output of Intel Fortran is:
$ ifort -warn all associate_test.f90
$ ./a.out
In associate:
3
1 2 3
5.000000 3.000000 8.000000
In block:
Finalizing column 0
3
1 2 3
5.000000 3.000000 8.000000
Finalizing column 3
I would expect finalization to occur at the end of the associate construct.
Is this mandated by the standard, or should we avoid associating with an expression that results in a derived type?
In the result from ifort, does the output line finalizing column 0 come from the finalization of the left-hand side in the assignment?