lfortran / lfortran

Official main repository for LFortran
https://lfortran.org/
Other
942 stars 152 forks source link

Assert statement #3941

Open certik opened 5 months ago

certik commented 5 months ago

Related proposal and discussion: https://github.com/j3-fortran/fortran_proposals/issues/70

There are several approaches:

Fortran subroutine

We implement a Fortran function assert in a module, import a module and use it. That was done here:

https://github.com/lfortran/lfortran/pull/3940

use lcompilers_assert, only: assert
call assert(abs(x-y) < 1e-10_dp)

One can also consider providing floating point specific assert:

use lcompilers_assert, only: assert_close
call assert_close(x, y)

Intrinsic subroutine

call assert(abs(x-y) < 1e-10_dp)

Intrinsic statement

assert(abs(x-y) < 1e-10_dp)
certik commented 3 months ago

One can emulate the statement assert(abs(x-y) < 1e-10_dp) using a macro.

One could probably also emulate the subroutine call call assert(abs(x-y) < 1e-10_dp) using a macro that would transform it into call assert_impl(expr="abs(x-y) < 1e-10_dp", filename="/path/to/test_file.f90", function="test_abs", line=134, cond=abs(x-y) < 1e-10_dp) or something like that.