lfortran icon indicating copy to clipboard operation
lfortran copied to clipboard

Wrong output using allocate with source= in a module subroutine

Open harperjf opened this issue 2 years ago • 2 comments

This program gives different wrong output each time it runs, but no error message, unlike my bug report #3819 where the subroutine was internal. It is now in a module. Evidence:

(lf2) john:~/Test/Glist$ cat allocinmodule.f90 
! allocinmodule.f90
module test_m ! allocate with source=- is a F2008 feature
    implicit none
contains
subroutine test( n )
    integer, intent(in) :: n
    integer, allocatable :: arr(:)

    allocate( arr( 2 ), source= n )
    print *, arr
    !! No explicit deallocation here.
end
end module

program allocinmodule
  use test_m
  call test( n= 100 )
  call test( n= 200 )
end program allocinmodule
  
(lf2) john:~/Test/Glist$ lfortran allocinmodule.f90
0 0 
1536891703 5 
(lf2) john:~/Test/Glist$ lfortran allocinmodule.f90
0 0 
1680326320 5 
(lf2) john:~/Test/Glist$ 

harperjf avatar Apr 19 '24 08:04 harperjf

Same problem if array arr was deallocated immediately after printing it.

harperjf avatar Apr 19 '24 22:04 harperjf

This works with main. Let's add a test to close this.

 % gfortran c.f90 && ./a.out
         100         100
         200         200
% lfortran c.f90 
100    100
200    200

HarshitaKalani avatar Feb 19 '26 14:02 HarshitaKalani