Search code examples
fortrangfortran

gfortran compiler bug?


I work in an environment where we have a custom preprocessor for our fortran code. I use the pre-processor to toggle logical parameters as a sort of poor man's function pointers. I realized today that I had a situation similar to this:

  program main
  logical,parameter :: untrue=.false.
  if(untrue)then
    call func1()
  else
    call func2()
  endif
  end

  subroutine func2()
  print*,"Hello, World!"
  end

In other words, func1 is not anywhere defined, but, this compiles with gfortran (version 4.4 and 4.6) because presumably the compiler optimizes that call away. I don't have other compilers to check against at the moment, does this code compile elsewhere? Could/Should this be considered a compiler bug?

Part of the reason I toggle logical switches (instead of including/not including code) is so that the compiler can still check the interface/syntax (if capable) inside that block of code (and issue appropriate warnings for things that aren't simple function calls). Does this imply those tests aren't being performed or just that the function isn't required by the linker so it all goes through Ok?


Solution

  • It is not a compiler bug. The absence of a referenced external subprogram is not something that the standard requires a Fortran processor to diagnose. Other processors may or may not complain. Things like optimisation settings may affect the outcome - Intel Fortran 12.1.5 complains with optimisation off and does not complain with optimisation on.

    For your example code, where func2 is an external subprogram, Fortran's separate compilation model for program units means that procedure interface checking is not required. With many processors it is not likely to happen (practically func2 could perhaps be in a separate file that is compiled long after the main program, perhaps on a different machine, perhaps in a situation where the source for the main program is no longer available). If you want guaranteed procedure interface checking then you need to ensure the procedures have an explicit interface, say by putting them in a module. The procedure pointer language feature, introduced in the Fortran 2003 standard and supported by gfortran 4.6 at least, may also help.