8000 No panic on symbol lookup fail by arihant2math · Pull Request #5555 · RustPython/RustPython · GitHub
[go: up one dir, main page]

Skip to content

No panic on symbol lookup fail #5555

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
no panic on symbol lookup fail
Signed-off-by: Ashwin Naren <arihant2math@gmail.com>
  • Loading branch information
arihant2math committed Mar 3, 2025
commit aeb9715ae66e23263763da06a792bd2cc5b83acb
14 changes: 11 additions & 3 deletions compiler/codegen/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -507,9 +507,17 @@ impl Compiler {
self.check_forbidden_name(&name, usage)?;

let symbol_table = self.symbol_table_stack.last().unwrap();
let symbol = symbol_table.lookup(name.as_ref()).unwrap_or_else(||
panic!("The symbol '{name}' must be present in the symbol table, even when it is undefined in python."),
);
let symbol = match symbol_table.lookup(name.as_ref()) {
Some(s) => s,
None => {
return Err(self.error_loc(
CodegenErrorType::SymbolLookupError {
symbol: name.to_string(),
},
self.current_source_location,
));
}
};
let info = self.code_stack.last_mut().unwrap();
let mut cache = &mut info.name_cache;
enum NameOpType {
Expand Down
4 changes: 4 additions & 0 deletions compiler/codegen/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ pub enum CodegenErrorType {
DuplicateStore(String),
InvalidMatchCase,
NotImplementedYet, // RustPython marker for unimplemented features
SymbolLookupError { symbol: String },
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because this is not a user-side Python error but our own RustPython implementation bug, let's not expose it here.

}

impl std::error::Error for CodegenErrorType {}
Expand Down Expand Up @@ -85,6 +86,9 @@ impl fmt::Display for CodegenErrorType {
}
NotImplementedYet => {
write!(f, "RustPython does not implement this feature yet")
},
SymbolLookupError { symbol } => {
write!(f, "The symbol {symbol} must be present in the symbol table, even when it is undefined in python")
}
}
}
Expand Down
Loading
0