On Saturday, 8 July 2017 at 10:15:39 UTC, Walter Bright wrote:
C compilers (and by extension C++ compilers) usually have an extension which allows a function to be marked as one that never returns. The point of this is it enables improved data flow analysis and better code being generated.

Noreturn functions crop up in things like assert's and enforce's. DMD internally hardcodes a few functions it knows about that are noreturn, and the DMD optimizer and codegen take advantage of it.

But when people write their own assert's and enforce's, this falls apart. While the programs will still work, they won't be as efficient as they could be.

Having an @noreturn attribute will take care of that:

   @noreturn void ThisFunctionExits();

Yes, it's another builtin attribute and attributes are arguably a failure in language design.

Has anyone a better idea? Does anyone want to write a DIP for this?

Example:

DMC uses a pragma to do it:

    void ThisFunctionExits();
    #pragma noreturn(ThisFunctionExits);

GCC uses an attribute:

    void ThisFunctionExits() __attribute__ ((__noreturn__));

VC uses:

    __declspec(noreturn) void ThisFunctionExits();

Some questions...

What kinds of control flow does this apply to? My guess is that you consider a function to be "no-return" so long as it never returns control directly back to the caller. The examples I can think of would be functions that prevent returning to the caller by calling exit, asserting, throwing or even just executing an infinite loop (expecting the program to exit via signal or some other means). Is this right?

Another question I had was would the compiler provide control flow checking to make sure that the function does not return?

Also, I can imagine some optimizations that can be done with this information, would you mind sharing the optimization(s) you had in mind?

Reply via email to