mirror of
https://github.com/RustPython/Parser.git
synced 2025-07-14 16:45:25 +00:00
fixed scoping issue for nonlocals - now also indirect outer scopes are supported
This commit is contained in:
parent
097b6a1889
commit
50c0b99136
1 changed files with 13 additions and 5 deletions
|
@ -221,11 +221,19 @@ impl<'a> SymbolTableAnalyzer<'a> {
|
||||||
} else {
|
} else {
|
||||||
match symbol.scope {
|
match symbol.scope {
|
||||||
SymbolScope::Nonlocal => {
|
SymbolScope::Nonlocal => {
|
||||||
// check if name is defined in parent table!
|
let scope_depth = self.tables.len();
|
||||||
let parent_symbol_table = self.tables.last();
|
if scope_depth > 0 {
|
||||||
if let Some((symbols, _)) = parent_symbol_table {
|
// check if the name is already defined in any outer scope
|
||||||
let scope_depth = self.tables.len();
|
// therefore
|
||||||
if !symbols.contains_key(&symbol.name) || scope_depth < 2 {
|
if scope_depth < 2
|
||||||
|
|| !self
|
||||||
|
.tables
|
||||||
|
.iter()
|
||||||
|
.skip(1) // omit the global scope as it is not non-local
|
||||||
|
.rev() // revert the order for better performance
|
||||||
|
.any(|t| t.0.contains_key(&symbol.name))
|
||||||
|
// true when any of symbol tables contains the name -> then negate
|
||||||
|
{
|
||||||
return Err(SymbolTableError {
|
return Err(SymbolTableError {
|
||||||
error: format!("no binding for nonlocal '{}' found", symbol.name),
|
error: format!("no binding for nonlocal '{}' found", symbol.name),
|
||||||
location: Default::default(),
|
location: Default::default(),
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue