www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 20472] New: [REG 2.068] slicing a static array results in

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

          Issue ID: 20472
           Summary: [REG 2.068] slicing a static array results in another
                    static array
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Keywords: wrong-code
          Severity: regression
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: schveiguy yahoo.com

If I slice a static array, the type should be a dynamic array:

int[16] x;

pragma(msg, typeof(x[])); // int[]
auto y = x[];
pragma(msg, typeof(y)); // int[]

However, if you slice a static array, and call a template or regular function
which accepts a static array that matches the slice size, for some reason the
compiler turns it into a static array again:

import std.stdio;

void foo(size_t N)(int[N] arr)
{
    writeln("static: ", N);
}

void foo()(int[] arr)
{
    writeln("dynamic");
}

void main()
{
    int[16] x;
    foo(x);         // static 16
    foo(x[]);       // static 16
    foo(x[0 .. 5]); // static 5 (!)
    auto y = x[];
    foo(y);         // dynamic
}

Note the only way to solve this is to create a new variable. The resulting
output should be:

static 16
dynamic
dynamic
dynamic

The end result means workarounds for ensuring you pass a slice and not the
entire static array can be overridden by the compiler. This is related to issue
16519, as there's no easy workaround.

run.dlang.io seems to suggest it happened in 2.068.2, someone on the forums
suggested PR https://github.com/dlang/dmd/pull/4779

Conversation on forums:
https://forum.dlang.org/post/qud5mk$2h9q$1 digitalmars.com

--
Dec 30 2019