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.