Value Range Propigation Spec

Walter Bright via Digitalmars-d digitalmars-d at puremagic.com
Thu Oct 23 12:35:42 PDT 2014


On 10/23/2014 8:29 AM, Shammah Chancellor wrote:
> Several of us working on it were a little surprised that DMD does not compile this:
>
> void main() {
>      long l = 42;
>      int i = l;
> }
>
> Is this expected to compile?

No. As bearophile mentioned, VRP only applies to an expression. The compiler 
front end does not do dataflow analysis to affect semantics. (The optimizer 
does, but that is a later pass and does not affect the semantics.)


> Planned?

Not currently.


> My post was based on my thought that
> this *did* compile and we were wondering rules for when the range would be
> reset.   If `l` were __gshared for example, the range wouldn't be deterministic.

To do it reliably would require dataflow analysis.


> Surely there should be some rules document?  What we've implemented, is that the
> value range is calculated at float80 precision, and it's min and max can fit in
> the ultimate type for a cast, then it's good to go. We only look at the actual
> expression being implicitly cast.

Just be careful with floats about accumulated roundoff errors. This is not a 
simple problem.



More information about the Digitalmars-d mailing list