Skip to content

feat(ts): translate Saturating<T> with its std impls - #343

Merged
kixelated merged 5 commits into
mainfrom
quest/m3/ts-saturating
Oct 31, 2026
Merged

kixelated merged 5 commits into
mainfrom
quest/m3/ts-saturating

Conversation

@kixelated

@kixelated kixelated commented Oct 31, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

std::num::Saturating<T> refused wherever it was made, as "constructing std::num::Saturating<u8> from another crate", so Saturating(a).cmp(&Saturating(b)) and every operator on one failed before reaching its impl. Closes quest/m3/ts-saturating.md.

Approach

Saturating<T> is its integer, as Wrapping<T> is (#326), and shares its lowering: library::overflowing returns a num::Overflow (Wrap or Saturate) with the integer, and num::overflowing picks wrapping* or saturating*. Every place that treated Wrapping as its value (type, construction, field, copy, drop, default, fmt, eq, cmp, hash, sets and maps) now treats Saturating the same.

Std's impls at nightly-2026-10-15, under library/core/src, and how each translates:

std translates as
num/saturating.rs:39 derives PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Default, Hash; pub T field the integer's eq, cmp, hash, and default; .0 and Saturating(x) are the value
num/saturating.rs:44-84 Debug, Display, Binary, Octal, LowerHex, UpperHex the integer's, by each trait
num/saturating.rs:86-214 shifts, all commented out none exist; num::overflowing refuses a Saturating shift with a span should one ever reach it
num/saturating.rs:217-331 Add, Sub, Mul, their assign forms, by Saturating and by T, and the & forwards rt.<int>SaturatingAdd, Sub, Mul
num/saturating.rs:350-384 Div is saturating_div (num/int_macros.rs:2145: MIN / -1 is MAX, zero panics) new rt.<int>SaturatingDiv for signed widths; unsigned is the integer's /
num/saturating.rs:388-424 Rem is the integer's % the checked %: panics on a zero divisor and on MIN % -1, as std's does
num/saturating.rs:426-554 Not, BitXor, BitOr, BitAnd, and assign forms the integer's
num/saturating.rs:1006-1017 Neg for signed widths is saturating_neg saturatingSub(0, a)
iter/traits/accum.rs:82-135, 207 Sum, Product, and their & forms, unsigned widths only a fold by saturatingAdd or saturatingMul from 0 or 1

Audit of siblings, fixed here: the integer's saturating_div and saturating_neg, and NonZero's saturating_add, saturating_mul, and saturating_neg, which refused as unsupported calls.

Proof: ts/fixtures/lang/src/saturates.rs runs 24 kinds over every width, signed and unsigned, 8 to 128 bits, against native wasm32. Operands are 0, 1, 3, both bounds, the sign bit and its neighbours (MIN / -1, MIN % -1), the quarter-range values whose products cross the bounds, and, at 128 bits, values past 2^64. A mutation check (making i8SaturatingDiv wrap at MIN / -1) failed the differential with 3 differences.

Impact

  • Translator output: Saturating<T> translates as T. rt.i8SaturatingDiv through rt.i128SaturatingDiv and rt.isizeSaturatingDiv are new runtime exports. Nothing breaks.

Alternatives

  • A separate Saturating path beside Wrapping's: duplicates every operator site, so it was dropped for the shared Overflow mode.

Follow-ups

  • quest/m3/ts-int-methods.md: inherent methods of integers, NonZero, Wrapping, and Saturating (pow, abs, count_ones, saturating_pow, wrapping_neg, ...) still refuse with a span; the reject fixture pins Saturating::pow.

Choices made without asking

  • Folded the integer and NonZero saturating methods from the audit into this PR, since each reuses the same runtime functions.

Review

(written by Claude Opus 5.5)

kixelated and others added 5 commits October 31, 2026 04:13
Quest-Claim: 092385aa9067e2f4
Saturating shares Wrapping's lowering through an Overflow mode. Integer
saturating_div and saturating_neg, and NonZero's saturating_add,
saturating_mul, and saturating_neg translate too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
# Conflicts:
#	ts/fixtures/lang/expected/harness.ts
#	ts/fixtures/lang/src/harness.rs
# Conflicts:
#	ts/fixtures/lang/expected/harness.ts
#	ts/fixtures/lang/src/harness.rs
#	ts/fixtures/reject/census.md
#	ts/fixtures/reject/expected.txt
#	ts/fixtures/reject/src/lib.rs
#	ts/fixtures/reject/ts.toml
@kixelated kixelated changed the title Saturating feat(ts): translate Saturating<T> with its std impls Oct 31, 2026
@kixelated
kixelated marked this pull request as ready for review October 31, 2026 12:11
@coderabbitai

coderabbitai Bot commented Oct 31, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 37 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: d0395f15-2153-4f8e-91ad-b37e3ee3b489
Commits

Reviewing files that changed from the base of the PR and between 84fccfe and 12d5bd4.

Files selected for processing: 29
  • quest/m3/README.md
  • quest/m3/ts-int-methods.md
  • quest/m3/ts-saturating.md
  • ts/README.md
  • ts/fixtures/lang/caller.ts
  • ts/fixtures/lang/expected/harness.ts
  • ts/fixtures/lang/expected/lib.ts
  • ts/fixtures/lang/expected/saturates.Gauge.ts
  • ts/fixtures/lang/expected/saturates.ts
  • ts/fixtures/lang/src/harness.rs
  • ts/fixtures/lang/src/lib.rs
  • ts/fixtures/lang/src/saturates.rs
  • ts/fixtures/reject/census.md
  • ts/fixtures/reject/expected.txt
  • ts/fixtures/reject/src/lib.rs
  • ts/fixtures/reject/ts.toml
  • ts/runtime/rt/int.ts
  • ts/src/lower/body.rs
  • ts/src/lower/collections.rs
  • ts/src/lower/copy.rs
  • ts/src/lower/drop.rs
  • ts/src/lower/fmt.rs
  • ts/src/lower/iter.rs
  • ts/src/lower/library.rs
  • ts/src/lower/mir/call.rs
  • ts/src/lower/mir/place.rs
  • ts/src/lower/mir/rvalue.rs
  • ts/src/lower/mod.rs
  • ts/src/lower/num.rs
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@kixelated
kixelated merged commit d75a02f into main Oct 31, 2026
2 checks passed
@kixelated
kixelated deleted the quest/m3/ts-saturating branch October 31, 2026 12:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant