in/out with -release

Jonathan M Davis jmdavisProg at gmx.com
Sat Mar 5 14:05:10 PST 2011


On Saturday 05 March 2011 09:15:48 user at domain.invalid wrote:
> On 03/04/2011 09:22 PM, Jonathan M Davis wrote:
> > On Friday 04 March 2011 20:14:32 Kai Meyer wrote:
> >> I have an 'enforce' function call in an 'in' block for a function. When
> >> I compile with "-release -O -inline", the in/out blocks appear to be
> >> skipped. It's a simple verification for a dynamic array to not have a
> >> length of 0. In debug mode, the test condition hits the enforce in the
> >> 'in' block, but in release mode it does not. In both release and debug
> >> mode, the same exact enforce function works properly.
> >> 
> >> So am I to understand that -release will skip in/out blocks entirely?
> > 
> > Of course. It uses asserts. asserts are disabled in -release. Asserts are
> > for debugging, testing, and verifying code when developing, not for code
> > which is released. So, you get the benefit of the test when you don't
> > have -release and the benefit of speed when you do have -release. If an
> > assertion fails, your code logic is invalid. It's for validating your
> > code, not user input or whatnot.
> > 
> > enforce, on the other hand, is not a language primitive. It's not
> > intended for testing or debugging. It's intended to be used in
> > production code to throw an exception when its condition fails. If an
> > enforce fails, that generally means that you had bad input somewhere or
> > that an operation failed or whatnot. It's not intended for testing the
> > logic of your code like assert is intended to do. It's simply a
> > shorthand way to throw an exception when your program runs into a
> > problem.
> > 
> > - Jonathan M Davis
> 
> I don't think I understand your response entirely. I understand that
> asserts are disabled in -release mode. I understand that enforce is a
> function that comes with std.exception, and the code isn't hard to follow.
> 
> What I'm confused about is the in block, and why it is skipped in
> -release mode. You say "It uses asserts." I didn't put an assert in my
> in block, I put an enforce. So I'm guessing that you are indicating that
> the in block is treated like an assert, and is disabled with the
> -release flag.
> 
> But I think after reading your post you've helped clarify that what I'm
> checking (that you can't pop an empty stack) based on user input is
> something I should be checking with an enforce inside the function, and
> not an assert or enforce inside the in block.
> 
> I still think I would like it if you could be a little more explicit
> about the in/out blocks. Are they always disabled entirely (skipped)
> with -release, or just certain things?
> 
> Thanks for your help!

You're not really supposed to throw exceptions from in, out, or invariant 
blocks. You're supposed to use assertions in there. That's how the whole DbC 
thing is designed in D ( http://www.digitalmars.com/d/2.0/dbc.html ). So, while 
you _can_ throw exceptions from in, out, and invariant blocks, they _will_ be 
compiled out when compiling with -release. in, out, invariant just aren't 
intended for exceptions.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list