Skip to content

Commit b911395

Browse files
committed
Add assert_always subroutine
This procedure is similar to assert, but enforcement is unconditionally enabled (ignores the `ASSERTIONS` define). Potentially useful in callers where performance is not a concern but correctness checking is critical. Currently undocumented.
1 parent 43aa0fd commit b911395

2 files changed

Lines changed: 20 additions & 7 deletions

File tree

src/assert/assert_subroutine_m.F90

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ module assert_subroutine_m
3333
!!
3434
implicit none
3535
private
36-
public :: assert
36+
public :: assert, assert_always
3737

3838
#ifndef USE_ASSERTIONS
3939
# if ASSERTIONS
@@ -47,7 +47,8 @@ module assert_subroutine_m
4747
interface
4848

4949
pure module subroutine assert(assertion, description, diagnostic_data)
50-
!! If assertion is .false., error-terminate with a character stop code that contains diagnostic_data if present
50+
!! If assertion is .false. and enforcement is enabled (e.g. via -DASSERTIONS=1),
51+
!! then error-terminate with a character stop code that contains diagnostic_data if present
5152
implicit none
5253
logical, intent(in) :: assertion
5354
!! Most assertions will be expressions such as i>0
@@ -57,6 +58,14 @@ pure module subroutine assert(assertion, description, diagnostic_data)
5758
!! Data to include in an error ouptput: may be of an intrinsic type or a type that extends characterizable_t
5859
end subroutine
5960

61+
pure module subroutine assert_always(assertion, description, diagnostic_data)
62+
!! Same as above but always enforces the assertion (regardless of ASSERTIONS)
63+
implicit none
64+
logical, intent(in) :: assertion
65+
character(len=*), intent(in) :: description
66+
class(*), intent(in), optional :: diagnostic_data
67+
end subroutine
68+
6069
end interface
6170

6271
end module assert_subroutine_m

src/assert/assert_subroutine_s.F90

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,18 @@
1010
contains
1111

1212
module procedure assert
13-
use characterizable_m, only : characterizable_t
14-
15-
character(len=:), allocatable :: header, trailer
1613

1714
toggle_assertions: &
1815
if (enforce_assertions) then
16+
call assert_always(assertion, description, diagnostic_data)
17+
end if toggle_assertions
18+
19+
end procedure
20+
21+
module procedure assert_always
22+
use characterizable_m, only : characterizable_t
23+
24+
character(len=:), allocatable :: header, trailer
1925

2026
check_assertion: &
2127
if (.not. assertion) then
@@ -59,8 +65,6 @@
5965

6066
end if check_assertion
6167

62-
end if toggle_assertions
63-
6468
contains
6569

6670
pure function string(numeric) result(number_as_string)

0 commit comments

Comments
 (0)