Skip to content
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 1 addition & 47 deletions components/calendar/fuzz/fuzz_targets/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,50 +41,6 @@ impl From<DateDuration> for icu_calendar::types::DateDuration {
}
}

impl DateDuration {
/// Temporal doesn't care about dates outside of -271821-04-20 to +275760-09-13
/// and will not let you attempt to add up to them even with overflow: constrain.
///
/// We should eventually be applying some limits to this code in ICU4X.
/// We currently do not and `Date::try_added()` will panic for large `days` values.
///
/// <https://github.com/unicode-org/icu4x/issues/3964>
///
/// For now, fuzz what we can for Temporal's needs.
///
/// This code is copied from <https://github.com/boa-dev/temporal/pull/615>
/// so that we are testing temporal_rs behavior.
fn is_valid_for_temporal(&self) -> bool {
// Temporal range is -271821-04-20 to +275760-09-13
// This is (roughly) the maximum year duration that can exist for ISO
const TEMPORAL_MAX_ISO_YEAR_DURATION: u32 = 275760 + 271821;
// Double it. No calendar has years that are half the size of ISO years.
const YEAR_DURATION: u32 = 2 * TEMPORAL_MAX_ISO_YEAR_DURATION;
// Assume every year is a leap year, calculate a month range
const MONTH_DURATION: u32 = YEAR_DURATION * 13;
// Our longest year is 390 days
const DAY_DURATION: u32 = YEAR_DURATION * 390;
const WEEK_DURATION: u32 = DAY_DURATION / 7;


if self.years > YEAR_DURATION {
return false;
}
if self.months > MONTH_DURATION {
return false;
}
if self.weeks > WEEK_DURATION {
return false;
}
if self.days > DAY_DURATION.into() {
return false;
}

true

}
}

