Assertions getting corrupted

Shachar Shemesh shachar at weka.io
Thu Oct 26 06:04:56 UTC 2017


I'm going to hate myself for suggesting this, but here goes.

There's a fundamental problem with scope(exit) and scope(failure). 
Consider the following code:

{
   a = allocate_something();
   scope(exit) a.cleanup();

   ...

   assert(a.nothingHorribleWentWrong);
}

Ideally, if that assert fails, you'd want the core dump and backtrace 
from the assertion point. That's the earliest point in which the problem 
is visible.

Except, all too often, what will happen is that the assert will throw an 
AssertionError. The scope(exit) will run, and then a.cleanup will 
segfault because, well, something horrible *did* go wrong. This makes it 
much more difficult to find out what actually went wrong.

Target: scope(failure) and scope(exit) should not run when the exception 
thrown is an AssertError.

Which leaves the floor open to two questions:
1. What other exceptions shouldn't run scope(exit) and scope(failure)?
2. Do we want scope(something) that *will* run on AssertError?

Obviously, the answer to 2 is linked to 1.

I think a reasonable approach is to say "scope(failxit) should on all 
Throwables except Errors". Note that this is not the same as saying 
"scope(failxit) runs only on Exceptions".

As for 2, that's the part I'm going to hate myself for. I will not 
object to adding "scope(fatal_error)", that do run on those cases 
(though I think just adding catch for those rare cases ought to be enough).

Shachar


More information about the Digitalmars-d mailing list