Skip to content

Commit d5ecc34

Browse files
ntreldlang-bot
authored andcommitted
[spec] Improve noreturn docs
The type of `assert(0)` is noreturn. `noreturn.init` lowers to `assert(0)`. A function returning type `noreturn` is covariant with a function returning any other type. See also `throw`.
1 parent d848617 commit d5ecc34

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

spec/expression.dd

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2854,7 +2854,8 @@ $(H4 $(LNAME2 assert-ct, Compile-time Evaluation))
28542854
$(P If the first $(I AssignExpression) consists entirely of compile time constants,
28552855
and evaluates to `false`, it is a special case - it
28562856
signifies that subsequent statements are unreachable code.
2857-
Compile Time Function Execution (CTFE) is not attempted.
2857+
Compile Time Function Execution (CTFE) calls are not attempted for the evaluation.
2858+
Such an $(GLINK AssertExpression) has type $(DDSUBLINK spec/type, noreturn, `noreturn`).
28582859
)
28592860

28602861
$(P This allows the compiler to suppress an error when there is a

spec/type.dd

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -903,13 +903,18 @@ $(H3 $(LNAME2 noreturn, $(D noreturn)))
903903
A value of type `noreturn` will never be produced and the compiler can
904904
optimize such code accordingly.)
905905

906+
$(P `noreturn.init` lowers to $(DDSUBLINK spec/expression, assert-ct, `assert(0)`).)
907+
906908
$(P A function that $(DDSUBLINK spec/function, function-return-values, never returns)
907909
has the return type `noreturn`. This can
908-
occur due to an infinite loop or always throwing an exception.)
910+
occur due to e.g. an infinite loop or always throwing an exception.
911+
A function returning type `noreturn` is covariant with a function returning any other type.)
909912

910913
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
911914
---
912-
noreturn abort(const(char)[] message);
915+
noreturn abort(string message);
916+
917+
int function(string) fp = &abort; // OK
913918

914919
int example(int i)
915920
{
@@ -925,7 +930,9 @@ int example(int i)
925930
)
926931

927932
$(P `noreturn` is defined as $(D typeof(*null)). This is because
928-
dereferencing a null literal halts execution.)
933+
dereferencing a null literal (typically) halts execution.)
934+
935+
$(P See also: $(GLINK2 expression, ThrowExpression))
929936

930937

931938
$(SPEC_SUBNAV_PREV_NEXT declaration, Declarations, property, Properties)

0 commit comments

Comments
 (0)