Skip to content

Commit 3edbf73

Browse files
WalterBrightdlang-bot
authored andcommitted
ImportC: document __attribute((noreturn))
1 parent 0c422cb commit 3edbf73

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

spec/importc.dd

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,32 @@ $(H2 $(LNAME2 extensions, Extensions))
173173

174174
$(H2 $(LNAME2 gnu-clang-extensions, Gnu and Clang Extensions))
175175

176+
$(P `gcc` and `clang` are presumed to have the same behavior w.r.t. extensions,
177+
so `gcc` as used here refers to both.)
178+
179+
$(H3 $(LNAME2 noreturn, `__attribute__((noreturn))`))
180+
181+
$(P `__attribute__((noreturn))` marks a function as never returning.
182+
`gcc` set this as an attribute of the function, it is
183+
not part of the function's type. In D, a function that never returns
184+
has the return type `noreturn`. The difference can be seen with the
185+
code:)
186+
---
187+
attribute((noreturn)) int foo();
188+
size_t x = sizeof(foo());
189+
---
190+
$(P This code is accepted by `gcc`, but makes no sense for D. Hence,
191+
although it works in ImportC, it is not representable as D code,
192+
meaning one must use judgement in creating a .di file to interface
193+
with C `noreturn` functions.)
194+
195+
$(P Furthermore, the D compiler takes advantage of `noreturn` functions
196+
by issuing compile time errors for unreachable code. Such unreachable
197+
code, however, is valid C11, and the ImportC compiler will accept it.)
198+
199+
$(BEST_PRACTICE C code that uses the `noreturn` attribute should at the
200+
very least set the return type to `void`.)
201+
176202

177203
$(H2 $(LNAME2 visualc-extensions, Visual C Extensions))
178204

0 commit comments

Comments
 (0)