Repository navigation
feat(ts): translate Saturating<T> with its std impls - #343
Merged
Merged
Conversation
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
marked this pull request as ready for review
October 31, 2026 12:11
|
Warning Review limit reachedYou'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. View limit details
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
std::num::Saturating<T>refused wherever it was made, as "constructingstd::num::Saturating<u8>from another crate", soSaturating(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, asWrapping<T>is (#326), and shares its lowering:library::overflowingreturns anum::Overflow(WraporSaturate) with the integer, andnum::overflowingpickswrapping*orsaturating*. Every place that treatedWrappingas its value (type, construction, field, copy, drop, default, fmt, eq, cmp, hash, sets and maps) now treatsSaturatingthe same.Std's impls at
nightly-2026-10-15, underlibrary/core/src, and how each translates:num/saturating.rs:39derivesPartialEq,Eq,PartialOrd,Ord,Clone,Copy,Default,Hash;pub Tfield.0andSaturating(x)are the valuenum/saturating.rs:44-84Debug,Display,Binary,Octal,LowerHex,UpperHexnum/saturating.rs:86-214shifts, all commented outnum::overflowingrefuses aSaturatingshift with a span should one ever reach itnum/saturating.rs:217-331Add,Sub,Mul, their assign forms, bySaturatingand byT, and the&forwardsrt.<int>SaturatingAdd,Sub,Mulnum/saturating.rs:350-384Divissaturating_div(num/int_macros.rs:2145:MIN / -1isMAX, zero panics)rt.<int>SaturatingDivfor signed widths; unsigned is the integer's/num/saturating.rs:388-424Remis the integer's%%: panics on a zero divisor and onMIN % -1, as std's doesnum/saturating.rs:426-554Not,BitXor,BitOr,BitAnd, and assign formsnum/saturating.rs:1006-1017Negfor signed widths issaturating_negsaturatingSub(0, a)iter/traits/accum.rs:82-135, 207Sum,Product, and their&forms, unsigned widths onlysaturatingAddorsaturatingMulfrom 0 or 1Audit of siblings, fixed here: the integer's
saturating_divandsaturating_neg, andNonZero'ssaturating_add,saturating_mul, andsaturating_neg, which refused as unsupported calls.Proof:
ts/fixtures/lang/src/saturates.rsruns 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 (makingi8SaturatingDivwrap atMIN / -1) failed the differential with 3 differences.Impact
Saturating<T>translates asT.rt.i8SaturatingDivthroughrt.i128SaturatingDivandrt.isizeSaturatingDivare new runtime exports. Nothing breaks.Alternatives
Saturatingpath besideWrapping's: duplicates every operator site, so it was dropped for the sharedOverflowmode.Follow-ups
NonZero,Wrapping, andSaturating(pow,abs,count_ones,saturating_pow,wrapping_neg, ...) still refuse with a span; the reject fixture pinsSaturating::pow.Choices made without asking
NonZerosaturating methods from the audit into this PR, since each reuses the same runtime functions.Review
quest review.collections.rs. That came from a stale base: the branch predated fix(ts): type a tuple's cmp as total #341, so the diff showed fix(ts): type a tuple's cmp as total #341 reversed. Merging origin/main resolved it.just ciinsidenix developpassed on 12d5bd4.(written by Claude Opus 5.5)