Skip to content

fix(ts): keep a closure's write through a captured &mut - #185

Merged
kixelated merged 3 commits into
mainfrom
quest/m2/ts-closure-write
Oct 28, 2026
Merged

kixelated merged 3 commits into
mainfrom
quest/m2/ts-closure-write

Conversation

@kixelated

@kixelated kixelated commented Oct 28, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

A closure that writes a variable it captured and then returns it lost the write. let mut n = 0; let mut f = || { n += 1; n }; f(); n read 0 in the translation. Found writing #147's stock fixture.

Approach

The root cause is return_assigned, the pass that folds x = v; return x; into return v;. It assumed x was a local of the body it ran on. In a closure body, x can be a capture: either the caller's variable, captured by &mut, or the closure's own copy of a move capture, which its next call reads. Either way the write was dropped. With the write gone, the caller's let also became const.

The pass now folds only the body's own locals, its Slot::Var names, and still skips any name a cleanup reads. Fixing it at that check covers every capture path: by reference or move, FnMut or FnOnce, through a generic, and nested closures.

Tests

  • Reproduced first: the new captures::captures case gave 530 differences against native Rust without the fix, and none with it.
  • The case runs over every integer width. It covers a closure called once and twice, a write on one branch only, an FnOnce that moves what it owns, once/twice generics, a move FnMut called twice, a field captured alone, a &mut parameter, and a closure inside another.
  • No other snapshot changed.
  • just check and just test pass.

Impact

  • Public API: none. The translator's output changes only where it was wrong.

Alternatives

  • Exclude only the names of captured variables. Rejected: any name that is not a local of the body is unsafe to fold, so the list of allowed names is the simpler rule.

Follow-ups

  • The related #147 report is a separate root cause and is not fixed here. for _ in r.by_ref() {} on a RangeInclusive, followed by using r, still fails tsc: const iter: rt.RangeInclusive<rt.usize> = rt.iterBorrow(r) gives Type 'Iter<usize>' is missing ... lo, hi, exhausted, int. The cause is how IntoIterator for &mut I is typed, not closure writes. It could be a quest under ts-range-fold or one of its own.

(written by Opus 5.5)

🤖 Generated with Claude Code

kixelated and others added 2 commits October 28, 2026 10:45
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
return_assigned folded `x = v; return x` into `return v` for any name,
but in a closure body `x` may be a capture: the caller's variable by
`&mut`, or a `move` closure's own copy. It now folds only the body's
own locals.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kixelated
kixelated marked this pull request as ready for review October 28, 2026 18:05
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 28, 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-28T18:25:24.307824Z 26dff1e New commits
🔒 Security Review ✅ Completed 2026-10-28T18:26:41.473147Z 26dff1e 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: c04d447

No actionable bugs found. Restricting return-assignment folding to this body's own Slot::Var names is a sound, simpler boundary than enumerating capture kinds. It preserves the existing cleanup-read exclusion; closure capture snapshots and borrowed places remain outside the allowlist, and nested closures are lowered independently.

The differential fixture covers repeated calls, move captures, conditional writes, generic FnOnce/FnMut calls, fields, &mut parameters and nested closures across integer widths. I inspected the lowering change, surrounding capture/name allocation and return-folding logic, fixture registration and generated output. Static review only: I did not run the test suite or independently verify the reported test results.

…rite

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kixelated

Copy link
Copy Markdown
Collaborator Author

Summary: return_assigned now folds x = v; return x only when x is one of the body's own locals, so a closure's write to a captured variable stays. Covered by captures::captures across every integer width. Merged main (#152, #168), keeping both new harness cases. No maintainer decisions. The by_ref mistype from #147 has a separate root cause and is listed under Follow-ups.

(written by Opus 5.5)

@kixelated
kixelated merged commit 626ea54 into main Oct 28, 2026
1 check passed
@kixelated
kixelated deleted the quest/m2/ts-closure-write branch October 28, 2026 18:45
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