[Issue 24231] New: Can't emplace immutable nested class

d-bugmail at puremagic.com d-bugmail at puremagic.com
Tue Nov 7 01:26:09 UTC 2023


https://issues.dlang.org/show_bug.cgi?id=24231

          Issue ID: 24231
           Summary: Can't emplace immutable nested class
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: normal
          Priority: P1
         Component: druntime
          Assignee: nobody at puremagic.com
          Reporter: snarwin+bugzilla at gmail.com

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.

--


More information about the Digitalmars-d-bugs mailing list