[Bug libfortran/95293] Fortran not passing array by reference

kargl at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Sat May 23 22:26:52 GMT 2020


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95293

kargl at gcc dot gnu.org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |kargl at gcc dot gnu.org

--- Comment #1 from kargl at gcc dot gnu.org ---
(In reply to David Sagan from comment #0)
> The test program is:
> 
> program test
> implicit none
> 
> type cb_struct
>   real :: cbar = 0
> end type
> 
> type scratch_struct
>   type (cb_struct) :: cc(10)
>   real :: zz(10) = 0
> end type
> 
> type (scratch_struct), target :: s1
> 
> !
> 
> s1%cc(2)%cbar = 7
> call sub2(s1%cc%cbar)
> print *, 'Sub2 result:', s1%cc(2)%cbar, '  ! Wrong. Should be 10'
> 
> s1%zz(2) = 7
> call sub3(s1%zz)
> print *, 'Sub3 result:', s1%zz(2)
> 
> !--------------------------------------
> contains
> 
> subroutine sub2(vec)
>   real, target, intent(inout) :: vec(:)
>   real, pointer :: ptr
>   ptr => vec(1)
>   print *, 'In sub2:', associated(ptr, s1%cc(1)%cbar), vec(2)
>   vec(2) = 23
>   s1%cc(2)%cbar = 10
> end subroutine sub2
> 
> subroutine sub3(vec)
>   real, target, intent(inout) :: vec(:)
>   real, pointer :: ptr
>   ptr => vec(1)
>   print *, 'In sub3:', associated(ptr, s1%zz(1)), vec(2)
>   vec(2) = 23
>   s1%zz(2) = 10
> end subroutine sub3
> 
> end program
> 
> 
> Running gives:
> 
> > gfortran test.f90
> > ./a.out 
>  In sub2: F   7.00000000    
>  Sub2 result:   23.0000000       ! Wrong. Should be 10
>  In sub3: T   7.00000000    
>  Sub3 result:   10.0000000
> 
> I get the same result on Linux and Mac with gcc 8.3.1 and gcc 9.3.

% gfcx -Warray-temporaries -o z b.f90

b.f90:18:10:

   18 | call sub2(s1%cc%cbar)
      |          1
Warning: Creating array temporary at (1) [-Warray-temporaries]


More information about the Gcc-bugs mailing list