Skip to content

codegen: preserve symbol tables across copied finally bodies - #8507

Merged
youknowone merged 1 commit into
RustPython:mainfrom
youknowone:agent/finally-symbol-table-cursors
Aug 13, 2026
Merged

codegen: preserve symbol tables across copied finally bodies#8507
youknowone merged 1 commit into
RustPython:mainfrom
youknowone:agent/finally-symbol-table-cursors

Conversation

@youknowone

@youknowone youknowone commented Aug 13, 2026

Copy link
Copy Markdown
Member

Summary

  • preserve the current symbol-table cursors while codegen emits the extra copy of a finally body used by an early return, break, or continue
  • add coverage for lambdas, comprehensions, nested functions, and nested classes in copied finally bodies

Root cause

Leaving a try block early compiles its finally body once for the early-exit path and again for the statement's normal cleanup paths. The first copy consumed the nested-scope symbol-table cursors. When the later copy reached a comprehension or another nested scope, codegen reported SyntaxError: no symbol table available.

Saving and restoring all three cursor components around the early-exit copy makes that speculative emission cursor-neutral, matching the existing treatment of duplicated codegen paths.

This surfaced while importing pytest: _pytest/pytester.py has a generator hook that returns from a try and uses comprehensions in finally.

Validation

  • cargo fmt --all -- --check
  • cargo clippy --workspace --exclude rustpython_wasm --exclude rustpython-venvlauncher --exclude rustpython-capi --all-targets
  • cargo test --workspace --exclude rustpython_wasm --exclude rustpython-venvlauncher --exclude rustpython-capi
  • (cd crates/capi && cargo test) — 102 passed
  • CI-equivalent release build with threading,jit and the explicit stdlib/sqlite/rustls feature set
  • extra_tests: 410 passed
  • byte-compiled pytest 9.1.1's _pytest/pytester.py

Summary by CodeRabbit

  • Bug Fixes

    • Fixed try/finally handling to preserve correct behavior after returns, breaks, and continues.
    • Ensured finally blocks execute exactly once during returns.
    • Corrected generator behavior when returning from try blocks, including proper StopIteration values.
    • Preserved nested comprehension and scope behavior in duplicated finally code.
  • Tests

    • Added regression coverage for control flow, generators, nested scopes, and try/finally execution.

@coderabbitai

coderabbitai Bot commented Aug 13, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yml

Review profile: CHILL

Plan: Pro Plus

Run ID: 0d54839a-8b9d-49c2-8236-de9b078a6285

📥 Commits

Reviewing files that changed from the base of the PR and between 212c0d0 and 71cd9bd.

📒 Files selected for processing (2)
  • crates/codegen/src/compile.rs
  • extra_tests/snippets/syntax_try.py

📝 Walkthrough

Walkthrough

The compiler now restores symbol-table cursors after compiling duplicated finally bodies. Regression tests cover returns, loop control flow, nested scopes, single cleanup execution, and generator return values.

Changes

try/finally compilation

Layer / File(s) Summary
Restore symbol-table cursors
crates/codegen/src/compile.rs
The compiler saves and restores symbol-table cursors around extra finally body compilation.
Validate try/finally control flow
extra_tests/snippets/syntax_try.py
Tests cover return, break, continue, nested scopes, single finally execution, and generator StopIteration values.

Estimated code review effort: 2 (Simple) | ~10 minutes

Possibly related PRs

Suggested reviewers: shaharnaveh

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@youknowone
youknowone marked this pull request as ready for review August 13, 2026 04:16
@youknowone
youknowone merged commit 525ba8c into RustPython:main Aug 13, 2026
27 of 28 checks passed
@youknowone
youknowone deleted the agent/finally-symbol-table-cursors branch August 13, 2026 04:16
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