www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 16779] New: VRP for array literals does not work with

https://issues.dlang.org/show_bug.cgi?id=16779

          Issue ID: 16779
           Summary: VRP for array literals does not work with templated
                    functions taking a static array if the size is
                    inferred
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: issues.dlang jmdavisProg.com

void main()
{
    foo1([1, 2, 3, 4]);
    foo2([1, 2, 3, 4]);
    foo3!ubyte([1, 2, 3, 4]);
    foo4!(ubyte, 4)([1, 2, 3, 4]);
    foo4!ubyte([1, 2, 3, 4]);
}


void foo1(ubyte[] arr)
{
}

void foo2(ubyte[4] arr)
{
}

void foo3(T)(T[] arr)
{
}

void foo4(T, size_t n)(auto ref T[n] arr)
{
}

The first four calls compile just fine. The fifth one does not. I don't see any
reason why it shouldn't be able to work given that the others do, and it's
inconsistent that it doesn't. It also makes it impossible for a function like

T[n] staticArray(T, size_t n)(auto ref T[n] arr)
{
    return arr;
}

to use VRP and compile with

auto sa = staticArray!ubyte([1, 2, 3, 4]);

which means that it's not possible to duplicate the semantics of

ubyte[4] sa = [1, 2, 3, 4];

with such a function. Obviously, it can be gotten around via casting, but the
direct initialization doesn't require it, and it's not required in the first
four function calls in the example above - just the one where the size is
inferred. So, if it were fixed so that the fifth call were consistent with the
other four, then a function like the staticArray function here could duplicate
the semantics of the direct initialization (aside from the fact that it
inferred the size, which is the point of using the function rather than just
initializing the variable with the literal).

--
Nov 25 2016