fix symtable for named_expressions by youknowone · Pull Request #6604 · RustPython/RustPython
📝 Walkthrough
Walkthrough
The changes enhance the symbol table builder with context-aware error checking. New contextual state flags track whether expressions are processed within type parameters, annotations, type aliases, or inner comprehension loops. Error messages now reflect the precise context, and new validation logic prohibits named expressions in type parameters and detects inner-loop comprehension rebindings.
Changes
| Cohort / File(s) | Summary |
|---|---|
Symbol table context and validation crates/codegen/src/symboltable.rs |
Added contextual state fields (in_annotation, in_type_alias, in_comp_inner_loop_target, scope_info) to track expression processing context. Enhanced register_name() to enforce INNER_LOOP_CONFLICT checks for comprehension rebindings. Modified scan_type_param_bound_or_default() signature to accept scope_name and scope_info for richer error messages. Updated scan_comprehension() to toggle in_comp_inner_loop_target for inner generators. Extended scan_type_params() to detect duplicate type parameter names. Enhanced TypeVar, ParamSpec, TypeVarTuple, and TypeAlias handling to pass context information and track in_type_alias state. Introduced dedicated error messages indicating exact context (e.g., "within a type parameter", "in a TypeVar bound"). |
Estimated code review effort
🎯 3 (Moderate) | ⏱️ ~20 minutes
Possibly related PRs
- compile_type_param_bound_or_default #6005: Modifies
scan_type_param_bound_or_default()and type-parameter-scoping logic, directly intersecting with this PR's type-parameter validation enhancements. - Remove misplaced SymbolScope::TypeParams #5975: Modifies symbol table handling for type-parameter scopes and introduces context-aware error checks similar to this PR's approach.
- Refactor compile_function #6001: Modifies symbol table expression-scanning to restrict named expressions in type parameter scopes, aligning with this PR's validation goals.
Poem
🐰 A rabbit checks each context with care,
Named expressions forbidden in type-param air,
Comprehensions rebind? Not here, my friend!
With scope-aware errors, the mistakes we'll mend,
From bounds to defaults, precision's our way,
The symbol table grows wiser today!
Pre-merge checks and finishing touches
❌ Failed checks (1 warning)
| Check name | Status | Explanation | Resolution |
|---|---|---|---|
| Docstring Coverage | Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. | You can run @coderabbitai generate docstrings to improve docstring coverage. |
✅ Passed checks (2 passed)
| Check name | Status | Explanation |
|---|---|---|
| Description Check | ✅ Passed | Check skipped - CodeRabbit’s high-level summary is enabled. |
| Title check | ✅ Passed | The title directly describes the main change: fixing symbol table handling for named expressions (walrus operator) with context-aware error detection and reporting. |
✨ Finishing touches
- 📝 Generate docstrings
📜 Recent review details
Configuration used: Path: .coderabbit.yml
Review profile: CHILL
Plan: Pro
⛔ Files ignored due to path filters (1)
Lib/test/test_named_expressions.pyis excluded by!Lib/**
📒 Files selected for processing (1)
crates/codegen/src/symboltable.rs
🧰 Additional context used
📓 Path-based instructions (1)
**/*.rs
📄 CodeRabbit inference engine (.github/copilot-instructions.md)
**/*.rs: Follow the default rustfmt code style by runningcargo fmtto format Rust code
Always run clippy to lint code (cargo clippy) before completing tasks and fix any warnings or lints introduced by changes
Follow Rust best practices for error handling and memory management
Use the macro system (pyclass,pymodule,pyfunction, etc.) when implementing Python functionality in Rust
Files:
crates/codegen/src/symboltable.rs
🧬 Code graph analysis (1)
crates/codegen/src/symboltable.rs (1)
crates/codegen/src/compile.rs (5)
new(338-345)new(366-408)name(909-911)default(128-133)default(332-334)
🔇 Additional comments (10)
crates/codegen/src/symboltable.rs (10)
588-592: LGTM!The new error path correctly handles the case where a named expression in a comprehension is encountered within a type parameter scope. The error message aligns with CPython's behavior.
624-631: LGTM!The new state tracking fields are well-documented and appropriately typed. Using
&'static strforscope_infois efficient for the string literal error message contexts.
770-774: LGTM!The save-and-restore pattern correctly handles nested annotations and properly restores the previous state after scanning.
1043-1056: LGTM!The
in_type_aliasflag is correctly scoped to the value expression only, not the name being defined. The save-and-restore pattern properly handles nested type aliases.
1093-1126: LGTM!The context-aware validation logic correctly prioritizes specific scope info over general context flags. The error messages are clear and include source locations for better diagnostics.
1472-1477: LGTM!The inner loop target tracking correctly identifies non-first generators in comprehensions.
Note: If
scan_expressionreturns an error,in_comp_inner_loop_targetwon't be reset, but since errors propagate up and terminate symbol table building, this is acceptable.
1501-1527: LGTM!The function properly manages scope lifecycle with cleanup happening before returning the result, ensuring
scope_infois restored andleave_scopeis called even whenscan_expressionfails.
1530-1548: LGTM!The duplicate type parameter detection is correctly implemented with clear error messages and proper source locations.
1567-1621: LGTM!The scope naming and context info are well-structured, correctly distinguishing between TypeVar bounds vs. constraints based on tuple expressions, and providing meaningful scope names for each type parameter variant.
1760-1775: LGTM!The inner loop conflict check correctly implements PEP 572's restriction, preventing comprehension inner loops from rebinding variables that were previously used as named expression targets.
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 @coderabbitai help to get the list of available commands and usage tips.