Skip to content

Commit b138cc5

Browse files
authored
[AssertExpression] Improve docs (#3572)
* [AssertExpression] Improve docs Add contract and other links. Move compile-time evaluated condition paragraph down to its own subheading (avoid distracting from the runtime evaluation docs which are quite complicated). Add short example. Change link to precondition (contracts spec is a stub). Move second argument sentence down to its own subheading. Reword paragraph about compiler switches. Remove 'continuing execution' list item - that seems to be covered by the first sentence (whether the condition is evaluated or not). For CT evaluation, avoid mentioning 'release mode' which was undefined. * Add closing `)` for IMPLEMENTATION_DEFINED
1 parent d6933ba commit b138cc5

File tree

2 files changed

+52
-39
lines changed

2 files changed

+52
-39
lines changed

spec/contracts.dd

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ $(H2 $(LNAME2 assert_contracts, Assert Contract))
3030

3131
$(H2 $(LNAME2 pre_post_contracts, Pre and Post Contracts))
3232

33-
$(P See $(LINK2 function, contracts). )
33+
$(P See:)
34+
* $(DDSUBLINK spec/function, preconditions, $(D in) contracts).
35+
* $(DDSUBLINK spec/function, postconditions, $(D out) contracts).
36+
3437

3538
$(H2 $(LNAME2 Invariants, Invariants))
3639

spec/expression.dd

Lines changed: 48 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2383,31 +2383,26 @@ $(GNAME AssertArguments):
23832383
$(GLINK AssignExpression) $(D ,) $(GLINK AssignExpression) $(D ,)$(OPT)
23842384
)
23852385

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)
23872387
has occurred and the program enters an $(I Invalid State).
23882388
)
23892389

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+
---
23952394

23962395
$(P $(I AssertExpression) has different semantics if it is in a
23972396
$(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).
24032398
)
24042399

24052400
$(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.
24072402
)
24082403

24092404
$(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.
24112406
)
24122407

24132408
$(P The type of an $(I AssertExpression) is $(D void).
@@ -2417,21 +2412,51 @@ $(GNAME AssertArguments):
24172412
of the program is undefined.)
24182413

24192414
$(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,
24212416
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:
24242419
$(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
24292423
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.
24312449
)
2432-
If the optional second $(I AssignExpression) is provided, the implementation may
2433-
evaluate it and print the resulting message upon assert failure:
24342450

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+
)
24352460
----
24362461
void main()
24372462
{
@@ -2443,21 +2468,6 @@ $(GNAME AssertArguments):
24432468

24442469
$(CONSOLE core.exception.AssertError@test.d(3) an error message)
24452470

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-
)
24612471

24622472
$(H3 $(LNAME2 mixin_expressions, Mixin Expressions))
24632473

0 commit comments

Comments
 (0)