Consume nested scope tables in optimized-out asserts#7438
Consume nested scope tables in optimized-out asserts#7438youknowone merged 1 commit intoRustPython:mainfrom
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yml Review profile: CHILL Plan: Pro Run ID: ⛔ Files ignored due to path filters (1)
📒 Files selected for processing (1)
📝 WalkthroughWalkthroughAdds logic to consume skipped nested symbol tables when optimized asserts are transformed, traversing expressions (lambdas, comprehensions, generators, etc.) to push/pop symbol tables so symbol-table state stays aligned with AST traversal. Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
📝 Coding Plan
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. Comment |
aa20ab0 to
774803b
Compare
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
crates/codegen/src/compile.rs (1)
9252-9264: Nice regression test; consider adding two edge-case asserts.Please also add optimized-assert cases where nested scopes appear in (1) lambda defaults and (2) the first comprehension iterator expression, since those are key cursor-order edge paths.
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@crates/codegen/src/compile.rs` around lines 9252 - 9264, Add two edge-case variants to the optimized-assert nested-scope test: one where the nested-scope appears inside a lambda default and one where it appears as the first iterator expression of a comprehension. Update the test function test_optimized_assert_preserves_nested_scope_order (which calls compile_exec_optimized) to include code strings that mirror the existing test but place the nested-list expression into a lambda default parameter (e.g. def f(..., fn=lambda: <nested-scope> or default using a list comprehension result) and another code string where the first comprehension iterator uses the nested-scope expression (e.g. _formats = [self._types_mapping[type(item)] for item in sequence]; then a comprehension like [x for x in (_formats_generator) for y in ...] ), ensuring both are passed to compile_exec_optimized so the optimizer’s cursor-order handling for lambda defaults and first-comprehension-iterator paths is exercised.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@crates/codegen/src/compile.rs`:
- Around line 7649-7656: The current match arm that immediately calls
self.consume_scope() for ast::Expr::Lambda and comprehension nodes skips
traversal of child expressions that are evaluated in the enclosing scope (e.g.,
lambda default argument expressions and the first iterator/iterable in
comprehensions), causing next_sub_table misalignment; change the handling so you
first explicitly walk the child expressions that are evaluated in the outer
scope (for Lambda: walk default values / kw_defaults and annotations; for
ListComp/SetComp/DictComp/Generator: walk the initial iterator/iterable and any
target/expression parts that belong to the outer scope) and only then call
self.consume_scope(), instead of unconditionally skipping child traversal or
calling ast::visitor::walk_expr(self, expr); update the match arms for
ast::Expr::Lambda and each comprehension variant in compile.rs to perform these
targeted walks and then consume_scope to preserve correct scope-consumption
ordering.
---
Nitpick comments:
In `@crates/codegen/src/compile.rs`:
- Around line 9252-9264: Add two edge-case variants to the optimized-assert
nested-scope test: one where the nested-scope appears inside a lambda default
and one where it appears as the first iterator expression of a comprehension.
Update the test function test_optimized_assert_preserves_nested_scope_order
(which calls compile_exec_optimized) to include code strings that mirror the
existing test but place the nested-list expression into a lambda default
parameter (e.g. def f(..., fn=lambda: <nested-scope> or default using a list
comprehension result) and another code string where the first comprehension
iterator uses the nested-scope expression (e.g. _formats =
[self._types_mapping[type(item)] for item in sequence]; then a comprehension
like [x for x in (_formats_generator) for y in ...] ), ensuring both are passed
to compile_exec_optimized so the optimizer’s cursor-order handling for lambda
defaults and first-comprehension-iterator paths is exercised.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yml
Review profile: CHILL
Plan: Pro
Run ID: fc469d55-ceed-4c2f-8c45-73f8d39213b9
⛔ Files ignored due to path filters (1)
Lib/test/_test_multiprocessing.pyis excluded by!Lib/**
📒 Files selected for processing (1)
crates/codegen/src/compile.rs
When -O flag removes assert statements, any nested scopes (generators, comprehensions, lambdas) inside the assert expression still have symbol tables in the sub_tables list. Without consuming them, the next_sub_table index gets misaligned, causing later scopes to use wrong symbol tables. Walk the skipped assert expression with an AST visitor to find and consume nested scope symbol tables, keeping the index aligned with AST traversal order.
774803b to
7a2f43c
Compare
📦 Library DependenciesThe following Lib/ modules were modified. Here are their dependencies: [x] lib: cpython/Lib/types.py dependencies:
dependent tests: (52 tests)
Legend:
|
When -O flag removes assert statements, any nested scopes (generators, comprehensions, lambdas) inside the assert expression still have symbol tables in the sub_tables list. Without consuming them, the next_sub_table index gets misaligned, causing later scopes to use wrong symbol tables.
Walk the skipped assert expression with an AST visitor to find and consume nested scope symbol tables, keeping the index aligned with AST traversal order.
Summary by CodeRabbit
Bug Fixes
Tests