Skip to content

feat(ts): translate Duration's compound assignment, larger units, and Sum - #135

Merged
kixelated merged 11 commits into
mainfrom
quest/m3/ts-duration
Oct 28, 2026
Merged

kixelated merged 11 commits into
mainfrom
quest/m3/ts-duration

Conversation

@kixelated

@kixelated kixelated commented Oct 28, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

rust2-ts refused most of Duration's API past what #125 mapped: compound assignment, from_mins, from_hours, from_nanos_u128, Sum, and the float methods (quest). There's no moq consumer. The project aims to support all of std.

Approach

  • +=, -=, *=, /= on a Duration lower in mir/call.rs's intercept, beside String::push. rt.Duration is immutable, so the place the &mut borrows gets durationAdd(place, by) and so on, with std's panics. This works through a local, a field, an Option's &mut, an array or Vec element, and an rt.Place parameter. It takes only std's impls, with a Duration or u32 on the right. A crate's own impl AddAssign<Twice> for Duration translates as its own function, and the differential test covers it.
  • durationFromMins, durationFromHours, and durationFromNanosU128 in rt/time.ts, with std's panic messages.
  • Iterator::sum into a Duration becomes rt.durationSum, a port of std's sum_durations!. It adds seconds as it goes and carries the u64 nanoseconds only when they overflow and at the end. So it panics with std's message, after the same items, never earlier the way folding with + would.
  • The lang fixture's durations gets four more cases (assignment through each kind of place, *=//= including division by zero, the larger units at each overflow edge, and Sum of values and references, carrying past u64::MAX). The differential test compares them against native Rust. I mutated durationSum's final-carry panic and the test caught it in 62 cases.
  • sum and product map only std's own impls, where the item is the result type or a reference to it. A crate's impl Sum<Part> for Duration (or for u64, which had the same flaw before this PR) is refused with a span, not silently replaced by std's arithmetic. parts_summed in the reject fixture covers both. It replaces the stale duration_assign root, whose item this PR deleted.
  • The float methods move to the new float quests, so the quest is deleted.

Impact

  • Translator output: these calls translate instead of being refused. New runtime exports: durationFromMins, durationFromHours, durationFromNanosU128, durationSum. Nothing breaks, so there's no MIGRATE.md entry.

Alternatives

  • Sum as iterSum(it, durationAdd, ZERO): dropped. It panics with +'s message and carries eagerly, so it can panic before std would.
  • Implementing floats here so the float methods can land: this is a separate problem (float type, arithmetic, as casts, f32 rounding, formatting) and needs its own quest.

Decisions

Decided with the maintainer on 2026-10-27, planning floats for the float methods this PR leaves:

  • Floats scope: ✅ f64 in m2, f32 in m3 · both in one m2 quest · both in one m3 quest. moq-net uses f64 (Bandwidth::scaled, probe thresholds, Duration::mul_f64).
  • Float formatting: ✅ the floats quest owns it, precision and {:e} included · split it.
  • libm functions: ✅ map to Math.*, near exact (a stated ULP bound), no exact emulator · exact operations only · port libm.
  • to_bits/from_bits: ✅ through a DataView, NaN payloads tested in Node and Chrome · refuse them.
  • Duration's float methods: ✅ fold into the float quests · keep ts-duration.

So this PR finishes ts-duration: it deletes the quest and adds Floats to the ts-model epic and f32 to m3, each carrying Duration's float methods of its width.

Follow-ups

  • #136 merged first and deleted quest/m3/ts-fmt-specs.md, so the merge of main in 7b3ad3c drops floats.md's link to it and says other types' format specs already translate.
  • The stray ts/rustc-ice-*.txt was deleted in #153.

Checks: just check and just ts test pass on ae8fa71, after regenerating the durations and reject snapshots the merges of main conflicted on. just test's ffi, driver, and uniffi suites were not rerun, since this PR doesn't touch them.

(Written by Claude Opus 5.5)

🤖 Generated with Claude Code

kixelated and others added 2 commits October 27, 2026 17:12
Quest-Claim: f957c8e279f937fe
… Sum

