Could someone explain why the following Fortran code compiles and runs?:
PROGRAM wrong_array_shape
IMPLICIT NONE
REAL :: x(2)
INTEGER :: n
x = 1.
n = 2
WRITE(*, *) f(x, n)
CONTAINS
REAL FUNCTION f(x, n)
IMPLICIT NONE
INTEGER, INTENT(IN) :: n
REAL, INTENT(IN) :: x(n, n)
f = x(1, 1)
END FUNCTION f
END PROGRAM wrong_array_shape
The array x
that is declared in the main program is a one-dimensional array of length 2
, while the function is expecting a two-dimensional array of size n x n
. I expected this to fail to compile and/or fail to run, but it runs fine and writes out the result 1.000000...
to the screen.
I would like to have a compile-time warning (or preferably a fail) when compiling code that contains errors like this.
In case is is relevant, I am using gfortran 9.2.0 and compile with no special flags, I compile simply with gfortran wrong_array_shape.f90
.