Repository navigation
feat(ts): translate Duration's compound assignment, larger units, and Sum - #135
Conversation
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>
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>
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
kixelated
left a comment
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
💡 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".
| "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]))) |
There was a problem hiding this comment.
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 👍 / 👎.
There was a problem hiding this comment.
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)
…root Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
On the review of e51b5b1: (Written by Claude Opus 5.5) |
There was a problem hiding this comment.
💡 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".
| ("AddAssign" | "SubAssign" | "MulAssign" | "DivAssign", _) | ||
| if targ(0).is_some_and(|t| self.b.t.std_of(t) == Some(Std::Duration)) => |
There was a problem hiding this comment.
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 👍 / 👎.
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
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)
| } | ||
| // 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)) => |
There was a problem hiding this comment.
[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.
There was a problem hiding this comment.
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)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
kixelated
left a comment
There was a problem hiding this comment.
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)
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>
|
Merging. Since the last design change:
(Written by Claude Opus 5.5) |
…-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>
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 aDurationlower inmir/call.rs's intercept, besideString::push.rt.Durationis immutable, so the place the&mutborrows getsdurationAdd(place, by)and so on, with std's panics. This works through a local, a field, anOption's&mut, an array orVecelement, and anrt.Placeparameter. It takes only std's impls, with aDurationoru32on the right. A crate's ownimpl AddAssign<Twice> for Durationtranslates as its own function, and the differential test covers it.durationFromMins,durationFromHours, anddurationFromNanosU128inrt/time.ts, with std's panic messages.Iterator::suminto aDurationbecomesrt.durationSum, a port of std'ssum_durations!. It adds seconds as it goes and carries theu64nanoseconds 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.langfixture'sdurationsgets four more cases (assignment through each kind of place,*=//=including division by zero, the larger units at each overflow edge, andSumof values and references, carrying pastu64::MAX). The differential test compares them against native Rust. I mutateddurationSum's final-carry panic and the test caught it in 62 cases.sumandproductmap only std's own impls, where the item is the result type or a reference to it. A crate'simpl Sum<Part> for Duration(orfor u64, which had the same flaw before this PR) is refused with a span, not silently replaced by std's arithmetic.parts_summedin the reject fixture covers both. It replaces the staleduration_assignroot, whose item this PR deleted.Impact
durationFromMins,durationFromHours,durationFromNanosU128,durationSum. Nothing breaks, so there's noMIGRATE.mdentry.Alternatives
SumasiterSum(it, durationAdd, ZERO): dropped. It panics with+'s message and carries eagerly, so it can panic before std would.ascasts,f32rounding, formatting) and needs its own quest.Decisions
Decided with the maintainer on 2026-10-27, planning floats for the float methods this PR leaves:
f64in m2,f32in m3 · both in one m2 quest · both in one m3 quest. moq-net usesf64(Bandwidth::scaled, probe thresholds,Duration::mul_f64).{:e}included · split it.Math.*, near exact (a stated ULP bound), no exact emulator · exact operations only · port libm.to_bits/from_bits: ✅ through aDataView, NaN payloads tested in Node and Chrome · refuse them.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
quest/m3/ts-fmt-specs.md, so the merge of main in 7b3ad3c dropsfloats.md's link to it and says other types' format specs already translate.ts/rustc-ice-*.txtwas deleted in #153.Checks:
just checkandjust ts testpass on ae8fa71, after regenerating thedurationsandrejectsnapshots 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