Fix scope error

This commit is contained in:
lynskylate 2020-08-08 01:53:13 +08:00
parent a6be7eea26
commit f6eb1168bc
2 changed files with 9 additions and 3 deletions

View file

@ -298,7 +298,15 @@ impl<O: OutputStream> Compiler<O> {
SymbolScope::Global => bytecode::NameScope::Global,
SymbolScope::Nonlocal => bytecode::NameScope::NonLocal,
SymbolScope::Unknown => bytecode::NameScope::Free,
SymbolScope::Local => bytecode::NameScope::Local,
SymbolScope::Local => {
// Only in function block, we use load local
// https://github.com/python/cpython/blob/master/Python/compile.c#L3582
if self.ctx.in_func() {
bytecode::NameScope::Local
} else {
bytecode::NameScope::Free
}
}
}
}

View file

@ -281,8 +281,6 @@ impl<'a> SymbolTableAnalyzer<'a> {
fn analyze_unknown_symbol(&self, symbol: &mut Symbol) {
if symbol.is_assigned || symbol.is_parameter {
symbol.scope = SymbolScope::Local;
} else if symbol.is_referenced {
symbol.scope = SymbolScope::Unknown;
} else {
// Interesting stuff about the __class__ variable:
// https://docs.python.org/3/reference/datamodel.html?highlight=__class__#creating-the-class-object