assert(0) behavior

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


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). Not being able to 
have a message for assert(0) would be just as bad as not having a 
message for any other assertion. By arguing that assert(0) should 
only have a message in debug mode, you'd be arguing for something 
like

static if(assert)
     assert(0, msg);
else
     assert(0);

which is pointless IMHO. I can definitely see an argument that we 
should be printing out something before the HLT instruction in 
-release mode, but as it stands, it's part of the spec that 
assert(0) just becomes a HLT instruction and so there's no 
message, and I don't think that should be a surprise given that 
the fact that assert(0) becomes a HLT instruction with -release 
is the key difference between it and other assertions. You don't 
get a warning about assertions having messages either, and their 
messages don't show up with -release. Instead, you get undefined 
behavior if they would have failed.

- Jonathan M Davis


More information about the Digitalmars-d mailing list