Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Cleaner panic output for codegen errors #5716

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

Merged
merged 4 commits into from
Apr 30, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fix errors and formatting
Signed-off-by: Ashwin Naren <arihant2math@gmail.com>
  • Loading branch information
arihant2math committed Apr 19, 2025
commit fc331a154fdc8a08650773e4a621fbccdde9efac
45 changes: 23 additions & 22 deletions 45 compiler/codegen/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,15 +212,15 @@ macro_rules! emit {
};
}

fn print_location(zelf: &mut Compiler) {
fn print_location(zelf: &Compiler<'_>) {
arihant2math marked this conversation as resolved.
Show resolved Hide resolved
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<T>(zelf: &mut Compiler, r: InternalResult<T>) -> T {
if let Err(r_err) = r {
fn unwrap_internal<T>(zelf: &Compiler<'_>, r: InternalResult<T>) -> T {
if let Err(ref r_err) = r {
eprintln!("=== CODEGEN PANIC INFO ===");
eprintln!("This IS an internal error: {}", r_err);
print_location(zelf);
Expand All @@ -229,7 +229,7 @@ fn unwrap_internal<T>(zelf: &mut Compiler, r: InternalResult<T>) -> T {
r.unwrap()
}

fn compiler_unwrap_option<T>(zelf: &mut Compiler, o: Option<T>) -> T {
fn compiler_unwrap_option<T>(zelf: &Compiler<'_>, o: Option<T>) -> T {
if let None = o {
eprintln!("=== CODEGEN PANIC INFO ===");
eprintln!("This IS an internal error, an option was unwrapped during codegen");
Expand All @@ -239,15 +239,15 @@ fn compiler_unwrap_option<T>(zelf: &mut Compiler, o: Option<T>) -> T {
o.unwrap()
}

fn compiler_result_unwrap<T, E>(zelf: &mut Compiler, result: Result<T, E>) -> 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<T, E: std::fmt::Debug>(zelf: &Compiler<'_>, result: Result<T, E>) -> 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)]
Expand Down Expand Up @@ -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<Cow<str>>, but everything is borrowed from ast structs; we never
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions 4 compiler/codegen/src/ir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ impl CodeInfo {
locations.clear()
}

CodeObject {
Ok(CodeObject {
flags,
posonlyarg_count,
arg_count,
Expand All @@ -173,7 +173,7 @@ impl CodeInfo {
cellvars: cellvar_cache.into_iter().collect(),
freevars: freevar_cache.into_iter().collect(),
cell2arg,
}
})
}

fn cell2arg(&self) -> Option<Box<[i32]>> {
Expand Down
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.