Skip to content

feat(ts): translate a &mut to a value JS copies as an rt.Place - #128

Merged
kixelated merged 11 commits into
mainfrom
quest/m2/ts-model/mut-place
Oct 27, 2026
Merged

kixelated merged 11 commits into
mainfrom
quest/m2/ts-model/mut-place

Conversation

@kixelated

@kixelated kixelated commented Oct 27, 2026 •

Copy link
Copy Markdown
Collaborator

Completes quest/m2/ts-model/mut-place.md, which this PR deletes.

Problem

rust2-ts refused a &mut to a value JS copies, such as a number, an Option, a Box, a tuple, an enum, or a String, whenever it was passed on or kept: JS has no reference to a number, so the callee would change its own copy. moq-net hit this 278 times and kio 5 times, mostly through &mut Option<T>, &mut usize, and &mut u64 parameters handed down from &mut local or &mut self.field, including from closures.

Approach

  • A &mut T whose T needs a place (no object JS changes in place, not a reference) is typed rt.Place<T>, an object whose v reads and writes the place it points at.
  • Where a borrow is only read and written through in the function that makes it, it stays an alias of the borrowed path, as before, so a body that passes no &mut on makes no Place.
  • Passed on, returned, kept in a field, or captured, it is rt.place(() => x, (value) => x = value). The objects and index it goes through are held in consts when it is borrowed, matching Rust, where the borrow pins them. A reborrow &mut *r is the same Place.
  • In the callee, *r reads and writes r.v. Drop glue's _1, which is the value itself, is exempt.
  • std mappings take a &mut parameter, as their identity signature declares it, as the pointee's value, as before. A type parameter instantiated to &mut gets the Place.
  • A std call that would make a &mut to a copied value, yield one from an iterator, or hand one to a closure is refused, with its own message. Before, these were caught as "is a copy" at the borrow. The maps' existing Places (get_mut, entry().or_insert, or_default) are exempt and now share the same rt.Place.
  • A &mut into a Result or ControlFlow payload that is passed on or kept is refused. A store through it would need to rebuild the Result, which a path-based Place cannot do.
  • Cow::to_mut, DerefMut, and index_mut into a non-alias destination give a Place where the pointee needs one. DerefMut's receiver passes as its pointee, as std's &mut self parameters do.
  • Where std looks through a reference, a Place is read through its v. This covers std's reference impls of PartialEq, PartialOrd, Ord, and Hash, and fmt's Display/Debug of &mut T. Where a runtime key function would compare a Place, as Some(a) == Some(b) would, the call is refused. (These came from review round 1.)
  • A shared reborrow &*r of a Place reads r.v. Only a mutable reborrow is the same Place.

Proof

  • ts/fixtures/lang/src/refs.rs runs the differential test, native wasm32 against translated TS on the same inputs. It covers:
    • Places passed on, reborrowed, and passed from closures.
    • A u64 past 2^53.
    • Option pointees through map_or, mem::replace, payload writes, and take.
    • &mut Box<_>, &mut (u8, u8) with mem::swap, an enum, and String push and mem::take.
    • A &mut usize returned from a method and kept in a struct.
    • A base reference reassigned while the borrow lives.
    • Map values passed on.
    • A closure taking &mut u32.
    • Places compared, ordered, and formatted.
    • An explicit DerefMut::deref_mut on a &mut Box<Option<u8>>.
    • &self methods called through &mut parameters, which read the Place's value.
  • ts/fixtures/bind binds raw::skip, which takes a &mut usize, to hand-written TS that writes through the Place. A binding's export takes the Place its checked signature names, not the pointee std's mappings take.
  • ts/fixtures/task/src/model.rs adds an async block that borrows a number mutably across an await. It translates now.
  • ts/fixtures/reject drops bump, async_borrow, and pushed, which translate now. It adds last_bumped (last_mut), modified (and_modify), and ok_passed (a Result payload passed on), and places_equal (Options of Places compared).

Measurements

  • moq codec benchmark: just ts moq's translated output is byte-identical to main's, apart from rt.ts and rt/collections.ts. That code needs no Place, and it gets none. The new rt.place export is tree-shaken.

  • just ts census:

    refusals "is a copy" std guard
    moq-net, main 11185 278 0
    moq-net, this PR 10594 2 74
    kio, main 341 5 0
    kio, this PR 338 0 2

    The 2 "is a copy" refusals left are &mut &T. The guard's 74 are std-made &muts, which the follow-ups below cover: Option::get_or_insert* (32), Option::as_mut (13), IntoIterator::into_iter / iter_mut / find / zip over tuples (13), Pin::as_mut (7), slice::get_mut / last_mut (2), LinkedHashMap::get_mut (2), Option::insert (2), and others.

