1

Let's assume I have the following fortran program

program foobar
implicit none

interface
    pure function foo (var1, var2) result(ans)
        implicit none
        integer, dimension(:),          intent(in) :: var1
        integer, dimension(size(var1)), intent(in) :: var2
        integer, dimension(product(var1 + var2)) :: ans
    end function foo

    pure function bar (n, var1, var2) result(ans)
         implicit none
         integer,                        intent(in) :: n
         integer, dimension(:),          intent(in) :: var1
         integer, dimension(size(var1)), intent(in) :: var2
         integer, dimension(n)                      :: ans
   end function bar
end interface

write (*,*) foo([1, 1, 1], [2, 2, 2])
! write (*,*) bar(27, [1, 1, 1], [2, 2, 2])   
end program foobar

pure function foo (var1, var2) result(ans)
     implicit none
     integer, dimension(:),          intent(in) :: var1
     integer, dimension(size(var1)), intent(in) :: var2
     integer, dimension(product(var1 + var2))   :: ans
     ans = 0
end function foo

pure function bar (n, var1, var2) result(ans)
     implicit none
     integer,                        intent(in) :: n
     integer, dimension(:),          intent(in) :: var1
     integer, dimension(size(var1)), intent(in) :: var2
     integer, dimension(n) :: ans
     ans = 0
end function bar

If I compile this code with

gfortran -fcheck=all -o bbb bbb.F08

then I get the following error during execution

Fortran runtime error: Array bound mismatch for dimension 1 of array 'var2' (0/3)

If I omit the -fcheck=all option, the code compiles and runs just fine. If I comment the first write in the main program and uncomment the second one, the code compiles and runs fine, regardless of whether I use the -fcheck=all option. I couldn't check with other compilers, since gfortran is the only one that I have access to at the moment.

As far as I understand, the compiler cannot determine the size of the array and therefore the check fails during runtime. Is this interpretation correct? Secondly, is there some rule as to what I may place inside dimension() declarations, such that the compiler can still do some bounds checking at runtime? Should I always pass explicitly the size of an array? That way I could probably get around these issues, but it would make calling these functions quite cumbersome.

2 Answers 2

1

First, to answer what can be in a dimension declaration for your result variable ans.

ans is an explicit-shape array, so the bound you provide must be a specification expression. Because it is a result variable it needn't be a constant expression, just a restricted expression. Details of how a restricted expression is made up are given in Fortran 2008 7.1.11.

Let's look at the things that make up the expression product(var1 + var2). If that's a restricted expression, you're fine to use it as you have.

var1 and var2 are dummy variables without the intent(out) and optional attributes. So that's good. What's next?

+ here is the intrinsic operation. Tick. Making var1+var2 a restricted expression.

product is a standard intrinsic function which is not a specification inquiry function, and it has argument a restricted expression, and its result in this case is a scalar integer epxression.

So, all that said: it's fine.

And, as Vladimir F says: it's a compiler bug. This just confirms it's not a programmer bug.

Sign up to request clarification or add additional context in comments.

Comments

1

This is a compiler bug, already fixed in recent versions. On my computer it fails with gfortran 4.7.and runs OK with pre-release of gfortran-5.

3 Comments

With ifort14.0, attaching -check gives an error message "catastrophic error: Internal compiler error: ..." Even if I moved foo() and bar() inside a module and delete their interfaces, still the same error (if -check is given). On the other hand, Oracle Fortran compiler 12.4 (for Linux) had no problem with -C. So mysterious...
"Internal compiler error", obviously these corners are not too well tested in some compilers.
It fails for me with gfortran 5.1 from the TDM 64bit distribution on Windows 8.1 and gfortran 4.8 on OpenSuse 13.2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.