On Monday, 28 July 2014 at 12:08:39 UTC, Daniel Murphy wrote:
"John Colvin" wrote in message news:iguetbdxlyilavliz...@forum.dlang.org...

To what extent can a compiler use assertions? Can it work backwards from an assert to affect previous code?

void foo(int a)
{
    enforce(a & 1);
    assert(a & 1);
}

The assert is dead code, because it will never be reached if (a & 1) is false.

void bar()
{
    assert(a & 1);
    enforce(a & 1);
}

The throw inside enforce is dead code, because it will never be reached if (a & 1) is false.

The compiler is free to remove dead code, because it doesn't change the program's behaviour.

Ok. What about this:

int c;

void foo(int a)
{
    if(a < 0) c++;
    assert(a > 0);
}

I presume that cannot be optimised away entirely to:

void foo(int a) {}

?

Reply via email to