Skip to content

Commit aeb9715

Browse files
committed
no panic on symbol lookup fail
Signed-off-by: Ashwin Naren <arihant2math@gmail.com>
1 parent 6daee1b commit aeb9715

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

compiler/codegen/src/compile.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -507,9 +507,17 @@ impl Compiler {
507507
self.check_forbidden_name(&name, usage)?;
508508

509509
let symbol_table = self.symbol_table_stack.last().unwrap();
510-
let symbol = symbol_table.lookup(name.as_ref()).unwrap_or_else(||
511-
panic!("The symbol '{name}' must be present in the symbol table, even when it is undefined in python."),
512-
);
510+
let symbol = match symbol_table.lookup(name.as_ref()) {
511+
Some(s) => s,
512+
None => {
513+
return Err(self.error_loc(
514+
CodegenErrorType::SymbolLookupError {
515+
symbol: name.to_string(),
516+
},
517+
self.current_source_location,
518+
));
519+
}
520+
};
513521
let info = self.code_stack.last_mut().unwrap();
514522
let mut cache = &mut info.name_cache;
515523
enum NameOpType {

compiler/codegen/src/error.rs

+4
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ pub enum CodegenErrorType {
3333
DuplicateStore(String),
3434
InvalidMatchCase,
3535
NotImplementedYet, // RustPython marker for unimplemented features
36+
SymbolLookupError { symbol: String },
3637
}
3738

3839
impl std::error::Error for CodegenErrorType {}
@@ -85,6 +86,9 @@ impl fmt::Display for CodegenErrorType {
8586
}
8687
NotImplementedYet => {
8788
write!(f, "RustPython does not implement this feature yet")
89+
},
90+
SymbolLookupError { symbol } => {
91+
write!(f, "The symbol {symbol} must be present in the symbol table, even when it is undefined in python")
8892
}
8993
}
9094
}

0 commit comments

Comments
 (0)