File tree Expand file tree Collapse file tree 2 files changed +34
-2
lines changed Expand file tree Collapse file tree 2 files changed +34
-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,38 @@ $(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
+ 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
+
561
593
$(SPEC_SUBNAV_PREV_NEXT declaration, Declarations, property, Properties)
562
594
)
563
595
You can’t perform that action at this time.
0 commit comments