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

Fix Issue 18142 - checkedint opOpAssign doesn't accept a checkedint #6516

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 22 additions & 6 deletions std/experimental/checkedint.d
Original file line number Diff line number Diff line change
Expand Up @@ -916,20 +916,25 @@ if (isIntegral!T || is(T == Checked!(U, H), U, H))
Returns: A reference to `this`.
*/
ref Checked opOpAssign(string op, Rhs)(const Rhs rhs) return
if (isIntegral!Rhs || isFloatingPoint!Rhs || is(Rhs == bool))
if (isIntegral!Rhs || isFloatingPoint!Rhs || is(Rhs == bool) ||
is(typeof(Checked!(Rhs, Hook)(rhs))))
{
static assert(is(typeof(mixin("payload" ~ op ~ "=rhs")) == T));

static if (hasMember!(Hook, "hookOpOpAssign"))
static assert(is(typeof(mixin("payload" ~ op ~ "=rhs")) == T) ||
is(typeof(Checked!(Rhs, Hook)(rhs))));
static if (is(Rhs == Checked!(X, Y), X, Y))
Copy link
Member

Choose a reason for hiding this comment

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

Why let the compiler do the infersion again?
You already know that its Checked!(Rhs, Hook), no?

{
return this.opOpAssign!op(rhs.get);
}
else static if (hasMember!(Hook, "hookOpOpAssign"))
{
hook.hookOpOpAssign!op(payload, rhs);
return this;
}
else
{
alias R = typeof(get + rhs);
auto r = opBinary!op(rhs).get;
import std.conv : unsigned;

Copy link
Member

Choose a reason for hiding this comment

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

Unrelated change. Please avoid.

static if (ProperCompare.hookOpCmp(R.min, min.get) < 0 &&
hasMember!(Hook, "onLowerBound"))
{
Expand All @@ -951,8 +956,8 @@ if (isIntegral!T || is(T == Checked!(U, H), U, H))
}
}
payload = cast(T) r;
return this;
}
return this;
}

///
Expand Down Expand Up @@ -980,6 +985,17 @@ if (isIntegral!T || is(T == Checked!(U, H), U, H))
x += 1;
assert(MyHook.thereWereErrors);
}

@system unittest
{
auto x1 = Checked!int(10);
auto x2 = Checked!int(10);
x1 += x2;
assert(x1.get == 20);
Copy link
Member

Choose a reason for hiding this comment

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

.get isn't necessary here.

auto x3 = Checked!(Checked!int)(10);
x1 += x3;
assert(x1.get == 30);
Copy link
Member

Choose a reason for hiding this comment

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

Would be great to add two more tests:

  • different hook
  • different type, but one that coerces to int (e.g. short)

}
Copy link
Member

Choose a reason for hiding this comment

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

Sadly this needs to be outside of the template as otherwise it would be instantiated for every template.
That's the reason for static if (is(T == int) && is(Hook == void)) unittest btw.
Also we typically add a link to the respective bugzilla issue like:

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

}

/**
Expand Down