(j3.2006) ERROR STOP stop-code in PURE procedures

Damian Rouson damian
Wed Mar 18 18:39:43 EDT 2015


All,

At the most recent J3 meeting, there was a discussion of whether to allow the ERROR STOP stop-code to be anything other than scalar-default-char-constant-expr or scalar-int-constant-expr.   I believe Bill commented that this would be syntactic sugar (not necessarily using that terminology) that a user could always emulate simply by writing to ERROR_UNIT just before the execution of the ERROR STOP.  There is one case, however, where the latter approach fails: the draft Fortran 2015 standard allows ERROR STOP inside PURE procedures, but input/output in PURE procedures is disallowed. 

This severely limits the use of general error-handling code inside PURE procedures.  For example, it can be useful to write a general collection of assertion procedures, one very simple example of which follows:

 pure subroutine assert(assertion,error_text)
    logical, intent(in) :: assertion
    character(len=*), intent(in) :: error_text
    if (.not. assertion) error stop "scalar_assert: assertion fails with the message" // error_text
 end subroutine

I think it aids code clarity to be able to call such assertions at the beginning and end of every procedure in a style inspired by programming by contract.  Without the ability to have a non-constant stop-code, however, such procedures are much less useful because they either (1) can?t be PURE and therefore can't be called inside PURE procedures or (2) must print the same stop-code no matter what procedure calls the assertion procedure.
 
I hope the addition of non-constant stop-codes can be considered as wart removal. It addresses the interaction between two existing features PURE and ERROR STOP in a way that only becomes possible in the draft standard.  If others agree, I volunteer to work on the proposed edits.

Damian

P.S. In anticipation of the draft standard, the pre-release GFortran 5.0 compiler added support for ERROR STOP inside PURE procedures about three months ago.



More information about the J3 mailing list