[Issue 17604] New: Reject mutable fields initialized with reference types to data segment
via Digitalmars-d-bugs
digitalmars-d-bugs at puremagic.com
Wed Jul 5 08:09:42 PDT 2017
https://issues.dlang.org/show_bug.cgi?id=17604
Issue ID: 17604
Summary: Reject mutable fields initialized with reference types
to data segment
Product: D
Version: D2
Hardware: All
OS: All
Status: NEW
Keywords: accepts-invalid
Severity: normal
Priority: P1
Component: dmd
Assignee: nobody at puremagic.com
Reporter: dlang-bugzilla at thecybershadow.net
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.
--
More information about the Digitalmars-d-bugs
mailing list