www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 14245] New: Immutable reference to immutable field in


          Issue ID: 14245
           Summary: Immutable reference to immutable field in constructor
                    allows breaking type system
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P1
         Component: DMD
          Assignee: nobody puremagic.com
          Reporter: schuetzm gmx.net

The spec allows immutable fields to be initialized in a constructor:

Unfortunately, it still allows taking references to them before they are
constructed. This allows breaking the type system:

    struct S {
        immutable int x;
        this(int a) {
            import std.stdio;
            immutable int* b = &this.x;
            writeln(*b); // prints 0
            this.x = a;
            writeln(*b); // prints value of a

Suggestion: Disallow taking immutable references (pointers, `ref`) before a
field's initialization. Const references are ok.

Mar 04 2015