◐ Shell
clean mode source ↗

Align nested code object bytecode parity by youknowone · Pull Request #7942 · RustPython/RustPython

📝 Walkthrough

Walkthrough

This PR adds AST-tail predicates and tuple-nonfolding, wires those predicates into finally/try/orelse/while/for/match codegen preservation and borrow decisions, introduces a per-instruction for-loop break-cleanup flag and IR guards to prevent unsafe rewrites, refines jump sizing/duplication logic, and adds tests plus stable constant normalization for disassembly.

Changes

Try/except control-flow and for-break pattern handling

Layer / File(s) Summary
Statement-shape and truthiness helpers
crates/codegen/src/compile.rs
Adds nested-with/async-comprehension detection, conditional finalbody and handlers-end-with-scope-exit predicates, store-name-then-same-name-method-call probe, and treats tuple constants as non-foldable for truthiness and unary not.
Finally/handler label preservation & try-else wiring
crates/codegen/src/compile.rs
Expands preserve_finally logic, introduces bare-handler successor preservation flags, tracks try-start barrier state, and disables load-fast borrowing for try/else when preservation predicates apply.
While/for end-label reuse and break cleanup tracking
crates/codegen/src/compile.rs
Reuse empty while end label only when orelse empty and body ends with break; mark load-fast barrier on effective-after block; add and use mark_last_for_loop_break_cleanup_jump() and initialize instruction option.
Match/case successor and jump emission
crates/codegen/src/compile.rs
Add OR-pattern/case-context flags, create explicit successor when current block ends with conditionals, emit JumpNoInterrupt for arms with async comprehensions, and adjust OR/default borrow-disabling.
Unit tests and helpers
crates/codegen/src/compile.rs
Add many CPython-parity tests and a helper to inspect load-fast/load-fast-borrow pairs.

IR-level flags and control-flow rewrite guards

Layer / File(s) Summary
Instruction flag and synthetic init
crates/codegen/src/ir.rs
Adds InstructionInfo.for_loop_break_cleanup_jump, initializes it for synthetic instructions and test helpers, and exposes is_conditional_jump as pub(crate).
Jump sizing and threading guards
crates/codegen/src/ir.rs
Add block->instr index and extended-forward-jumps tracking for sizing, widen EXTENDED_ARG logic, and detect threads_break_cleanup to block unsafe threading.
Duplication/clone and passthrough refinements
crates/codegen/src/ir.rs
Introduce block_has_break_continue_cleanup_jump, lineful_shared_jump_back_target, is_for_break_cleanup_block, and jump_targets_exception_region_entry; refine duplication/reorder decisions and end-return duplication gating.
Peephole and load-fast passthroughs
crates/codegen/src/ir.rs
Make tuple constant truthiness unknown (no fold), update folded-load escape detection, add has_protected_warm_fallthrough_predecessor and try_except_return_end passthrough, and prevent location propagation for sentinel overrides.

Symbol table and tooling

Layer / File(s) Summary
mangle_name guard and tests
crates/codegen/src/symboltable.rs
Return original name when class prefix becomes empty after stripping underscores; add unit tests.
dis_dump constant normalization
scripts/dis_dump.py
Add _normalize_const_repr() and use it for LOAD_CONST argrepr extraction to produce cross-interpreter stable constant representations (floats, tuples, frozensets).

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~40 minutes

Possibly related PRs

Poem

🐰 I hopped through blocks where handlers close,
I marked the jumps where cleanup flows,
Tuples no longer fold in haste,
Labels kept in CPython's taste,
A careful rabbit left no trace.

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (1 warning, 1 inconclusive)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 49.21% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
Title check ❓ Inconclusive The title 'Align nested code object bytecode parity' is vague and uses generic phrasing that doesn't convey specific information about the substantive changes made across four files. Consider a more specific title that highlights the main changes, such as 'Refactor compiler control-flow and bytecode parity for nested code objects' or similar.
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ 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 and usage tips.