assume, assert, enforce, @safe

Daniel Murphy via Digitalmars-d digitalmars-d at puremagic.com
Thu Jul 31 11:14:13 PDT 2014


"Tofu Ninja"  wrote in message news:mhhtxjlrvtqhzztxidbe at forum.dlang.org...

> With that logic(and the proposed optimizations that this whole thing is 
> about), weird stuff like this happens...
>
> void foo(int x)
> {
>      if(x != 0) throw ...;
>      assert(x == 0);
> }
>
> The if check could be removed because assert will be assumed to always be 
> true in release... so x could never not equal 0.... the assert just nuked 
> my scrubbing logic...

The if can't be removed - and it's fairly easy to see why.  In the control 
flow path that contains the assert, the compiler is _already_ sure that x == 
0.  The assert adds no new information.

The assumption the compiler can make is "if the program got to here, this 
condition must be true".  The qualification is extremely important.

The corner case is "assert(0)".  It means "if the program got to here, the 
impossible has happened."

So with this:

void foo(int x)
{
    if(x != 0) throw ...;
    assert(0);
}

the compiler doesn't have to bother checking x at all. 



More information about the Digitalmars-d mailing list