Skip to content

Commit 8b0bf72

Browse files
committed
[spec] Document noreturn
1 parent a47558e commit 8b0bf72

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

spec/importc.dd

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,8 +255,8 @@ $(H2 $(LNAME2 gnu-clang-extensions, Gnu and Clang Extensions))
255255
$(P `__attribute__((noreturn))` marks a function as never returning.
256256
`gcc` set this as an attribute of the function, it is
257257
not part of the function's type. In D, a function that never returns
258-
has the return type `noreturn`. The difference can be seen with the
259-
code:)
258+
has the return type $(GLINK2 type, noreturn). The difference can be
259+
seen with the code:)
260260
$(CCODE
261261
attribute((noreturn)) int foo();
262262
size_t x = sizeof(foo());

spec/type.dd

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,38 @@ $(H3 $(LNAME2 string, $(D string)))
558558

559559
$(P A $(DDSUBLINK spec/arrays, strings, $(I string) is a special case of an array.))
560560

561+
$(H3 $(LNAME2 noreturn, $(D noreturn)))
562+
563+
$(P `noreturn` is the $(LINK2 https://en.wikipedia.org/wiki/Bottom_type, bottom type)
564+
which can implicitly convert to any type, including `void`.
565+
A value of type `noreturn` will never be produced and the compiler can
566+
optimize such code accordingly.)
567+
568+
$(P A function that never returns has the return type `noreturn`. This can
569+
occur due to an infinite loop or always throwing an exception.
570+
This is particularly useful when inferring types.)
571+
572+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
573+
---
574+
noreturn abort(const(char)[] message);
575+
576+
int example(int i)
577+
{
578+
if (i < 0)
579+
{
580+
// abort does not return, so it doesn't need to produce an int
581+
int val = abort("less than zero");
582+
}
583+
// ternary expression's common type is still int
584+
return i != 0 ? 1024 / i : abort("calculation went awry.");
585+
}
586+
---
587+
)
588+
589+
$(P `noreturn` is defined as $(D typeof(*null)). This is because
590+
dereferencing a null literal halts execution.)
591+
592+
561593
$(SPEC_SUBNAV_PREV_NEXT declaration, Declarations, property, Properties)
562594
)
563595

0 commit comments

Comments
 (0)