digitalmars.D.bugs - [Issue 14245] New: Immutable reference to immutable field in
- via Digitalmars-d-bugs (31/31) Mar 04 2015 https://issues.dlang.org/show_bug.cgi?id=14245
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: nobody puremagic.com Reporter: schuetzm gmx.net 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. --
Mar 04 2015