Thread overview | |||||
---|---|---|---|---|---|
|
January 04, 2013 More value range analysis? | ||||
---|---|---|---|---|
| ||||
I think the value range analysis was designed to be limited to single expressions to keep it simple to implement, fast to run to keep compilation time low, and to avoid flow analysis. But with immutability there is much less need for flow analysis. So maybe it's possible to introduce in D value range analysis across different expressions if those expressions are immutable. A simple example: void main(string[] args) { ubyte x = cast(ubyte)args.length; immutable int y = x; // Here y must be in [0, 256[. } Two other useful cases: void main() { foreach (immutable i; 0 .. 256) { // Here i is in [0, 10[. ubyte x = i; // No need for a cast here. } int[10] a; foreach (immutable i; 0 .. a.length) { // Here i is in [0, a.length[, // this is useful if a.length is immutable. a[i] = 5; // No array bound tests here. } } In D there are situations where immutability is not enough; a simple option is just to ignore such cases (this means in such cases the value range analysis will not work across different expressions, and this code will keep generating four error messages): immutable uint x; immutable ubyte y; static this() { x = 255; y = x; x++; y = x; } class Foo { immutable uint a; immutable ubyte b; this() { a = 255; b = a; a++; b = a; } } void main() {} Bye, bearophile |
January 04, 2013 Re: More value range analysis? | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | > void main(string[] args) {
> ubyte x = cast(ubyte)args.length;
> immutable int y = x;
> // Here y must be in [0, 256[.
> }
Sorry, that example was meant to be:
void main(string[] args) {
immutable ubyte x = cast(ubyte)args.length;
immutable int y = x;
// Here y must be in [0, 256[.
ubyte z = y;
}
Bye,
bearophile
|
January 04, 2013 Re: More value range analysis? | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | On 01/04/2013 04:13 AM, bearophile wrote:
>> void main(string[] args) {
>> ubyte x = cast(ubyte)args.length;
>> immutable int y = x;
>> // Here y must be in [0, 256[.
>> }
>
> Sorry, that example was meant to be:
>
>
> void main(string[] args) {
> immutable ubyte x = cast(ubyte)args.length;
> immutable int y = x;
> // Here y must be in [0, 256[.
> ubyte z = y;
> }
>
>
> Bye,
> bearophile
The first version is fine. The range computed for 'y' is [0..256[ in both cases.
|
Copyright © 1999-2021 by the D Language Foundation