-
-
Notifications
You must be signed in to change notification settings - Fork 694
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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")) | ||
{ | ||
|
@@ -951,8 +956,8 @@ if (isIntegral!T || is(T == Checked!(U, H), U, H)) | |
} | ||
} | ||
payload = cast(T) r; | ||
return this; | ||
} | ||
return this; | ||
} | ||
|
||
/// | ||
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
auto x3 = Checked!(Checked!int)(10); | ||
x1 += x3; | ||
assert(x1.get == 30); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would be great to add two more tests:
|
||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
|
||
} | ||
|
||
/** | ||
|
There was a problem hiding this comment.
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?