ERROR STOP in a simple procedure

The F2023 standard allows an ERROR STOP statement in a pure procedure, but is it allowed in a simple procedure?

Yes, that is my reading of the public Fortran 2023 draft. In J3/23-007r1, 15.8 (pp. 346–347) makes a SIMPLE procedure subject to the PURE constraints, then adds C15111–C15117. Those extra constraints do not prohibit ERROR STOP.

The expressions used by the statement still have to obey the SIMPLE restrictions—for example, the stop code cannot read a host-associated variable (C15115). A literal error stop 1 does not have that problem.

Source: https://j3-fortran.org/doc/year/23/23-007r1.pdf#page=360

This is based on the draft text; I haven’t checked compiler support.

Yeah, as noted you can call it most anywhere.

There is a surprising amount of variation in the behavior of the family of STOP statements.

Some will provide a traceback on one compiler but not on another. The messages displayed may vary from nothing to displaying something like “STOP N”. The output might go to stdout or stderr. It may or may not flush the output.

If using coarrays (or OpenMP for that matter) you should definitely become familiar with section 11.4 of the Fortran standard.

Section 11.4 of the standard specifies the details but leaves quite a bit about the behavior up to the platform. The new QUIET argument helps control the variation but is not implemented on several platforms so far.

I program that calls different stop variants can be called from several compilers using the Fortran playground. I could not see how to pass a command line argument (yet) in the playground so I changed the demo program to default to ‘all’. Typically you would call it with just one name.

