Bug 17604 – Reject mutable fields initialized with reference types to data segment

Status
RESOLVED
Resolution
DUPLICATE
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2017-07-05T15:09:42Z
Last change time
2017-08-23T10:28:12Z
Keywords
accepts-invalid, safe
Assigned to
No Owner
Creator
Vladimir Panteleev
See also
https://issues.dlang.org/show_bug.cgi?id=13646, https://issues.dlang.org/show_bug.cgi?id=15612

Comments

Comment #0 by dlang-bugzilla — 2017-07-05T15:09:42Z
Consider: ////////// test.d ///////// class C { Object o = new Object; int[] arr = new int[4]; } /////////////////////////// The code above currently compiles, but will behave in a surprising manner. A user coming from another language may expect that there is a new instance of the object and array instantiated for every instance of C; however, this is not the case. In reality, .o and .arr will initially always point to the same data segment address for every instance of C. This anti-feature not only allows breaking the type system, but has been a repeated source of confusion for D users (see e.g. issue 13890, issue 15604). This code is invalid and should be rejected.
Comment #1 by schveiguy — 2017-08-22T13:48:57Z
Duplicate of 2947?
Comment #2 by dfj1esp02 — 2017-08-22T14:05:10Z
Well, any type system violation is safety violation, so not sure about the keyword. *** This issue has been marked as a duplicate of issue 2947 ***
Comment #3 by dlang-bugzilla — 2017-08-22T17:34:57Z
Thanks. Definitely not surprised this was filed before. I prefer the description on this bug but I'm a little biased ;)
Comment #4 by schveiguy — 2017-08-22T17:57:49Z
There's a certain extra weight attached to such a low bug number, especially when it has so many duplicates.
Comment #5 by dfj1esp02 — 2017-08-23T10:28:12Z
(In reply to Vladimir Panteleev from comment #3) > I prefer the description on this bug but I'm a little biased ;) This scenario needs dynamic initialization, and D requires an explicit constructor for it for whatever reason, so disallowing is the the most straightforward solution.