From aeb9715ae66e23263763da06a792bd2cc5b83acb Mon Sep 17 00:00:00 2001 From: Ashwin Naren Date: Sun, 23 Feb 2025 22:49:27 -0800 Subject: [PATCH] no panic on symbol lookup fail Signed-off-by: Ashwin Naren --- compiler/codegen/src/compile.rs | 14 +++++++++++--- compiler/codegen/src/error.rs | 4 ++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/compiler/codegen/src/compile.rs b/compiler/codegen/src/compile.rs index ce2e5627f0..c9465c7edf 100644 --- a/compiler/codegen/src/compile.rs +++ b/compiler/codegen/src/compile.rs @@ -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 { diff --git a/compiler/codegen/src/error.rs b/compiler/codegen/src/error.rs index 581f229712..6f8749ed13 100644 --- a/compiler/codegen/src/error.rs +++ b/compiler/codegen/src/error.rs @@ -33,6 +33,7 @@ pub enum CodegenErrorType { DuplicateStore(String), InvalidMatchCase, NotImplementedYet, // RustPython marker for unimplemented features + SymbolLookupError { symbol: String }, } impl std::error::Error for CodegenErrorType {} @@ -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") } } }