Skip to content

fix(ts): operands evaluate in order around indexing - #54

Merged
kixelated merged 3 commits into
mainfrom
quest/m2/ts-index-order
Oct 25, 2026
Merged

kixelated merged 3 commits into
mainfrom
quest/m2/ts-index-order

Conversation

@kixelated

@kixelated kixelated commented Oct 25, 2026 •

Copy link
Copy Markdown
Collaborator

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 a Vec: the index's statements ran before f().
  • 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 called g, not f.
  • a[{ n += 1; 0 }] += n added the new n, 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. operands and range_bounds already did this inline and now call it. The integer Index mapping and copy_within use it too.
  • A function-pointer call lowers its callee and arguments through operands, together.
  • A compound assignment lowers its place's statements after saving the value, as plain assignment already did.
  • copy_within without 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's order case, 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.md and deletes it in the same PR, since the PR finishes it.

Impact

  • None to the public API. Output changes only where an operand later in the order has statements of its own. The codec snapshots are unchanged.

Alternatives

  • Spilling the indexed value every time: rejected, since it would add a const to every index whose operand has no statements.
  • Not covered, because the cases cannot be observed or are refused:
    • DebugList::entries lowers its list before its items. The items cannot reach the formatter while the builder borrows it, so the order cannot be observed.
    • mem::replace and mem::swap on an indexed place are refused in THIR, since the place is not a path.
    • Built-in indexing of a place, such as a[{ a = b; 0 }], reads the place after its index in Rust too. The lowering already does the same.

Follow-ups

  • When an operand has already been saved, after can save it again, as in const arg3 = arg2. This happens only when a later operand has statements. It costs an alias.
  • fix(ts): struct update on a std range reads its base #53 also edited range_bounds; main is merged in, and its full-range and struct-update paths now share Body::after.

🤖 Generated with Claude Code

https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn

kixelated and others added 2 commits October 25, 2026 09:01
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
@kixelated
kixelated marked this pull request as ready for review October 25, 2026 16:20
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 25, 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-25T19:52:12.336285Z 4c20e47 New commits
🔒 Security Review ✅ Completed 2026-10-25T19:53:07.035098Z 4c20e47 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: 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.

@kixelated
kixelated merged commit d876a35 into main Oct 25, 2026
1 check passed
@kixelated
kixelated deleted the quest/m2/ts-index-order branch October 27, 2026 20:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant