[Issue 22563] Nested structs, if not escaping, shouldn't allocate context (just like delegates)

d-bugmail at puremagic.com d-bugmail at puremagic.com
Fri Dec 3 22:02:53 UTC 2021


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

--- Comment #5 from Stanislav Blinov <stanislav.blinov at gmail.com> ---
Well... the `static S s;` simply shouldn't compile at all for exactly that
reason, so that's one extra bug:

struct WTF(T)
{
    static T x;
    void foo() { x.foo(); }
}

@safe
void main()
{
    int a;
    struct Nested
    {
        @safe:

        void foo() { ++a; } // boom1

        ~this() { foo(); }  // boom2
    }
}

But the reason `consumeThingWithContext` is not inferred safe is the escape:

Error: scope variable `s` assigned to non-scope parameter `p` calling
context.main.Nested.opAssign

The compiler is doing exactly the same thing for delegates too at the moment -
it infers such escape as @system.

A delegate is context + one function pointer. A nested struct is effectively a
superset of that, with an added advantage of type information for function
pointers. There's no reason (other than implementation complexity) to support
allocation elision for one and not the other.

--


More information about the Digitalmars-d-bugs mailing list