Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
28507 Discussions

Using results of impure functions in automatic allocation

V-T
New Contributor I
550 Views

Consider the following minimal working example:

module lib
    type t
    contains
        procedure::f,g,h
    end type
contains
    integer function f(this)
        class(t),intent(in)::this
        real::r
        ! adding the following calculation to avoid inlining of the function result in the function calls
        call RANDOM_NUMBER(r)
        f=42*(1+r)
        print*,"I'm an impure function"
    end
    function g(this)result(c)
        class(t),intent(in)::this
        ! using the result of an impure function for automatic allocation of a CHARACTER variable
        character(len=this%f())::c
        c='42'
    end
    function h(this)result(a)
        class(t),intent(in)::this
        ! using the result of an impure function for automatic allocation of an array
        integer::a(this%f())
        a=42
    end
end

program prog
    use lib
    type(t)::o
    print*,o%g()
    print*,o%h()
end

A smaller example was already discussed here. The Fortran standard doesn't allow using the results of impure functions for automatic allocation of CHARACTER variables or arrays. But the code above compiles successfully with the Intel compiler (ifort 2021.8.0 and ifx 2023.0.0 on Ubuntu 18.04.2 LTS). Compiling the code with "-stand f18" doesn't change anything.

I suppose, it is a compiler bug, because running the compiled code leads to unexpected behavior: the message `I'm an impure function` is printed 5 times, even though the function `f` is called only twice.

Labels (2)
1 Solution
Barbara_P_Intel
Moderator
348 Views

With the release of oneAPI HPC Toolkit 2023.2 this week, the Fortran compilers now print error messages.

Please download this release and check it out.

 

$ ifx func.f90
func.f90(18): error #7508: A specification function must be a pure function.   [F]
        character(len=this%f())::c
---------------------------^
func.f90(24): error #7508: A specification function must be a pure function.   [F]
        integer::a(this%f())
------------------------^
compilation aborted for func.f90 (code 1)

$ ifort func.f90
func.f90(18): error #7508: A specification function must be a pure function.   [F]
        character(len=this%f())::c
---------------------------^
func.f90(24): error #7508: A specification function must be a pure function.   [F]
        integer::a(this%f())
------------------------^
compilation aborted for func.f90 (code 1)

View solution in original post

0 Kudos
2 Replies
Barbara_P_Intel
Moderator
471 Views

I filed a bug report, CMPLRLLVM-45330, to print an error message and determine why "I'm an impure function" is printed 5x. I'll let you know when the fix is available.


0 Kudos
Barbara_P_Intel
Moderator
349 Views

With the release of oneAPI HPC Toolkit 2023.2 this week, the Fortran compilers now print error messages.

Please download this release and check it out.

 

$ ifx func.f90
func.f90(18): error #7508: A specification function must be a pure function.   [F]
        character(len=this%f())::c
---------------------------^
func.f90(24): error #7508: A specification function must be a pure function.   [F]
        integer::a(this%f())
------------------------^
compilation aborted for func.f90 (code 1)

$ ifort func.f90
func.f90(18): error #7508: A specification function must be a pure function.   [F]
        character(len=this%f())::c
---------------------------^
func.f90(24): error #7508: A specification function must be a pure function.   [F]
        integer::a(this%f())
------------------------^
compilation aborted for func.f90 (code 1)
0 Kudos
Reply