Skip to content

feat(ts): lower Option's and Result's combinators from core's MIR - #129

Merged
kixelated merged 13 commits into
mainfrom
quest/m2/ts-model/core-mir
Oct 27, 2026
Merged

kixelated merged 13 commits into
mainfrom
quest/m2/ts-model/core-mir

Conversation

@kixelated

@kixelated kixelated commented Oct 27, 2026 •

Copy link
Copy Markdown
Collaborator

Closes the core MIR quest of the model epic.

Problem

Every Option and Result combinator was a hand-written runtime function plus a mapping in library.rs, each one re-deriving what core's drops and unwinding do (discards), and every combinator without one was refused: moq-net's census refused these calls 1,066 times. Consumer: moq-net's model (ts-model).

Approach

  • Lower them from core's MIR, as the crate's own functions: Translator::lowers(instance) replaces the did.is_local() checks at the call, function-value, and translated-function sites, and library::from_mir picks the std functions it covers: Option's and Result's methods, and their Clone, unless they take a &mut or raw pointer, are unsafe, or library maps them as one expression or the runtime's panic (is_some, as_ref, copied, unwrap, expect, is_ok, ...). A Clone that only copies stays the copy. The mappings and runtime functions this replaces are deleted (optionMap, optionMapOr, optionUnwrapOr, optionOkOr, result{Ok,Err,Map,MapErr,AndThen,InspectErr,UnwrapOr}).
  • Build std from source, unwinding: the wasm32 sysroot aborts, so core's MIR has no cleanups. The CLI passes -Zbuild-std=core,alloc,std,panic_unwind,panic_abort and -Cpanic=unwind -Zalways-encode-mir on core, alloc, and std through profile.dev.package.*.rustflags, which cargo's fingerprint sees; CARGO_UNSTABLE_PROFILE_RUSTFLAGS goes to cargo clean too, since Check now gives both the same environment. panic_abort keeps aborting for cdylib dependencies. A std crate built to abort is refused when one of its bodies lowers, so a --release or prebuilt std can't silently lose drops. rust-src vendors std's dependencies, so this works offline.
  • Fail loud inside core: a refusal in a std body points at the crate's call that first reached it, with , in \std::option::Option::::map`. A std body that changes anything in place (assignment through a projection, &mutborrow) is refused, since the crate's write analysis reads only the crate's bodies; so is one with plain integerAdd/Sub/Mul`, which core's MIR wraps where the translator checks.
  • Output: std's instances go under their crate's module path, core/option.ts and core/result.ts (module_of for any crate, which crates reuses). One instance per closure keeps each call site monomorphic; Program::merge then merges the instances of one std function whose output is identical into the first in sort order, iterating as merges make callers identical.
  • Reuse a rebuilt Result: Ok(x) built from (r as Ok).0 of a stable r is r itself (map_err's Ok, and_then's Err), since a write into an rt.Result's payload replaces the result. The spike measured Node 1.31x slower without it.

Proof:

  • lang gains a combinators case (differential against wasm32, 50,000 cases): is_some_and, is_none_or, and_then, filter, or_else, map_or_else, xor, and, or, zip, inspect, ok_or_else, is_ok_and, is_err_and, inspect_err, as_ref, copied, unwrap_or_default, Clone of Option<String> and Result<Vec<u8>, String> changed in place after, and panics inside closures std calls. Every closure logs its call, so laziness and call counts are compared.
  • task's options drop scenario gains panic points 6 and 7 inside filter's and and_then's closures, plus zip, or_else, is_some_and, map_or_else, and Clone of guards, through the differential test and the host unwinding test. Stripping std's cleanups on purpose fails the unwinding test (104 of 5,114 scripts differ).
  • A closure Option::map drops on None, as Result::map_err does on Ok and Result::and_then on Err: feat(ts): drop what std's iterators own #101 listed these in discards by hand. Lowered from core's MIR, the closure's drop is core's own (drop(_2) under a drop flag), so those entries go with the mappings. It is refused at the crate's call site with , in the std function until closures can drop (#115); reject's discarded_closure pins all three, with and_then added. A differential test of them belongs with feat(ts): drop what closures own #115, which makes them translate.
  • A Copy payload rebuilt into another Result and changed in place there leaves the original's, through core's map_err and the crate's own match (lang's combinators, from review).

