scope(success) lowered to try-catch ?

Neia Neutuladh neia at ikeran.org
Mon Jun 18 03:58:47 UTC 2018


On Sunday, 17 June 2018 at 10:58:29 UTC, Cauterite wrote:
> Is there a reason scope(success) needs to set up for exception 
> handling?
> Or is this a bug / potential enhancement ?

If you had no exception handling in place, you'd need to 
duplicate code in the output. For instance:

void foo()
{
   scope(success) writeln("success!");
   if (a) return;
   if (b) return;
   throw new Exception;
}

This would have to be lowered to:

void foo()
{
   if (a) { writeln("success!"); return; }
   if (b) { writeln("success!"); return; }
   throw new Exception;
   writeln("success!");  // maybe omitted with flow analysis
}

Now imagine there were 20 places you return from the function 
early. Now imagine this is in a loop body, where you can leave it 
via goto, break, continue, return, or end-of-block. And wrapped 
in several if statements.

You generate smaller code with the exception handling system. The 
compiler only has to pay attention to scope guards in the code 
that handles it directly, instead of at every flow control 
statement. Add to that the fact that -betterC is pretty recent 
and scope guards are more than ten years old, and you get this 
hole in the compiler.


More information about the Digitalmars-d-learn mailing list