Repository navigation
fix(ts): operands evaluate in order around indexing - #54
Conversation
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn
A vector indexed by an integer, the slice `copy_within` reads, a function
pointer, and a compound assignment's value now evaluate before a later operand's
statements, as Rust evaluates them: `f()[{ ... }]` runs `f()` first. Without an
end, `copy_within` reads its slice once. `Body::after` holds the pattern that
`operands` and `range_bounds` already shared.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn
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: a69c1d6
No actionable bugs found in changed code. The shared after helper sensibly consolidates operand-order preservation; indexing, function-pointer calls and copy_within use it, while compound assignment saves prior values. This addresses demonstrated ordering problems without unconditional spills. Fixtures cover observable effects and callee/value mutation.
Verification: static diff and surrounding lowering inspection. No builds or differential tests run. The separate #53 RangeFull effect-loss issue is in unchanged code, not a new #54 finding.
# Conflicts: # ts/src/lower/library.rs
Problem
The THIR lowering lowered some operands to an expression, then emitted a later operand's statements before that expression was read, so the later operand ran first. Rust evaluates left to right. #49 found two of these; looking for the same shape turned up two more and one double evaluation:
f()[{ ... }]on aVec: the index's statements ran beforef().s.copy_within(r, { ... }):dest's statements ran before the slice and the range were read. Without an end, the slice expression was also evaluated twice, once for.length.f({ f = g; x })through a function pointer calledg, notf.a[{ n += 1; 0 }] += nadded the newn, not the old one.MIR already got every case right.
Approach
Body::after(earlier, pre, out)emits a later operand's statements, saving the earlier operands first if there are any.operandsandrange_boundsalready did this inline and now call it. The integerIndexmapping andcopy_withinuse it too.operands, together.copy_withinwithout an end saves the slice before reading its length.shapes::indexed, called from fix(ts): fields written out of declaration order evaluate as written #49'sordercase, logs each operand as it runs or changes an operand read earlier. It passes the differential test under both lowerings and failed under THIR before the fix.This PR also files the quest
quest/m2/ts-index-order.mdand deletes it in the same PR, since the PR finishes it.Impact
Alternatives
constto every index whose operand has no statements.DebugList::entrieslowers its list before its items. The items cannot reach the formatter while the builder borrows it, so the order cannot be observed.mem::replaceandmem::swapon an indexed place are refused in THIR, since the place is not a path.a[{ a = b; 0 }], reads the place after its index in Rust too. The lowering already does the same.Follow-ups
aftercan save it again, as inconst arg3 = arg2. This happens only when a later operand has statements. It costs an alias.range_bounds; main is merged in, and its full-range and struct-update paths now shareBody::after.🤖 Generated with Claude Code
https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn