diff --git a/compiler/codegen/src/compile.rs b/compiler/codegen/src/compile.rs index 67b5d3b67..b8cf20531 100644 --- a/compiler/codegen/src/compile.rs +++ b/compiler/codegen/src/compile.rs @@ -212,15 +212,15 @@ macro_rules! emit { }; } -fn print_location(zelf: &mut Compiler) { +fn print_location(zelf: &Compiler<'_>) { let start = zelf.source_code.source_location(zelf.current_source_range.start()); let end = zelf.source_code.source_location(zelf.current_source_range.end()); eprintln!("LOCATION: {} from {}:{} to {}:{}", zelf.source_code.path.to_owned(), start.row, start.column, end.row, end.column); } /// Better traceback for internal error -fn unwrap_internal(zelf: &mut Compiler, r: InternalResult) -> T { - if let Err(r_err) = r { +fn unwrap_internal(zelf: &Compiler<'_>, r: InternalResult) -> T { + if let Err(ref r_err) = r { eprintln!("=== CODEGEN PANIC INFO ==="); eprintln!("This IS an internal error: {}", r_err); print_location(zelf); @@ -229,7 +229,7 @@ fn unwrap_internal(zelf: &mut Compiler, r: InternalResult) -> T { r.unwrap() } -fn compiler_unwrap_option(zelf: &mut Compiler, o: Option) -> T { +fn compiler_unwrap_option(zelf: &Compiler<'_>, o: Option) -> T { if let None = o { eprintln!("=== CODEGEN PANIC INFO ==="); eprintln!("This IS an internal error, an option was unwrapped during codegen"); @@ -239,15 +239,15 @@ fn compiler_unwrap_option(zelf: &mut Compiler, o: Option) -> T { o.unwrap() } -fn compiler_result_unwrap(zelf: &mut Compiler, result: Result) -> T { - if let Err(r_err) = r { - eprintln!("=== CODEGEN PANIC INFO ==="); - eprintln!("This IS an internal error, an result was unwrapped during codegen"); - print_location(zelf); - eprintln!("=== END PANIC INFO ==="); - } - r.unwrap() -} +// fn compiler_result_unwrap(zelf: &Compiler<'_>, result: Result) -> T { +// if result.is_err() { +// eprintln!("=== CODEGEN PANIC INFO ==="); +// eprintln!("This IS an internal error, an result was unwrapped during codegen"); +// print_location(zelf); +// eprintln!("=== END PANIC INFO ==="); +// } +// result.unwrap() +// } /// The pattern context holds information about captured names and jump targets. #[derive(Clone)] @@ -411,9 +411,9 @@ impl Compiler<'_> { fn pop_code_object(&mut self) -> CodeObject { let table = self.pop_symbol_table(); assert!(table.sub_tables.is_empty()); - unwrap_internal(self, self.code_stack - .pop()) - .finalize_code(self.opts.optimize) + let pop = self.code_stack.pop(); + let stack_top = compiler_unwrap_option(self, pop); + unwrap_internal(self, stack_top.finalize_code(self.opts.optimize)) } // could take impl Into>, but everything is borrowed from ast structs; we never @@ -520,7 +520,8 @@ impl Compiler<'_> { self.current_block().instructions.pop(); // pop Instruction::Pop } Stmt::FunctionDef(_) | Stmt::ClassDef(_) => { - let store_inst = compiler_unwrap_option(zelf, self.current_block().instructions.pop()); // pop Instruction::Store + let pop_instructions = self.current_block().instructions.pop(); + let store_inst = compiler_unwrap_option(self, pop_instructions); // pop Instruction::Store emit!(self, Instruction::Duplicate); self.current_block().instructions.push(store_inst); } @@ -578,8 +579,8 @@ impl Compiler<'_> { self.check_forbidden_name(&name, usage)?; let symbol_table = self.symbol_table_stack.last().unwrap(); - let symbol = unwrap_internal(symbol_table.lookup(name.as_ref()).ok_or_else(|| - InternalError::MissingSymbol(name.to_owned()) + let symbol = unwrap_internal(self, symbol_table.lookup(name.as_ref()).ok_or_else(|| + InternalError::MissingSymbol(name.to_string()) )); let info = self.code_stack.last_mut().unwrap(); let mut cache = &mut info.name_cache; @@ -1514,8 +1515,8 @@ impl Compiler<'_> { } for var in &*code.freevars { let table = self.symbol_table_stack.last().unwrap(); - let symbol = unwrap_internal(table.lookup(var).ok_or_else(|| { - InternalError::MissingSymbol(var) + let symbol = unwrap_internal(self, table.lookup(var).ok_or_else(|| { + InternalError::MissingSymbol(var.to_owned()) })); let parent_code = self.code_stack.last().unwrap(); let vars = match symbol.scope { @@ -1599,7 +1600,7 @@ impl Compiler<'_> { // Check if the class is declared global let symbol_table = self.symbol_table_stack.last().unwrap(); - let symbol = unwrap_internal(symbol_table.lookup(name.as_ref()).ok_or_else(|| Err(InternalError::MissingSymbol(name.to_owned())))); + let symbol = unwrap_internal(self, symbol_table.lookup(name.as_ref()).ok_or_else(|| InternalError::MissingSymbol(name.to_owned()))); let mut global_path_prefix = Vec::new(); if symbol.scope == SymbolScope::GlobalExplicit { global_path_prefix.append(&mut self.qualified_path); diff --git a/compiler/codegen/src/ir.rs b/compiler/codegen/src/ir.rs index 00a74954a..915de373b 100644 --- a/compiler/codegen/src/ir.rs +++ b/compiler/codegen/src/ir.rs @@ -155,7 +155,7 @@ impl CodeInfo { locations.clear() } - CodeObject { + Ok(CodeObject { flags, posonlyarg_count, arg_count, @@ -173,7 +173,7 @@ impl CodeInfo { cellvars: cellvar_cache.into_iter().collect(), freevars: freevar_cache.into_iter().collect(), cell2arg, - } + }) } fn cell2arg(&self) -> Option> {