Skip to content

fix(ts): read a tuple's parts as they were when it was made - #148

Merged
kixelated merged 4 commits into
mainfrom
quest/m2/ts-swap-parts
Oct 28, 2026
Merged

kixelated merged 4 commits into
mainfrom
quest/m2/ts-swap-parts

Conversation

@kixelated

@kixelated kixelated commented Oct 28, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

A tuple built from variables, with a field read after one of those variables is reassigned, read the variable's new value. (a, b) = (b, a) on two &[u8] in a loop lowered to a = b; b = a;. The same happened for &str and &Point, and for any reference operand. Found by #130, which left the swap out of its splits fixture. Finishes quest/m2/ts-swap-parts.md.

The cause was rereadable in ts/src/lower/mir/mod.rs. It accepted any path, and any held-back reference temporary whose value is a path, "which borrow checking keeps as it is". That is true of a borrow &x. It is not true of a copy of a reference b, whose variable can be assigned another while the copy lives. var_parts (place_expr in place.rs) and held_part then read the tuple's field as that path.

Approach

Whether a reference part rereads the same is now decided from MIR, not from the lowered expression:

  • A place nothing changes (assigned at most once, never changed in place, no dereference): it rereads.
  • A copy of a reference (Use, CopyForDeref): it rereads only if the place it is copied from does.
  • A borrow (Ref): borrow checking keeps the borrowed place. A replaceable reference along the path breaks that, so a borrow through a dereference rereads only if the reference it starts from does. A dereference past the first projection never rereads.
  • A held-back temporary: Pending.rereads is judged when it is held, before what it reads folds into it. A borrow uses the rule above. Anything else must be a constant or a name that stays put, as before.
  • A local read as its referent (Slot::Alias): judged by its assignment, as above.

The aggregate already keeps a part that doesn't reread in a const part, and var_parts reads that instead.

The other var_parts reader, Arguments::new in call.rs, reads its array right after the array is built, with nothing in between, so it needs no change.

Tests:

  • The swap is back in splits (case 4), inside a loop, with the slices kept whole by an opaque call.
  • shapes::copies swaps a &str pair, a &Point pair, and a u8 pair in a loop.
  • On main, the &[u8], &str, and &Point swaps differ from native Rust in the lang differential; the u8 swap was already correct.
  • All pass now.

Impact

  • Public API: the translator's output only. No Rust API changes.
  • Snapshot changes outside the new cases:
    • fmt.ts: renames only.
    • DecodeError and drops.arcs: one const each in Display code for a call's result that is a member path, such as thiserror's as_display(&self.size). A call can return either the place it borrows or a reference it was given, and the expression can't tell which, so only a bare stable name is trusted there.

Choices made without asking

  • Calls returning a member path are not trusted. This costs the two consts above, in cold formatting code. Telling a referent from a holder would mean typing the returned path.
  • The borrow rule is relaxed for a whole variable. It accepts a borrow of a multi-assigned variable with no dereference, such as &err, since the borrow keeps it. alias() is stricter and stays unchanged.

Alternatives

  • Materialize every reference part. This is simpler, but it added consts across fmt, cows, iters, the task fixture's drops, and the codec (about 300 snapshot lines).
  • Reuse alias() as the rule. It rejects borrows of multi-assigned variables, and it reads a held-back source as stable. That source's folded value may be a reassigned variable.

Follow-ups

  • alias()'s copy arm treats a held-back source local as stable, though its folded value may name a variable assigned again. No miscompile was found through it, but it has the same shape as this bug. Worth an audit quest.

Checks

  • just check: passes.
  • just ts test: passes (lang differential, 50k cases).
  • Not run: the ffi, driver, and uniffi suites, which this change doesn't touch.

🤖 Generated with Claude Code

(Written by Claude Opus 5.5)

kixelated and others added 2 commits October 27, 2026 19:36
Quest-Claim: f7fd94ad57eeb7d3
A reference part of a tuple read later was its operand's path, even when
that path is a variable assigned another first, so `(a, b) = (b, a)` in a
loop lowered to `a = b; b = a;`. Whether a part rereads the same is now
judged from MIR: a copy of a reference by where it is copied from, a
borrow by what it borrows.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kixelated kixelated changed the title A tuple's parts read after their variables change fix(ts): read a tuple's parts as they were when it was made Oct 28, 2026
@kixelated
kixelated marked this pull request as ready for review October 28, 2026 03:28
@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-28T07:18:54.728340Z f6fd60c New commits
🔒 Security Review ✅ Completed 2026-10-28T07:19:54.622988Z f6fd60c 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: e00ca14

No actionable bugs found in the changed code. Distinguishing a borrow from a copied reference is the right direction; carrying that distinction through pending-expression folding addresses the swap failure without changing ownership semantics. The conservative fallback for dereferences and call-derived member paths is preferable to assuming those paths are stable. Materializing all reference parts would be simpler, but the localized MIR-based rule is reasonable given the demonstrated output overhead.

The new loop fixtures cover byte-slice, string, object-reference and scalar swaps, and the snapshots preserve both source values before assignment. A useful follow-up is differential coverage for reborrows through a reassigned reference and indexed borrows, which exercise the new predicate's less direct branches.

Verification: static inspection of the diff, surrounding aggregate/alias/pending-value handling and generated snapshots only; I did not run the test suites or independently verify the checks reported in the PR.

# Conflicts:
#	ts/fixtures/lang/expected/fmt.ts
@kixelated

Copy link
Copy Markdown
Collaborator Author

Merge summary:

  • Merged main into the branch (f6fd60c). The only textual conflict was the quest/m2/README.md link list, resolved by the quest merge driver: this quest's entry stays removed. Code files (lower/mir/{mod,place,rvalue}.rs, shapes.ts) auto-merged with feat(ts): translate options of values that can be undefined #134.
  • On the merged head: just check and just ts test pass locally, with no snapshot drift. CI Check passed.
  • Review: Codex reviewed b0cedf4 with no findings and gave the base-merge head 👍. The earlier automated comment from the maintainer's account does not count as a review.
  • quest check --base origin/main passes. Main has since gained feat(ffi)!: name a field apart from the method of its name with field@ #145 (ffi only, no overlap).
  • Squash-merging as authorized by the maintainer.

Follow-up from the PR body, for /quest-plan: audit alias()'s copy arm, which treats a held-back source local as stable.

(Written by Claude Opus 5.5)

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