www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - noreturn?

reply =?ISO-8859-1?Q?Alex_R=F8nne_Petersen?= <xtzgzorex gmail.com> writes:
Hi,

If memory serves me right, there were some proposals in the past to add 
a  noreturn attribute to the language. Where did this go?

In writing unwinding mechanisms for my VM, I find myself actually 
needing some sort of  noreturn function attribute that would tell the 
compiler that the function does not return, rather than having to insert 
an assert(false); statement.

-- 
- Alex
May 13 2012
next sibling parent Iain Buclaw <ibuclaw ubuntu.com> writes:
On 13 May 2012 17:13, Alex R=F8nne Petersen <xtzgzorex gmail.com> wrote:
 Hi,

 If memory serves me right, there were some proposals in the past to add a
  noreturn attribute to the language. Where did this go?

 In writing unwinding mechanisms for my VM, I find myself actually needing
 some sort of  noreturn function attribute that would tell the compiler th=

 the function does not return, rather than having to insert an assert(fals=

 statement.

I proposed it - and it didn't go anywhere. :^) Only way to hint it at the moment would be to use gdc and pragma(attribute, noreturn) - you still need the assert(0) at the end though to stop the frontend producing a compile time error. --=20 Iain Buclaw *(p < e ? p++ : p) =3D (c & 0x0f) + '0';
May 13 2012
prev sibling next sibling parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Alex Rønne Petersen:

 In writing unwinding mechanisms for my VM, I find myself 
 actually needing some sort of  noreturn function attribute that 
 would tell the compiler that the function does not return,

Maybe Walter will listen you more if you show an example of where/why you would use noreturn. Bye, bearophile
May 14 2012
parent reply =?UTF-8?B?QWxleCBSw7hubmUgUGV0ZXJzZW4=?= <xtzgzorex gmail.com> writes:
On 14-05-2012 13:53, bearophile wrote:
 Alex Rønne Petersen:

 In writing unwinding mechanisms for my VM, I find myself actually
 needing some sort of  noreturn function attribute that would tell the
 compiler that the function does not return,

Maybe Walter will listen you more if you show an example of where/why you would use noreturn. Bye, bearophile

As a very simple example, consider an intrinsic in a virtual machine: extern (C) int do_some_work(VMContext ctx, int value) { if (value != 42) ctx.engine.raiseException("Bad value!"); /* no can do; assert(false) needed here */ return value; } Note that raiseException() unwinds the native stack. It is noreturn. -- - Alex
May 14 2012
parent =?UTF-8?B?QWxleCBSw7hubmUgUGV0ZXJzZW4=?= <xtzgzorex gmail.com> writes:
On 14-05-2012 14:13, Alex Rønne Petersen wrote:
 On 14-05-2012 13:53, bearophile wrote:
 Alex Rønne Petersen:

 In writing unwinding mechanisms for my VM, I find myself actually
 needing some sort of  noreturn function attribute that would tell the
 compiler that the function does not return,

Maybe Walter will listen you more if you show an example of where/why you would use noreturn. Bye, bearophile

As a very simple example, consider an intrinsic in a virtual machine: extern (C) int do_some_work(VMContext ctx, int value) { if (value != 42) ctx.engine.raiseException("Bad value!"); /* no can do; assert(false) needed here */ return value; } Note that raiseException() unwinds the native stack. It is noreturn.

Even more trivial examples... longjmp and abort. ;) -- - Alex
May 14 2012
prev sibling parent reply Timon Gehr <timon.gehr gmx.ch> writes:
On 05/13/2012 06:13 PM, Alex Rnne Petersen wrote:
 Hi,

 If memory serves me right, there were some proposals in the past to add
 a  noreturn attribute to the language. Where did this go?

 In writing unwinding mechanisms for my VM, I find myself actually
 needing some sort of  noreturn function attribute that would tell the
 compiler that the function does not return, rather than having to insert
 an assert(false); statement.

What about making the compiler detect this pattern instead of adding an attribute? I think it expresses exactly what you want to express, and it does not require additional syntax. void foo()out{ assert(false); }body{ ... }
May 14 2012
next sibling parent reply =?ISO-8859-1?Q?Alex_R=F8nne_Petersen?= <xtzgzorex gmail.com> writes:
On 14-05-2012 23:59, Timon Gehr wrote:
 On 05/13/2012 06:13 PM, Alex Rnne Petersen wrote:
 Hi,

 If memory serves me right, there were some proposals in the past to add
 a  noreturn attribute to the language. Where did this go?

 In writing unwinding mechanisms for my VM, I find myself actually
 needing some sort of  noreturn function attribute that would tell the
 compiler that the function does not return, rather than having to insert
 an assert(false); statement.

What about making the compiler detect this pattern instead of adding an attribute? I think it expresses exactly what you want to express, and it does not require additional syntax. void foo()out{ assert(false); }body{ ... }

While that might be an easier way to do it, I'm not sure if that would have unintended side-effects (admittedly, I can't think of any upfront). -- - Alex
May 14 2012
parent Timon Gehr <timon.gehr gmx.ch> writes:
On 05/15/2012 12:14 AM, Alex Rnne Petersen wrote:
 On 14-05-2012 23:59, Timon Gehr wrote:
 On 05/13/2012 06:13 PM, Alex Rnne Petersen wrote:
 Hi,

 If memory serves me right, there were some proposals in the past to add
 a  noreturn attribute to the language. Where did this go?

 In writing unwinding mechanisms for my VM, I find myself actually
 needing some sort of  noreturn function attribute that would tell the
 compiler that the function does not return, rather than having to insert
 an assert(false); statement.

