Bug 3550 – array.dup violates const/invariant without a cast.

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
Other
OS
Linux
Creation time
2009-11-24T19:47:00Z
Last change time
2015-06-09T01:27:04Z
Keywords
accepts-invalid, patch
Assigned to
nobody
Creator
schveiguy
Blocks
2573

Comments

Comment #0 by schveiguy — 2009-11-24T19:47:31Z
simple test case: import std.stdio; class C { int x; } void foo(const(C)[] arg) { auto arg2 = arg.dup; foreach(c; arg2) c.x = 5; } void main() { C[] arr; arr ~= new C; foo(arr); writefln("%d", arr[0].x); } This compiles and outputs 5. I believe dup should statically fail if the element type has any references in it and the type is const or invariant. Likewise, I think idup should fail if the element type has any references in it. These properties are not supposed to be "you're on your own" territory IMO. This is related to bug 1339
Comment #1 by schveiguy — 2009-11-24T20:02:10Z
I should clarify: I think idup should fail if the element has any *non-immutable* references in it. Note this leaves us with no ways to dup a const array with references in it! Should we have cdup? A workaround to both would be to always allow dup when an explicit cast is involved. i.e.: auto arg2 = cast(const(C)[])arg.dup;
Comment #2 by dfj1esp02 — 2010-08-03T13:25:17Z
Just thought, there must be this bug...
Comment #3 by yebblies — 2011-07-08T20:47:59Z
Comment #4 by bugzilla — 2011-10-02T22:33:46Z