Dear gfortran developers,
I would like to report a possible gfortran bug involving "associate" with
the result of an internal complex function.
The same code gives different kinds for the associated complex expression
depending on whether the function is defined in a module or as an internal
procedure in the main program.
Minimal reproducer:
module m
use, intrinsic :: iso_fortran_env, only: wp=>real64
implicit none(type, external)
contains
complex(wp) function myfunc(x)
complex(wp), intent(in) :: x
myfunc = sin(x)
end function myfunc
end module m
program demo
use, intrinsic :: iso_fortran_env, only: wp=>real64
#if defined(MOD_CONTAINS)
use m, only: myfunc
#endif
implicit none(type, external)
character(len=150) :: fmt1
complex(wp) :: z
fmt1='(1X,A10, 2X, F0.12, 5X, A18, 2X, I0)'
z = (1.0_wp, 2.0_wp)
associate(k => myfunc(z))
print fmt1, 'k%re: ', k%re, 'kind(k%re)', kind(k%re)
print fmt1, 'k%im: ', k%im, 'kind(k%im)', kind(k%im)
print fmt1, 'aimag(k):', aimag(k), 'kind(aimag(k)):', kind(aimag(k))
end associate
#if defined(PROG_CONTAINS)
contains
complex(wp) function myfunc(x)
complex(wp), intent(in) :: x
myfunc = sin(x)
end function myfunc
#endif
end program demo
With the function as an internal procedure:
$ gfortran -cpp -DPROG_CONTAINS -o demo main.f90 && ./demo
k%re: 3.165778398514 kind(k%re) 4
k%im: 1.959601044655 kind(k%im) 4
aimag(k): 1.959601044655 kind(aimag(k)): 4
With the function in a module:
$ gfortran -cpp -DMOD_CONTAINS -o demo main.f90 && ./demo
k%re: 3.165778513216 kind(k%re) 8
k%im: 1.959601041422 kind(k%im) 8
aimag(k): 1.959601041422 kind(aimag(k)): 8
I would expect both cases to give kind 8, since myfunc is declared as
returning complex(wp), where wp is real64. The module version behaves
correctly, while the internal procedure version seems to lose the declared
kind of the complex function result inside the associate construct.
*Compiler version*:
- GCC version 17.0.0 20260506 (experimental) (tested online:
https://godbolt.org/ )
- GCC 14.2.0-4ubuntu2~24.04.1 (tested locally on Ubuntu 24.04.4 LTS)
Best regards,
Samir
--
s.ouchene