Bug 24449 – immutable data can be mutated after initialization in shared static constructor

Status
NEW
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2024-03-23T21:59:12Z
Last change time
2024-12-13T19:34:01Z
Keywords
safe
Assigned to
No Owner
Creator
Nick Treleaven
See also
https://issues.dlang.org/show_bug.cgi?id=20712
Moved to GitHub: dmd#20421 →

Comments

Comment #0 by nick — 2024-03-23T21:59:12Z
import std.stdio; immutable int x; shared static this() { x = 5; // OK, initialization not assignment x.writeln(); // 5 x = 6; // should error x++; // should error assert(x == 7); } It should not be possible to assign immutable data after it is initialized in the shared static constructor. That would make those consistent with class constructors: class C { immutable int x; this() { x = 5; x = 6; // error, x initialized multiple times } }
Comment #1 by nick — 2024-03-23T22:23:44Z
This can also cause a safety violation when the shared static constructor is @safe, in particular see: https://issues.dlang.org/show_bug.cgi?id=5207#c11
Comment #2 by robert.schadek — 2024-12-13T19:34:01Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/20421 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB