[PR #9] [MERGED] RETURN: allow references to calls and closures outside exit values #14

Closed
opened 2026-11-02 15:32:30 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/horizon-rust/horizon-mixins/pull/9
Author: @levzzz5154
Created: 10/30/2026
Status: ✅ Merged
Merged: 10/30/2026
Merged by: @levzzz5154

Base: main ← Head: feat/return-hooks-temporaries


📝 Commits (2)

  • 9a6f6da RETURN: allow references to calls and closures outside exit values
  • 01160a9 RETURN: a closure's own return is not an exit; closure locals keep out of ordinals

📊 Changes

7 files changed (+288 additions, -20 deletions)

View changed files

📝 README.md (+4 -3)
➕ crates/horizon-mixins-cli/tests/cases/returns.rs (+170 -0)
📝 crates/horizon-mixins-cli/tests/integration.rs (+3 -0)
📝 crates/horizon-mixins-core/src/plan.rs (+67 -11)
📝 crates/horizon-mixins-core/src/scopes.rs (+20 -3)
📝 docs/advanced-injections.md (+20 -2)
📝 docs/mixin-api.md (+4 -1)

📄 Description

🤖 Opened by levzzz5154's AI agent (Claude Code, Claude Opus 5.5), not by levzzz5154 themselves.

Closes #6.

What and why

A RETURN hook on Horizon's Game::picked_stack was refused with E_UNSUPPORTED_CONTROL_FLOW ("calls beneath a reference may change temporary lifetimes") because of decode_components(Some(&Tag::Compound(all))) in a statement. Once that passes, the same target is refused again for the closures in its if let condition (.filter(|s| ...), .and_then(|s| ...)); the validator only reports the last error, so the issue saw one of the two.

Both checks run over the whole body for every hook kind except HEAD. RETURN only rewrites the exit values: each return operand and the function tail become a helper call argument, ({ fn helper.. helper })(value). Nothing else in the body changes, so:

  • a call beneath a reference outside the exit values keeps its temporary scope and lifetime extension exactly (same tokens, same statement);
  • a closure outside the exit values is not rewritten (ReturnRewrite already skips closures, and a return inside one leaves the closure, not the target; always_exits never counts a closure as an exit).

So for RETURN:

  • references to calls and plain closures are allowed outside exit values;
  • inside exit values both are still refused (exit_value_error), so the moved value never contains them;
  • ?, async, await, unsafe and const are still refused everywhere, including inside closures (the visitor still descends into them);
  • other hook kinds on the same target (BEFORE_CALL, AFTER_CALL, value and variable modifiers) still run the whole-body check for themselves, since they rewrite inside the body.

Checks

  • cargo fmt --all --check
  • cargo clippy --workspace --all-targets --locked -- -D warnings
  • cargo test --workspace --locked: 92 passed. New tests/cases/returns.rs:
    • a compiled fixture with a Drop-traced temporary under a reference in a call argument, a let-extended reference, closures (one with its own return), an early return and a tail. It checks the hook runs on both exits and the drop order is unchanged (argument, body, helper, extended). Without the fix it is refused.
    • refusals: references to calls and closures inside return operands and the tail, ? and async inside a closure, and a BEFORE_CALL on the same body.
  • Against Horizon main (exported with git archive): a RETURN mod on horizon_client::game::block_items::Game<'_>::picked_stack plans and applies, and the stage passes cargo check -p horizon-client.

Follow-up (review)

  • A closure's own return is no longer a RETURN capture context, and lets inside closures no longer shift capture ordinals ($local_ref:later:0 is the function's own first later). Neither case was reachable before this PR, since RETURN refused closures. New test return_hooks_capture_locals_past_closures_with_their_own_returns_and_lets, which fails with E_LOCAL_NOT_VISIBLE without the fix.
  • The temporaries test now executes a closure's own return and checks that the helper runs once per call.
  • docs/mixin-api.md, README.md and the RETURN paragraph no longer describe the old restriction. 93 tests pass.

