Repository navigation
Conversation
|
need to fix the build first. Generally speaking, reasonable. |
| bool isFailure() const { return U_FAILURE(errorCode_); } | ||
|
|
||
| // TODO: un-inline | ||
| Length32 operator+(int32_t b) const { |
There was a problem hiding this comment.
You also need the int32_t + Length32 operator (which cannot be a member, so usually for the binary operators make neither a member).
I think currently, std::numeric_limits<int32_t>::max() + Length32(1, ec) would compile, but result in an unchecked (and undefined) addition via the implicit conversion to int32_t.
Add a test for that, and then fix it.
There was a problem hiding this comment.
Understood.
However, I would prefer to keep this simple, and not implement what we are not likely to need.
Idea: I could turn the int32_t conversion operator into an explicit getter. Call sites would need to call .get(), which is slightly less nice, but because the object doesn't automatically decay into a plain integer, we don't accidentally get bad behavior when we haven't overloaded some operator.
There was a problem hiding this comment.
I would prefer simple for the user (so something that actually behaves like an integer, without having to .get() or think about which way an addition or multiplication needs to be written) over simple for the author.
I can help with writing the operators if you want, I have been writing C++ operators as a hobby for the past twelve years: https://github.com/search?q=repo%3Amockingbirdnest%2FPrincipia+%22operator%2B%22&type=code.
There was a problem hiding this comment.
I much prefer the simple solution here, and the very minor inconvenience of having to .get(), rather than a complex, custom value type where we have to implement all kinds of unnecessary operators just in case someone were to use them.
You can see now what this looks like.
There was a problem hiding this comment.
You can see now what this looks like.
Worse, and less safe (you have moved an operation from an increment of the checked integer to an increment of the unchecked integer).
There was a problem hiding this comment.
Here is something with all the binary operator+s in its API for all integers, all the constructors, but only one implementation of the addition and only one implementation of the overflow checking logic.
Doing this comprehensively just takes less code than having this partial pile of overloads.
There was a problem hiding this comment.
(Missing the positivity check, but you get the idea.)
There was a problem hiding this comment.
Nice, thanks.
I replaced overloads with templates.
Your code does not deal with a few things:
- does not check for non-negative, as you noted
- operator+() takes two Length32's but does not propagate the error code of both
- you make the same mistake i did initially, and pass the source object's error code reference to the target
- if the source uses its internal one, then the target must do so, too -- otherwise we get a dangling reference
- thus, constructing a result object needs different code than constructing a fresh one
So I templatized, but I kept the simple member operators.
(I also fixed a bug: My previous operator code was writing to errorCode_ which could be the internal one, even though this is const =-)
Also, by auto-decaying to an int32_t, we still get all kinds of unchecked default operator implementations. This is why I want to keep .get().
There was a problem hiding this comment.
Also, by auto-decaying to an int32_t, we still get all kinds of unchecked default operator implementations. This is why I want to keep .get().
This is good enough to replace the existing calls to uprv_add32_overflow on sizes, where it is clearly an improvement.
But if the goal is to use it all over the place to calm down the static analysers that keep complaining about overflows, it needs to behave a lot more uniformly like a checked numeric type, and so I would want deal with that by implementing the default operators in question.
There was a problem hiding this comment.
My goal is to make overflow checks easier.
I assume that there is a somewhat limited set of places where we need to either deal with size_t lengths or allocate buffers with slop, or allocate buffers for appending/concatenating, etc. Clearly more places than we have been checking, but nothing like all of the integer operations in the library.
| } | ||
| return {0, errorCode_}; | ||
| } | ||
|
|
There was a problem hiding this comment.
I would expect to see at least operator- (both directions), operator--, operator+=, and operator*= (it is not unreasonable to subtract from a length, and we should then check that the result is nonnegative).
There was a problem hiding this comment.
Here is an example (signed, so Length32 would not do, we would need some signed version of it) which would clearly benefit from +=:
icu/icu4c/source/i18n/number_decimalquantity.cpp
Lines 252 to 256 in b73930d
There was a problem hiding this comment.
Hm. I had looked at the overflow checks in common, but not in i18n.
We can of course add more operators. What I don't want is having to add every possible numeric operator just to avoid the risk of a gap. I don't know how to do that if the object decays to a raw integer.
And signed... I like checking for non-negative for lengths and capacities.
Here, yes, we would need a signed type.
What are you thinking? Separate class? Template parameter? Runtime setter??
There was a problem hiding this comment.
What are you thinking?
- That if we are going to do this in more than a dozen places (not just the places where we check now, the reason why we started talking about this and why you convinced yourself to do this is that we are getting pushed to check more), this needs to be fairly comprehensive.
- That we clearly need two types, signed and unsigned, because nonnegativity is a very common property to want to check, and because negative numbers nevertheless exist.
- That this then raises the question of the return type of operations: clearly a difference of positives can be negative, and a product of a positive with something signed is signed.
- That checked arithmetic with subtypes for positives is something that has been done already (http://www.ada-auth.org/standards/2yaarm/html/AA-3-5-4.html, http://www.ada-auth.org/standards/2yaarm/html/AA-4-5.html, http://www.ada-auth.org/standards/2yaarm/html/AA-A-1.html#p12), and that we should not reinvent this design piecemeal.
- That the operations whose values can be signed should return the signed type, that their sign should be checked on assignment to the unsigned type.
There was a problem hiding this comment.
Re .get()… I guess I can live with it, especially if we use this more and it starts propagating, marking the boundary may be OK. But commutative +, *, etc. seems like a requirement if we are going to use it widely.
Length32 + Length32 is probably also something that would then be needed; it is a bit distasteful because of the two error codes becoming one, but I suspect in practice they would both be the same and we can just pick one.
There was a problem hiding this comment.
Makes sense.
Can we do this incrementally, adding features as needed?
Just looking at adjustMagnitude(), first thing is we need negatives.
Maybe add a template parameter to the class for whether it's non-negative?
And maybe spiffy typedefs for brevity?
Safe32<Type>, Length32=Safe32<NON_NEG>, Int32=Safe32<ANY> ...?
There was a problem hiding this comment.
Can we do this incrementally, adding features as needed?
Yes, but I would like to keep the increments algebraically sensible, so that they are not an endless source of surprise (in particular, having n * 2 but not 2 * n seems user-hostile and harmful to readability; I notice you are flipping the arguments of an uprv_mul32_overflow when translating them to *, and the original order is the one I would have expected).
Certainly I would be fine with deferring multiplication of checked integers until we need it (let alone modulo and division).
In general when I implement operators, I follow these concepts, they are a good way of not forgetting something that would be expected to be there: https://github.com/mockingbirdnest/Principia/blob/7b84a16e0d5365f55990d455bd37b4be1ec975bb/base/algebra.hpp#L68. Some of this is application-specific (the whole homogeneous_meow stuff comes from physical quantities, though it applies to polynomials too), but perhaps it might inform us.
Note the asymmetry of affine; I am a lot less bothered about the lack of 1 + n than the lack of 2 * n.
There was a problem hiding this comment.
And maybe spiffy typedefs for brevity?
Yes. We might templatize however we feel like in the implementation, but I would want callers to not know or care about it.
There was a problem hiding this comment.
(I think one first increment is just the nonnegative type; once you start having that, you end up wanting quite a lot more operators, so it might make sense to leave it to a subsequent PR.)
Draft: Please comment on the shape of the new class and its call sites.
New internal class Length32:
The common-library code is changed to use Length32 instead of C-style functions.
Checklist