Skip to content

Commit ba9736b

Browse files
Merge pull request RustPython#1328 from youknowone/symtable-sequence-store
Fix symtable scan_expression not to mark sequence as Load
2 parents 8980adc + b73f3d6 commit ba9736b

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

compiler/src/symboltable.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,7 @@ impl SymbolTableBuilder {
585585
}
586586
Bytes { .. } => {}
587587
Tuple { elements } | Set { elements } | List { elements } | Slice { elements } => {
588-
self.scan_expressions(elements, &ExpressionContext::Load)?;
588+
self.scan_expressions(elements, context)?;
589589
}
590590
Comprehension { kind, generators } => {
591591
match **kind {
@@ -710,7 +710,8 @@ impl SymbolTableBuilder {
710710
let location = Default::default();
711711

712712
// Some checks:
713-
if table.symbols.contains_key(name) {
713+
let containing = table.symbols.contains_key(name);
714+
if containing {
714715
// Role already set..
715716
match role {
716717
SymbolUsage::Global => {
@@ -747,7 +748,7 @@ impl SymbolTableBuilder {
747748
}
748749

749750
// Insert symbol when required:
750-
if !table.symbols.contains_key(name) {
751+
if !containing {
751752
let symbol = Symbol::new(name);
752753
table.symbols.insert(name.to_string(), symbol);
753754
}

tests/snippets/comprehensions.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,17 @@
2727
w = [x for x, in z]
2828
assert w == [9, 10]
2929

30+
# generator targets shouldn't affect scopes out of comprehensions
31+
[a for a in range(5)]
32+
assert 'a' not in locals()
33+
assert 'a' not in globals()
34+
35+
[b for a, b in [(1, 1), (2, 2)]]
36+
assert 'b' not in locals()
37+
assert 'b' not in globals()
38+
39+
{b: c for b, c in {1: 2}.items()}
40+
assert 'b' not in locals()
41+
assert 'c' not in locals()
42+
assert 'b' not in globals()
43+
assert 'c' not in globals()

0 commit comments

Comments
 (0)