Repository navigation
fix(ts): read a tuple's parts as they were when it was made - #148
Conversation
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>
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: 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
|
Merge summary:
Follow-up from the PR body, for (Written by Claude Opus 5.5) |
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 toa = b; b = a;. The same happened for&strand&Point, and for any reference operand. Found by #130, which left the swap out of itssplitsfixture. Finishesquest/m2/ts-swap-parts.md.The cause was
rereadableints/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 referenceb, whose variable can be assigned another while the copy lives.var_parts(place_exprinplace.rs) andheld_partthen 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:
Use,CopyForDeref): it rereads only if the place it is copied from does.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.Pending.rereadsis 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.Slot::Alias): judged by its assignment, as above.The aggregate already keeps a part that doesn't reread in a
const part, andvar_partsreads that instead.The other
var_partsreader,Arguments::newincall.rs, reads its array right after the array is built, with nothing in between, so it needs no change.Tests:
splits(case 4), inside a loop, with the slices kept whole by an opaque call.shapes::copiesswaps a&strpair, a&Pointpair, and au8pair in a loop.main, the&[u8],&str, and&Pointswaps differ from native Rust in the lang differential; theu8swap was already correct.Impact
fmt.ts: renames only.DecodeErroranddrops.arcs: oneconsteach inDisplaycode for a call's result that is a member path, such as thiserror'sas_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
consts above, in cold formatting code. Telling a referent from a holder would mean typing the returned path.&err, since the borrow keeps it.alias()is stricter and stays unchanged.Alternatives
fmt,cows,iters, the task fixture's drops, and the codec (about 300 snapshot lines).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).🤖 Generated with Claude Code
(Written by Claude Opus 5.5)