-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Interaction of addition and checked conversion #15489
Comments
If |
it could be non-trivial, since the result would require a |
We generally have allowed anything that makes sense on a value-preserving level. If integers are the same size then as long as the result is an unsigned integer, either interpretation (subtraction of 40, or addition of reinterpret(UInt,-40) yields the same result). To @vtjnash's point, I think that's a different issue, because we do allow overflow just fine. |
We should probably make this work but it can't work by normal promotion/conversion, which is annoying. If we promoted |
Is there any case (for fixed size integer, |
We have generic comparison routines (comparing signed and unsigned integers). Comparison is very similar to subtraction. Thus one can make the case that mixed signed/unsigned arithmetic should also work. These comparisons have a slightly more complex implementation since they first test whether e.g. the signed argument is negative (and then returning the obvious result) before performing the actual comparison. A similar approach should work here: function +(x::Unsigned, y::Signed)
y<0 && return x - unsigned(-y)
x + unsigned(y)
end This isn't quite right yet (since the expression |
Yes, +1 for leaving conversion and promotion alone, but implementing |
Ok, has the decision been made to allow this, in which case I'd like to implement this soon. This is super annoying. |
It seems people like the suggestion in general. It's now a question of looking at the details, which only become apparent once there is a concrete implementation (read: "pull request"). |
x-ref: #9292 |
@StefanKarpinski proposes this basic approach here:
In other words, use |
The reasoning is that we don't check for overflow on arithmetic operations so checking for conversion errors when promoting to a common type but then allowing that operation to overflow is silly – you may as well just guarantee that the result is correct in the modulus of the result type. |
also skip checks for integer bitwise ops
also skip checks for integer bitwise ops
also skip checks for integer bitwise ops
also skip checks for integer bitwise ops
also skip checks for integer bitwise ops
also skip checks for integer bitwise ops
#23827) also skip checks for integer bitwise ops
Consider for example:
It is understandable why this happens, but the behavior is annoying. At least for integers of the same size, this addition is unambiguous.
The text was updated successfully, but these errors were encountered: