J3/04-219
Date: 2004-01-20
To: J3
From: fortran.com
Subject: ASSERT() function/instrinsic
Reference: Pub-111
This was submitted by Tim Connors tconnors+f20xx@astro.swin.edu.au
===========================================================
Number:
Title: ASSERT() function/instrinsic
Submitted by: J3
Status: For Consideration
References:
Basic Functionality: Several languages implement an assert() like function that
asserts that a boolean is true. If not, then line number/file
of the caller, register contents, backtrace, a message
supplied by the user (with C's printf like rules) may be
displayed.
While one can write a subroutine in fortran to do some of
this, one can never truly come up with a satisfactory
solution - you might have to come up with one routine that
accepts a character string, another one that accepts a
string and an array of integers to print out diagnostics,
etc. And one can rarely invoke the debugger feature that
the compiler may or may not supply (eg, Intel FC can
display contents of variables and a backtrace upon certain
error conditions, when in debugging mode), or the
location of the error in the source code.
An ideal implementation would be specified by the standard,
where the standard specifies that assert accepts a logical,
a write() like syntax, complete with format specifier, and
which lun to output to.
A minimal implementation of the standard would test the
logical assertion, if false, print the message to lun,
and then exit.
A maximal implementation may print whatever it feels
necessary before exiting (preferably, all of the above).
Rationale: Assertions are useful features for testing "this cannot happen"
conditions, and well as general debugging.
Good programming style says you should always test to make
sure your assumptions are valid and sane. If you want to know
exactly which assumption failed, you have to write out the line
number/file near where the test failed, which is error prone if
source changes often. Since the compiler knows exactly which
line an assert() is called from, it might as well be utilised.
Estimated Impact: If implemented as a intrinsic, there will be another reserved
word alongside the likes of write(). The compiler may well be
able to use exactly the same code it generates for write, with
only minor modifications.
If implemented as a function/subroutine which must be called,
I have no idea whether compilers can make use of the same
syntax that write() accepts, but no new reserved word will be
required.
Detailed Specification:
History: Submitted as Pub-111