@@ -2383,31 +2383,26 @@ $(GNAME AssertArguments):
2383
2383
$(GLINK AssignExpression) $(D ,) $(GLINK AssignExpression) $(D ,)$(OPT)
2384
2384
)
2385
2385
2386
- $(P The first $(I AssignExpression) must evaluate to true. If it does not, an $(I Assert Failure)
2386
+ $(P The first $(I AssignExpression) must evaluate to ` true` . If it does not, an $(I Assert Failure)
2387
2387
has occurred and the program enters an $(I Invalid State).
2388
2388
)
2389
2389
2390
- $(P If the first $(I AssignExpression) consists entirely of compile time constants,
2391
- and evaluates to false, it is a special case; it
2392
- signifies that it is unreachable code.
2393
- Compile Time Function Execution (CTFE) is not attempted.
2394
- )
2390
+ ---
2391
+ int i = fun();
2392
+ assert(i > 0);
2393
+ ---
2395
2394
2396
2395
$(P $(I AssertExpression) has different semantics if it is in a
2397
2396
$(DDLINK spec/unittest, Unit Tests, $(D unittest)) or
2398
- $(DDLINK spec/contracts, Contract Programming, $(D in) contract).
2399
- )
2400
-
2401
- $(P The second $(I AssignExpression), if present, must be implicitly
2402
- convertible to type $(D const(char)[]).
2397
+ $(DDSUBLINK spec/function, preconditions, $(D in) contract).
2403
2398
)
2404
2399
2405
2400
$(P If the first $(I AssignExpression) is a reference to a class instance for
2406
- which a $(DDSUBLINK spec/class, invariants, class Invariant) exists, the class $(I Invariant) must hold.
2401
+ which a $(DDSUBLINK spec/class, invariants, class * Invariant* ) exists, the class $(I Invariant) must hold.
2407
2402
)
2408
2403
2409
2404
$(P If the first $(I AssignExpression) is a pointer to a struct instance for
2410
- which a struct $(I Invariant) exists, the struct $(I Invariant) must hold.
2405
+ which a $(DDSUBLINK spec/ struct, Invariant, struct $(I Invariant) ) exists, the struct $(I Invariant) must hold.
2411
2406
)
2412
2407
2413
2408
$(P The type of an $(I AssertExpression) is $(D void).
@@ -2417,21 +2412,51 @@ $(GNAME AssertArguments):
2417
2412
of the program is undefined.)
2418
2413
2419
2414
$(IMPLEMENTATION_DEFINED Whether the first $(I AssertExpression) is evaluated
2420
- or not at runtime is typically set with a compiler switch. If it is not evaluated,
2415
+ or not ( at runtime) is typically set with a compiler switch. If it is not evaluated,
2421
2416
any side effects specified by the $(I AssertExpression) may not occur.
2422
- The behavior if the first $(I AssertExpression) is evaluated and is false
2423
- is also typically set with a compiler switch and may include these options:
2417
+ The behavior when the first $(I AssertExpression) evaluates to ` false`
2418
+ is also typically set with a compiler switch, and may include these options:
2424
2419
$(OL
2425
- $(LI continuing execution)
2426
- $(LI immediately halting via execution of a special CPU instruction)
2427
- $(LI aborting the program)
2428
- $(LI calling the assert failure function in the corresponding C
2420
+ $(LI Immediately halting via execution of a special CPU instruction)
2421
+ $(LI Aborting the program)
2422
+ $(LI Calling the assert failure function in the corresponding C
2429
2423
runtime library)
2430
- $(LI throwing the $(D AssertError) exception in the D runtime library)
2424
+ $(LI Throwing the $(D AssertError) exception in the D runtime library)
2425
+ )
2426
+ )
2427
+
2428
+ $(BEST_PRACTICE
2429
+ $(OL
2430
+ $(LI Do not have side effects in either $(I AssignExpression) that subsequent code
2431
+ depends on.)
2432
+ $(LI $(I AssertExpression)s are intended to detect bugs in the program.
2433
+ Do not use them for detecting input or environmental errors.)
2434
+ $(LI Do not attempt to resume normal execution after an $(I Assert Failure).)
2435
+ )
2436
+ )
2437
+
2438
+ $(H4 $(LNAME2 assert-ct, Compile-time Evaluation))
2439
+
2440
+ $(P If the first $(I AssignExpression) consists entirely of compile time constants,
2441
+ and evaluates to `false`, it is a special case - it
2442
+ signifies that subsequent statements are unreachable code.
2443
+ Compile Time Function Execution (CTFE) is not attempted.
2444
+ )
2445
+
2446
+ $(P The implementation may handle the case of the first $(I AssignExpression) evaluating to `false`
2447
+ at compile time differently - even when other `assert`s are ignored,
2448
+ it may still generate a $(D HLT) instruction or equivalent.
2431
2449
)
2432
- If the optional second $(I AssignExpression) is provided, the implementation may
2433
- evaluate it and print the resulting message upon assert failure:
2434
2450
2451
+ $(P See also: $(DDSUBLINK spec/version, static-assert, `static assert`).)
2452
+
2453
+ $(H4 $(LNAME2 assert-message, Assert Message))
2454
+
2455
+ $(P The second $(I AssignExpression), if present, must be implicitly
2456
+ convertible to type $(D const(char)[]).
2457
+ When present, the implementation may evaluate it and print the
2458
+ resulting message upon assert failure:
2459
+ )
2435
2460
----
2436
2461
void main()
2437
2462
{
@@ -2443,21 +2468,6 @@ $(GNAME AssertArguments):
2443
2468
2444
2469
$(CONSOLE core.exception.AssertError@test.d(3) an error message)
2445
2470
2446
- $(P The implementation may handle the case of the first $(I AssignExpression) evaluating at compile
2447
- time to false differently in that in release mode
2448
- it may simply generate a $(D HLT) instruction or equivalent.
2449
- )
2450
- )
2451
-
2452
- $(BEST_PRACTICE
2453
- $(OL
2454
- $(LI Do not have side effects in either $(I AssignExpression) that subsequent code
2455
- depends on.)
2456
- $(LI $(I AssertExpressions) are intended to detect bugs in the program, do
2457
- not use for detecting input or environmental errors.)
2458
- $(LI Do not attempt to resume normal execution after an $(I Assert Failure).)
2459
- )
2460
- )
2461
2471
2462
2472
$(H3 $(LNAME2 mixin_expressions, Mixin Expressions))
2463
2473
0 commit comments