RFC: Value range propagation for if-else

Lionello Lunesu via Digitalmars-d digitalmars-d at puremagic.com
Tue Jun 17 23:40:20 PDT 2014


Hi,

I got this thing working and I think it's about time I get some comments 
on it.

I've been wanting to extend Value Rang Propagation (VRE) for some time 
now. Mostly because of the fix to the troublesome "signed-unsigned 
comparisons" issue. Enabling VRE for if-else and "&&" will fix many of 
the false-positive warnings given out by the fix to bug 259 by allowing 
code like this: if (signed > 0 && signed < unsigned) { .. }

Have a look at the branch:
https://github.com/lionello/dmd/compare/if-else-range

There, I've also added a __traits(intrange, <expression>) which returns 
a tuple with the min and max for the given expression. It's used in the 
test case as follows:


     const i = foo ? -1 : 33;

     if (i)
       static assert(__traits(intrange, i) == Tuple!(-1, 33));
     else
     {
       //static assert(i == 0); TODO
       static assert(__traits(intrange, i) == Tuple!(0, 0));
     }

     if (i == 33)
     {
       //static assert(i == 33); TODO
       static assert(__traits(intrange, i) == Tuple!(33, 33));
     }
     else
       static assert(__traits(intrange, i) == Tuple!(-1, 32));

     if (10 <= i)
       static assert(__traits(intrange, i) == Tuple!(10, 33));
     else
       static assert(__traits(intrange, i) == Tuple!(-1, 9));


It would be nice if this can be used by CTFE as well.

Destroy?

L.


More information about the Digitalmars-d mailing list