checkedint call removal

Daniel Murphy via Digitalmars-d digitalmars-d at puremagic.com
Mon Jul 28 06:09:42 PDT 2014


"John Colvin"  wrote in message news:zzmgkwlzggrrtdtjbico at forum.dlang.org...
> 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)
> {
>      assert(a > 0);
> }
>
> of course you can't optimise away the check.

No, because that would change the observable behaviour.

The compiler can do stuff like:

uint x = ...;
assert(x < 256);
ubyte y = x & 0xFF;

becomes

uint x = ...;
assert(x < 256);
ubyte y = x;

or

assert(y == 8);
auto n = x / y;

becomes

assert(y == 8);
auto n = y >> 3;

Allowing things like this:

assert(x  < 256);
ubyte y = x; // no mask/cast required

is technically possible but questionable.

You could also (in theory) propagate range information via an out contract:

int myMathFunc(int x)
out(result)
{
    assert(result >= 0 && result < 17);
}
body
{
    < some incomprehensible mathmatical stuff that the compiler can't 
understand >
}

void main()
{
    auto v = myMathFunc(99);
    ubyte b = v; // According to the out contract, v must fit in a ubyte
} 



More information about the Digitalmars-d mailing list