Skip to content

Commit fd0bf98

Browse files
committed
Fix symbol table sub_table desync for non-simple annotation targets
Non-simple annotations (subscript/attribute/parenthesized targets like `a[0]: expr`) were scanned in the annotation scope during symbol table analysis, creating sub_tables for any comprehensions. But codegen only compiles simple name annotations into __annotate__, so those sub_tables were never consumed. This caused subsequent simple annotations' comprehension sub_tables to get the wrong index, resulting in "the symbol 'X' must be present in the symbol table" errors. Fix: skip entering annotation scope for non-simple annotations since they are never compiled into __annotate__.
1 parent 5eadae8 commit fd0bf98

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

Lib/test/test_type_annotations.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -776,7 +776,6 @@ def test_non_name_annotations(self):
776776

777777
class RegressionTests(unittest.TestCase):
778778
# gh-132479
779-
@unittest.expectedFailure # TODO: RUSTPYTHON; SyntaxError: the symbol 'unique_name_6' must be present in the symbol table
780779
def test_complex_comprehension_inlining(self):
781780
# Test that the various repro cases from the issue don't crash
782781
cases = [

crates/codegen/src/symboltable.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1462,7 +1462,15 @@ impl SymbolTableBuilder {
14621462
self.scan_expression(target, ExpressionContext::Store)?;
14631463
}
14641464
}
1465-
self.scan_annotation(annotation)?;
1465+
// Only scan annotation in annotation scope for simple name targets.
1466+
// Non-simple annotations (subscript, attribute, parenthesized) are
1467+
// never compiled into __annotate__, so scanning them would create
1468+
// sub_tables that desynchronize the annotation scope's sub_table index.
1469+
let is_simple_name =
1470+
*simple && matches!(&**target, Expr::Name(_));
1471+
if is_simple_name {
1472+
self.scan_annotation(annotation)?;
1473+
}
14661474
if let Some(value) = value {
14671475
self.scan_expression(value, ExpressionContext::Load)?;
14681476
}

0 commit comments

Comments
 (0)