Skip to content

feat(ts): translate the rest of format specs - #136

Merged
kixelated merged 5 commits into
mainfrom
quest/m3/ts-fmt-specs
Oct 28, 2026
Merged

kixelated merged 5 commits into
mainfrom
quest/m3/ts-fmt-specs

Conversation

@kixelated

@kixelated kixelated commented Oct 28, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

rust2-ts refused every format spec beyond fill, alignment, width, #, 0, x, X, and ?. {:.N?} is the usual way to print a Duration (found by #125), and rust2 aims to support all of std. Closes the quest quest/m3/ts-fmt-specs.md.

Approach

Each spec goes in the runtime's port of core::fmt, not into per-type special cases:

  • Template decoding (src/lower/fmt.rs): reads the precision, +, {:x?}/{:X?}, and the indirect-count bits. A count from an argument (Count::Arg) becomes rt.fmtCountOf(arg) in the spec object. The - flag is decoded and dropped, because no std impl reads it.
  • Count arguments: Argument::from_usize lowers to rt.fmtCount(n). This panics with std's Formatting argument out of range past u16::MAX and returns an rt.Count: a Fmt that carries count. Because a Count is a Fmt, an arguments array that is not built in place stays typed as rt.Fmt[].
  • fmtPad cuts to the precision in chars before padding, as Formatter::pad does. bool, char, str, (), the error Displays, and a user's f.pad all get this.
  • fmtPadIntegral writes +.
  • Integers (rt/int.ts): each type gets Debug (Display, or hex under {:x?}/{:X?}), Binary, Octal, LowerExp, and UpperExp. Exp is a port of impl_Exp that works on decimal digit strings: it rounds half to even to the precision and carries into the exponent. Debug of an integer is now rt.u8Debug and the like instead of rt.u8Display.
  • Duration Debug (rt/time.ts): a port of fmt_decimal, covering precision, half-to-even rounding that carries into the whole part (up to 18446744073709551616s past u64::MAX), zero-fill past 9 digits, and +.
  • Formatting-trait dispatch (library.rs): std's impls of Octal, Binary, LowerExp, and UpperExp now route through fmt::std_impl, so a user impl that forwards to its field translates.

Only {:p} is still refused. The README and the reject fixture now say so.

Tests: the lang fixture adds every new spec on every integer width, strings and chars cut by a precision, derived Debug under {:x?}, {:#X?}, and {:+.1?}, user Binary/Octal/Exp impls, counts by position, by name, and via .*, evaluation order with counts, a count at and past u16::MAX, {:e} rounding ties and carries, and Duration precision ties. caller.ts's existing named cases cover u64::MAX seconds with 999,999,999 nanoseconds. I mutation-checked both half-to-even rules: flipping either one fails the differential test.

Impact

  • Translator output: these specs now translate instead of being refused. Debug of an integer calls rt.<int>Debug.
  • Runtime, additive only:
    • Spec gains plus, debugHex, and precision.
    • New rt.Count, rt.fmtCount, and rt.fmtCountOf.
    • New rt.<int>Debug, Binary, Octal, LowerExp, and UpperExp for every integer type.
  • Formatter::pad now honors a precision. That is a fix: it now matches std.

Nothing breaks, so there is no MIGRATE.md entry.

Choices made without asking

  • Count arguments are a Fmt that carries count (rt.Count). The alternative was widening the Argument type to Fmt | number, which would put casts on every array that is not built in place.
  • {:-} is accepted and ignored, as std ignores it.
  • Formatter::precision() and sign_plus() are not mapped because nothing needs them yet. A call to either is still refused with its span.

Alternatives

Lowering std's integer formatting from core's MIR was ruled out: it uses MaybeUninit buffers and unsafe code, which are outside the subset.

Follow-ups

  • Formatter::precision(), sign_plus(), fill(), and align(), for user impls that forward options. This needs a consumer first.
  • {:p} remains refused because JS has no addresses.

Checks

  • just check at the repo root: passes.
  • just ts test: all ts tests pass, including the 50,000-case lang differential.
  • The ffi tests were not run; this PR doesn't touch ffi.

(Written by Claude Opus 5.5)

🤖 Generated with Claude Code

kixelated and others added 2 commits October 27, 2026 17:12
Quest-Claim: a8a25f9a13039018
A precision, widths and precisions from arguments, `+`, `{:x?}` and `{:X?}`,
and `{:b}`, `{:o}`, `{:e}`, and `{:E}` now translate, byte for byte as
core::fmt writes them, proven by differential tests. Only `{:p}` is refused.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kixelated kixelated changed the title The rest of format specs feat(ts): translate the rest of format specs Oct 28, 2026
@kixelated
kixelated marked this pull request as ready for review October 28, 2026 01:57
@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-28T02:53:41.217343Z a42c6c4 New commits
🔒 Security Review ✅ Completed 2026-10-28T02:53:15.253994Z a42c6c4 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: 77224d0

No actionable bugs found in the reviewed changes. The shared formatting-runtime approach is sound: precision is applied before string padding, integer Debug retains hex options through nested formatting, and decimal-string rounding avoids losing wide-integer precision. Keeping count arguments callable avoids widening every existing formatting-argument array; that complexity is justified. The differential fixtures meaningfully cover indirect counts, evaluation order, rounding ties/carries, and Duration overflow into the next whole second.

Verification limits: static inspection of lowering, runtime helpers, generated fixtures and tests only. I did not run builds, tests, or independently verify the reported differential/mutation results. Pointer formatting and the documented Formatter accessor follow-ups remain outside this change.

# Conflicts:
#	ts/fixtures/lang/expected/harness.ts
#	ts/fixtures/lang/src/harness.rs
@kixelated

Copy link
Copy Markdown
Collaborator Author

Merged main in 2cb7de4. ts/fixtures/lang/src/harness.rs conflicted with #140, which added iters::skips as a new dispatch arm. Resolved to op % 54: refs at 50, this PR's fmt::counts and fmt::rounding at 51 and 52, and skips as the fallback. I regenerated expected/harness.ts from the translator. just ts test and just check both pass.

(Written by Claude Opus 5.5)

@kixelated

Copy link
Copy Markdown
Collaborator Author

Merge summary:

  • Merged main twice. The first merge needed a hand resolution in the lang harness dispatch against #140, described above. The second merged cleanly. Both merges were reviewed by Codex with no findings.
  • Fixed a grammar slip in ts/README.md's "Not yet" list, in the {:p} entry.
  • #135 is still open, and its floats.md links to the quest this PR deletes. feat(ts): translate Duration's compound assignment, larger units, and Sum #135's merge agent drops that link.
  • just check, just ts test, CI, and quest check --base origin/main all pass on a42c6c4.

(Written by Claude Opus 5.5)

@kixelated
kixelated merged commit 903e459 into main Oct 28, 2026
1 check passed
@kixelated
kixelated deleted the quest/m3/ts-fmt-specs branch October 28, 2026 03:26
kixelated added a commit that referenced this pull request Oct 28, 2026
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>
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