Comment #0 by bearophile_hugs — 2013-07-10T04:41:33Z
(This is an improvement for Issue 10018 , but it's sufficiently different, so I have opened a new issue.)
Currently this fails:
void main(in string[] args) {
immutable ushort x = args.length % 5;
immutable ubyte y = x;
}
dmd 2.064alpha gives:
temp.d(3): Error: cannot implicitly convert expression (x) of type immutable(ushort) to immutable(ubyte)
Issue 10018 asks to propagate the range of immutable values, allowing that code to compile with no errors.
- - - - - - - - - - -
This is an improvement of that idea:
void main(in string[] args) {
immutable size_t x = args.length;
assert(x < 256);
immutable ubyte y = x;
}
x is an immutable full-range size_t. But the assert should change the range of x, allowing the successive assignment of y to be accepted.
- - - - - - - - - - -
Another possible idea is to allow code like:
void main(in string[] args) {
immutable size_t x = args.length;
if (x < 256) {
immutable ubyte y = x;
}
}
Comment #1 by nick — 2023-01-03T18:44:51Z
Note that `args.length % 5` does compile now. The other two do not as VRP is not tracked across statements.
Comment #2 by robert.schadek — 2024-12-13T18:09:06Z