Census and bench:

  • moq-net's census: refused calls to Option's and Result's methods fall from 1,066 to 126. The rest are &mut methods (get_or_insert, replace, insert), PartialEq/From impls, and std calls a core body reaches (Default::default in unwrap_or_default, Clone::clone of a payload). Total refusals fall from 11,185 to 10,581 and kio's from 341 to 332; bodies that now lower surface what they reach, as rows that end in a std path.
  • just ts bench against main, measured before merging feat(ts): drop what std's iterators own #101, fix(ts): fold matches an enclosing match decided #110, and feat(ts): translate Duration as std's seconds and nanoseconds #125: the codec bundle is 31,739 / 9,920 bytes (raw / gzip) against 31,729 / 9,943, the async handoff 14,346 / 3,424 against 14,341 / 3,423. Codec and handoff timings sit within the A/A route's spread in Node, Bun, Chromium, and Firefox (handoff depth 16: 1060/872/573/1160 against main's 1110/797/612/1140, A/A 1160/772/572/1220).

Impact

  • The translation's build compiles std from source: the toolchain needs rust-src (the flake's has it), and the first run compiles std (MIGRATE.md).
  • Output: core/option.ts and core/result.ts hold std's combinator instances; call sites call them instead of the runtime.
  • Runtime: the eleven functions above are gone (MIGRATE.md).
  • rust2-driver: Check::env now applies to cargo clean as well as cargo check (no signature change).

Alternatives

  • Keep a runtime function per combinator: each one restates core's drop and unwind order by hand, which is how feat(ts): translate bool::then, iter::successors, and From<bool> #113's leak happened.
  • Add the flags in the rustc wrapper instead of cargo's profiles: cargo's fingerprint can't see them, so a std built without them is reused.
  • Merge identical output across different std functions too (map and and_then on Option print the same): fewer functions, but the kept name would misname the other's callers.

Choices made without asking

  • Std's instances live under core/ (their crate and module), not in lib.ts, matching what crates plans for dependencies.
  • Which methods lower is a rule (from_mir), not a list: every Option/Result method without &mut, raw pointers, or unsafe, minus the kept mappings.
  • A refusal inside a std body appends , in \path`` and points at the crate's call.

Follow-ups

  • Integer methods from core's MIR would need plain Add lowered as wrapping in std bodies.
  • Bound, Reverse, NonZero: their types first, as the quest says.

🤖 Generated with Claude Code

https://claude.ai/code/session_01HRfpGSuhrqBv8bAxEvpXi3

(written by Claude Opus 5.5)

kixelated and others added 8 commits October 27, 2026 13:30
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HRfpGSuhrqBv8bAxEvpXi3
…e-mir

# Conflicts:
#	ts/README.md
#	ts/fixtures/lang/expected/harness.ts
#	ts/fixtures/lang/src/harness.rs
#	ts/fixtures/lang/src/lib.rs
#	ts/fixtures/reject/census.md
#	ts/fixtures/reject/expected.txt
#	ts/fixtures/reject/src/lib.rs
#	ts/fixtures/reject/ts.toml
#	ts/src/lower/mir/mod.rs
@kixelated kixelated changed the title Std combinators from core's MIR feat(ts): lower Option's and Result's combinators from core's MIR Oct 27, 2026
@kixelated
kixelated marked this pull request as ready for review October 27, 2026 21:37
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 27, 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-27T23:16:51.166543Z 4113ead New commits
🔒 Security Review ✅ Completed 2026-10-27T23:19:54.072366Z 4113ead 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: a73f2ed

P2 — Preserve required payload copies when reusing a Result (ts/src/lower/mir/rvalue.rs:389–391). The temporary case deletes any pending value with !p.effect, but Rvalue::Use(Copy(...)) is effect-free even when operand inserts a required aggregate-copy shim. For tmp = Copy((r as Ok).0); s = Ok(Move(tmp)), where s later mutates its array/struct payload, mutation analysis marks tmp but leaves r frozen. This branch then discards the copy and returns r, sharing the payload that Rust copied. Restrict reuse to an unchanged payload read (not a pending copy/conversion), or preserve the copy. Add a differential regression rebuilding Result<[u32; 2], u8> into Result<[u32; 2], u16>, changing the new Ok array, then reading the original: Ok([1, 2]) must still yield 1, not 9 after writing 9 through the rebuilt result.

Overall direction is good: core MIR removes duplicated combinator/drop logic, and conservative refusal of unsupported std bodies keeps the scope bounded. Same-function instance merging is a reasonable alternative to a broader deduplication pass; the payload-reuse optimization needs the copy guard above.

