https://issues.dlang.org/show_bug.cgi?id=14245

          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: [email protected]
          Reporter: [email protected]

The spec allows immutable fields to be initialized in a constructor:
http://dlang.org/class.html#field-init

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.

--

Reply via email to