Comment #0 by bearophile_hugs — 2013-07-11T12:53:29Z
This issue is between an enhancement request and a rejects-valid bug report.
uint i = 100;
void main(in string[] args) {
auto j = args.length;
ubyte x1 = (i ^^ 2) % 256; // OK
ubyte x2 = (i ^^ 3) % 256; // OK
ubyte[] arr = [(i ^^ 2) % 256, (i ^^ 3) % 256]; // OK!
ubyte y = [(i ^^ 2) % 256, (i ^^ 3) % 256][j]; // Error
}
DMD 2.064alpha gives:
test2.d(7): Error: cannot implicitly convert expression ([(uint __powtmp8 = i;
, __powtmp8 * __powtmp8) % 256u, (uint __powtmp9 = i;
, __powtmp9 * __powtmp9 * __powtmp9) % 256u][j]) of type uint to ubyte
The lines with x1 and x2 are accepted, because the range analysis is able to infer those are in-range assignments. While the assignment of y is refused, despite all the contents of the array can be inferred as castable to ubyte, as shown in assignment of arr.
I think the line with y should be accepted.
Perhaps the issue shown here causes the difference between the two following cases of "generate", where the first is accepted and the second is refused:
ubyte generate1(s...)() {
ubyte[10] result;
foreach (immutable i, ref item; result)
item = s[0][0] << 4;
return result[0];
}
ubyte generate2(s...)() {
ubyte[10] result;
foreach (immutable i, ref item; result)
item = s[0][i % 3] << 4; // line 11
return result[0];
}
void main() {
enum ubyte[16] data = [1, 2, 3, 4];
auto g1 = generate1!data; // OK
auto g2 = generate2!data; // error
}
Where DMD 2.064alpha gives:
test.d(10): Error: cannot implicitly convert expression (cast(int)[cast(ubyte)1u, cast(ubyte)2u, cast(ubyte)3u, cast(ubyte)4u, cast(ubyte)0u, cast(ubyte)0u, cast(ubyte)0u, cast(ubyte)0u, cast(ubyte)0u, cast(ubyte)0u, cast(ubyte)0u, cast(ubyte)0u, cast(ubyte)0u, cast(ubyte)0u, cast(ubyte)0u, cast(ubyte)0u][i % 3u] << 4) of type int to ubyte
test.d(16): Error: template instance test.generate2!([cast(ubyte)1u, cast(ubyte)2u, cast(ubyte)3u, cast(ubyte)4u, cast(ubyte)0u, cast(ubyte)0u, cast(ubyte)0u, cast(ubyte)0u, cast(ubyte)0u, cast(ubyte)0u, cast(ubyte)0u, cast(ubyte)0u, cast(ubyte)0u, cast(ubyte)0u, cast(ubyte)0u, cast(ubyte)0u]) error instantiating
Issue discussed here:
http://forum.dlang.org/thread/[email protected]
Comment #1 by robert.schadek — 2024-12-13T18:09:12Z