fuzz_target!(|data: FuzzInput| {
let Some(date) = data.ymd.to_date(data.cal, true) else { return };

Expand All @@ -94,8 +50,6 @@ fuzz_target!(|data: FuzzInput| {
} else {
Some(Overflow::Reject)
};
if !data.duration.is_valid_for_temporal() {
return;
}

let _ = date.try_added_with_options(data.duration.into(), options);
});
6 changes: 2 additions & 4 deletions components/calendar/fuzz/fuzz_targets/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use arbitrary::Arbitrary;
use icu_calendar::{Date, AnyCalendar};
use icu_calendar::types::{DateFields, MonthCode};
use icu_calendar::types::{DateFields, Month};
use icu_calendar::options::*;


Expand All @@ -28,8 +28,6 @@ impl Ymd {
Some(Overflow::Reject)
};

let code: MonthCode;

let mut fields = DateFields::default();
fields.extended_year = Some(self.year);
fields.day = Some(self.day);
Expand Down Expand Up @@ -93,7 +91,7 @@ impl From<AnyCalendarKind> for icu_calendar::AnyCalendarKind {
AnyCalendarKind::HijriTabularTypeIIThursday => Self::HijriTabularTypeIIThursday,
AnyCalendarKind::HijriUmmAlQura => Self::HijriUmmAlQura,
AnyCalendarKind::Iso => Self::Iso,
AnyCalendarKind::Japanese | AnyCalendarKind::JapaneseExtended => Self::Japanese,
AnyCalendarKind::Japanese => Self::Japanese,
AnyCalendarKind::Persian => Self::Persian,
AnyCalendarKind::Roc => Self::Roc,
}
Expand Down
2 changes: 1 addition & 1 deletion components/calendar/src/any_calendar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ macro_rules! make_any_calendar {
date: &Self::DateInner,
duration: $crate::types::DateDuration,
options: $crate::options::DateAddOptions,
) -> Result<Self::DateInner, $crate::DateError> {
) -> Result<Self::DateInner, $crate::error::DateAddError> {
let mut date = *date;
match (self, &mut date) {
$(
Expand Down
14 changes: 8 additions & 6 deletions components/calendar/src/cal/abstract_gregorian.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

use crate::cal::iso::{IsoDateInner, IsoEra};
use crate::calendar_arithmetic::{ArithmeticDate, DateFieldsResolver};
use crate::error::{DateError, DateFromFieldsError, EcmaReferenceYearError, UnknownEraError};
use crate::error::{
DateAddError, DateError, DateFromFieldsError, EcmaReferenceYearError, UnknownEraError,
};
use crate::options::DateFromFieldsOptions;
use crate::options::{DateAddOptions, DateDifferenceOptions};
use crate::preferences::CalendarAlgorithm;
Expand Down Expand Up @@ -50,12 +52,12 @@ impl<Y: GregorianYears> DateFieldsResolver for AbstractGregorian<Y> {
}

#[inline]
fn year_info_from_era(
fn extended_year_from_era_year(
&self,
era: &[u8],
era_year: i32,
) -> Result<Self::YearInfo, UnknownEraError> {
Ok(self.0.extended_from_era_year(Some(era), era_year)? + Y::EXTENDED_YEAR_OFFSET)
) -> Result<i32, UnknownEraError> {
self.0.extended_from_era_year(Some(era), era_year)
}

#[inline]
Expand Down Expand Up @@ -147,7 +149,7 @@ impl<Y: GregorianYears> Calendar for AbstractGregorian<Y> {
date: &Self::DateInner,
duration: types::DateDuration,
options: DateAddOptions,
) -> Result<Self::DateInner, DateError> {
) -> Result<Self::DateInner, DateAddError> {
date.added(duration, &AbstractGregorian(IsoEra), options)
}

Expand Down Expand Up @@ -289,7 +291,7 @@ macro_rules! impl_with_abstract_gregorian {
date: &Self::DateInner,
duration: crate::types::DateDuration,
options: crate::options::DateAddOptions,
) -> Result<Self::DateInner, DateError> {
) -> Result<Self::DateInner, $crate::error::DateAddError> {
let $self_ident = self;
crate::cal::abstract_gregorian::AbstractGregorian($eras_expr)
.add(&date.0, duration, options)
Expand Down
2 changes: 1 addition & 1 deletion components/calendar/src/cal/buddhist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::preferences::CalendarAlgorithm;
use crate::{
cal::abstract_gregorian::{impl_with_abstract_gregorian, GregorianYears},
calendar_arithmetic::ArithmeticDate,
types, Date, DateError, RangeError,
types, Date, RangeError,
};
use tinystr::tinystr;

Expand Down
10 changes: 6 additions & 4 deletions components/calendar/src/cal/coptic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

use crate::calendar_arithmetic::ArithmeticDate;
use crate::calendar_arithmetic::DateFieldsResolver;
use crate::error::{DateError, DateFromFieldsError, EcmaReferenceYearError, UnknownEraError};
use crate::error::{
DateAddError, DateError, DateFromFieldsError, EcmaReferenceYearError, UnknownEraError,
};
use crate::options::DateFromFieldsOptions;
use crate::options::{DateAddOptions, DateDifferenceOptions};
use crate::{types, Calendar, Date, RangeError};
Expand Down Expand Up @@ -66,11 +68,11 @@ impl DateFieldsResolver for Coptic {
}

#[inline]
fn year_info_from_era(
fn extended_year_from_era_year(
&self,
era: &[u8],
era_year: i32,
) -> Result<Self::YearInfo, UnknownEraError> {
) -> Result<i32, UnknownEraError> {
match era {
b"am" => Ok(era_year),
_ => Err(UnknownEraError),
Expand Down Expand Up @@ -184,7 +186,7 @@ impl Calendar for Coptic {
date: &Self::DateInner,
duration: types::DateDuration,
options: DateAddOptions,
) -> Result<Self::DateInner, DateError> {
) -> Result<Self::DateInner, DateAddError> {
date.0.added(duration, self, options).map(CopticDateInner)
}

Expand Down
12 changes: 6 additions & 6 deletions components/calendar/src/cal/east_asian_traditional.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
use crate::calendar_arithmetic::{ArithmeticDate, ToExtendedYear};
use crate::calendar_arithmetic::{DateFieldsResolver, PackWithMD};
use crate::error::{
DateError, DateFromFieldsError, EcmaReferenceYearError, LunisolarDateError, MonthError,
UnknownEraError,
DateAddError, DateError, DateFromFieldsError, EcmaReferenceYearError, LunisolarDateError,
MonthError, UnknownEraError,
};
use crate::options::{DateAddOptions, DateDifferenceOptions};
use crate::options::{DateFromFieldsOptions, Overflow};
Expand Down Expand Up @@ -588,18 +588,18 @@ impl<R: Rules> DateFieldsResolver for EastAsianTraditional<R> {
}

#[inline]
fn year_info_from_era(
fn extended_year_from_era_year(
&self,
_era: &[u8],
_era_year: i32,
) -> Result<Self::YearInfo, UnknownEraError> {
) -> Result<i32, UnknownEraError> {
// This calendar has no era codes
Err(UnknownEraError)
}

#[inline]
fn year_info_from_extended(&self, extended_year: i32) -> Self::YearInfo {
debug_assert!(crate::calendar_arithmetic::VALID_YEAR_RANGE.contains(&extended_year));
debug_assert!(crate::calendar_arithmetic::GENEROUS_YEAR_RANGE.contains(&extended_year));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thought: This check doesn't really achieve anything since GENEROUS_YEAR_RANGE is bigger than the RD range. I guess it's harmless

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this was added deliberately because previously it was quite haphazard, it's nice to have a check right before we call the tricky calendrical APIs.

self.0.year(extended_year)
}

Expand Down Expand Up @@ -724,7 +724,7 @@ impl<R: Rules> Calendar for EastAsianTraditional<R> {
date: &Self::DateInner,
duration: types::DateDuration,
options: DateAddOptions,
) -> Result<Self::DateInner, DateError> {
) -> Result<Self::DateInner, DateAddError> {
date.0.added(duration, self, options).map(ChineseDateInner)
}

Expand Down
17 changes: 11 additions & 6 deletions components/calendar/src/cal/ethiopian.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
use crate::cal::coptic::CopticDateInner;
use crate::cal::Coptic;
use crate::calendar_arithmetic::{ArithmeticDate, DateFieldsResolver};
use crate::error::{DateError, DateFromFieldsError, EcmaReferenceYearError, UnknownEraError};
use crate::error::{
DateAddError, DateError, DateFromFieldsError, EcmaReferenceYearError, UnknownEraError,
};
use crate::options::DateFromFieldsOptions;
use crate::options::{DateAddOptions, DateDifferenceOptions};
use crate::types::DateFields;
Expand Down Expand Up @@ -93,14 +95,17 @@ impl DateFieldsResolver for Ethiopian {
}

#[inline]
fn year_info_from_era(
fn extended_year_from_era_year(
&self,
era: &[u8],
era_year: i32,
) -> Result<Self::YearInfo, UnknownEraError> {
) -> Result<i32, UnknownEraError> {
match (self.era_style(), era) {
(EthiopianEraStyle::AmeteMihret, b"am") => Ok(era_year + AMETE_MIHRET_OFFSET),
(_, b"aa") => Ok(era_year + AMETE_ALEM_OFFSET),
(EthiopianEraStyle::AmeteMihret, b"am") => Ok(era_year),
(EthiopianEraStyle::AmeteMihret, b"aa") => {
Ok(era_year - AMETE_MIHRET_OFFSET + AMETE_ALEM_OFFSET)
}
(EthiopianEraStyle::AmeteAlem, b"aa") => Ok(era_year),
(_, _) => Err(UnknownEraError),
}
}
Expand Down Expand Up @@ -190,7 +195,7 @@ impl Calendar for Ethiopian {
date: &Self::DateInner,
duration: types::DateDuration,
options: DateAddOptions,
) -> Result<Self::DateInner, DateError> {
) -> Result<Self::DateInner, DateAddError> {
Coptic
.add(&date.0, duration, options)
.map(EthiopianDateInner)
Expand Down
2 changes: 1 addition & 1 deletion components/calendar/src/cal/gregorian.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::cal::abstract_gregorian::{
use crate::calendar_arithmetic::ArithmeticDate;
use crate::error::UnknownEraError;
use crate::preferences::CalendarAlgorithm;
use crate::{types, Date, DateError, RangeError};
use crate::{types, Date, RangeError};
use tinystr::tinystr;

impl_with_abstract_gregorian!(Gregorian, GregorianDateInner, CeBce, _x, CeBce);
Expand Down
12 changes: 6 additions & 6 deletions components/calendar/src/cal/hebrew.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

use crate::calendar_arithmetic::{ArithmeticDate, DateFieldsResolver, PackWithMD, ToExtendedYear};
use crate::error::{
DateError, DateFromFieldsError, EcmaReferenceYearError, LunisolarDateError, MonthError,
UnknownEraError,
DateAddError, DateError, DateFromFieldsError, EcmaReferenceYearError, LunisolarDateError,
MonthError, UnknownEraError,
};
use crate::options::{DateAddOptions, DateDifferenceOptions};
use crate::options::{DateFromFieldsOptions, Overflow};
Expand Down Expand Up @@ -135,13 +135,13 @@ impl DateFieldsResolver for Hebrew {
}

#[inline]
fn year_info_from_era(
fn extended_year_from_era_year(
&self,
era: &[u8],
era_year: i32,
) -> Result<Self::YearInfo, UnknownEraError> {
) -> Result<i32, UnknownEraError> {
match era {
b"am" => Ok(HebrewYear::compute(era_year)),
b"am" => Ok(era_year),
_ => Err(UnknownEraError),
}
}
Expand Down Expand Up @@ -290,7 +290,7 @@ impl Calendar for Hebrew {
date: &Self::DateInner,
duration: types::DateDuration,
options: DateAddOptions,
) -> Result<Self::DateInner, DateError> {
) -> Result<Self::DateInner, DateAddError> {
date.0.added(duration, self, options).map(HebrewDateInner)
}

Expand Down
14 changes: 8 additions & 6 deletions components/calendar/src/cal/hijri.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ use crate::calendar_arithmetic::ArithmeticDate;
use crate::calendar_arithmetic::DateFieldsResolver;
use crate::calendar_arithmetic::PackWithMD;
use crate::calendar_arithmetic::ToExtendedYear;
use crate::error::{DateError, DateFromFieldsError, EcmaReferenceYearError, UnknownEraError};
use crate::error::{
DateAddError, DateError, DateFromFieldsError, EcmaReferenceYearError, UnknownEraError,
};
use crate::options::DateFromFieldsOptions;
use crate::options::{DateAddOptions, DateDifferenceOptions};
use crate::types::DateFields;
Expand Down Expand Up @@ -844,22 +846,22 @@ impl<R: Rules> DateFieldsResolver for Hijri<R> {
}

#[inline]
fn year_info_from_era(
fn extended_year_from_era_year(
&self,
era: &[u8],
era_year: i32,
) -> Result<Self::YearInfo, UnknownEraError> {
) -> Result<i32, UnknownEraError> {
let extended_year = match era {
b"ah" => era_year,
b"bh" => 1 - era_year,
_ => return Err(UnknownEraError),
};
Ok(self.year_info_from_extended(extended_year))
Ok(extended_year)
}

#[inline]
fn year_info_from_extended(&self, extended_year: i32) -> Self::YearInfo {
debug_assert!(crate::calendar_arithmetic::VALID_YEAR_RANGE.contains(&extended_year));
debug_assert!(crate::calendar_arithmetic::GENEROUS_YEAR_RANGE.contains(&extended_year));
self.0.year(extended_year)
}

Expand Down Expand Up @@ -940,7 +942,7 @@ impl<R: Rules> Calendar for Hijri<R> {
date: &Self::DateInner,
duration: types::DateDuration,
options: DateAddOptions,
) -> Result<Self::DateInner, DateError> {
) -> Result<Self::DateInner, DateAddError> {
date.0.added(duration, self, options).map(HijriDateInner)
}

Expand Down
10 changes: 6 additions & 4 deletions components/calendar/src/cal/indian.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

use crate::calendar_arithmetic::ArithmeticDate;
use crate::calendar_arithmetic::DateFieldsResolver;
use crate::error::{DateError, DateFromFieldsError, EcmaReferenceYearError, UnknownEraError};
use crate::error::{
DateAddError, DateError, DateFromFieldsError, EcmaReferenceYearError, UnknownEraError,
};
use crate::options::DateFromFieldsOptions;
use crate::options::{DateAddOptions, DateDifferenceOptions};
use crate::types::DateFields;
Expand Down Expand Up @@ -69,11 +71,11 @@ impl DateFieldsResolver for Indian {
}

#[inline]
fn year_info_from_era(
fn extended_year_from_era_year(
&self,
era: &[u8],
era_year: i32,
) -> Result<Self::YearInfo, UnknownEraError> {
) -> Result<i32, UnknownEraError> {
match era {
b"shaka" => Ok(era_year),
_ => Err(UnknownEraError),
Expand Down Expand Up @@ -223,7 +225,7 @@ impl Calendar for Indian {
date: &Self::DateInner,
duration: types::DateDuration,
options: DateAddOptions,
) -> Result<Self::DateInner, DateError> {
) -> Result<Self::DateInner, DateAddError> {
date.0.added(duration, self, options).map(IndianDateInner)
}

Expand Down
Loading
Loading