$ dmd druntime/src/object.d -o- -unittest -preview=dip1000
druntime/src/object.d(393): Error: reference to stack allocated value returned by `new F(1)` assigned to non-scope parameter `lhs`
druntime/src/object.d(393): Error: reference to stack allocated value returned by `new G(1)` assigned to non-scope parameter `rhs`
Caused by https://github.com/dlang/dmd/pull/14367
Comment #1 by ibuclaw — 2022-09-21T21:39:54Z
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);
}