On 6/17/18 11:58 PM, Neia Neutuladh wrote:
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
}

Yep, it's a good point. But also not the only way to do this. If you are returning void, just a goto would work:

void foo()
{
   if(a) { goto L1; }
   if(b) { goto L1; }
   throw new Exception;
L1:
   writeln("success1");
}

If you are returning a value, then you can establish a local variable with the return value, and use a goto that way. It's already doing this with "did it succeed", so adding another local variable is pretty trivial.

Bottom line is, the compiler understands flow control and can insert structures like this without a huge impact.

I also think it's possible for the compiler to detect that a try/catch clause is trivially omitted if we do it in the right way.


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.

These are all pretty easy to deal with. After all, they are essentially glorified gotos.


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.

I think the last point here is exactly why it was done this way -- the original design of the compiler was to expect there were always exception handling, so why not use it?

-Steve

Reply via email to