-
Notifications
You must be signed in to change notification settings - Fork 226
Description
Our calendars currently use i32
year values, and there are a couple ways that that can cause arithmetic overflow. The simplest way is when converting between different eras: i32::MAX reiwa
is going to be an out-of-range extended_year. More subtle cases happen due to use of various calendars for internal calculation, or things like "get the R.D. as milliseconds". Some of these cases can be solved by using wider integer types, but by and large i32::MAX
is not really a useful year to try and shove into this API.
We should ideally never be debug asserting in a way reachable to users.
#7062 attempts to improve the situation by adding coarse guards to pan-Calendar construction APIs: all year
/extended_year
inputs to from_fields
/from_codes
are now checked to be within ±2²⁷. It does not attempt to do anything about Date::try_new_from_specificcalendar
, calendar conversions, or calendar arithmetic.
#7065 goes further to remove most sources of overflow errors except the small ones really close to i32::MAX
. There's an open question there about Chinese: whether we should do millisecond math in i128, or error on those large dates.
We can and should try and expand the coarse year range. @robertbastian thinks we can get it to be ±2³⁰ (i32::MAX / 2
). That would in and of itself be quite good, I think it would require us to run the simple chinese approximation in i128.
Regardless of the expansion of the range, we have some decisions to make. Note that all of these basically impact really large dates, which don't necessarily actually matter too much. It mostly matters that applications can use this code without experiencing strange behavior when users input overlarge dates.
The main one is that whether these ranges should be per calendar or pan-calendar. Per calendar means that we can be more or less liberal for individual calendars, pan calendar means that all calendars. This would mean that calendar conversions cannot be used to produce dates that cannot be directly produced, and thus all dates continue to roundtrip without error.
#7062 avoids the question by making it an input constraint on specific APIs: it's not really trying to figure out whether it should be per-calendar or pan-calendar, it is just saying that any year
input on a pan-calendar API has a rough constraint.
If we decide it should be per-calendar, what happens on calendar conversion?
In either case, what happens on out-of-range duration arithmetic?
Note that these answers are entangled with implementation detail: it's not sufficient to say that we have per-calendar R.D. ranges, because from an implementation perspective you may need to guard against something before you have an R.D. to work with.