Jump to page: 1 2 3
Thread overview
@noreturn property
Oct 21, 2010
Iain Buclaw
Oct 21, 2010
Bernard Helyer
Oct 21, 2010
bearophile
Oct 21, 2010
Iain Buclaw
Oct 21, 2010
Justin Johansson
Oct 21, 2010
Leandro Lucarella
Oct 21, 2010
Daniel Gibson
Oct 21, 2010
Iain Buclaw
Oct 21, 2010
Leandro Lucarella
Oct 21, 2010
Iain Buclaw
Oct 21, 2010
Ezneh
Oct 21, 2010
bearophile
Oct 21, 2010
Rainer Deyke
Oct 21, 2010
Iain Buclaw
Oct 22, 2010
Rainer Deyke
Oct 22, 2010
Iain Buclaw
Oct 22, 2010
Stewart Gordon
Oct 25, 2010
Stewart Gordon
Oct 21, 2010
Nick Sabalausky
Oct 21, 2010
bearophile
Oct 21, 2010
Leandro Lucarella
Oct 22, 2010
Iain Buclaw
Oct 22, 2010
Stewart Gordon
October 21, 2010
A few standard library functions, such as 'abort' and 'exit', cannot return. However there is no way in DMD to let the compiler know about this. Currently in D2, you must either have a 'return' or 'assert(0)' statement at the end of a function body. It would be nice however if you can give hints to the compiler to let it know that a function is never going to return.

Example:

@noreturn void fatal()
{
    print("Error");
    exit(1);
}

The 'noreturn' keyword would tell the compiler that 'fatal' cannot return, and can then optimise without regard to what would happen if 'fatal' ever did return. This should also allow fatal to be used instead of a return or assert statement.

Example:

int mycheck(int x)
{
    if (x > 1)
        return OK;
    fatal();
}


Thoughts?
October 21, 2010
>
> Thoughts?

I really, really like the idea.
October 21, 2010
Iain Buclaw:

> @noreturn void fatal()
> {
>     print("Error");
>     exit(1);
> }

See also the same feature in GNU C: http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html#index-g_t_0040code_007bnoreturn_007d-function-attribute-2455

Bye,
bearophile
October 21, 2010
On Thu, 21 Oct 2010 11:54:26 +0000, Iain Buclaw wrote:

> A few standard library functions, such as 'abort' and 'exit', cannot return. However there is no way in DMD to let the compiler know about this. Currently in D2, you must either have a 'return' or 'assert(0)' statement at the end of a function body. It would be nice however if you can give hints to the compiler to let it know that a function is never going to return.
> 
> Example:
> 
> @noreturn void fatal()
> {
>     print("Error");
>     exit(1);
> }
> 
> The 'noreturn' keyword would tell the compiler that 'fatal' cannot return, and can then optimise without regard to what would happen if 'fatal' ever did return. This should also allow fatal to be used instead of a return or assert statement.
> 
> Example:
> 
> int mycheck(int x)
> {
>     if (x > 1)
>         return OK;
>     fatal();
> }
> 
> 
> Thoughts?


It would be useful for std.exception.enforce(), as you could end a function with enforce(false).

-Lars
October 21, 2010
On Thu, 21 Oct 2010 08:52:35 -0400, Lars T. Kyllingstad <public@kyllingen.nospamnet> wrote:

> On Thu, 21 Oct 2010 11:54:26 +0000, Iain Buclaw wrote:
>
>> A few standard library functions, such as 'abort' and 'exit', cannot
>> return. However there is no way in DMD to let the compiler know about
>> this. Currently in D2, you must either have a 'return' or 'assert(0)'
>> statement at the end of a function body. It would be nice however if you
>> can give hints to the compiler to let it know that a function is never
>> going to return.
>>
>> Example:
>>
>> @noreturn void fatal()
>> {
>>     print("Error");
>>     exit(1);
>> }
>>
>> The 'noreturn' keyword would tell the compiler that 'fatal' cannot
>> return, and can then optimise without regard to what would happen if
>> 'fatal' ever did return. This should also allow fatal to be used instead
>> of a return or assert statement.
>>
>> Example:
>>
>> int mycheck(int x)
>> {
>>     if (x > 1)
>>         return OK;
>>     fatal();
>> }
>>
>>
>> Thoughts?
>
>
> It would be useful for std.exception.enforce(), as you could end a
> function with enforce(false).

1. It doesn't work that way.  The function has to *never* return, no matter what the arguments.
2. assert(false) already does this.

-Steve
October 21, 2010
On Thu, 21 Oct 2010 09:14:01 -0400, Steven Schveighoffer wrote:

> On Thu, 21 Oct 2010 08:52:35 -0400, Lars T. Kyllingstad <public@kyllingen.nospamnet> wrote:
> 
>> On Thu, 21 Oct 2010 11:54:26 +0000, Iain Buclaw wrote:
>>
>>> A few standard library functions, such as 'abort' and 'exit', cannot return. However there is no way in DMD to let the compiler know about this. Currently in D2, you must either have a 'return' or 'assert(0)' statement at the end of a function body. It would be nice however if you can give hints to the compiler to let it know that a function is never going to return.
>>>
>>> Example:
>>>
>>> @noreturn void fatal()
>>> {
>>>     print("Error");
>>>     exit(1);
>>> }
>>>
>>> The 'noreturn' keyword would tell the compiler that 'fatal' cannot return, and can then optimise without regard to what would happen if 'fatal' ever did return. This should also allow fatal to be used instead of a return or assert statement.
>>>
>>> Example:
>>>
>>> int mycheck(int x)
>>> {
>>>     if (x > 1)
>>>         return OK;
>>>     fatal();
>>> }
>>>
>>>
>>> Thoughts?
>>
>>
>> It would be useful for std.exception.enforce(), as you could end a
>> function with enforce(false).
> 
> 1. It doesn't work that way.  The function has to *never* return, no matter what the arguments.

Ah, of course. :)


> 2. assert(false) already does this.

Except that assert(false) throws an Error (or issues a HLT in release mode), while enforce(false) throws an Exception.

But you are absolutely right, it won't work anyway, so I guess I'll just have to suck it up and write 'throw new Exception'... :)

-Lars
October 21, 2010
== Quote from Lars T. Kyllingstad (public@kyllingen.NOSPAMnet)'s article
> On Thu, 21 Oct 2010 11:54:26 +0000, Iain Buclaw wrote:
> > A few standard library functions, such as 'abort' and 'exit', cannot return. However there is no way in DMD to let the compiler know about this. Currently in D2, you must either have a 'return' or 'assert(0)' statement at the end of a function body. It would be nice however if you can give hints to the compiler to let it know that a function is never going to return.
> >
> > Example:
> >
> > @noreturn void fatal()
> > {
> >     print("Error");
> >     exit(1);
> > }
> >
> > The 'noreturn' keyword would tell the compiler that 'fatal' cannot return, and can then optimise without regard to what would happen if 'fatal' ever did return. This should also allow fatal to be used instead of a return or assert statement.
> >
> > Example:
> >
> > int mycheck(int x)
> > {
> >     if (x > 1)
> >         return OK;
> >     fatal();
> > }
> >
> >
> > Thoughts?
> It would be useful for std.exception.enforce(), as you could end a
> function with enforce(false).
> -Lars

Or with any of the helper functions in core.exception, for that matter; such as
onOutOfMemoryError().

Regards
Iain
October 21, 2010
On 21/10/2010 10:54 PM, Iain Buclaw wrote:
> A few standard library functions, such as 'abort' and 'exit', cannot return.
> However there is no way in DMD to let the compiler know about this.
> Currently in D2, you must either have a 'return' or 'assert(0)' statement at
> the end of a function body. It would be nice however if you can give hints to
> the compiler to let it know that a function is never going to return.
>
> Example:
>
> @noreturn void fatal()
> {
>      print("Error");
>      exit(1);
> }
>
> The 'noreturn' keyword would tell the compiler that 'fatal' cannot return, and
> can then optimise without regard to what would happen if 'fatal' ever did
> return. This should also allow fatal to be used instead of a return or assert
> statement.

Yes, well, and while others may say that I must be drunk to
suggest so, the problem lies in the D type system, albeit,
the lack thereof.

Further, I know that my previous "canoe" joke did not go down
very well or was not understood by the wider D community.

Thankfully questions posed by your post give credence to
my prior "joke" about the D type system lacking a formal
substance (i.e. being close to water).

There is a good answer awaiting but it is not a hack such
as @noreturn.

Cheers
Justin Johansson
October 21, 2010
Iain Buclaw, el 21 de octubre a las 11:54 me escribiste:
> A few standard library functions, such as 'abort' and 'exit', cannot return. However there is no way in DMD to let the compiler know about this. Currently in D2, you must either have a 'return' or 'assert(0)' statement at the end of a function body. It would be nice however if you can give hints to the compiler to let it know that a function is never going to return.
> 
> Example:
> 
> @noreturn void fatal()
> {
>     print("Error");
>     exit(1);
> }
> 
> The 'noreturn' keyword would tell the compiler that 'fatal' cannot return, and can then optimise without regard to what would happen if 'fatal' ever did return. This should also allow fatal to be used instead of a return or assert statement.
> 
> Example:
> 
> int mycheck(int x)
> {
>     if (x > 1)
>         return OK;
>     fatal();
> }
> 
> 
> Thoughts?

You want to include in the language what you can do (or at least could)
do in GDC using:

pragma(GNU_attribute, noreturn)) void fatal()
{
     print("Error");
     exit(1);
}

?

-- 
Leandro Lucarella (AKA luca)                     http://llucax.com.ar/
----------------------------------------------------------------------
GPG Key: 5F5A8D05 (F8CD F9A7 BF00 5431 4145  104C 949E BFB6 5F5A 8D05)
----------------------------------------------------------------------
The world's best known word is "okay"
The second most well-known word is "Coca-Cola"
October 21, 2010
Iain Buclaw Wrote:

> 
> 
> Thoughts?

This can be a great idea.

In the same way, I saw something that can be good too about returned values in Nimrod :

http://force7.de/nimrod/tut1.html#discard-statement

Hope the explanation is sufficient. This way, the programmer knows when he "throws away" a returned value.


I think that could be great too.
« First   ‹ Prev
1 2 3