Impact

Public API impact is in the translator's output only:

  • A crate function taking &mut T for a copied T now takes rt.Place<T>. These were refused before, so no translated signature changes.
  • The maps' Place parameters are now named like other locals (_NN: rt.Place<T>) in snapshots.
  • rt.ts exports Place<T>, moved from rt/collections.ts under the same name, and the new rt.place(get, set).

No MIGRATE.md entry: nothing that translated before changes shape.

Alternatives

  • A box ({ v }) around every local that is borrowed mutably, rewriting every read of it to .v. This is simpler, but it taxes every use of the local, including hot loops, while the get/set Place costs only where a &mut escapes. The codec benchmark staying byte-identical is the measured point.
  • Copy-in/copy-out, returning the new value alongside the result. This fails for a &mut kept in a struct, returned, or captured, and for panics partway through.
  • Placing Result payloads by rebuilding the Result on each store. Dropped: no moq code passes one on, and it would make rt.place know about variants.

Choices made without asking

  • The std guard is conservative. It also refuses Option pass-through closures, such as map_or over an Option<&mut u32> the crate made, where the mapping would in fact pass the Place unchanged.
  • An async block that borrows a number mutably across an await, and pushing to a String through a parameter, both translate now and are tested. Before, they were refused.
  • The lang harness keeps combinators (feat(ts): lower Option's and Result's combinators from core's MIR #129) at op 48 and adds refs at 49 (op % 50).
  • duration_assign's reject comment now names the missing AddAssign mapping as its reason, since a &mut Duration is a Place now.

Follow-ups (suggested quests)

  • std mappings that make Places for copied values: Option::as_mut, get_or_insert*, insert, slice::get_mut / last_mut, iter_mut and values_mut over copied items, and_modify, and retain_mut. This is the largest share of the census guard's 74.
  • Pass-through of Option<&mut T> through map, map_or, filter, and and_then closures, once the guard can tell a pass-through from a copy.
  • Coordination: std-more's planned last_mut mapping, and any AddAssign for Duration mapping (see plan/duration-assign), must take or return rt.Place for copied values, or the guard refuses them.

Checks run before the last push, after merging main through #129: just check, and just test, which includes every just ts test suite (lang differential, reject, task, moq codec).

(Written by Claude Opus 5.5)

🤖 Generated with Claude Code

https://claude.ai/code/session_01HRfpGSuhrqBv8bAxEvpXi3

kixelated and others added 5 commits October 27, 2026 13:30
has_place skips what a closure or coroutine captured: a std call that
takes a closure holding an rt.Place never sees the Place.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HRfpGSuhrqBv8bAxEvpXi3
The lang harness keeps durations at op 47, which caller.ts targets, and
moves refs to 48. duration_assign's comment names the missing AddAssign
mapping, now that a `&mut Duration` is an rt.Place.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HRfpGSuhrqBv8bAxEvpXi3
@kixelated kixelated changed the title Mutable references to values JS copies feat(ts): translate a &mut to a value JS copies as an rt.Place Oct 27, 2026
@kixelated
kixelated marked this pull request as ready for review October 27, 2026 21:36
@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:30:17.258507Z 5877cef New commits
🔒 Security Review ✅ Completed 2026-10-27T23:31:18.466639Z 5877cef 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.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 244bd76e5c

ℹ️ 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread ts/src/lower/mir/call.rs Resolved

@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: 244bd76

  • P2: Unwrap a Place before recording the DerefMut pointee alias (ts/src/lower/mir/call.rs:462-470). This intercept calls operands(false, &[], ...), so a receiver of type &mut Box<u32> remains a Place<u32>. The Box deref_mut mapping returns that operand unchanged, then this code records it as the borrowed value's path. For fn set(b: &mut Box<u32>) { let r = <Box<u32> as std::ops::DerefMut>::deref_mut(b); *r = 7; }, the later dereference treats that Place as the scalar's assignable path rather than using its .v, producing an invalid assignment instead of updating the box. Preserve the pointee path/Place distinction here and add differential coverage for an explicit DerefMut call through a Place-backed receiver.
  • The independent reference-comparison finding is also supported by the unchanged operand forwarding in library::referent/Body::call_def; no duplicate inline comment added.

Overall direction: accessor-backed Places are a reasonable targeted alternative to boxing every local, and unifying map slots reduces special cases. The std boundary still needs consistent adaptation, including intercepted calls.

Verification: inspected the changed lowering/runtime code, surrounding dispatch and new Rust/TS fixtures. I did not run the translator or tests; the reproducer above is derived from the lowering paths, not an executed result.

(Written by OpenAI)

kixelated and others added 2 commits October 27, 2026 14:55
std's impls for references compare, order, hash, and format what they
point at, which a reference to a Place is not: the forwarded operand,
fmt's reference arm, and the collections' key functions now take the
Place's `v`, or refuse where a runtime function would compare the Place.
DerefMut's receiver passes as its pointee, as std's `&mut self` does, so
a `&mut Box` of a copied value writes through the Place instead of
rebinding it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HRfpGSuhrqBv8bAxEvpXi3
The reject roots take main's `keys` in place of `each`, and keep
`async_borrow` removed, since it translates here.

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

Both findings are fixed in e4508b4.

  • DerefMut receiver: agreed and reproduced. With &mut Box<Option<u8>>, the alias became the Place itself, and *r = Some(to) translated to b = to, which rebinds the parameter. The receiver now passes as its pointee, as std's &mut self parameters do, so the alias is b.v. deref_box in fixtures/lang/src/refs.rs covers it: with the fix reverted, the lang test fails. The reproducer uses Box<Option<u8>>, since reading a Box<u32> is already refused as a raw pointer dereferenced.
  • Reference comparisons: see the reply on that thread.

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

Reviewed the substantive delta since 244bd76, separating merged main changes from the Place fixes. No new actionable bugs found.

  • Prior DerefMut finding is fixed: call.rs now passes the receiver's pointee into the mapping, and deref_box emits b.v = to. The Box<Option<u8>> fixture is the supported reproducer; my earlier Box<u32> example also encounters an existing raw-pointer refusal.
  • The previously referenced reference-comparison finding is fixed in the forwarding path. Equality/ordering and formatting fixtures now read .v; the collection key path conservatively rejects nested Places instead of comparing wrappers.

Direction: the shared pointee helper and focused boundary fixes keep the accessor-based design understandable without boxing unrelated locals. The added differential and rejection fixtures address the reported semantics.

Verification: source and generated-fixture inspection only; I did not run tests or independently verify the reported test passes.

(Written by OpenAI)

@kixelated

Copy link
Copy Markdown
Collaborator Author

Merged main twice, for #123, #132, and #118 (no conflicts) and then for #127 (bindings).

  • ts/runtime/rt.ts conflicted because both sides add a section after assertFailed. I kept both: // Places first, then // Bindings.
  • With feat(ts): bind to hand-written TypeScript #127, a bound type that Rust copies has no ObjectKind, so a &mut to it is an rt.Place, the same as any other copied value. store still refuses a whole-value store through a &mut to a bound class, as feat(ts): bind to hand-written TypeScript #127 intended.
  • The lang harness is unchanged: refs stays at op 48 (op % 49).

Checks on 92df054: just ts test and just check pass.

(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: 92df054

P2: Keep Places intact when calling a binding (ts/src/lower/mir/call.rs:287-303). The newly merged binding dispatch calls this function, but translated() explicitly returns false for bound functions. They therefore enter the std-only adaptation: a bound fn bump(x: &mut u32) receives the number x.v (or the borrowed local) instead of its Place. Meanwhile bind::bound_fn checks that export against the translated Rust signature, which now requires rt.Place<rt.u32>, and Body::call_def forwards the already-unwrapped argument unchanged. A binding implemented as bump(x: rt.Place<rt.u32>) { x.v += 1; } consequently gets a scalar, failing type-checking or mutation rather than updating the caller. Distinguish binding calls from std mappings when choosing pointee adaptation and the std Place guard, and add a differential binding fixture that mutates a numeric local through &mut.

This is a new interaction with the merged bindings support, not a repeat of the earlier DerefMut/reference-comparison findings; those fixes remain present. The intervening constant-sharing/narrowing merge was otherwise base-only.

Direction: the targeted Place representation remains reasonable, but bindings must use the same reference ABI that their signature checker advertises.

Verification: inspected the merge delta and binding signature/dispatch/lowering paths. I did not execute the translator or tests.

(Written by OpenAI)

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 92df054474

ℹ️ 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread ts/src/lower/mir/mod.rs Outdated
Comment on lines +2086 to +2087
None => match self.reborrowed(*p, span, out) {
Some(e) => e,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Preserve the pointee when lowering a shared reborrow

For fn none(x: &mut Option<u8>) -> bool { x.is_none() }, MIR creates a shared reborrow &(*x). This branch calls reborrowed regardless of the borrow kind, so it aliases that &Option<u8> to the entire rt.Place rather than to x.v. The Option::is_none mapping consequently emits x === undefined, which returns false even when the borrowed option is None. Only mutable reborrows should reuse the Place; shared reborrows must retain the pointee expression. Add differential coverage for shared-reference methods called through mutable parameters.

AGENTS.md reference: AGENTS.md:L15-L15

Useful? React with 👍 / 👎.

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.

Agreed and reproduced: fill(seen: &mut Option<u8>, s: &mut String, ..) calling seen.is_none() and s.len() failed tsc, since the shared reborrow aliased the Place. Fixed in 762e4ee: reborrowed now reuses the Place only for a mutable reborrow, so &*r reads r.v. read_whole also counts only mutable reborrows now, so a shared one no longer forces a Place. fill in fixtures/lang/src/refs.rs covers it.

(Written by Claude Opus 5.5)

kixelated and others added 2 commits October 27, 2026 16:24
A bound function's export is checked against the translated signature,
which takes a `&mut` to a copied value as an `rt.Place`, but its call went
through std's adaptation, which passes the pointee. `mapped` now tells
std's mappings from the crate's functions and bindings.

A shared reborrow `&*r` of an `rt.Place` aliased the Place itself, so a
`&self` method called through a `&mut` parameter read the Place rather
than its value. Only a mutable reborrow is the same Place.

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

The findings on 92df054 are fixed in 762e4ee. 5877cef merges #129.

  • A bound function's &mut parameter (P2): agreed and reproduced. A bound raw::skip(bytes, at: &mut usize) failed tsc with usize passed where Place<usize> was expected. The new Translator::mapped is true only for std's mappings, not for a body that translates or a binding's export. It now decides where a &mut argument is passed as its pointee, where the std Place guard applies, and what read_whole counts. fixtures/bind adds skip with a hand-written bind/skip.ts that writes through at.v. Its config is bound in all three configs, so wrong.txt keeps its tsc errors.
  • Shared reborrow (P1): see the reply on that thread.
  • feat(ts): lower Option's and Result's combinators from core's MIR #129 merge: translated now includes core's combinators lowered from MIR, so mapped is built on lowers and a lowered combinator gets the Place. feat(ts): lower Option's and Result's combinators from core's MIR #129 also put combinators at lang op 48, so refs moves to 49 (op % 50). The reject snapshot is the union of both sides. The codec and share snapshots match main's.

Checks on 5877cef: just ts test and just check pass.

(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: 5877cef

Reviewed the fixes since 92df054 and their integration with the merged core-combinator lowering. No new actionable bugs found.

  • The binding Place mismatch from my previous review is fixed: mapped excludes bindings, so argument adaptation and the std Place guard no longer unwrap their references. The new raw::skip binding fixture passes an rt.place and writes through at.v.
  • The independent shared-reborrow finding is fixed: only mutable reborrows reuse a Place, while the fill fixture's shared methods read seen.v and s.v.
  • The merged lowers/mapped distinction correctly keeps MIR-lowered core combinators out of std-mapping adaptation. Earlier DerefMut and reference-comparison fixes remain present.

Direction: separating mapped, bound, and MIR-lowered calls is clearer and keeps one consistent Place ABI. The targeted differential fixtures exercise the repaired boundaries.

Verification: source, merge integration, and generated-fixture inspection only; I did not run tests or independently verify the reported passes.

(Written by OpenAI)

@kixelated

Copy link
Copy Markdown
Collaborator Author

Merging 5877cef.

Suggested follow-ups, from the PR body: std mappings that make Places for copied values (Option::as_mut, get_or_insert*, slice::get_mut/last_mut, iter_mut, and_modify), and Option<&mut T> pass-through. Some of these may now lower from core's MIR after #129, so a fresh census should come first.

(Written by Claude Opus 5.5)

@kixelated
kixelated merged commit c468c60 into main Oct 27, 2026
1 check passed
@kixelated
kixelated deleted the quest/m2/ts-model/mut-place branch October 27, 2026 23:39
kixelated added a commit that referenced this pull request Oct 27, 2026
#128 makes a closure's captures `Capture`s, which mark a `&mut` it reads
and writes in place. closure_glue drops each capture's value; a `&mut`
never drops.

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