Description
Proposal
Problem statement
For every integer operation with a symbolic operator, there is a corresponding checked_*
method that returns None
if an overflow occurs...
except for bitshifts, whose checked_*
functions do something entirely different.
For example, 255_u8.checked_shl(1)
evaluates to Some(254)
.
The closest existing functions are EDIT: the overflowing_{shl,shr}
, but those must also return a result in the overflowing case, ruling out implementations that check for overflow before doing the shift.overflowing_*
functions ALSO don't do what you would expect.
Motivating examples or use cases
This could be used to detect overflow when parsing certain types of positional integer notation, from ascii decimal to certain VLQs.
Solution Sketch
- (optional) Deprecate
checked_shr
andchecked_shl
and give them less confusing names. - Add new shift functions that return
None
on overflow
Links and related work
- the check_shl_overflow macro used in the linux kernel
What happens now?
This issue contains an API change proposal (or ACP) and is part of the libs-api team feature lifecycle. Once this issue is filed, the libs-api team will review open proposals as capability becomes available. Current response times do not have a clear estimate, but may be up to several months.
Possible responses
The libs team may respond in various different ways. First, the team will consider the problem (this doesn't require any concrete solution or alternatives to have been proposed):
- We think this problem seems worth solving, and the standard library might be the right place to solve it.
- We think that this probably doesn't belong in the standard library.
Second, if there's a concrete solution:
- We think this specific solution looks roughly right, approved, you or someone else should implement this. (Further review will still happen on the subsequent implementation PR.)
- We're not sure this is the right solution, and the alternatives or other materials don't give us enough information to be sure about that. Here are some questions we have that aren't answered, or rough ideas about alternatives we'd want to see discussed.