`

Section 11.4: STOP and ERROR STOP statements

``txt
14 11.4 STOP and ERROR STOP statements

  15        R1162 stop-stmt                             is   STOP[stop-code ] [ , QUIET = scalar-logical-expr]
  16        R1163 error-stop-stmt                       is   ERRORSTOP[stop-code ] [ , QUIET = scalar-logical-expr]
  17        R1164 stop-code                             is   scalar-default-char-expr
  18                                                    or scalar-int-expr

  19        C1176 (R1164) The scalar-int-expr shall be of default kind.

  20        Execution of a STOP statement initiates normal termination of execution. Execution of an ERROR STOP
  21        statement initiates error termination of execution.

  22        When an image is terminated by a STOP or ERROR STOP statement, its stop code, if any, is made available
  23        in a processor-dependent manner. If the stop-code is an integer, it is recommended that the value be used as
  24        the process exit status, if the processor supports that concept. If the stop-code in a STOP statement is of type
  25        character or does not appear, or if an end-program-stmt is executed, it is recommended that the value zero be
  26        supplied as the process exit status, if the processor supports that concept. If the stop-code in an ERROR STOP
  27        statement is of type character or does not appear, it is recommended that a processor-dependent nonzero value
  28        be supplied as the process exit status, if the processor supports that concept.

  29        If QUIET= is omitted or the scalar-logical-expr has the value false:

  30            • if any exception (17) is signaling on that image, the processor shall issue a warning indicating which
  31               exceptions are signaling, and this warning shall be on the unit identified by the named constant ERROR_-
  32               UNIT from the intrinsic module ISO_FORTRAN_ENV (16.10.2.9);
  33            • if a stop code is specified, it is recommended that it be made available by formatted output to the same
  34               unit.

  35        If QUIET= appears and the scalar-logical-expr has the value true, no output of signaling exceptions or stop code
  36        shall be produced.

            NOTE1
             When normal termination occurs on more than one image,
             it is expected that a processor-dependent summary

of
any stop codes and signaling exceptions will be made
available.

            NOTE2

            If the integer stop-code is used as the process exit
            status, the processor might be able to interpret only
            values

within a limited range, or only a limited
portion of the integer value (for example, only the
least-significant 8
bits).

open demo_stop in Fortran Playground

source of demo_stop.f90
program demo_stop
   ! select which STOP call to make from command line
   use, intrinsic :: iso_fortran_env, only : stderr=>ERROR_UNIT
   implicit none
   integer                      :: stopcode, i
   character(len=:),allocatable :: which, message
   character(len=*),parameter   :: types(*)=[character(len=20) :: &
      'basic',  &
      'text', &
      'zero', &
      'nonzero', &
      'variable', &
      'expression', &
      'error', &
      'errornum', &
      'errorstring']

   which=get_arg(1)
   if(which=='')which='all'

   select case(which)
      ! normal terminations:
      ! A STOP with no non-zero numeric parameter is a normal
      ! termination and generally returns a zero status value if the
      ! system supports return statuses
    case('basic'); stop    ! usually displays nothing
    case('zero');  stop 0  ! sometimes displays "STOP 0" or "0"
    case('text');  stop 'That is all, folks!'
      !
      ! All other stops are generally used to indicate an error or
      ! special exit type
    case('nonzero');               stop 10
    case('variable'); stopcode=11;   stop stopcode
    case('expression'); stopcode=11; stop 110/stopcode
    case('string'); message='oops';  stop 'ERROR:['//message//']'
      ! Error terminations:
      ! ERROR STOP is always an error stop, even without a stop-code
      ! ERROR STOP often displays a traceback but that is not required
    case('error')
      error stop
    case('errornum')
      stopcode=10
      error stop stopcode+3
    case('errorstring')
      message='That is all, folks!'
      error stop 'ERROR:'//message
    case('all')
      call platform()
      which=get_arg(0)
      do i=1,size(types)
         write(*,*)which//' '//types(i)
         call cmd(which//' '//types(i))
      enddo
    case default
      if(which.eq.'')then
         write(*,'(a)')'enter a stop type on command line.'
      else
         write(*,'(a)')'unknown selection '//which
      endif
      write(*,'(*(a,1x))')'types are',types
   end select
contains

   elemental impure subroutine cmd(command)
      ! a functional interface for calling system commands
      use, intrinsic :: iso_fortran_env, only : &
      & stderr=>ERROR_UNIT, stdout=>OUTPUT_UNIT
      character(len=*),intent(in) :: command
      logical                    :: wait
      integer                    :: exitstat
      integer                    :: cmdstat
      character(len=256)         :: cmdmsg
      wait=.false.
      exitstat=0
      cmdstat=0
      call execute_command_line(command=command,wait=wait, &
      & exitstat=exitstat,cmdstat=cmdstat,cmdmsg=cmdmsg)
      if(cmdstat.ne.0)then
         flush(stdout)
         write(stderr,'(a)')trim(cmdmsg)
         flush(stderr)
      endif
      if(exitstat.ne.0)then
         flush(stdout)
         write(stderr,'(*(g0))')'exitstat=',exitstat,':',trim(command)
         flush(stderr)
      endif
      !cmd=merge(.true.,.false.,exitstat==0)
   end subroutine cmd

   function get_arg(n,status) result(arg)
      integer,intent(in)          :: n
      integer,intent(out),optional :: status
      integer                     :: argument_length, istat
      character(len=:),allocatable :: arg
      !
      ! allocate string big enough to hold command line argument
      !
      call get_command_argument( number=n, length=argument_length )
      if(allocated(arg))deallocate( arg )
      allocate(character(len=argument_length) :: arg )
      call get_command_argument(n, arg, status=istat )
      if(present(status)) status=istat
   end function get_arg

subroutine platform()
use, intrinsic :: iso_fortran_env, only : compiler_version
use, intrinsic :: iso_fortran_env, only : compiler_options
implicit none
character(len=:),allocatable :: version, options
character(len=*),parameter   :: nl=new_line('a')
integer                      :: where, start, break, i, last, col
   version=compiler_version()//' '
   options=' '//compiler_options()
   start=1
   do
      where=index(options(start:),' -')
      if(where.eq.0)exit
      break=where+start-1
      options(break:break)=nl
      start=where
   enddo
   if(start.eq.1)then
      do
         where=index(options(start:),' /')
         if(where.eq.0)exit
         break=where+start-1
         options(break:break)=nl
         start=where
      enddo
   endif
   last=len_trim(version)+1
   col=0
   do i=1,len_trim(version)
    col=col+1
    if(version(i:i).eq.' ')last=i
    if(col.gt.76)then
       version(last:last)=nl
       col=0
    endif
   enddo
   print '(a,/,3x,*(a))', 'This file was compiled by :', inset(version)
   if(options.ne.'')then
      print '(*(a))', 'using the options :', inset(options)
   endif
end subroutine platform

function inset(string) result(longer)
character(len=*),intent(in)  :: string
character(len=:),allocatable :: longer
character(len=*),parameter   :: nl=new_line('a')
integer                      :: i
   longer=''
   do i=1,len(string)
      longer=longer//string(i:i)
      if(string(i:i).eq.nl)then
         longer=longer//'   '
      endif
   enddo
end function inset

end program demo_stop

Run it with multiple compilers here.

Results

Fortran playground compiler comparison
Operation: Compile and Run
Times in seconds. Other = Total - Compile - Run.

=== gfortran: PASS ===
GNU Fortran (GCC) 16.2.0
Settings: {“preset”:“default”,“standard”:“default”,“warnings”:false,“fast_math”:false,“additional”:}
Flags: (compiler default or unavailable)
total: 2.196 compile: 0.279 run: 0.063 other: 1.854
— Compilation stdout —
Build: gfortran -ffree-line-length-none input_p.f90 -o input_p.exe
Build: PASS

— End Compilation stdout —
— Compilation stderr —

— End Compilation stderr —
— Program stdout —
This file was compiled by :
GCC version 16.2.0
using the options :
-mtune=generic
-march=x86-64
-ffree-line-length-none
-fpre-include=/usr/include/finclude/x86_64-linux-gnu/math-vector-fortran.h
/tmp/p2f_execution_erv0ebzl/fortran/input_p.exe basic
/tmp/p2f_execution_erv0ebzl/fortran/input_p.exe text
/tmp/p2f_execution_erv0ebzl/fortran/input_p.exe zero
/tmp/p2f_execution_erv0ebzl/fortran/input_p.exe nonzero
/tmp/p2f_execution_erv0ebzl/fortran/input_p.exe variable
/tmp/p2f_execution_erv0ebzl/fortran/input_p.exe expression
/tmp/p2f_execution_erv0ebzl/fortran/input_p.exe error
/tmp/p2f_execution_erv0ebzl/fortran/input_p.exe errornum
/tmp/p2f_execution_erv0ebzl/fortran/input_p.exe errorstring

— End Program stdout —
— Program stderr —
STOP That is all, folks!
STOP 0
STOP 10
STOP 11
STOP 10
ERROR STOP

Error termination. Backtrace:
ERROR STOP 13

Error termination. Backtrace:
#0 0x2afcd00f9f83 in ???
#1 0x2afcd00fac39 in ???
#2 0x2afcd00fc045 in ???
#3 0x401c21 in ???
#4 0x402d33 in ???
#5 0x2afcd0578ca7 in ???
#6 0x2afcd0578d64 in ???
#7 0x401200 in ???
#8 0xffffffffffffffff in ???
#0 0x2b4ffe48bf83 in ???
#1 0x2b4ffe48cc39 in ???
#2 0x2b4ffe48e0c2 in ???
#3 0x401c3a in ???
#4 0x402d33 in ???
#5 0x2b4ffe90aca7 in ???
#6 0x2b4ffe90ad64 in ???
#7 0x401200 in ???
#8 0xffffffffffffffff in ???
ERROR STOP ERROR:That is all, folks!

Error termination. Backtrace:
#0 0x2b4af705cf83 in ???
#1 0x2b4af705dc39 in ???
#2 0x2b4af705f045 in ???
#3 0x401d33 in ???
#4 0x402d33 in ???
#5 0x2b4af74dbca7 in ???
#6 0x2b4af74dbd64 in ???
#7 0x401200 in ???
#8 0xffffffffffffffff in ???

— End Program stderr —

=== gfortran17: PASS ===
GNU Fortran (GCC) 17.0.0 20261018 (experimental)
Settings: {“preset”:“default”,“standard”:“default”,“warnings”:false,“fast_math”:false,“additional”:}
Flags: (compiler default or unavailable)
total: 2.885 compile: 0.493 run: 0.065 other: 2.327
— Compilation stdout —
Build: p2f-gfortran17 -ffree-line-length-none input_p.f90 -o input_p.exe
Build: PASS

— End Compilation stdout —
— Compilation stderr —

— End Compilation stderr —
— Program stdout —
This file was compiled by :
GCC version 17.0.0 20261018 (experimental)
using the options :
-mtune=generic
-march=x86-64
-ffree-line-length-none
-fpre-include=/usr/include/finclude/x86_64-linux-gnu/math-vector-fortran.h
/tmp/p2f_execution_2_1os3hp/fortran/input_p.exe basic
/tmp/p2f_execution_2_1os3hp/fortran/input_p.exe text
/tmp/p2f_execution_2_1os3hp/fortran/input_p.exe zero
/tmp/p2f_execution_2_1os3hp/fortran/input_p.exe nonzero
/tmp/p2f_execution_2_1os3hp/fortran/input_p.exe variable
/tmp/p2f_execution_2_1os3hp/fortran/input_p.exe expression
/tmp/p2f_execution_2_1os3hp/fortran/input_p.exe error
/tmp/p2f_execution_2_1os3hp/fortran/input_p.exe errornum
/tmp/p2f_execution_2_1os3hp/fortran/input_p.exe errorstring

— End Program stdout —
— Program stderr —
STOP That is all, folks!
STOP 0
STOP 11
STOP 10
STOP 10
ERROR STOP

Error termination. Backtrace:
ERROR STOP 13

Error termination. Backtrace:
#0 0x2b8aa49ab213 in ???
#1 0x2b8aa49abec9 in ???
#2 0x2b8aa49ad2d5 in ???
#3 0x401c21 in ???
#4 0x402d64 in ???
#5 0x2b8aa4e31ca7 in ???
#6 0x2b8aa4e31d64 in ???
#7 0x401200 in ???
#8 0xffffffffffffffff in ???
#0 0x2ae614d20213 in ???
#1 0x2ae614d20ec9 in ???
#2 0x2ae614d22352 in ???
#3 0x401c3a in ???
#4 0x402d64 in ???
#5 0x2ae6151a6ca7 in ???
#6 0x2ae6151a6d64 in ???
#7 0x401200 in ???
#8 0xffffffffffffffff in ???
ERROR STOP ERROR:That is all, folks!

Error termination. Backtrace:
#0 0x2b3af9a99213 in ???
#1 0x2b3af9a99ec9 in ???
#2 0x2b3af9a9b2d5 in ???
#3 0x401d33 in ???
#4 0x402d64 in ???
#5 0x2b3af9f1fca7 in ???
#6 0x2b3af9f1fd64 in ???
#7 0x401200 in ???
#8 0xffffffffffffffff in ???

— End Program stderr —

=== ifx: PASS ===
ifx (IFX) 2025.3.3 20260319
Settings: {“preset”:“default”,“standard”:“default”,“warnings”:false,“fast_math”:false,“additional”:}
Flags: (compiler default or unavailable)
total: 3.808 compile: 0.761 run: 0.062 other: 2.985
— Compilation stdout —
Build: p2f-ifx input_p.f90 -o input_p.exe
Build: PASS

— End Compilation stdout —
— Compilation stderr —

— End Compilation stderr —
— Program stdout —
This file was compiled by :
Intel(R) Fortran Compiler for applications running on Intel(R) 64, Version
2025.3.3 Build 20260319
using the options :
-o input_p.exe
/tmp/p2f_execution_yzj6naz3/fortran/input_p.exe basic
/tmp/p2f_execution_yzj6naz3/fortran/input_p.exe text
/tmp/p2f_execution_yzj6naz3/fortran/input_p.exe zero
/tmp/p2f_execution_yzj6naz3/fortran/input_p.exe nonzero
/tmp/p2f_execution_yzj6naz3/fortran/input_p.exe variable
/tmp/p2f_execution_yzj6naz3/fortran/input_p.exe expression
/tmp/p2f_execution_yzj6naz3/fortran/input_p.exe error
/tmp/p2f_execution_yzj6naz3/fortran/input_p.exe errornum
/tmp/p2f_execution_yzj6naz3/fortran/input_p.exe errorstring

— End Program stdout —
— Program stderr —
That is all, folks!
0
10
11
10
13
ERROR:That is all, folks!

— End Program stderr —

=== flang: PASS ===
Debian flang version 23.1.5 (++20261020014245+93acfd24c599-1~exp1~20261020134308.90)
Settings: {“preset”:“default”,“standard”:“default”,“warnings”:false,“fast_math”:false,“additional”:}
Flags: (compiler default or unavailable)
total: 2.563 compile: 0.521 run: 0.032 other: 2.011
— Compilation stdout —
Build: flang-23 input_p.f90 -o input_p.exe
Build: PASS

— End Compilation stdout —
— Compilation stderr —

— End Compilation stderr —
— Program stdout —
This file was compiled by :
Debian flang version 23.1.5
using the options :
-o input_p.exe
/tmp/p2f_execution_rolkntnr/fortran/input_p.exe basic
/tmp/p2f_execution_rolkntnr/fortran/input_p.exe text
/tmp/p2f_execution_rolkntnr/fortran/input_p.exe zero
/tmp/p2f_execution_rolkntnr/fortran/input_p.exe nonzero
/tmp/p2f_execution_rolkntnr/fortran/input_p.exe variable
/tmp/p2f_execution_rolkntnr/fortran/input_p.exe expression
/tmp/p2f_execution_rolkntnr/fortran/input_p.exe error
/tmp/p2f_execution_rolkntnr/fortran/input_p.exe errornum
/tmp/p2f_execution_rolkntnr/fortran/input_p.exe errorstring

— End Program stdout —
— Program stderr —
Fortran STOP: code 11

Fortran STOP: code 10

Fortran STOP
Fortran STOP
Fortran STOP: That is all, folks!
Fortran ERROR STOP: code 1

Fortran ERROR STOP: code 13

Fortran STOP: code 10

Fortran ERROR STOP: ERROR:That is all, folks!

— End Program stderr —

=== lfortran: PASS ===
LFortran version: 0.66.0
Settings: {“preset”:“default”,“standard”:“default”,“warnings”:false,“fast_math”:false,“additional”:}
Flags: (compiler default or unavailable)
total: 3.077 compile: 0.276 run: 0.032 other: 2.769
— Compilation stdout —
Build: lfortran --no-style-suggestions --no-color --implicit-interface --separate-compilation --legacy-array-sections --realloc-lhs-arrays input_p.f90 -o input_p.exe
Build: PASS

— End Compilation stdout —
— Compilation stderr —

— End Compilation stderr —
— Program stdout —
This file was compiled by :
LFortran version 0.66.0
using the options :
–no-style-suggestions
–no-color
–implicit-interface
–separate-compilation
–legacy-array-sections
–realloc-lhs-arrays
-o input_p.exe
/tmp/p2f_execution_xryn8hkm/fortran/input_p.exe basic
/tmp/p2f_execution_xryn8hkm/fortran/input_p.exe text
/tmp/p2f_execution_xryn8hkm/fortran/input_p.exe zero
/tmp/p2f_execution_xryn8hkm/fortran/input_p.exe nonzero
/tmp/p2f_execution_xryn8hkm/fortran/input_p.exe variable
/tmp/p2f_execution_xryn8hkm/fortran/input_p.exe expression
/tmp/p2f_execution_xryn8hkm/fortran/input_p.exe error
/tmp/p2f_execution_xryn8hkm/fortran/input_p.exe errornum
/tmp/p2f_execution_xryn8hkm/fortran/input_p.exe errorstring

— End Program stdout —
— Program stderr —
STOP
STOP That is all, folks!
STOP 0
STOP 10
exitstat=10:/tmp/p2f_execution_xryn8hkm/fortran/input_p.exe nonzero
STOP 11
exitstat=11:/tmp/p2f_execution_xryn8hkm/fortran/input_p.exe variable
STOP 10
exitstat=10:/tmp/p2f_execution_xryn8hkm/fortran/input_p.exe expression
ERROR STOP
exitstat=1:/tmp/p2f_execution_xryn8hkm/fortran/input_p.exe error
ERROR STOP 13
exitstat=13:/tmp/p2f_execution_xryn8hkm/fortran/input_p.exe errornum
ERROR STOP ERROR:That is all, folks!
exitstat=1:/tmp/p2f_execution_xryn8hkm/fortran/input_p.exe errorstring

— End Program stderr —

=== ofort: FAIL ===
ofort 0.1.0 (commit 77301d0ca146, built on 2022-01-01 05:00 by gcc -O2)
Settings: {“preset”:“default”,“standard”:“default”,“warnings”:false,“fast_math”:false,“additional”:}
Flags: (compiler default or unavailable)
total: 1.976 compile: - run: 0.032 other: 1.944
Compilation: No separate compilation stage (interpreter).
— Program stdout —

— End Program stdout —
— Program stderr —
input.f90:117: Syntax error at line 117: expected ‘(’ but found ‘=’
line 117: where=index(options(start:),’ -')

— End Program stderr —
Output comparison: whitespace; stdout only; atol=0, rtol=0
Reference: gfortran
gfortran: Reference
gfortran17: Different — Token 9: reference “16.2.0”, output “17.0.0”.
ifx: Different — Token 7: reference “GCC”, output “Intel(R)”.
flang: Different — Token 7: reference “GCC”, output “Debian”.
lfortran: Different — Token 7: reference “GCC”, output “LFortran”.
ofort: Not comparable — Requires a successful, complete execution and reference.