www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 10615] New: More range value analysis for indexed array

http://d.puremagic.com/issues/show_bug.cgi?id=10615

           Summary: More range value analysis for indexed array
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Keywords: rejects-valid
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: bearophile_hugs eml.cc



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

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jul 11 2013