Value Range Propigation Spec

Stefan Koch via Digitalmars-d digitalmars-d at puremagic.com
Fri Oct 24 02:06:18 PDT 2014


Hi, I am the guy who implemented the (currently incomplete) vrp 
for sdc.

I see vrp as a tool to avoid _unnecessary_ casts. _Not_ as means 
to avoid _all_ casts.

> void main(in string[] args) {
>     immutable size_t len = args.length % 10;
>     ubyte x = len;
>     ubyte[] a;
>     foreach (immutable i; 0u .. len)
>         a ~= i;
> }

The problem with vrp for non-static immutable values is, that vrp 
becomes a runtime-thing and I would like to avoid that!
Tracking the range of a Variable at runtime could cause 
significant overhead!

Doing static analysis and tracking Variable-Ranges and 
assignments at compile-time, is possible but difficult and there 
are a number of reasons why not to do it.

1. Slows compilation down
2. implementation is not straight-forward and bugs could be hard 
to find.
3. Code that relays on this may not be easy to read, and it may 
be hard for the programmer to see why a particular assignment 
does not need a cast!

For simple code like the one posted above it is easy to see why 
it works.
anything mod 10 can only be in the range of [-9 .. 9]

But if we have much logic and control-flow between the assignment 
and the definition of the assigning variable then not haveing an 
explicit cast could cause a bit of puzzlement.


More information about the Digitalmars-d mailing list