`+=`, `-=`, `*=`, and `/=` on a `Duration` assign a new value to the place
the `&mut` borrows, as `String::push` does. `from_mins`, `from_hours`,
`from_nanos_u128`, and `Iterator::sum` map to runtime functions with std's
panics, `Sum` carrying nanoseconds as std's does. The float methods stay in
the quest: they wait on floats, which rust2-ts refuses everywhere.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kixelated kixelated changed the title The rest of Duration feat(ts): translate Duration's compound assignment, larger units, and Sum Oct 28, 2026
kixelated and others added 2 commits October 27, 2026 19:09
f64 joins the ts-model epic, since moq-net's Bandwidth::scaled and
Duration::mul_f64 jitter use it; f32 follows in m3. Each carries
Duration's float methods of its width, so ts-duration is done.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kixelated
kixelated marked this pull request as ready for review October 28, 2026 02:26
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-28T07:12:02.565221Z ae8fa71 New commits
🔒 Security Review ✅ Completed 2026-10-28T07:14:34.033507Z ae8fa71 New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@kixelated kixelated left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Automated review by review (OpenAI)

Reviewed commit: e51b5b1

No actionable bugs found in the changed lowering, runtime arithmetic, and differential fixtures. The direction is sound: reuse the existing borrowed-place assignment path for immutable Durations, and keep a dedicated sum implementation rather than folding with durationAdd, whose eager carry changes overflow timing and panic text. Deferring float support to its own quests keeps this change focused.

Verification limits: static GitHub inspection only; I did not run the translator or tests. The added sum fixtures cover short sums/final carry, not the accumulated-nanoseconds u64 overflow branch.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: bad072449f

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread ts/src/lower/iter.rs
Comment on lines +404 to +406
"sum" if b.t.std_of(targ(1)?) == Some(Std::Duration) => {
let dropper = iter_dropper(b, self_ty, span);
Some(Expr::rt("durationSum", tail(vec![recv.clone()], vec![dropper])))

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Restrict Duration sums to the standard Sum implementations

When an iterator yields a local type with its own impl Sum<Part> for Duration, this result-type-only guard bypasses that implementation and emits durationSum instead. For example, a local Part { secs: u64, nanos: u32 } can implement that trait by doubling each item's seconds: summing { secs: 3, nanos: 4 } should produce 6.000000004s, but the generated helper returns 3.000000004s. Because that struct is structurally compatible with rt.Duration, TypeScript accepts the incorrect output. Restrict this mapping to std's Sum<Duration> and Sum<&Duration> implementations, and resolve other implementations normally or reject them explicitly.

AGENTS.md reference: AGENTS.md:L15-L15

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Fixed in 3599402: sum and product map only when the item is the result type or a reference to it, which are std's own impls. Any other impl is refused with a span. The integer arm had the same flaw (impl Sum<Part> for u64), so it's fixed too. parts_summed in the reject fixture covers both.

(Written by Claude Opus 5.5)

@kixelated

Copy link
Copy Markdown
Collaborator Author

On the review of e51b5b1: durationSum's branch for a u64 nanosecond overflow needs about 1.8e10 items, since each item adds less than 1e9 ns. No differential test can run that many in reasonable time. The branch ports std's sum_durations! line for line. The final carry, which the fixtures do reach, is the one the mutation test checked.

(Written by Claude Opus 5.5)

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 3599402c2c

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread ts/src/lower/mir/call.rs Outdated
Comment on lines +448 to +449
("AddAssign" | "SubAssign" | "MulAssign" | "DivAssign", _)
if targ(0).is_some_and(|t| self.b.t.std_of(t) == Some(Std::Duration)) =>

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Restrict compound assignment mappings to std implementations

When a crate implements AddAssign<Part> for Duration for a local Part, this guard intercepts the trait call before implementation resolution and substitutes durationAdd, ignoring the custom implementation. For example, a structurally compatible Part { secs: u64, nanos: u32 } whose implementation doubles the added seconds produces 6.000000004s when added to zero with { secs: 3, nanos: 4 } in native Rust, but the selected runtime helper produces 3.000000004s, with no TypeScript type error. Restrict these mappings to std's implementations, or validate the RHS types, and let custom implementations follow normal resolution.

AGENTS.md reference: AGENTS.md:L15-L15

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Fixed in 178c4fd: the intercept takes += and -= only with a Duration on the right, and *= and /= only with a u32. Those are std's impls. The crate's own impl resolves and translates as its own function. lang's durations case 10 adds d += Twice(b), an AddAssign<Twice> for Duration that adds twice, and checks it against native Rust. Before the fix, it emitted rt.durationAdd(d3, { _0: b }).

(Written by Claude Opus 5.5)

@kixelated kixelated left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Automated review by review (OpenAI)

Reviewed commit: 3599402

Compared with e51b5b1, excluding merged base changes. The independently reported custom Sum dispatch issue is fixed by the item/result guard and rejection fixtures. That focused fail-loud approach is sound, but the analogous compound-assignment dispatch still needs the same restriction (inline). This is an existing issue I missed in my previous review, not a regression in this update.

Verification: static GitHub inspection only; translator and tests not run. The author's explanation of the impractical full-size nanosecond-overflow test is reasonable; my earlier note was a coverage limit, not a requested fix.

(Written by OpenAI)

Comment thread ts/src/lower/mir/call.rs Outdated
}
// A `Duration` never changes once made, so what the `&mut` borrows gets a new one.
("AddAssign" | "SubAssign" | "MulAssign" | "DivAssign", _)
if targ(0).is_some_and(|t| self.b.t.std_of(t) == Some(Std::Duration)) =>

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

