Skip to content

Commit 4a0ee1d

Browse files
committed
Fix nonlocal checks
Related to RustPython#2189
1 parent f682a94 commit 4a0ee1d

File tree

1 file changed

+28
-5
lines changed

1 file changed

+28
-5
lines changed

compiler/src/symboltable.rs

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -954,10 +954,10 @@ impl SymbolTableBuilder {
954954
// Some checks:
955955
let containing = table.symbols.contains_key(name);
956956
if containing {
957+
let symbol = table.symbols.get(name).unwrap();
957958
// Role already set..
958959
match role {
959960
SymbolUsage::Global => {
960-
let symbol = table.symbols.get(name).unwrap();
961961
if let SymbolScope::Global = symbol.scope {
962962
// Ok
963963
} else {
@@ -968,10 +968,33 @@ impl SymbolTableBuilder {
968968
}
969969
}
970970
SymbolUsage::Nonlocal => {
971-
return Err(SymbolTableError {
972-
error: format!("name '{}' is used prior to nonlocal declaration", name),
973-
location,
974-
})
971+
if symbol.is_parameter {
972+
return Err(SymbolTableError {
973+
error: format!("name '{}' is parameter and nonlocal", name),
974+
location,
975+
});
976+
}
977+
if symbol.is_referenced {
978+
return Err(SymbolTableError {
979+
error: format!("name '{}' is used prior to nonlocal declaration", name),
980+
location,
981+
});
982+
}
983+
if symbol.is_annotated {
984+
return Err(SymbolTableError {
985+
error: format!("annotated name '{}' can't be nonlocal", name),
986+
location,
987+
});
988+
}
989+
if symbol.is_assigned {
990+
return Err(SymbolTableError {
991+
error: format!(
992+
"name '{}' is assigned to before nonlocal declaration",
993+
name
994+
),
995+
location,
996+
});
997+
}
975998
}
976999
_ => {
9771000
// Ok?

0 commit comments

Comments
 (0)