Hello Samir,

You have found a bug in the ASSOCIATE construct, as the version below
demonstrates. Please submit a bug report to bugzilla.

module m
        use, intrinsic :: iso_fortran_env, only: wp=>real64
        implicit none(type, external)

        contains
            complex(wp) function myfunc_m(x)
            complex(wp), intent(in) :: x
            myfunc_m = sin(x)
        end function myfunc_m
end module m

program demo
    use, intrinsic :: iso_fortran_env, only: wp=>real64
    use m, only: myfunc_m
    implicit none(type, external)
    character(len=150) :: fmt1
    complex(wp) :: z, res

    fmt1='(1X,A10, 2X, F0.12, 5X, A18, 2X, I0)'
    z = (1.0_wp, 2.0_wp)

    print *, 'module version'
    associate(k => myfunc_m(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

    print *, ''
    print *, 'contained version with associate'
    associate(k => myfunc_c(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

    print *, ''
    print *, 'contained version'
    res = myfunc_c(z)
    print fmt1, 'res%re: ',   res%re,     'kind(res%re)', kind(res%re)
    print fmt1, 'res%im: ',   res%im,     'kind(res%im)', kind(res%im)
    print fmt1, 'aimag(res):', aimag(res), 'kind(aimag(res)):', kind(aimag(res))

contains
        complex(kind = wp) function myfunc_c(x)
           complex(wp), intent(in) :: x
           myfunc_c = real (sin(x), kind = wp)
        end function myfunc_c

end program demo

Regards

Thanks

On Wed, 6 May 2026 at 12:29, s.ouchene <[email protected]> wrote:
>
> 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

Reply via email to