assert(condition[, message]) patch

Brad Roberts braddr at puremagic.com
Wed May 31 23:45:53 PDT 2006


On Wed, 31 May 2006, Brad Roberts wrote:

> On Wed, 31 May 2006, Walter Bright wrote:
> 
> > braddr at puremagic.com wrote:
> > > I have been toying with D to bring myself up to speed and I found myself
> > > writing
> > > a lot of unit tests, always a good thing.  In those unit tests I found
> > > myself
> > > writing a lot of blocks like:
> > > 
> > > if (cond)
> > > {
> > > writefln("some debugging output to make my life easier");
> > > assert(false);
> > > }
> > > 
> > > I know many don't like unit tests to have output, but I do.
> > 
> > Why not:
> > 
> > 	assert(!cond); // some debugging output to make my life easier
> > 
> > ? You'll need to go look at the source anyway when the assert trips, so what
> > advantage is there to print the comment?
> 
> I've reapplied the referenced patch to dmd/gdc and it works when used 
> properly.  However, exactly like it was before, it's got a number of bugs 
> when used improperly.  I need to learn more about dmd and gdc both to fix 
> the thing to be robust enough to actually merge in.

The current patch:

    http://www.puremagic.com/~braddr/d/assert-optional-message-0.2-gdc-0.18.diff
    http://www.puremagic.com/~braddr/d/assert-test.d


There's a couple issues with the code as it stands:

1) most of the code is in the gdc layer, meaning that to incorporate with 
dmd walter will have to do some work.  This can't be helped, probably.

2) rather than introduce a new _d_assert_with_message() api to the 
runtime, I've chosen to default to "" if no assert message is specified.

3) the runtime code in phobos/std/assertinternal.d recognizes "" as 'no 
message'

4) assert(condition, non-string-constant) produces an ICE in the gdc glue 
layer

5) assert(condition, non-string-expression) produces an odd error message:  
Integer constant expression expected instead of (test7func)()


I'm continuing to work on 4 and 5.  For now, I'm leaving 1-3 as is unless 
there's some outcry that guilts me into working on them more.


Later,
Brad



More information about the Digitalmars-d mailing list