I would like to know whether it is possible in modern Fortran to assign an allocatable array using itself, or part of it, to do it. Here it is a simple example:
module modu
implicit none
type :: t
integer :: i
end type
contains
subroutine assign(a,b)
type(t), allocatable, intent(out) :: a(:)
type(t), intent(in) :: b
allocate(a(1))
a(1) = b
end subroutine
end module
!----------------------
program test
use modu
implicit none
type(t), allocatable :: a(:)
allocate(a(1))
a(1)%i = 2
call assign(a, a(1))
print*, a(1)%i
end program
This code gives the corect answer with ifort 18 and returns "Segmentation fault" with gfortran 7.4.
NOTE: The original problem was a bit more complex since call assign(a, a(1))
should be replaced by call assign(a, a(1)+b)
having operator + properly overloaded, but the conclusion (respect ifort and gfortran) is the same.
NOTE: In the thread checking for self-assignment in fortran overloaded assignment, @IanH makes a distinction between call assign(a,a)
and call assign(a,(a))
but I believe that it does not solve this problem because I have allocatable arguments.
NOTE: In the thread Automatic array allocation upon assignment in Fortran, @francescalus explains automatic allocation on intrinsic assignment but again I believe that it does not apply here.