Bug 3064 – Invalid array operation accepted, generates bad code

Status
RESOLVED
Resolution
FIXED
Severity
major
Priority
P2
Component
dmd
Product
D
Version
D1 (retired)
Platform
x86_64
OS
Linux
Creation time
2009-06-11T09:23:00Z
Last change time
2014-04-18T09:12:08Z
Keywords
accepts-invalid, wrong-code
Assigned to
nobody
Creator
matti.niemenmaa+dbugzilla

Comments

Comment #0 by matti.niemenmaa+dbugzilla — 2009-06-11T09:23:41Z
The following compiles in DMD 1.045, but shouldn't: void main() { int[] a = [1,2]; int[] b = [1,2]; a[] += b; assert (b[0] == 1); assert (b[1] == 2); assert (a[0] == 2); assert (a[1] == 4); } Currently, the code compiles but the third assertion fails, since DMD generates code as though b were an int. Note that the following both compiles and doesn't assert: void main() { int[] a = [1,2]; int[] b = [1,2]; a[] += b; assert (b[0] == 1); assert (b[1] == 2); assert (a[0] == 1+*cast(int*)&b); assert (a[1] == 2+*cast(int*)&b); } This is, of course, nonsense. The error is in the line 'a[] += b': according to http://www.digitalmars.com/d/1.0/arrays.html "[t]he rvalue can be an expression consisting either of an array slice of the same length and type as the lvalue or an expression of the element type of the lvalue, in any combination." Thus the line is incorrect and should read 'a[] += b[]', and that indeed works.
Comment #1 by clugdbug — 2010-05-03T01:39:52Z
Fixed DMD1.059 and 2.044
Comment #2 by bearophile_hugs — 2010-05-03T04:08:26Z
Reopened, because this wrong code compiles still with dmd v2.044, the bug persists: void main() { int[] a = [1,2]; int[] b = [1,2]; a[] += b; assert (b[0] == 1); assert (b[1] == 2); assert (a[0] == 2); assert (a[1] == 4); }
Comment #3 by clugdbug — 2010-05-03T04:34:59Z
(In reply to comment #2) > Reopened, because this wrong code compiles still with dmd v2.044, the bug > persists: > > void main() { > int[] a = [1,2]; > int[] b = [1,2]; > a[] += b; > assert (b[0] == 1); > assert (b[1] == 2); > assert (a[0] == 2); > assert (a[1] == 4); > } Oops, it's fixed only in my personal copy, not in the official DMD.
Comment #4 by bugzilla — 2010-05-31T19:03:32Z