assert(0) behavior

Jonathan M Davis via Digitalmars-d digitalmars-d at puremagic.com
Tue Aug 4 14:39:56 PDT 2015


On Tuesday, 4 August 2015 at 21:07:20 UTC, Steven Schveighoffer 
wrote:
> On 8/4/15 4:56 PM, Jonathan M Davis wrote:
>> On Tuesday, 4 August 2015 at 20:48:52 UTC, Steven 
>> Schveighoffer wrote:
>>> On 8/4/15 4:43 PM, Steven Schveighoffer wrote:
>>>> I should say, any assert(0) with a message printed that is 
>>>> possible to
>>>> trigger in release mode is an error. If you can ensure it's 
>>>> only
>>>> possible to trigger when compiled in debug mode (or for unit 
>>>> tests),
>>>> then assert(0, msg) is fine.
>>>
>>> In that vein, would it be possible to make this a warning? 
>>> i.e. if you
>>> compile a file in -release mode, and the compiler encounters 
>>> an
>>> assert(0, msg), it emits a warning?
>>
>> I fail to see why it's a problem if assert(0) has a message. 
>> It's
>> exactly the same as any other assertion except that it stays 
>> even with
>> -release, and you don't get the message then (whereas with a 
>> normal
>> assertion, you wouldn't get anything).
>
> Seeing an assert with a message gives the impression that you 
> will see the message if you get there. I get that normal 
> asserts may not even trigger. But the concept of "oh, if I see 
> that message I know what happened" really goes out the window 
> if you are never going to see it.
>
> As I said earlier, any line in druntime that has an assert(0, 
> msg) is a bug, because the message is never seen.

It stills indicate why the assertion is there, which can be 
helpful to maintainers, and it _is_ possible to build druntime in 
debug mode. It's just not released that way (though it's been 
argued before that druntime/Phobos should be released with both 
debug and release builds).

And arguing against assert(0) with a message in druntime because 
it's not going to show the message normally would basically be 
the same as arguing against having any assertions in there at 
all, because they wouldn't show up in a typical build either. But 
that doesn't mean that they shouldn't be there, just that they're 
not as useful as they might otherwise be, because druntime is 
almost always built in release mode.

> Doing the static if thing isn't the right answer either. The 
> right answer is to choose one or the other (and by choosing to 
> print a message, you could either throw an assert error 
> directly, or print the message specifically and then assert(0) 
> ).
>
> So basically:
>
> assert(0, msg);
>
> becomes
>
> printSomehow(msg);
> assert(0);
>
> With druntime it's difficult to actually print the message (and 
> my PR is trying to fix that).

I really so no difference between having a message with an 
assert(0) as with any other assertion. You're not going to see 
either with -release. It's just that assert(0) kills your program 
instead of giving you undefined behavior.

I'm certainly not opposed to have a message be printed before the 
HLT instruction with assert(0), but I don't at all agree that the 
fact that the message is not seen in -release is a reason not to 
have a message.

- Jonathan M Davis


More information about the Digitalmars-d mailing list