Skip to content

Commit bcf4953

Browse files
ntreldlang-bot
authored andcommitted
[spec/expression] Improve assert(0) docs
Add example. Add rationale for halt.
1 parent 5c78d55 commit bcf4953

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

spec/expression.dd

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2744,11 +2744,30 @@ $(H4 $(LNAME2 assert-ct, Compile-time Evaluation))
27442744
Compile Time Function Execution (CTFE) is not attempted.
27452745
)
27462746

2747+
$(P This allows the compiler to suppress an error when there is a
2748+
missing return statement:)
2749+
2750+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
2751+
---
2752+
int f(int x)
2753+
{
2754+
if (x > 0)
2755+
{
2756+
return 5 / x;
2757+
}
2758+
assert(0);
2759+
// no need to use a dummy return statement here
2760+
}
2761+
---
2762+
)
2763+
27472764
$(P The implementation may handle the case of the first $(I AssignExpression) evaluating to `false`
27482765
at compile time differently - even when other `assert`s are ignored,
27492766
it may still generate a $(D HLT) instruction or equivalent.
27502767
)
27512768

2769+
$(RATIONALE Halting the program prevents undefined behaviour from occurring.)
2770+
27522771
$(P See also: $(DDSUBLINK spec/version, static-assert, `static assert`).)
27532772

27542773
$(H4 $(LNAME2 assert-message, Assert Message))
@@ -2765,7 +2784,7 @@ $(H4 $(LNAME2 assert-message, Assert Message))
27652784
}
27662785
----
27672786

2768-
$(P When compiled and run, it will produce the message:)
2787+
$(P When compiled and run, typically it will produce the message:)
27692788

27702789
$(CONSOLE [email protected](3) an error message)
27712790

spec/function.dd

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,8 @@ $(H2 $(LNAME2 function-return-values, Function Return Values))
400400
unless:)
401401
$(UL
402402
$(LI the function executes an infinite loop)
403-
$(LI the function executes an `assert(0)` statement)
403+
$(LI the function executes an $(DDSUBLINK spec/expression, assert-ct,
404+
`assert(0)` statement))
404405
$(LI the function evaluates an expression of type
405406
$(DDSUBLINK spec/type, noreturn, `noreturn`))
406407
$(LI the function contains inline assembler code)

0 commit comments

Comments
 (0)