🤖 Generated with Claude Code


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/horizon-rust/horizon-mixins/pull/9 **Author:** [@levzzz5154](https://github.com/levzzz5154) **Created:** 10/30/2026 **Status:** ✅ Merged **Merged:** 10/30/2026 **Merged by:** [@levzzz5154](https://github.com/levzzz5154) **Base:** `main` ← **Head:** `feat/return-hooks-temporaries` --- ### 📝 Commits (2) - [`9a6f6da`](https://github.com/horizon-rust/horizon-mixins/commit/9a6f6dafc4b943f7199a7be101798ca8b3a526cc) RETURN: allow references to calls and closures outside exit values - [`01160a9`](https://github.com/horizon-rust/horizon-mixins/commit/01160a913cc9028eee193a9cfb9e8c3864bdb1e1) RETURN: a closure's own return is not an exit; closure locals keep out of ordinals ### 📊 Changes **7 files changed** (+288 additions, -20 deletions) <details> <summary>View changed files</summary> 📝 `README.md` (+4 -3) ➕ `crates/horizon-mixins-cli/tests/cases/returns.rs` (+170 -0) 📝 `crates/horizon-mixins-cli/tests/integration.rs` (+3 -0) 📝 `crates/horizon-mixins-core/src/plan.rs` (+67 -11) 📝 `crates/horizon-mixins-core/src/scopes.rs` (+20 -3) 📝 `docs/advanced-injections.md` (+20 -2) 📝 `docs/mixin-api.md` (+4 -1) </details> ### 📄 Description > 🤖 Opened by levzzz5154's AI agent (Claude Code, Claude Opus 5.5), not by levzzz5154 themselves. Closes #6. ## What and why A RETURN hook on Horizon's `Game::picked_stack` was refused with `E_UNSUPPORTED_CONTROL_FLOW` ("calls beneath a reference may change temporary lifetimes") because of `decode_components(Some(&Tag::Compound(all)))` in a statement. Once that passes, the same target is refused again for the closures in its `if let` condition (`.filter(|s| ...)`, `.and_then(|s| ...)`); the validator only reports the last error, so the issue saw one of the two. Both checks run over the whole body for every hook kind except HEAD. RETURN only rewrites the exit values: each `return` operand and the function tail become a helper call argument, `({ fn helper.. helper })(value)`. Nothing else in the body changes, so: - a call beneath a reference outside the exit values keeps its temporary scope and lifetime extension exactly (same tokens, same statement); - a closure outside the exit values is not rewritten (`ReturnRewrite` already skips closures, and a `return` inside one leaves the closure, not the target; `always_exits` never counts a closure as an exit). So for RETURN: - references to calls and plain closures are allowed outside exit values; - inside exit values both are still refused (`exit_value_error`), so the moved value never contains them; - `?`, async, `await`, unsafe and const are still refused everywhere, including inside closures (the visitor still descends into them); - other hook kinds on the same target (BEFORE_CALL, AFTER_CALL, value and variable modifiers) still run the whole-body check for themselves, since they rewrite inside the body. ## Checks - `cargo fmt --all --check` - `cargo clippy --workspace --all-targets --locked -- -D warnings` - `cargo test --workspace --locked`: 92 passed. New `tests/cases/returns.rs`: - a compiled fixture with a Drop-traced temporary under a reference in a call argument, a `let`-extended reference, closures (one with its own `return`), an early return and a tail. It checks the hook runs on both exits and the drop order is unchanged (`argument`, `body`, `helper`, `extended`). Without the fix it is refused. - refusals: references to calls and closures inside return operands and the tail, `?` and async inside a closure, and a BEFORE_CALL on the same body. - Against Horizon `main` (exported with `git archive`): a RETURN mod on `horizon_client::game::block_items::Game<'_>::picked_stack` plans and applies, and the stage passes `cargo check -p horizon-client`. ## Follow-up (review) - A closure's own `return` is no longer a RETURN capture context, and `let`s inside closures no longer shift capture ordinals (`$local_ref:later:0` is the function's own first `later`). Neither case was reachable before this PR, since RETURN refused closures. New test `return_hooks_capture_locals_past_closures_with_their_own_returns_and_lets`, which fails with `E_LOCAL_NOT_VISIBLE` without the fix. - The temporaries test now executes a closure's own `return` and checks that the helper runs once per call. - `docs/mixin-api.md`, `README.md` and the RETURN paragraph no longer describe the old restriction. 93 tests pass. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-02 15:32:30 +07:00
Sign in to join this conversation.
No labels
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
horizon-rust/horizon-mixins#14
No description provided.