www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - nogc exceptions?

reply Manu <turkeyman gmail.com> writes:
What's the story with  nogc exceptions?
There was DIP1008 or whatever, I didn't follow it because I never use
exceptions... but I kinda need it.

IIRC, the idea was the throw statement transfer ownership of the
exception to the runtime, and then use a `scope` catch block, such
that the runtime can be sure it owns the only reference at the end of
the catch block and it can free the exception rather than transfer it
to the GC?

How did it fail?
May 18 2019
parent reply Nicholas Wilson <iamthewilsonator hotmail.com> writes:
On Sunday, 19 May 2019 at 04:24:39 UTC, Manu wrote:
 What's the story with  nogc exceptions?
 There was DIP1008 or whatever, I didn't follow it because I 
 never use
 exceptions... but I kinda need it.
It used to not work, but it does now.
 IIRC, the idea was the throw statement
Specifically it works with `throw new [My]Exception(args);` and only that form (since otherwise you could leak it).
 transfer ownership of the exception to the runtime, and then 
 use a `scope` catch block, such that the runtime can be sure it 
 owns the only reference
Well try throw new Exception("foo"); catch(scope Exception ex) {} fails with Error: basic type expected, not scope Error: found scope when expecting ) Error: semicolon expected, not ) Error: found ) instead of statement Error: unrecognized declaration but I think that was done for compatibility with other code that could store the exception. But regardless try throw new Exception("foo"); catch(Exception ex) {} will allocate a reference counted exception with malloc(?) (you can probably edit druntime to make it use some other scheme) which will deallocate at the end of the scope.
 at the end of the catch block and it can free the exception 
 rather than transfer it to the GC?
 How did it fail?
It used to not actually do the thing it was supposed to do (as in it would still call the GC). It was fixed.
May 18 2019
parent reply Manu <turkeyman gmail.com> writes:
On Sat, May 18, 2019 at 9:55 PM Nicholas Wilson via Digitalmars-d
<digitalmars-d puremagic.com> wrote:
 On Sunday, 19 May 2019 at 04:24:39 UTC, Manu wrote:
 What's the story with  nogc exceptions?
 There was DIP1008 or whatever, I didn't follow it because I
 never use
 exceptions... but I kinda need it.
It used to not work, but it does now.
Okay...How do I do it? I just get: throw new Exception("ex"); > error : cannot use `new` in ` nogc` function
May 18 2019
parent reply Nicholas Wilson <iamthewilsonator hotmail.com> writes:
On Sunday, 19 May 2019 at 05:05:32 UTC, Manu wrote:
 Okay...How do I do it? I just get:
   throw new Exception("ex");
   > error : cannot use `new` in ` nogc` function
pass -preview=dip1008 to the compiler
May 18 2019
parent reply Manu <turkeyman gmail.com> writes:
On Sat, May 18, 2019 at 10:20 PM Nicholas Wilson via Digitalmars-d
<digitalmars-d puremagic.com> wrote:
 On Sunday, 19 May 2019 at 05:05:32 UTC, Manu wrote:
 Okay...How do I do it? I just get:
   throw new Exception("ex");
   > error : cannot use `new` in ` nogc` function
pass -preview=dip1008 to the compiler
Oh right. Is this a breaking change or something?
May 18 2019
parent reply Petar Kirov [ZombineDev] <petar.p.kirov gmail.com> writes:
On Sunday, 19 May 2019 at 05:59:11 UTC, Manu wrote:
 On Sat, May 18, 2019 at 10:20 PM Nicholas Wilson via 
 Digitalmars-d <digitalmars-d puremagic.com> wrote:
 On Sunday, 19 May 2019 at 05:05:32 UTC, Manu wrote:
 Okay...How do I do it? I just get:
   throw new Exception("ex");
   > error : cannot use `new` in ` nogc` function
pass -preview=dip1008 to the compiler
Oh right. Is this a breaking change or something?
Yes, at minimum it changes the type checking of the `throw new <Ex>` expression (e.g. it's no longer not nogc). It also makes such exceptions not garbage collected, but reference counted, so the user should not keep an exta reference around as the compiler and the runtime can't track that. But most of all, such changes needs to be tested with a larger set of third-party libraries before we can consider tuning them on by default. Some projects have already started doing that, but we're still in the early stages.
May 18 2019
parent Seb <seb wilzba.ch> writes:
On Sunday, 19 May 2019 at 06:09:41 UTC, Petar Kirov [ZombineDev] 
wrote:
 On Sunday, 19 May 2019 at 05:59:11 UTC, Manu wrote:
 On Sat, May 18, 2019 at 10:20 PM Nicholas Wilson via 
 Digitalmars-d <digitalmars-d puremagic.com> wrote:
 On Sunday, 19 May 2019 at 05:05:32 UTC, Manu wrote:
   > [...]
pass -preview=dip1008 to the compiler
Oh right. Is this a breaking change or something?
Yes, at minimum it changes the type checking of the `throw new <Ex>` expression (e.g. it's no longer not nogc). It also makes such exceptions not garbage collected, but reference counted, so the user should not keep an exta reference around as the compiler and the runtime can't track that. But most of all, such changes needs to be tested with a larger set of third-party libraries before we can consider tuning them on by default. Some projects have already started doing that, but we're still in the early stages.
Well as long as druntime and Phobos don't compile with it, it's unlikely that third-party projects will pick it up. See e.g. https://github.com/dlang/dmd/pull/8508
May 19 2019