-
Notifications
You must be signed in to change notification settings - Fork 220
Add RataDie::in_well_behaved_astronomical_range(), use to avoid panics #6876
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
base: main
Are you sure you want to change the base?
Add RataDie::in_well_behaved_astronomical_range(), use to avoid panics #6876
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok. Relaxing the calendar-specific guarantees outside of a very large range seems reasonable and sufficiently orthogonal to the other questions about proleptic approximations.
} | ||
|
||
// Assertion failure: | ||
// > assertion failed: Date::try_new_ummalqura_date(y, m, d, IslamicUmmAlQura::new_always_calculating()).is_ok() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
UAQ is not an astronomical calendar and it doesn't have this constructor.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah this is just the old test that anba wrote.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should still test that it doesn't panic.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
then you might as well test that Gregorian doesn't panic. there is no astronomical code
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah but it feels like overkill to test all the calendars. These are the iffy ones, some iffier than others. It's a test, it's fine if it tests extra things.
A client (anba) found it useful to test this, so I'm keeping it in.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The purpose of this test is not to test that the code in this PR is doing something. The purpose of this test is to test that large Temporal-allowed dates work without panicking in the calendars that have calendrical debug assertions. We should ideally test more dates.
In the future if we moved the chinese calendar over to the pinqi approximation I would still wish to continue testing it here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
by that logic we might change the implementation of every calendar, or add debug assertions to any calendar. then just add these tests to any_calendar.rs which has tests for all calendars.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean yeah but it's only a couple calendars that are insufficiently defined outside the modern range. I think there's a pretty clear destination here.
But I might just make this an AnyCalendar test. I hadn't done that mostly because I was trying to reuse anba's test, and also the current AnyCalendar test is kind of annoyingly merged. Might try and clean it up a bit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added more calendars.
any_calendar.rs has a massive omnibus test that I do not think we should expand. A good refactor to do here would be to design a "for all calendars" test utility.
Until then, I added some more calendars. I do not think we need to exhaustively list them all. I do think adding extra tests protects us from future issues.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should definitely test the calendars that are known-astronomical. For others, we can accept these unit tests but not necessarily require that they all be covered consistently.
/gemini review |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request introduces a mechanism to handle extreme dates in astronomical calendars by defining a 'well-behaved' range for RataDie
. The new function RataDie::in_well_behaved_astronomical_range()
is used to suppress debug assertions that would otherwise panic for dates far in the past or future. This change is applied to the Chinese-based and Islamic calendars. Additionally, a new test suite is added to ensure these extreme dates no longer cause panics. The approach is solid and effectively addresses the reported issues. I have one suggestion to improve the implementation of the new function by using constants for magic numbers, which will enhance readability and maintainability.
15d3036
to
0de8442
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR addresses panic issues in astronomical calendars when processing dates outside well-behaved ranges by adding range checks to debug assertions. The changes introduce a WELL_BEHAVED_ASTRONOMICAL_RANGE
constant and modify debug assertions to skip invariant checks for dates outside this range, preventing panics in extreme date scenarios while maintaining calendar correctness within expected ranges.
Key changes:
- Added
WELL_BEHAVED_ASTRONOMICAL_RANGE
constant to define the astronomical validity range - Modified debug assertions across multiple calendar implementations to skip checks outside the well-behaved range
- Added test cases to verify extreme date handling
Reviewed Changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 3 comments.
Show a summary per file
File | Description |
---|---|
utils/calendrical_calculations/src/chinese_based.rs | Added range constant and updated debug assertions for Chinese-based calendar calculations |
utils/calendrical_calculations/src/islamic.rs | Re-exported the range constant and updated Saudi Islamic calendar assertions |
components/calendar/src/provider/chinese_based.rs | Modified PackedChineseBasedYearInfo to handle out-of-range dates with clamping |
components/calendar/src/cal/hijri.rs | Updated Hijri calendar debug assertions to use the astronomical range check |
components/calendar/src/cal/chinese_based.rs | Added range checks for Chinese-based calendar date conversions |
components/calendar/src/tests/extrema.rs | Added new test file to verify extreme date handling |
components/calendar/src/tests/mod.rs | Added the new extrema test module |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
} | ||
|
||
// Assertion failure: | ||
// > assertion failed: Date::try_new_ummalqura_date(y, m, d, IslamicUmmAlQura::new_always_calculating()).is_ok() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
by that logic we might change the implementation of every calendar, or add debug assertions to any calendar. then just add these tests to any_calendar.rs which has tests for all calendars.
Attempting to address #4917, #5068, https://issues.chromium.org/issues/439624688.
Related: #5778
tc39/proposal-temporal#2869 has told us that we are allowed to break calendar-internal invariants outside of the useful range of the calendar.
We currently hit debug assertions for some of these dates. This patches it up by
This is a partial PR, I haven't fixed all of the assertions. We'll likely have to make the function in calendrical_calculations pub+hidden (or make a copy in
icu_calendar
) and use it.I would like feedback on the approach.