Bug 4791 – Assigning a static array to itself should be allowed

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2010-09-02T21:48:58Z
Last change time
2018-04-25T05:40:41Z
Keywords
pull, rejects-valid
Assigned to
No Owner
Creator
David Simcha
See also
https://issues.dlang.org/show_bug.cgi?id=11970

Comments

Comment #0 by dsimcha — 2010-09-02T21:48:58Z
void main() { int[2] foo; foo = foo; } object.Exception: overlapping array copy This is a ridiculous limitation and is bugs waiting to happen. (For example, Bug 4789.) Even if this requires a simple runtime check before calling memcpy() or whatever, IMHO it's worth it because the cost of a single pointer comparison is negligible in almost all cases and for tiny static arrays (where it might not be negligible) the compiler could just generate regular assignment instructions that are safe for the overlapping/identical case instead of using something like memcpy().
Comment #1 by bearophile_hugs — 2010-09-03T04:39:48Z
The only correct syntax to write that code is this, see bug 3971 void main() { int[2] foo; foo[] = foo[]; }
Comment #2 by dsimcha — 2010-09-03T05:25:43Z
Yes, and this is a nightmare for generic code.
Comment #3 by k.hara.pg — 2014-06-06T06:50:16Z
The root issue is a dup of bug 12212, and it's properly fixed in 2.066 git-head. I'll add a case to the dmd test suite to avoid regressions. https://github.com/D-Programming-Language/dmd/pull/3628
Comment #4 by github-bugzilla — 2014-06-06T07:30:27Z
Commits pushed to master at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/4157e764c620984376da81d67eb83e67fb725d6e fix Issue 4791 - Assigning a static array to itself should be allowed https://github.com/D-Programming-Language/dmd/commit/d2347f6265a63fc15a4c478f109b32b0eb67ad1b Merge pull request #3628 from 9rnsr/fix4791 Issue 4791 - Assigning a static array to itself should be allowed
Comment #5 by bearophile_hugs — 2014-06-06T07:40:34Z
(In reply to David Simcha from comment #2) > Yes, and this is a nightmare for generic code. A recent bug in my code (I have used a loop to avoid the overhead of calling the runtime function): void main() { int[3] a, b, c; foreach (immutable i; 0 .. a.length) c = a[i] + b[i]; } The correct foreach body is instead: c[i] = a[i] + b[i]; So I think there are disadvantages at not requiring a [] syntax for all array-wide operations.