www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 17604] New: Reject mutable fields initialized with reference

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 puremagic.com
          Reporter: dlang-bugzilla 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.

--
Jul 05