assert(false, "...") doesn't terminate program?!

Walter Bright newshound2 at digitalmars.com
Mon Oct 29 10:38:06 PDT 2012


On 10/29/2012 7:51 AM, Don Clugston wrote:> On 27/10/12 20:39, H. S. Teoh wrote:
 >> On Sat, Oct 27, 2012 at 08:26:21PM +0200, Andrej Mitrovic wrote:
 >>> On 10/27/12, H. S. Teoh <hsteoh at quickfur.ath.cx> wrote:
 >>>>          writeln("how did the assert not trigger??!!");    // how did we get
 >>>> here?!
 >>>
 >>> Maybe related to -release?
 >> [...]
 >>
 >> Haha, you're right, the assert is compiled out because of -release.
 >>
 >> But I disassembled the code, and didn't see the "auto x = 1/toInt()"
 >> either. Is the compiler optimizing that away?
 >
 > Yes, and I don't know on what basis it thinks it's legal to do that.

Because x is a dead assignment, and so the 1/ is removed. Divide by 0 faults are 
not considered a side effect. I think the code would be better written as:

     if (toInt() == 0) throw new Error();

If you really must have a divide by zero fault,

     if (toInt() == 0) divideByZero();

where:

     void divideByZero()
     {
          static int x;
          *cast(int*)0 = x / 0;
     }



More information about the Digitalmars-d mailing list