File tree Expand file tree Collapse file tree 2 files changed +33
-2
lines changed Expand file tree Collapse file tree 2 files changed +33
-2
lines changed Original file line number Diff line number Diff line change @@ -255,8 +255,8 @@ $(H2 $(LNAME2 gnu-clang-extensions, Gnu and Clang Extensions))
255
255
$(P `__attribute__((noreturn))` marks a function as never returning.
256
256
`gcc` set this as an attribute of the function, it is
257
257
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:)
260
260
$(CCODE
261
261
attribute((noreturn)) int foo();
262
262
size_t x = sizeof(foo());
Original file line number Diff line number Diff line change @@ -558,6 +558,37 @@ $(H3 $(LNAME2 string, $(D string)))
558
558
559
559
$(P A $(DDSUBLINK spec/arrays, strings, $(I string) is a special case of an array.))
560
560
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
+
561
592
$(SPEC_SUBNAV_PREV_NEXT declaration, Declarations, property, Properties)
562
593
)
563
594
You can’t perform that action at this time.
0 commit comments