Skip to content

Commit c3cd7f0

Browse files
authored
Merge pull request #3149 from ntrel/noreturn
[spec] Document `noreturn` Signed-off-by: Dennis <dkorpel@users.noreply.github.com> Merged-on-behalf-of: Dennis <dkorpel@users.noreply.github.com>
2 parents 2fa467e + cbb44b0 commit c3cd7f0

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-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: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,37 @@ $(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+
571+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
572+
---
573+
noreturn abort(const(char)[] message);
574+
575+
int example(int i)
576+
{
577+
if (i < 0)
578+
{
579+
// abort does not return, so it doesn't need to produce an int
580+
int val = abort("less than zero");
581+
}
582+
// ternary expression's common type is still int
583+
return i != 0 ? 1024 / i : abort("calculation went awry.");
584+
}
585+
---
586+
)
587+
588+
$(P `noreturn` is defined as $(D typeof(*null)). This is because
589+
dereferencing a null literal halts execution.)
590+
591+
561592
$(SPEC_SUBNAV_PREV_NEXT declaration, Declarations, property, Properties)
562593
)
563594

0 commit comments

Comments
 (0)