Repository navigation
feat(ts): lower Option's and Result's combinators from core's MIR - #129
Conversation
Quest-Claim: f32016f0a5278db1
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01HRfpGSuhrqBv8bAxEvpXi3
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
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: 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
|
Re the P2 on payload copies in
(written by Claude Opus 5.5) |
kixelated
left a comment
There was a problem hiding this comment.
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.
#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
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
|
Merged main twice by hand:
(written by Claude Opus 5.5) |
kixelated
left a comment
There was a problem hiding this comment.
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.
|
Landing 4113ead, squashed.
(written by Claude Opus 5.5) |
#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
Closes the core MIR quest of the model epic.
Problem
Every
OptionandResultcombinator was a hand-written runtime function plus a mapping inlibrary.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
Translator::lowers(instance)replaces thedid.is_local()checks at the call, function-value, and translated-function sites, andlibrary::from_mirpicks the std functions it covers:Option's andResult's methods, and theirClone, unless they take a&mutor raw pointer, areunsafe, orlibrarymaps them as one expression or the runtime's panic (is_some,as_ref,copied,unwrap,expect,is_ok, ...). AClonethat 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}).-Zbuild-std=core,alloc,std,panic_unwind,panic_abortand-Cpanic=unwind -Zalways-encode-miron core, alloc, and std throughprofile.dev.package.*.rustflags, which cargo's fingerprint sees;CARGO_UNSTABLE_PROFILE_RUSTFLAGSgoes tocargo cleantoo, sinceChecknow gives both the same environment.panic_abortkeeps aborting forcdylibdependencies. A std crate built to abort is refused when one of its bodies lowers, so a--releaseor prebuilt std can't silently lose drops.rust-srcvendors std's dependencies, so this works offline., 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.core/option.tsandcore/result.ts(module_offor any crate, which crates reuses). One instance per closure keeps each call site monomorphic;Program::mergethen merges the instances of one std function whose output is identical into the first in sort order, iterating as merges make callers identical.Result:Ok(x)built from(r as Ok).0of a stablerisritself (map_err'sOk,and_then'sErr), since a write into anrt.Result's payload replaces the result. The spike measured Node 1.31x slower without it.Proof:
langgains acombinatorscase (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,CloneofOption<String>andResult<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'soptionsdrop scenario gains panic points 6 and 7 insidefilter's andand_then's closures, pluszip,or_else,is_some_and,map_or_else, andCloneof 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).Option::mapdrops onNone, asResult::map_errdoes onOkandResult::and_thenonErr: feat(ts): drop what std's iterators own #101 listed these indiscardsby 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, inthe std function until closures can drop (#115);reject'sdiscarded_closurepins all three, withand_thenadded. A differential test of them belongs with feat(ts): drop what closures own #115, which makes them translate.Copypayload rebuilt into anotherResultand changed in place there leaves the original's, through core'smap_errand the crate's ownmatch(lang'scombinators, from review).Census and bench:
Option's andResult's methods fall from 1,066 to 126. The rest are&mutmethods (get_or_insert,replace,insert),PartialEq/Fromimpls, and std calls a core body reaches (Default::defaultinunwrap_or_default,Clone::cloneof 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 benchagainst 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
rust-src(the flake's has it), and the first run compiles std (MIGRATE.md).core/option.tsandcore/result.tshold std's combinator instances; call sites call them instead of the runtime.MIGRATE.md).rust2-driver:Check::envnow applies tocargo cleanas well ascargo check(no signature change).Alternatives
mapandand_thenonOptionprint the same): fewer functions, but the kept name would misname the other's callers.Choices made without asking
core/(their crate and module), not inlib.ts, matching what crates plans for dependencies.from_mir), not a list: everyOption/Resultmethod without&mut, raw pointers, orunsafe, minus the kept mappings., in \path`` and points at the crate's call.Follow-ups
Addlowered 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)