Open chiphogg opened 1 year ago
Checklist for features before we can call this "done".
(Note to future self.)
What work remains for the explicit-rep versions? It turns out that the explicit-rep unit conversions have three steps.
The existing utilities cover step 2, and steps 1 and 3 are basically the same. Thus, the main thing we need is a tool to detect when a static cast is lossy. This could be pretty tricky in general, because we need to worry about:
It may seem that the first step, casting to the common type, is always lossless. This isn't true, even for the integers: the common type of a signed type and some other type can be an unsigned type, which is obviously lossy. So once we make our static cast checker, we will need to call it on both entry and exit.
The overflow safety surface is pretty useful, but it's also just a heuristic. It can be too restrictive in some cases, and even perhaps too permissive in a few.
In practice, unit conversions should never happen in hot loops. Thus, it would be nice if every unit conversion could be checked at runtime. These checks can be very efficient. We can generate one at compile time for every conversion. For overflow risk, we can simply compare the actual runtime value to the (compile-time constant) threshold. And for truncation error, we can perform the mod operation.
Really, the only thing stopping us is: what do we do when the check fails? Different error handling strategies are appropriate for different domains. There is no "one true error handling strategy".
Fortunately, we can separate out two steps: there's the error handling, and then there's the checking as to whether it should trigger in the first place. For the latter, we can provide functions which simply return
bool
. Then each project can make their own "checked conversion" function that handles errors in their preferred way.