I am trying to resurrect some code I wrote about 10 years ago that attempts (a very feeble one I’ll admit) to create a generic list class using unlimited polymorphic data. The code was working with ifort and gfortran back then but now the lastest ifx gives the following error.
AlistDataClass.F90(76): error #5585: A statement that might result in the deallocation of a polymorphic entity is not permitted in a pure procedure.
If (ALLOCATED(data2%value)) DEALLOCATE(data2%value)
The procedure the error occurs in is declared ELEMENTAL (so its also pure). The value component of data2 is defined as:
CLASS(*), ALLOCATABLE :: value
So my question is what happened between the last time I tried to build this code about 10 years ago and now that causes the code to throw an error when it didn’t before. I guess one fix is to declare the procedure IMPURE ELEMENTAL. Again, I’m just curious why this is now an error.
Up through Fortran 2003 (inclusive) an ELEMENTAL procedure was automatically pure—you could not write an elemental procedure that had side-effects. The language defined the ELEMENTAL prefix to carry with it all the constraints of PURE, so you never had to (and were not allowed to) add a separate PURE keyword. This rule applied in the original standard that introduced user-defined elemental procedures (Fortran 95) and was unchanged in Fortran 2003.
The rule changed in Fortran 2008. Beginning with that revision you may add the prefix keyword IMPURE in front of ELEMENTAL; if you do so, the procedure is elemental but not pure, and it may perform I/O, modify global state, call impure procedures, etc. When the IMPURE prefix is absent, an elemental procedure is still required to be pure, so “elemental ⇒ pure” remains the default; it is just no longer mandatory.
Declaring the procedure to be IMPURE ELEMENTAL fixes the error. I guess this is the result of a later interpretation of the Standard related to not changing global data in a pure procedure. It’s just annoying to me that something stops working that worked before because of a different interpretation of the standard which flies in the face of the often used excuse used by some Committee members to kill proposed changes to the standard because they would break existing code.
I think this is one of the cases that were discovered after F2008 was published where it was determined that it was never intended to allow this use. So, we get the corrections to F2008 than then appear in F2018. You can see the committee discussion for items F08/0031 and F08/0032 here.
It’s fair to be annoyed at human frailty. I don’t think that honest mistakes and deliberate modifications can be judged in the same way.