Is the PURE keyword enforced or assumed to be true by a Fortran compiler?

Viewed 90

Is a Fortran 90+ compliant compiler required to always reject compiling functions marked as PURE, if the function violates a requirement of being PURE?

In other words, can I be sure (assuming the compiler is bug free), that if I mark an arbitrary function PURE, there will be a compile-time error instead of UB?

Contrast this with restrict in C++, where it is a promise from the programmer to the compiler, that the given pointer/reference is never aliased. The compiler does its job with this assumption and if at any point the pointers are aliased, you get UB at runtime.

2 Answers

In Fortran 2018 all of the constraints listed that apply to a "pure" subprogram are so-called numbered constraints (F2018, 15.7 p.2). This means that the compiler is required to be able to diagnose violations of any such constraint. (In practice, there's little motivation for a compiler to choose to let the programmer off the hook and allow such violations.) A violation of a numbered constraint does not need to be diagnosed at compile-time (instead of run-time), although that's generally the time it would be.

However, the constraints that apply to a "pure" subprogram are not necessarily those that are required to make the subprogram pure in a pure computer science sense.


There is a complementary question which can also be addressed here. The constraints I mention apply to pure subprograms, but these aren't the only places where a programmer can mark a procedure to be pure.

The constraints on pure subprograms apply where we have something we want to compile:

function f(x, y, z)
  ...
end function

Adding the PURE prefix to give instead

pure function f(x, y, z)
  ...
end function

is where assessment of the constraints would be expected: something which makes PURE inappropriate for f will elicit complaints.

Consider, though:

subroutine s(f)
  interface
    pure function f(x,y,z)
      ...
    end function f
  end interface
end subroutine s

Here we can add the PURE prefix to the dummy procedure's interface block. But we aren't defining a subprogram here and so the numbered constraints don't apply: we can't expect a compiler to check whether the actual argument procedure is pure. There is no numbered constraint (or syntax rule) which says the interface block must be correct for the ultimate use. That's our responsibility as a programmer, and one the compiler may trust we give take seriously. Your compiler will make a good stab at checking and pick up many cases, but it doesn't have to account for our wilful desire to obfuscate.

If you are writing a Fortran function yourself and mark it pure then the compiler must check that it conforms to the requirements. However, there are many options where you can lie to the compiler and slip a non-pure external (or even a non-Fortran) function where a pure function is promised in an interface block. In such cases it is indeed a promise even if holding it is required by the standard and violating it makes the prohram non-conforming and thus non-Fortran.

Related