mirror of
https://github.com/RustPython/Parser.git
synced 2025-07-25 14:04:28 +00:00
Fix global declaration error in the function
Fix Global symbol to prevent syntax error when global declaration for same variable in function. Fixes #1353
This commit is contained in:
parent
bff58fd626
commit
18c243854e
1 changed files with 11 additions and 4 deletions
|
@ -746,10 +746,15 @@ impl SymbolTableBuilder {
|
||||||
// Role already set..
|
// Role already set..
|
||||||
match role {
|
match role {
|
||||||
SymbolUsage::Global => {
|
SymbolUsage::Global => {
|
||||||
return Err(SymbolTableError {
|
let symbol = table.symbols.get(name).unwrap();
|
||||||
error: format!("name '{}' is used prior to global declaration", name),
|
if let SymbolScope::Global = symbol.scope {
|
||||||
location,
|
// Ok
|
||||||
})
|
} else {
|
||||||
|
return Err(SymbolTableError {
|
||||||
|
error: format!("name '{}' is used prior to global declaration", name),
|
||||||
|
location,
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
SymbolUsage::Nonlocal => {
|
SymbolUsage::Nonlocal => {
|
||||||
return Err(SymbolTableError {
|
return Err(SymbolTableError {
|
||||||
|
@ -806,6 +811,8 @@ impl SymbolTableBuilder {
|
||||||
SymbolUsage::Global => {
|
SymbolUsage::Global => {
|
||||||
if let SymbolScope::Unknown = symbol.scope {
|
if let SymbolScope::Unknown = symbol.scope {
|
||||||
symbol.scope = SymbolScope::Global;
|
symbol.scope = SymbolScope::Global;
|
||||||
|
} else if let SymbolScope::Global = symbol.scope {
|
||||||
|
// Global scope can be set to global
|
||||||
} else {
|
} else {
|
||||||
return Err(SymbolTableError {
|
return Err(SymbolTableError {
|
||||||
error: format!("Symbol {} scope cannot be set to global, since its scope was already determined otherwise.", name),
|
error: format!("Symbol {} scope cannot be set to global, since its scope was already determined otherwise.", name),
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue