Comment #0 by snarwin+bugzilla — 2023-11-07T01:26:09Z
As of DMD 2.105.3, the following program fails to compile.
---
void main()
{
import core.lifetime;
class O
{
int n = 123;
class I
{
int fun() => n;
}
}
auto buf = new void[](__traits(classInstanceSize, O.I));
emplace!(immutable(O.I))(buf, new immutable(O));
}
---
The error message is:
---
/usr/include/dmd/druntime/import/core/lifetime.d(116): Error: cannot modify `immutable` expression `chunk.this`
/usr/include/dmd/druntime/import/core/lifetime.d(209): Error: template instance `core.lifetime.emplace!(immutable(I), immutable(O))` error instantiating
test.d(14): instantiated from here: `emplace!(immutable(I), immutable(O))`
---
This is caused by emplace attempting to set the .outer field of the emplaced class instance to the value of its second argument (new immutable(O)). The assignment fails, because the emplaced instance is immutable, and its fields cannot be written to.
Comment #1 by robert.schadek — 2024-12-07T13:43:01Z