Bug 20472 – [REG 2.068] slicing a static array results in another static array

Status
RESOLVED
Resolution
FIXED
Severity
regression
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2019-12-30T18:16:15Z
Last change time
2021-11-04T22:45:00Z
Keywords
wrong-code
Assigned to
No Owner
Creator
Steven Schveighoffer
See also
https://issues.dlang.org/show_bug.cgi?id=16519

Comments

Comment #0 by schveiguy — 2019-12-30T18:16:15Z
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/[email protected]
Comment #1 by moonlightsentinel — 2019-12-30T18:49:22Z
Digger blames https://github.com/dlang/dmd/pull/4779 when using the following test case: void foo(size_t N)(ubyte[N], bool sa) { assert(sa); } void foo()(ubyte[], bool sa) { assert(!sa); } void main() { ubyte[16] x; foo(x, true); foo(x[], false); }
Comment #2 by boris2.9 — 2021-11-04T22:45:00Z
This was fixed by https://github.com/dlang/dmd/pull/12399 (2.096.1)