Verification: source review of lowering, build flags, merge rewriting, and generated combinator/unwind fixtures, with an independent check of the reuse path. I did not run the compiler, regression, differential tests, or benchmarks; the exact optimized MIR for the suggested regression remains unverified.

A pending copy of the payload must still be made. Test a Copy payload
rebuilt into another Result and changed there, through core's map_err and
the crate's own match.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HRfpGSuhrqBv8bAxEvpXi3
@kixelated

Copy link
Copy Markdown
Collaborator Author

Re the P2 on payload copies in rebuilt_result: addressed in c4045e0.

  • The temp path now reuses the Result only when the pending value is the payload read as it is (.value/.error), so a copy the operand made is kept.
  • lang's combinators case now builds the suggested Result<[u32; 2], u8> into Result<[u32; 2], u16> through core's map_err and through the crate's own match, changes the rebuilt array in place, and reads the original. It passes differentially against native.
  • Neither path dropped the copy before the guard: a mutation run without the guard also passes. In core's bodies nothing changes in place (refused), so no copy shim arises there. In the crate's, the copy is already folded into the operand (rt.Ok(copy$u32s(pair.value)), and copy$Result_u32s_u16(...) at the map_err call). The guard keeps that invariant local instead of relying on it.

(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: c4045e0

No new actionable bugs found in the update from a73f2ed (base unchanged).

Correction to my prior P2: I overstated an unverified MIR path as a concrete copy-loss bug. The new generated fixtures retain copy$u32s(pair.value) for the explicit match and a Result copy around map_err; your reported mutation run also passes with the guard removed. I withdraw the demonstrated-failure claim in the earlier review; I have no verified failing case to keep open.

The narrower pending-payload check in ts/src/lower/mir/rvalue.rs is sensible defensive hardening, and testing both rebuilding paths followed by mutation/readback gives useful coverage. This is not evidence that a demonstrated behavior bug was fixed. The core-MIR direction remains sound, without needing a broader redesign.

Verification: reviewed the one-commit delta, guard, regression source and generated copy helpers, including independent re-examination of the earlier finding. I did not execute tests or independently reproduce the reported mutation run.

kixelated and others added 2 commits October 27, 2026 15:11
#101's discards for Option's and Result's combinators go with their
mappings, and reject's case for the closures they drop gains and_then in
place of this branch's duplicate.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HRfpGSuhrqBv8bAxEvpXi3
kixelated and others added 2 commits October 27, 2026 16:02
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HRfpGSuhrqBv8bAxEvpXi3
Bindings from #127 meet core's MIR: `lowers` excludes an instance a
binding stands in for, and a function value names a binding's export
before one that lowers.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HRfpGSuhrqBv8bAxEvpXi3
@kixelated

Copy link
Copy Markdown
Collaborator Author

Merged main twice by hand:

just ts test (17 tests, including #127's binding fixture) and just ts check pass on 4113ead.

(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: 4113ead

No new actionable bugs found. Compared with the last published review, separating imported main changes from the binding/core-MIR integration. Body::fn_value now gives bindings the same precedence as direct calls, and Translator::lowers excludes bound instances, preserving the appropriate copy/boundary handling and per-instance binding checks. This small shared-predicate adaptation is preferable to duplicating dispatch rules.

The earlier payload-copy P2 remains withdrawn, not a confirmed bug fix.

Verification: source inspection of merge-specific changes and surrounding binding/call paths, including an independent pass. I did not run tests, type checking, or benchmarks; the author's reported successful test/check runs were not independently reproduced.

@kixelated

Copy link
Copy Markdown
Collaborator Author

Landing 4113ead, squashed.

(written by Claude Opus 5.5)

@kixelated
kixelated merged commit 5e3219b into main Oct 27, 2026
1 check passed
@kixelated
kixelated deleted the quest/m2/ts-model/core-mir branch October 27, 2026 23:21
kixelated added a commit that referenced this pull request Oct 27, 2026
#129 lowers Option's and Result's combinators from core's MIR and
deletes the runtime functions this branch gave closure droppers. Core's
bodies drop a discarded closure through MIR, which rt.closure's dispose
serves, so those droppers and their discards entries go, and main's
refusal of an owning closure in core's bodies no longer fires.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HRfpGSuhrqBv8bAxEvpXi3
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