July 06, 2023 [Issue 24034] New: Changing this in constructor allows to modify immutable members of other instance | ||||
---|---|---|---|---|
| ||||
https://issues.dlang.org/show_bug.cgi?id=24034 Issue ID: 24034 Summary: Changing this in constructor allows to modify immutable members of other instance Product: D Version: D2 Hardware: x86_64 OS: Linux Status: NEW Severity: normal Priority: P1 Component: dmd Assignee: nobody@puremagic.com Reporter: tim.dlang@t-online.de Pull request https://github.com/dlang/dmd/pull/15389 for issue 24024 allows to modify `this` for classes inside functions. In constructors this allows to modify immutable members of another instance of the same class. The constructor will now also return the wrong pointer. import std.stdio; class C { immutable int i; this(int i, C other) { if (other !is null) this = other; this.i = i; } } void main() { C c1 = new C(1, null); writeln(c1.i); // prints 1 C c2 = new C(2, c1); writeln(c1.i); // prints 2 writeln(c2.i); // prints 2 writeln(c1 is c2); // prints true } -- |
Copyright © 1999-2021 by the D Language Foundation