<div dir="ltr">On 13 May 2013 11:03, Walter Bright <span dir="ltr"><<a href="mailto:newshound2@digitalmars.com" target="_blank">newshound2@digitalmars.com</a>></span> wrote:<br><div class="gmail_extra"><div class="gmail_quote">
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="im">On 5/12/2013 5:00 PM, Manu wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
So, here's an issue that constantly drives me nuts, and an elegant solution<br>
seems so do-able.<br>
</blockquote>
<br></div>
It's a form of "data flow analysis". This is done by the optimizer, although it doesn't track ranges.<br>
<br>
It isn't quite as simple as you suggest, there are issues like dealing with arbitrary control flow.<div class="im"><br>
<br>
x &= 0xFFFF;<br>
short s = x; // Error! (but we know x is 0 .. 65535)<br>
<br></div>
Ok, but what about:<br>
<br>
x &= 0xFFFF;<br>
while (expr) {<br>
short s = x;<br>
x += 1;<br>
}<br>
<br>
There's a forward reference going on. We cannot semantically evaluate s=x until we semantically evaluate the rest of the function in order to know that x is or is not reassigned.<br>
</blockquote></div><br></div><div class="gmail_extra" style>It is done by the optimiser, but I'm suggesting that it doesn't only have value in terms of optimisation. It could improve language usability if performed in the front end.</div>
<div class="gmail_extra" style><br></div><div class="gmail_extra" style>In that context, unless the analysis is very powerful(/mature) and can statically determine 'expr', then x would need to be relaxed to unlimited at that point, since it obviously can't know the limits within (or after) that loop.</div>
<div class="gmail_extra" style>In this case, the compiler would complain that it can't determine x and you would need an explicit cast as usual.</div><div class="gmail_extra" style><br></div><div class="gmail_extra" style>
I can see the effective forward reference issue here, which could be addressed in time, but even just reasonably simple limits tracking (ie, within sequential code) would make a massive difference. I think programmers would intuitively understand this situation and wouldn't expect magic from the compiler.</div>
<div class="gmail_extra" style>The places where it matters the most are places where it's dead obvious what the numeric limits are, and it should be equally obvious to the compiler.</div><div class="gmail_extra" style>
<br></div><div class="gmail_extra" style>It's the sort of thing that could be improved with time, but even a very simple start would be a big help in many cases.</div></div>