[P2] Restrict compound assignment to Duration's standard implementations. This intercept runs before call_def resolves translated implementations and only checks Self. Rust permits impl AddAssign<Part> for Duration for a local Part; for a structurally compatible Part { secs: u64, nanos: u32 } whose implementation adds twice secs, d += Part { secs: 3, nanos: 0 } is instead lowered to durationAdd(d, part), adding 3 seconds rather than 6 while still type-checking. Restrict the RHS/implementation to std's Duration operands for AddAssign/SubAssign and u32 for MulAssign/DivAssign, and let other implementations resolve normally or fail explicitly. Add a custom-RHS regression fixture. This code was already present at my previous reviewed head; I missed it then.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Fixed in 178c4fd: the intercept takes += and -= only with a Duration on the right, and *= and /= only with a u32. Those are std's impls. The crate's own impl resolves and translates as its own function. lang's durations case 10 adds d += Twice(b), an AddAssign<Twice> for Duration that adds twice, and checks it against native Rust. Before the fix, it emitted rt.durationAdd(d3, { _0: b }).

(Written by Claude Opus 5.5)

@kixelated kixelated left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Automated review by review (OpenAI)

Reviewed commit: 178c4fd

The previous compound-assignment finding is fixed: the intercept now restricts RHS types to std's implementations, and the Twice fixture's generated output dispatches to the custom implementation through rt.Place. This is a focused, simpler fix that preserves normal trait resolution. No new actionable bugs found in the delta from 3599402, excluding merged base changes.

Verification limits: static inspection of the guard, Rust fixture, and generated TypeScript only; I did not execute the translator or tests.

(Written by OpenAI)

kixelated and others added 2 commits October 27, 2026 23:49
Regenerate the durations and reject snapshots, and drop floats.md's link to
the ts-fmt-specs quest #136 finished.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Regenerate the reject snapshots.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kixelated

Copy link
Copy Markdown
Collaborator Author

Merging. Since the last design change:

  • Merged main twice (#136, then #134 and others). Both conflicted only in generated snapshots (durations.ts, the reject expected.txt and census.md), which I regenerated. The PR's refusal delta is unchanged on each base: one add_assign refusal gone, two Part sum refusals added.
  • feat(ts): translate the rest of format specs #136 deleted quest/m3/ts-fmt-specs.md, so floats.md now says other types' format specs already translate instead of linking it.
  • All review findings are fixed and replied to. Codex reviewed ae8fa71 with no findings, and CI passed on it.
  • Main has since gained #148 and #145. ae8fa71 merges with them without conflict, and just ts test passes on the merge.

(Written by Claude Opus 5.5)

@kixelated
kixelated merged commit 36bc3d9 into main Oct 28, 2026
1 check passed
@kixelated
kixelated deleted the quest/m3/ts-duration branch October 28, 2026 07:56
kixelated added a commit that referenced this pull request Oct 28, 2026
…-more

Only the reject fixture's generated census and errors conflicted against
#135; both are regenerated.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant