www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 24231] New: Can't emplace immutable nested class

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 puremagic.com
          Reporter: snarwin+bugzilla 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.

--
Nov 06 2023