Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
16 changes: 14 additions & 2 deletions crates/perry-hir/src/lower/expr_call/url_date_instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,8 +313,20 @@ pub(super) fn try_url_date_weakref_instance(
return Ok(Ok(Expr::DateToLocaleTimeString(Box::new(date_expr))));
}
"toLocaleString" => {
let date_expr = lower_expr(ctx, &member.obj)?;
return Ok(Ok(Expr::DateToLocaleString(Box::new(date_expr))));
// When args (locale / options) are present and the
// receiver is not statically a Date, skip the
// DateToLocaleString fold so the arguments are
// preserved on the generic method-call path. Temporal
// types need this: their dispatch layer already accepts
// locale and options arguments correctly, but the fold
// was silently dropping them (see #5580). Zero-arg
// calls and statically-Date receivers still use the
// fast path to preserve existing Date behaviour.
if args.is_empty() || recv_class == Some("Date") {
let date_expr = lower_expr(ctx, &member.obj)?;
return Ok(Ok(Expr::DateToLocaleString(Box::new(date_expr))));
Comment on lines 315 to +327

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo '== Expr::DateToLocaleString codegen contract =='
ast-grep outline crates/perry-codegen/src/expr/env_clones.rs --match DateToLocaleString --view expanded || true
sed -n '72,113p' crates/perry-codegen/src/expr/env_clones.rs

echo
echo '== Date/toLocaleString runtime dispatch sites =='
rg -n -C4 'DateToLocaleString|js_date_to_locale_string|toLocaleString' \
  crates/perry-hir/src \
  crates/perry-runtime/src \
  crates/perry-codegen/src

Repository: PerryTS/perry

Length of output: 50370


🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo '== lowering branch =='
sed -n '300,335p' crates/perry-hir/src/lower/expr_call/url_date_instance.rs

echo
echo '== codegen for DateToLocaleString =='
ast-grep outline crates/perry-codegen/src/object/native_call_method/object_proto.rs --match DateToLocaleString --view expanded || true
sed -n '160,175p' crates/perry-codegen/src/object/native_call_method/object_proto.rs

echo
echo '== runtime Date toLocaleString helper =='
rg -n -C3 'js_date_to_locale_string|toLocaleString" =>|DateToLocaleString' crates/perry-runtime/src/object/native_call_method/object_proto.rs crates/perry-runtime/src/date.rs crates/perry-runtime/src/object -g '!**/target/**'

echo
echo '== date locale helpers signatures =='
rg -n -C2 'pub fn js_date_to_locale_string|pub fn js_number_to_locale_string|toLocaleString' crates/perry-runtime/src/date.rs crates/perry-runtime/src -g '!**/target/**'

Repository: PerryTS/perry

Length of output: 2674


🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo '== variant consumers =='
rg -n -C4 'Expr::DateToLocaleString|DateToLocaleString\(' crates/perry-* -g '!**/target/**'

echo
echo '== lowering branch context =='
sed -n '300,335p' crates/perry-hir/src/lower/expr_call/url_date_instance.rs

echo
echo '== native call dispatch for Date/Temporal toLocaleString =='
sed -n '60,110p' crates/perry-runtime/src/object/native_call_method/object_proto.rs

echo
echo '== any helper that accepts locale/options for Date toLocaleString =='
rg -n -C3 'js_date_to_locale_string\(|date_to_locale_string|temporal_locale_string|toLocaleString\("' crates/perry-runtime/src -g '!**/target/**'

Repository: PerryTS/perry

Length of output: 32517


Drop the static-Date fast path when args are present
recv_class == Some("Date") still sends date.toLocaleString(locales, options) through Expr::DateToLocaleString, and that variant only carries the receiver. Keep the fast path only for zero-arg calls so explicit locale/options on known Date values don’t get discarded.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@crates/perry-hir/src/lower/expr_call/url_date_instance.rs` around lines 315 -
327, The toLocaleString lowering in the url_date_instance expression path is
still routing statically known Date receivers with locale/options through the
DateToLocaleString fast path, which drops the arguments. Update the
"toLocaleString" branch in the expr-call lowering logic to use
Expr::DateToLocaleString only for zero-argument calls, and for any call with
args preserve the generic method-call path even when recv_class is Some("Date").

}
// fall through to generic method call with args intact
}
"getTimezoneOffset" => {
let date_expr = lower_expr(ctx, &member.obj)?;
Expand Down
109 changes: 50 additions & 59 deletions crates/perry-runtime/src/intl/date_collator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,45 +388,49 @@ fn format_components(
let has_time = hour_opt.is_some() || minute_opt.is_some() || second_opt.is_some();

let date_part = if has_date {
let has_m = month_opt.is_some();
let has_d = day_opt.is_some();
let has_y = year_opt.is_some();
let fmt_month = match month_opt {
Some("long") => MONTH_FULL[month.saturating_sub(1).min(11) as usize].to_string(),
Some("short") | Some("narrow") => {
MONTH_ABBR[month.saturating_sub(1).min(11) as usize].to_string()
}
Some("2-digit") => format!("{:02}", month),
_ => month.to_string(),
Some(_) => month.to_string(), // "numeric" or unrecognised
None => String::new(), // absent — do NOT leak the raw month
};
let fmt_day = match day_opt {
Some("2-digit") => format!("{:02}", day),
_ if day_opt.is_some() => day.to_string(),
_ => String::new(),
Some(_) => day.to_string(),
None => String::new(),
};
let fmt_year = match year_opt {
Some("2-digit") => format!("{:02}", year.rem_euclid(100)),
_ if year_opt.is_some() => year.to_string(),
_ => String::new(),
Some(_) => year.to_string(),
None => String::new(),
};
// Use named-month format for long/short/narrow, numeric M/D/YYYY otherwise.
// Named-month styles (long/short/narrow) use word-first layout.
// Numeric styles assemble the present fields with "/" separators —
// only fields whose option is Some appear in the output.
match month_opt {
Some("long") | Some("short") | Some("narrow") => {
let has_y = year_opt.is_some();
let has_d = day_opt.is_some();
Some(match (has_d, has_y) {
(true, true) => format!("{} {}, {}", fmt_month, fmt_day, fmt_year),
(true, false) => format!("{} {}", fmt_month, fmt_day),
(false, true) => format!("{} {}", fmt_month, fmt_year),
(false, false) => fmt_month,
})
}
Some("long") | Some("short") | Some("narrow") => Some(match (has_d, has_y) {
(true, true) => format!("{} {}, {}", fmt_month, fmt_day, fmt_year),
(true, false) => format!("{} {}", fmt_month, fmt_day),
(false, true) => format!("{} {}", fmt_month, fmt_year),
(false, false) => fmt_month,
}),
_ => {
let has_y = year_opt.is_some();
let has_d = day_opt.is_some();
Some(match (has_d, has_y) {
(true, true) => format!("{}/{}/{}", fmt_month, fmt_day, fmt_year),
(true, false) => format!("{}/{}", fmt_month, fmt_day),
(false, true) => format!("{}/{}", fmt_month, fmt_year),
(false, false) => fmt_month,
})
// Build "M/D/YYYY" using only the fields that are present.
let parts: Vec<&str> = [
if has_m { fmt_month.as_str() } else { "" },
if has_d { fmt_day.as_str() } else { "" },
if has_y { fmt_year.as_str() } else { "" },
]
.into_iter()
.filter(|s| !s.is_empty())
.collect();
Some(parts.join("/"))
}
}
} else {
Expand Down Expand Up @@ -652,9 +656,28 @@ pub(crate) fn temporal_locale_string(
second_opt.as_deref(),
)
} else {
// No options given — apply spec defaults for this Temporal type.
// No options given — apply ECMA-402 defaults for this Temporal
// type. The spec says `toLocaleString(locales, options)` is
// equivalent to `new Intl.DateTimeFormat(locales, options).format(this)`.
// With `options = undefined`, DTF always resolves to
// `{ year: "numeric", month: "numeric", day: "numeric" }`.
// Each Temporal type maps its fields to an epoch-ms value for
// formatting; the date components of that value are what the DTF
// defaults select. Type-specific component overrides (e.g.
// time for PlainTime) only apply when the user explicitly passes
// options — not as defaults.
//
// ZonedDateTime is the sole exception: its toLocaleString spec
// mandates that the output includes both date and time components
// when no options are given (analogous to how ZDT carries a
// timezone that no ordinary DTF object can represent).
match ctx {
TemporalLocaleCtx::PlainDate => (
TemporalLocaleCtx::PlainDate
| TemporalLocaleCtx::PlainDateTime
| TemporalLocaleCtx::Instant
| TemporalLocaleCtx::PlainTime
| TemporalLocaleCtx::PlainYearMonth
| TemporalLocaleCtx::PlainMonthDay => (
None,
None,
Some("numeric"),
Expand All @@ -664,9 +687,7 @@ pub(crate) fn temporal_locale_string(
None,
None,
),
TemporalLocaleCtx::PlainDateTime
| TemporalLocaleCtx::Instant
| TemporalLocaleCtx::ZonedDateTime => (
TemporalLocaleCtx::ZonedDateTime => (
None,
None,
Some("numeric"),
Expand All @@ -676,36 +697,6 @@ pub(crate) fn temporal_locale_string(
Some("2-digit"),
Some("2-digit"),
),
TemporalLocaleCtx::PlainTime => (
None,
None,
None,
None,
None,
Some("numeric"),
Some("2-digit"),
Some("2-digit"),
),
TemporalLocaleCtx::PlainYearMonth => (
None,
None,
Some("numeric"),
Some("numeric"),
None,
None,
None,
None,
),
TemporalLocaleCtx::PlainMonthDay => (
None,
None,
None,
Some("numeric"),
Some("numeric"),
None,
None,
None,
),
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,11 @@ pub(crate) unsafe fn js_object_default_to_locale_string(receiver: f64) -> f64 {
}
// #5580: a `Temporal.*` value formats via its own `toLocaleString` (a
// calendar-aware, type-specific rendering plus the spec's calendar-mismatch
// `RangeError`) rather than the `[object Object]` default. The HIR lowers
// `value.toLocaleString(...)` to the arg-less `Expr::DateToLocaleString`, so
// the locale/options arguments are not visible here; the no-argument form is
// dispatched through the Temporal method router.
// `RangeError`) rather than the `[object Object]` default. Calls that
// carry locale/options args bypass `Expr::DateToLocaleString` and reach
// the Temporal dispatch via the generic method-call path (which preserves
// args). Only the zero-arg form arrives here; dispatch it with an empty
// slice so the Temporal method applies its type-appropriate defaults.
#[cfg(feature = "temporal")]
if crate::temporal::is_temporal_value(receiver) {
return crate::temporal::dispatch::call_method(receiver, "toLocaleString", &[]);
Expand Down
Loading