Skip to content

Commit

Permalink
Fix Issue 18142 - checkedint opOpAssign doesn't accept a checkedint
Browse files Browse the repository at this point in the history
  • Loading branch information
andreiv05 committed May 20, 2018
1 parent b62baca commit c545c24
Showing 1 changed file with 22 additions and 6 deletions.
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))
{
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;

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);
auto x3 = Checked!(Checked!int)(10);
x1 += x3;
assert(x1.get == 30);
}
}

/**
Expand Down

0 comments on commit c545c24

Please sign in to comment.