Search code examples
fortranintel-fortran

Avoid implicit conversion between float and integer for an array index


I am looking for a way to force the Fortran compiler to give an error message when I am using a real number in a situation where I should not.

For example,

Real :: i1,i2
Real :: A(1000,1000) , B(2000,2000)

A(i1:i2,:) =B(i1:i2,1:1000)

I had a code like this and I was not realizing my declarations of i1 and i2. Sometimes I used to get problems because of that until I realized it. Apparently, Fortran makes an implicit conversion, which I would rather be informed about.

Is there no way a Fortran compiler can see that the array is not being referenced with expected indices?


Solution

  • The Fortran language specification requires an array subscript to be a (scalar) integer expression. Further, this is a part of the language that requires a valid Fortran compiler to be able to detect and report an attempt to violate this constraint.

    Some compilers will choose to report this use of a real array index by default as an error. Others may accept it as an extension and possibly providing a diagnostic warning. The Intel compiler (currently) defaults to silently accepting this as an extension. Even in those cases where a diagnostic isn't made, there should be an option to enable such reporting (for the compiler to be conforming to the Fortran specification).

    In the case of ifort, the option -stand:

    warning #6187: Fortran 2008 requires an INTEGER data type in this context.

    You can even combine this with -diag-error=6187 to upgrade the diagnostic to an error.