Bug 16779 – VRP for array literals does not work with templated functions taking a static array if the size is inferred

Status
NEW
Severity
normal
Priority
P3
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2016-11-25T16:25:53Z
Last change time
2024-12-13T18:50:48Z
Assigned to
No Owner
Creator
Jonathan M Davis
Moved to GitHub: dmd#17782 →

Comments

Comment #0 by issues.dlang — 2016-11-25T16:25:53Z
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).
Comment #1 by timothee.cour2 — 2018-02-15T02:54:36Z
same problem with tuples U[T.length] staticArray(U = CommonType!T, T...)(T a) { return [a]; } auto a = staticArray2!bute(1, 2); Error: cannot implicitly convert expression [_param_0, _param_1] of type int[] to byte[2]
Comment #2 by robert.schadek — 2024-12-13T18:50:48Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/17782 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB