[Issue 14245] New: Immutable reference to immutable field in constructor allows breaking type system
via Digitalmars-d-bugs
digitalmars-d-bugs at puremagic.com
Wed Mar 4 11:02:24 PST 2015
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 at puremagic.com
Reporter: schuetzm at 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.
--
More information about the Digitalmars-d-bugs
mailing list