Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(sumtype): avoid quadratic memory complexity on opEquals #8607

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions std/sumtype.d
Original file line number Diff line number Diff line change
Expand Up @@ -714,16 +714,24 @@ public:
{
static if (is(This == Rhs))
{
return AliasSeq!(this, rhs).match!((ref value, ref rhsValue) {
static if (is(typeof(value) == typeof(rhsValue)))
{
return value == rhsValue;
}
else
final switch(tag)
{
static foreach(idx, T; Types)
{
return false;
case idx:
final switch (rhs.tag)
{
static foreach(ridx, U; typeof(rhs).Types)
{
case ridx:
static if (is(immutable T == immutable U))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line introduces a bug. In the original version, the following unit test passes:

@safe unittest
{
    int* p = null;
    const int* cp = null;

    SumType!(int*, const(int*)) x = p;
    SumType!(int*, const(int*)) y = cp;

    assert(x != y);
}

With this PR, the unit test above will fail.

return get!T == rhs.get!U;
else
return false;
}
}
}
});
}
}
else
{
Expand Down