Skip to content

feat: introduce structured task scopes and affine cancellation tokens - #2280

Open
SuperPiIot wants to merge 3 commits into
limitlessprojects:mainfrom
SuperPiIot:fix-issue-1669
Open

SuperPiIot wants to merge 3 commits into
limitlessprojects:mainfrom
SuperPiIot:fix-issue-1669

Conversation

@SuperPiIot

@SuperPiIot SuperPiIot commented Oct 30, 2026 •

Copy link
Copy Markdown

Summary

Implements Structured Task Scopes (scope { ... }) and Affine JoinHandle[T] Lifetime Checking (HYB-E3501, HYB-E3502, HYB-E3503) natively in the Rust compiler crates (hy-syntax and hy-check), addressing #1669 and the maintainer design note in #1669 (comment).

Architectural Changes

  1. Single Lexical Containment Authority (compiler/crates/hy-check/src/walk.rs):
    • Extended FlatInfo<'a> with pub scope_end: HashMap<usize, usize> alongside managed_end and loop_end, providing FlatInfo::in_scope(idx) and FlatInfo::enclosing_scope(idx) as the single authority for lexical scope { ... } containment.
  2. Contextual scope Blocks & Handle-Producing task Expressions (compiler/crates/hy-syntax/src/parser.rs):
    • Parsed contextual scope { ... } and scope <ident> { ... } in statement position without reserving scope as a lexer keyword (preserving scope := 1 and scope(x) compatibility).
    • Parsed expression-position task <call> (e.g., h := task worker(i)) into the internal __hy_task_spawn call representation while keeping statement-level task worker() intact.
  3. Structured Task Scope & Affine Handle Checker (compiler/crates/hy-check/src/task_scope.rs):
    • HYB-E3501 (Unjoined Task Escape): Rejects bare task or _ := task ... inside scope, handles unjoined at normal or early scope exit (return, break, continue), handle overwrites before consumption, partial consumption across non-diverging if/match/select branches, and handle escapes via return/channel-send/call arguments/assignments.
    • HYB-E3502 (Affine JoinHandle Double Consumption / Use-After-Consume): Enforces single consumption across join(h), cancel(h), drain(h), and method syntax (h.join(), h.cancel(), h.drain()), including branch-sensitive consumption and defer.
    • HYB-E3503 (Foreign Callback Spawn): Rejects spawning a task inside a closure literal passed to a foreign/selector call unless wrapped in an inner scope { ... } or managed { ... }.
    • managed { ... } Escape Hatch: Queries FlatInfo::in_managed(idx) to downgrade HYB-E3501 and HYB-E3503 inside managed blocks while keeping HYB-E3502 (double-consume) enforced.
  4. Sandbox PATH Fix (tests/test_install_runtime.py):
    • Added "mv" to required_tools in InstallRuntimeTests._make_fake_curl_env so install.sh atomic staging succeeds in isolated test PATH environments.

Verification

  • cargo test -p hy-check --test task_scope_test (all positive and negative HYB-E3501 / HYB-E3502 / HYB-E3503 cases pass)
  • cargo test -p hy-syntax (all 135+ lexer/parser/corpus tests pass)
  • python3 -m unittest tests.test_install_runtime (all 14 runtime installer tests pass)
  • python3 tools/shepherd.py test-check & python3 tools/shepherd.py probe pass cleanly

Fixes #1669

Settlement Coordinates

  • Network: Base Mainnet (Chain ID 8453)
  • Asset: USDC
  • Wallet: 0xf084cF0076F91AA48C2370F427C399dAA8C1f07F

limitlessprojects#1669)

- Wire contextual `scope { ... }` / `scope <ident> { ... }` and expression-position `task <call>` into `hy-syntax`
- Extend `walk.rs` (`FlatInfo::scope_end`) as the single authority for lexical `scope` containment
- Implement `HYB-E3501`, `HYB-E3502`, and `HYB-E3503` diagnostics in `hy-check/src/task_scope.rs` with `managed` downgrade semantics
- Add integration test suite `compiler/crates/hy-check/tests/task_scope_test.rs`
- Add `mv` to `required_tools` in `tests/test_install_runtime.py` for sandboxed PATH test environments
- Remove standalone Go stub files (`compiler/*.go`)

Fixes limitlessprojects#1669
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot:conflict Shepherd: merge conflicts vs main bot:land-candidate Shepherd: fresh, gate-clean, verifiable (set from classify()) bot:stale-base Shepherd: behind main beyond threshold

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[wildcard] Structured Task Scopes & Affine Cancellation Tokens: eliminating goroutine leaks from GUARANTEES.md Row 12

1 participant