Error stop unlike stop can be used in pure procedures and should be used to signal errors. Pure-Fortran has a script xerror_loc.py to enhance an error stop message by appending the line of the file, module, and procedure name and the values of the variables that triggered the error. For example, for the code
module m
implicit none
contains
pure function dot(x, y) result(sum_xy)
real, intent(in) :: x(:), y(:)
real :: sum_xy
if (size(x) /= size(y)) error stop "size(x) /= size(y)"
sum_xy = sum(x*y)
end function dot
end module m
program main
use m
implicit none
print*,dot([2.0], [3.0, 4.0])
end program main
python xerror_loc.py xerror_stop_dot.f90 --condition-values --run-both --out temp.f90
gives
Applied 0 location tag rewrite(s) and 1 IF-block rewrite(s) across 1 file(s).
Build (original-fortran): gfortran xerror_stop_dot.f90 -o xerror_stop_dot.original-fortran.exe
<snip>
ERROR STOP size(x) /= size(y)
<snip>
Build (transformed-fortran): gfortran
<snip>
Build (transformed-fortran): PASS
Run (transformed-fortran): temp.transformed-fortran.exe
Run (transformed-fortran): FAIL (exit 1)
ERROR STOP size(x) /= size(y) [temp.f90::module m::function dot::line 7]: size(x) = 1, size(y) = 2
In the error message for the transformed code, the line number of the error stop and the values of size(x) and size(y) are shown. You could print these before the error stop, but print is not allowed in pure functions. The new code around the error stop is
block
character(len=1000) :: msg
write(msg, "(a,a,g0,a,g0)") &
& "size(x) /= size(y) [temp.f90::module m::function dot::line 7]", &
& ": size(x) = ", size(x), ", &
& size(y) = ", size(y)
error stop trim(msg)
end block
Using this tool you can get detailed information about why a program stopped without using a debugger or adding print statements (which may not be allowed).