Bug 6286 – Regression(2.054): Static arrays can not be assigned from const(T)[N] to T[N]

Status
RESOLVED
Resolution
FIXED
Severity
regression
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
Other
OS
Windows
Creation time
2011-07-11T01:52:00Z
Last change time
2015-06-09T05:14:51Z
Keywords
patch, rejects-valid
Assigned to
nobody
Creator
sludwig

Comments

Comment #0 by sludwig — 2011-07-11T01:52:55Z
The following snipped worked up to DMD 2.053 but fails on 2.054: --- void test() { const(int)[4] src = [1, 2, 3, 4]; int[4] dst; dst = src; // Error: cannot implicitly convert expression (src) of type const(int[4u]) to int[] dst[] = src[]; // still works } --- The assignment "T[N] = const(T)[N]" should work as long as "T = const(T)" works as there is no aliasing goind on but just a plain copy.
Comment #1 by issues.dlang — 2011-07-11T02:05:28Z
I don't think that this is a bug but rather than dmd used to be buggy with regards to this case. dst = src; is assigning src to dst. And assigning a dynamic array to a static one isn't legal. If dst = src; were allowed, then it would be inconsistent with the case where both dst and src are dynamic arrays. dst[] = src[]; on the other hand is specifically copying the elements of src to the elements of dst. So, it's possible that this is a regression and that dst = src; is supposed to just translate to dst[] = src[]; when dst is a static array, but I think that it's far more likely that this is a case where a long-standing bug was fixed.
Comment #2 by sludwig — 2011-07-11T04:09:49Z
Note that in the test case both, the source and the destination are static arrays of the same size. There is no reason why a direct assignment should not work here. I think the compiler mistakes the left side here as a dynamic array. In the case of really assigning a dynamic array to a static one, the decision is definitely not so obvious as it could lead to a hidden runtime error.
Comment #3 by yebblies — 2011-07-11T07:50:34Z
Comment #4 by issues.dlang — 2011-07-11T08:48:22Z
Ah yes. I didn't read carefully enough. If both src and dst are static arrays, then dst = src; should work. It's when one is a dynamic array that it shouldn't.
Comment #5 by bugzilla — 2011-08-05T12:34:48Z