Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More range value analysis for indexed array #17599

Open
dlangBugzillaToGithub opened this issue Jul 11, 2013 · 0 comments
Open

More range value analysis for indexed array #17599

dlangBugzillaToGithub opened this issue Jul 11, 2013 · 0 comments

Comments

@dlangBugzillaToGithub
Copy link

bearophile_hugs reported this on 2013-07-11T12:53:29Z

Transferred from https://issues.dlang.org/show_bug.cgi?id=10615

Description

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/lnitjekzqoozqmrqtuim@forum.dlang.org
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant