Skip to content

Commit 9d71d88

Browse files
authored
Reject on empty fields object in PlainYearMonth::with (#626)
Small fix for a bug in `PlainYearMonth::with`. This can still be handled downstream in the engines, if there is careful attention to the specification steps. We should still reject here to fix bug in Rust API.
1 parent 86c213a commit 9d71d88

1 file changed

Lines changed: 10 additions & 7 deletions

File tree

src/builtins/core/plain_year_month.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,11 @@ impl PlainYearMonth {
542542
// 4. Let calendar be yearMonth.[[Calendar]].
543543
// 5. Let fields be ISODateToFields(calendar, yearMonth.[[ISODate]], year-month).
544544
// 6. Let partialYearMonth be ? PrepareCalendarFields(calendar, temporalYearMonthLike, « year, month, month-code », « », partial).
545+
if fields.is_empty() {
546+
return Err(
547+
TemporalError::r#type().with_message("plainYearMonth fields cannot be empty")
548+
);
549+
}
545550
// 7. Set fields to CalendarMergeFields(calendar, fields, partialYearMonth).
546551
// 8. Let resolvedOptions be ? GetOptionsObject(options).
547552
// 9. Let overflow be ? GetTemporalOverflowOption(resolvedOptions).
@@ -942,19 +947,17 @@ mod tests {
942947
); // assert month code has changed
943948
assert_eq!(with_month_code.iso_month(), 5); // month is changed as well
944949

945-
// Day
946-
let fields = YearMonthCalendarFields::new();
947-
let with_day = base.with(fields, None).unwrap();
948-
assert_eq!(with_day.iso_year(), 2025); // year is not changed
949-
assert_eq!(with_day.iso_month(), 3); // month is not changed
950-
assert_eq!(with_day.iso.day, 1); // day is ignored
951-
952950
// All
953951
let fields = YearMonthCalendarFields::new().with_year(2001).with_month(2);
954952
let with_all = base.with(fields, None).unwrap();
955953
assert_eq!(with_all.iso_year(), 2001); // year is changed
956954
assert_eq!(with_all.iso_month(), 2); // month is changed
957955
assert_eq!(with_all.iso.day, 1); // day is ignored
956+
957+
// Empty fields -> throws error
958+
let fields = YearMonthCalendarFields::new();
959+
let empty = base.with(fields, None);
960+
assert!(empty.is_err());
958961
}
959962

960963
#[test]

0 commit comments

Comments
 (0)