Bug 22978 – Shared destructor is not able change immutable var but shared constructor can

Status
RESOLVED
Resolution
INVALID
Severity
enhancement
Priority
P4
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2022-04-03T15:32:04Z
Last change time
2024-03-23T22:01:37Z
Assigned to
No Owner
Creator
apham

Comments

Comment #0 by apz28 — 2022-04-03T15:32:04Z
struct X { Object o; } static immutable X x; shared static this() { x.o = new Object(); } shared static ~this() { x.o = null; // onlineapp.d(15): Error: cannot modify `immutable` expression `x.o` }
Comment #1 by nick — 2024-03-23T21:54:16Z
`x.o` is immutable. It is by design that `x` itself can be initialized in a shared static constructor. Initializing `x.o` directly is also OK (so long as it is not reassigned, that is not currently enforced though, but it is for a class constructor with immutable fields). It is correct that the destructor can't mutate immutable data.
Comment #2 by nick — 2024-03-23T22:01:37Z
> that is not currently enforced though That is issue 24449.