Should this be an error or warning

Looking at a tutorial I found on the web for Fortran 95 (Fortran 90/95 Programming Manual, Tanja van Mourik, 5ed 2005) to understand assumed shape arrays I see the following code and wonder if the definition for integer :: i should throw an error or warning when compiled. Neither gfortran nor lfortran do with -Wall.

program dummy_array_assumed
   implicit none
   integer, dimension(10) :: a
      subroutine fill_array(a)
         integer, dimension(:), intent(out) :: a
         ! this line was in the original, i think it's a typo, but should it get an error?
         integer :: i
      end subroutine fill_array
   end interface
   call fill_array(a)
   print *, a
end program dummy_array_assumed
subroutine fill_array(a)
   implicit none
   integer, dimension(:), intent(out) :: a
   integer :: i
   do i = 1, 10
      a(i) = i
   end do
end subroutine fill_array

The definition is superfluous and is likely a copy/paste artifact.



It won’t through an error.

GNU fortran assumes integers to be 4 bit integers by default.
So integer :: i is valid code.

I think the program is conforming Fortran. The contents of the interface body is a specification part with a few restrictions (e.g. no data statements or format statements), but I can’t find anything that would disallow integer :: i. So the compiler should not report an error.

Yes, it would be helpful for compilers to warn about this.

I think these superfluous declarations are allowed to cover the case where the actual declarations in the subroutine are mixed with local variables and dummy arguments. To create the interface block, the programmer can just copy the declarations as-is from the subroutine, without the need to edit out the extra stuff.

In Fortran, defining a variable like integer :: i inside a subroutine or function that doesn’t use this variable doesn’t necessarily result in a compilation error or warning. This is because Fortran compilers, by default, might not check for unused variables. The behavior can vary depending on the compiler and the specific compiler flags used.

In your code, the integer :: i declaration inside the interface block of the fill_array subroutine does seem superfluous. Since i is not used within the interface block, it doesn’t serve any purpose there. However, it’s crucial in the actual definition of the fill_array subroutine, where it’s used as the loop variable.

Compilers like gfortran and lfortran are designed to adhere to the Fortran standard, which allows for such declarations without use. The -Wall flag in gfortran enables a variety of warnings, but it may not cover every potential issue, like unused variables, unless specifically configured to do so. To get warnings about unused variables, you might need to use additional flags, such as -Wunused-variable or a similar flag, depending on the compiler.

In summary, while the integer :: i declaration inside the interface block is unnecessary, it doesn’t violate the Fortran standard and hence doesn’t result in an error or warning with the standard compilation flags. It’s good practice to remove such unused declarations to improve code clarity and maintainability.