Skip to content

fix(ts): fields written out of declaration order evaluate as written - #49

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

kixelated merged 5 commits into
mainfrom
quest/m2/ts-variant-order

Conversation

@kixelated

@kixelated kixelated commented Oct 25, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

The THIR lowering put an aggregate's fields in declaration order without keeping the order the source evaluates them, so a variant like Shape::Line { 1: f(), 0: g() } ran g() first. The same held for positional structs given by index, struct update syntax on a positional struct (the base was read before the fields that change it), and Range { end, start }, whether built, looped over with for, or used to slice. Named structs already spilled when they reordered. The MIR lowering was already correct.

Approach

  • One helper, Body::declared, puts written fields in declaration order and spills every non-constant value that would otherwise run after one written later. Only the longest tail already in order stays inline.
  • Struct (positional and plain named), enum variant, Range construction and range_bounds (slicing) use it. A class with named fields keeps its written-order object literal.
  • A for loop over a written Range evaluates its fields as written, spilling end and what comes before it.
  • simple is false for a reordered aggregate, so an if or match no longer becomes a ternary whose arm hoists spills.
  • The lang fixture gains shapes::order, whose field expressions log as they run, checked against native Rust under both lowerings (lang, lang_mir). Before the fix it failed under THIR on every case except the named struct and passed under MIR.

Impact

  • Public API: none.

Alternatives

  • Spill every non-constant field when any reorder, as named structs did. That's simpler but emits more temporaries. The tail rule emits the fewest.

Follow-ups

  • A value already spilled by operands (a later field had statements) gets spilled again as field. That's correct but redundant. Rare, and gone once MIR is the only lowering.
  • Range { start, ..r } (struct update on a std Range) drops its base silently in adt_expr and panics the driver in a for loop. Filed as Struct update on a std range in this PR.

🤖 Generated with Claude Code

https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn

(written by Opus 5.5)

kixelated and others added 2 commits October 24, 2026 22:43
Enum variants, positional structs given by index, struct update syntax on a
positional struct, and `Range` (built, looped over, or sliced with) now spill the
fields written before one they follow, as named structs already did. A
reordered aggregate is no longer `simple`, so a ternary never hoists its fields.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn
@kixelated kixelated changed the title Enum variant fields evaluate as written fix(ts): fields written out of declaration order evaluate as written Oct 25, 2026
@kixelated
kixelated marked this pull request as ready for review October 25, 2026 06:01
@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-25T15:17:27.317445Z 181c51d New commits
🔒 Security Review ✅ Completed 2026-10-25T15:17:54.706299Z 181c51d 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: 4930e23

No actionable bugs found. Sharing declaration-order lowering across aggregates and ranges is a sound simplification. Spilling the prefix preserves written evaluation order while leaving the ordered tail inline; marking reordered aggregates non-simple prevents conditional-arm spills from escaping their branch. Fixtures cover side effects, mutable reads, positional struct updates, conditional execution, range construction, loops and slicing.

Verification: GitHub-only static inspection of the complete patch, operand/spill machinery and generated fixtures. I did not run differential tests, compilation or benchmarks. The documented Range struct-update limitation predates this change and is not a new finding.

kixelated and others added 2 commits October 25, 2026 07:43
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 1e9123fa2c

ℹ️ 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread ts/src/lower/library.rs Resolved

@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: 181c51d

No new actionable bugs found. The independently reported receiver-ordering bug #49 (comment) is fixed: range_bounds saves earlier operands before emitting bound statements for slicing and copy_within. Buffering those statements centrally is a focused solution, and the logging-receiver fixture captures the ordering.

Verification: static inspection of the incremental patch, both callers, spill helper and generated fixture. No compilation or differential tests run. Previously documented Range struct-update limitations remain outside this fix.

@kixelated

Copy link
Copy Markdown
Collaborator Author

Merge summary:

  • Merged main after fix(ts): diverging calls through closures and generics at ! pass tsc #48 merged. No conflicts in the snapshots, and they regenerated unchanged.
  • Codex's P2 is fixed in 181c51d: a sliced value now evaluates before its range's spilled fields. The differential case reproduces it under THIR without the fix.
  • The follow-up is filed as quest/m2/ts-range-update.md (struct update on a std Range).
  • The maintainer accepted both recommendations: fewest-temporaries spilling, and the rare redundant re-spill.
  • Checks: just check, just test, quest check --base origin/main. CI passed. The OpenAI review found no new issues on 181c51d, and Codex gave it a thumbs up.

(written by Opus 5.5)

@kixelated
kixelated merged commit 5995ed3 into main Oct 25, 2026
1 check passed
kixelated added a commit that referenced this pull request Oct 25, 2026
Regenerates the snapshots for the cases #48 and #49 added, and names `!`
and `()` in a suffix (`pass$never`), which printed as an empty part.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn
@kixelated
kixelated deleted the quest/m2/ts-variant-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