[Issue 23354] [REG master] object.d(393): Error: reference to stack allocated value returned by 'new F(1)' assigned to non-scope parameter 'lhs'

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed Sep 21 21:39:54 UTC 2022


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

--- Comment #1 from Iain Buclaw <ibuclaw at gdcproject.org> ---
Test extracted from druntime.

Fails with -preview=dip1000, succeeds with -preview=dip1000
-checkaction=context.
---
/// If same exact type => one call to method opEquals
/// This test passes `@safe` because it defines a new opEquals with `@safe`
@safe void unittest1()
{
    class F
    {
        int flag;

        this(int flag)
        {
            this.flag = flag;
        }

        bool opEquals(const F o) const @safe nothrow pure
        {
            return flag == o.flag;
        }
    }

    assert(new F(0) == new F(0));
    assert(!(new F(0) == new F(1)));
}

/// General case => symmetric calls to method opEquals
@safe void unittest2()
{
    int fEquals, gEquals;

    class Base
    {
        int flag;
        this(int flag)
        {
            this.flag = flag;
        }
    }

    class F : Base
    {
        this(int flag) { super(flag); }

        bool opEquals(const Base o) @safe
        {
            fEquals++;
            return flag == o.flag;
        }
    }

    class G : Base
    {
        this(int flag) { super(flag); }

        bool opEquals(const Base o) @safe
        {
            gEquals++;
            return flag == o.flag;
        }
    }

    assert(new F(1) == new G(1));
    assert(fEquals == 1);
    assert(gEquals == 1);
}

--


More information about the Digitalmars-d-bugs mailing list