- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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)
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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)

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page