What about making the compiler detect this pattern instead of adding an attribute? I think it expresses exactly what you want to express, and it does not require additional syntax. void foo()out{ assert(false); }body{ ... }

While that might be an easier way to do it, I'm not sure if that would have unintended side-effects (admittedly, I can't think of any upfront).

Side effects: - You wouldn't have to add the (completely redundant) assert(false) to the call site anymore. - Ideally, the compiler will then optimize the code based on the pattern, namely, assertions that are unchecked in release mode. That may or may not be desirable. => Fix: Add 'hlt' to end of each method that has assert(false) out in release mode, and ban return statements from its body. I think it could go either way, but having both noreturn and out{ assert(false); } seems somewhat redundant to me.
May 14 2012
prev sibling next sibling parent Artur Skawina <art.08.09 gmail.com> writes:
On 05/14/12 23:59, Timon Gehr wrote:
 On 05/13/2012 06:13 PM, Alex Rønne Petersen wrote:
 Hi,

 If memory serves me right, there were some proposals in the past to add
 a  noreturn attribute to the language. Where did this go?

 In writing unwinding mechanisms for my VM, I find myself actually
 needing some sort of  noreturn function attribute that would tell the
 compiler that the function does not return, rather than having to insert
 an assert(false); statement.

What about making the compiler detect this pattern instead of adding an attribute? I think it expresses exactly what you want to express, and it does not require additional syntax. void foo()out{ assert(false); }body{ ... }

extern noreturn void foo(); artur
May 14 2012
prev sibling next sibling parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Mon, 14 May 2012 17:59:19 -0400, Timon Gehr <timon.gehr gmx.ch> wrote=
:

 On 05/13/2012 06:13 PM, Alex R=C3=B8nne Petersen wrote:
 Hi,

 If memory serves me right, there were some proposals in the past to a=


 a  noreturn attribute to the language. Where did this go?

 In writing unwinding mechanisms for my VM, I find myself actually
 needing some sort of  noreturn function attribute that would tell the=


 compiler that the function does not return, rather than having to ins=


 an assert(false); statement.

What about making the compiler detect this pattern instead of adding a=

 attribute? I think it expresses exactly what you want to express, and =

 does not require additional syntax.

 void foo()out{ assert(false); }body{ ... }

Is the out contract required in a method signature? That is, is it vali= d = to omit the out contract in the .di file? If it's not required, then this doesn't work, since the compiler may not= = have access to that information. Of course, we could just say "don't make such functions opaque"... -Steve
May 16 2012
parent reply Timon Gehr <timon.gehr gmx.ch> writes:
On 05/16/2012 02:27 PM, Steven Schveighoffer wrote:
 On Mon, 14 May 2012 17:59:19 -0400, Timon Gehr <timon.gehr gmx.ch> wrote:

 On 05/13/2012 06:13 PM, Alex Rønne Petersen wrote:
 Hi,

 If memory serves me right, there were some proposals in the past to add
 a  noreturn attribute to the language. Where did this go?

 In writing unwinding mechanisms for my VM, I find myself actually
 needing some sort of  noreturn function attribute that would tell the
 compiler that the function does not return, rather than having to insert
 an assert(false); statement.

What about making the compiler detect this pattern instead of adding an attribute? I think it expresses exactly what you want to express, and it does not require additional syntax. void foo()out{ assert(false); }body{ ... }

Is the out contract required in a method signature? That is, is it valid to omit the out contract in the .di file?

Contracts are supposed to be part of the method signature. Due to a bug, they are not for DMD.
 If it's not required, then this doesn't work, since the compiler may not
 have access to that information.

 Of course, we could just say "don't make such functions opaque"...

 -Steve

May 16 2012
parent Timon Gehr <timon.gehr gmx.ch> writes:
On 05/16/2012 05:29 PM, Steven Schveighoffer wrote:
 On Wed, 16 May 2012 10:47:33 -0400, Timon Gehr <timon.gehr gmx.ch> wrote:

 On 05/16/2012 02:27 PM, Steven Schveighoffer wrote:

 Is the out contract required in a method signature? That is, is it valid
 to omit the out contract in the .di file?

Contracts are supposed to be part of the method signature. Due to a bug, they are not for DMD.

OK, then I would support this as a method of specifying noreturn BTW, is this bug reported? -Steve

Yes. http://d.puremagic.com/issues/show_bug.cgi?id=6549 Stewart has turned it into an enhancement, but it is pretty clear that it is a bug in both the compiler and the dlang.org documentation. At least Andrei has confirmed that it should be fixed, I don't remember whether or not Walter contributed to that discussion.
May 16 2012
prev sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Wed, 16 May 2012 10:47:33 -0400, Timon Gehr <timon.gehr gmx.ch> wrote:

 On 05/16/2012 02:27 PM, Steven Schveighoffer wrote:

 Is the out contract required in a method signature? That is, is it valid
 to omit the out contract in the .di file?

Contracts are supposed to be part of the method signature. Due to a bug, they are not for DMD.

OK, then I would support this as a method of specifying noreturn BTW, is this bug reported? -Steve
May 16 2012