Repository navigation
feat(ts): translate a &mut to a value JS copies as an rt.Place - #128
Conversation
Quest-Claim: 82ad6b0c23befcab
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01HRfpGSuhrqBv8bAxEvpXi3
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
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. |
There was a problem hiding this comment.
💡 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".
kixelated
left a comment
There was a problem hiding this comment.
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 callsoperands(false, &[], ...), so a receiver of type&mut Box<u32>remains aPlace<u32>. The Boxderef_mutmapping returns that operand unchanged, then this code records it as the borrowed value's path. Forfn 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)
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
|
Both findings are fixed in e4508b4.
(Written by Claude Opus 5.5) |
kixelated
left a comment
There was a problem hiding this comment.
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.rsnow passes the receiver's pointee into the mapping, andderef_boxemitsb.v = to. TheBox<Option<u8>>fixture is the supported reproducer; my earlierBox<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)
|
Merged main twice, for #123, #132, and #118 (no conflicts) and then for #127 (bindings).
Checks on 92df054: (Written by Claude Opus 5.5) |
kixelated
left a comment
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
💡 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".
| None => match self.reborrowed(*p, span, out) { | ||
| Some(e) => e, |
There was a problem hiding this comment.
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 👍 / 👎.
There was a problem hiding this comment.
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)
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
|
The findings on 92df054 are fixed in 762e4ee. 5877cef merges #129.
Checks on 5877cef: (Written by Claude Opus 5.5) |
kixelated
left a comment
There was a problem hiding this comment.
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:
mappedexcludes bindings, so argument adaptation and the std Place guard no longer unwrap their references. The newraw::skipbinding fixture passes anrt.placeand writes throughat.v. - The independent shared-reborrow finding is fixed: only mutable reborrows reuse a Place, while the
fillfixture's shared methods readseen.vands.v. - The merged
lowers/mappeddistinction 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)
|
Merging 5877cef.
Suggested follow-ups, from the PR body: std mappings that make Places for copied values ( (Written by Claude Opus 5.5) |
#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
Completes quest/m2/ts-model/mut-place.md, which this PR deletes.
Problem
rust2-ts refused a
&mutto a value JS copies, such as a number, anOption, aBox, a tuple, an enum, or aString, 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 u64parameters handed down from&mut localor&mut self.field, including from closures.Approach
&mut TwhoseTneeds a place (no object JS changes in place, not a reference) is typedrt.Place<T>, an object whosevreads and writes the place it points at.&muton makes no Place.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 *ris the same Place.*rreads and writesr.v. Drop glue's_1, which is the value itself, is exempt.&mutparameter, as their identity signature declares it, as the pointee's value, as before. A type parameter instantiated to&mutgets the Place.&mutto 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 samert.Place.&mutinto aResultorControlFlowpayload that is passed on or kept is refused. A store through it would need to rebuild theResult, which a path-based Place cannot do.Cow::to_mut,DerefMut, andindex_mutinto a non-alias destination give a Place where the pointee needs one.DerefMut's receiver passes as its pointee, as std's&mut selfparameters do.v. This covers std's reference impls ofPartialEq,PartialOrd,Ord, andHash, and fmt'sDisplay/Debugof&mut T. Where a runtime key function would compare a Place, asSome(a) == Some(b)would, the call is refused. (These came from review round 1.)&*rof a Place readsr.v. Only a mutable reborrow is the same Place.Proof
ts/fixtures/lang/src/refs.rsruns the differential test, native wasm32 against translated TS on the same inputs. It covers:u64past 2^53.Optionpointees throughmap_or,mem::replace, payload writes, andtake.&mut Box<_>,&mut (u8, u8)withmem::swap, an enum, andStringpushandmem::take.&mut usizereturned from a method and kept in a struct.&mut u32.DerefMut::deref_muton a&mut Box<Option<u8>>.&selfmethods called through&mutparameters, which read the Place's value.ts/fixtures/bindbindsraw::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.rsadds an async block that borrows a number mutably across an await. It translates now.ts/fixtures/rejectdropsbump,async_borrow, andpushed, which translate now. It addslast_bumped(last_mut),modified(and_modify), andok_passed(aResultpayload passed on), andplaces_equal(Options of Places compared).Measurements
moq codec benchmark:
just ts moq's translated output is byte-identical to main's, apart fromrt.tsandrt/collections.ts. That code needs no Place, and it gets none. The newrt.placeexport is tree-shaken.just ts census: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/zipover 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:
&mut Tfor a copiedTnow takesrt.Place<T>. These were refused before, so no translated signature changes._NN: rt.Place<T>) in snapshots.rt.tsexportsPlace<T>, moved fromrt/collections.tsunder the same name, and the newrt.place(get, set).No
MIGRATE.mdentry: nothing that translated before changes shape.Alternatives
{ 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&mutescapes. The codec benchmark staying byte-identical is the measured point.&mutkept in a struct, returned, or captured, and for panics partway through.Resultpayloads by rebuilding theResulton each store. Dropped: no moq code passes one on, and it would makert.placeknow about variants.Choices made without asking
map_orover anOption<&mut u32>the crate made, where the mapping would in fact pass the Place unchanged.Stringthrough a parameter, both translate now and are tested. Before, they were refused.combinators(feat(ts): lower Option's and Result's combinators from core's MIR #129) at op 48 and addsrefsat 49 (op % 50).duration_assign's reject comment now names the missingAddAssignmapping as its reason, since a&mut Durationis a Place now.Follow-ups (suggested quests)
Option::as_mut,get_or_insert*,insert,slice::get_mut/last_mut,iter_mutandvalues_mutover copied items,and_modify, andretain_mut. This is the largest share of the census guard's 74.Option<&mut T>throughmap,map_or,filter, andand_thenclosures, once the guard can tell a pass-through from a copy.last_mutmapping, and anyAddAssign for Durationmapping (seeplan/duration-assign), must take or returnrt.Placefor copied values, or the guard refuses them.Checks run before the last push, after merging main through #129:
just check, andjust test, which includes everyjust ts testsuite (lang differential, reject, task, moq codec).(Written by Claude Opus 5.5)
🤖 Generated with Claude Code
https://claude.ai/code/session_01HRfpGSuhrqBv8bAxEvpXi3