Skip to content

Commit fc331a1

Browse files
committed
fix errors and formatting
Signed-off-by: Ashwin Naren <arihant2math@gmail.com>
1 parent 12ceb96 commit fc331a1

2 files changed

Lines changed: 25 additions & 24 deletions

File tree

‎compiler/codegen/src/compile.rs‎

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -212,15 +212,15 @@ macro_rules! emit {
212212
};
213213
}
214214

215-
fn print_location(zelf: &mut Compiler) {
215+
fn print_location(zelf: &Compiler<'_>) {
216216
let start = zelf.source_code.source_location(zelf.current_source_range.start());
217217
let end = zelf.source_code.source_location(zelf.current_source_range.end());
218218
eprintln!("LOCATION: {} from {}:{} to {}:{}", zelf.source_code.path.to_owned(), start.row, start.column, end.row, end.column);
219219
}
220220

221221
/// Better traceback for internal error
222-
fn unwrap_internal<T>(zelf: &mut Compiler, r: InternalResult<T>) -> T {
223-
if let Err(r_err) = r {
222+
fn unwrap_internal<T>(zelf: &Compiler<'_>, r: InternalResult<T>) -> T {
223+
if let Err(ref r_err) = r {
224224
eprintln!("=== CODEGEN PANIC INFO ===");
225225
eprintln!("This IS an internal error: {}", r_err);
226226
print_location(zelf);
@@ -229,7 +229,7 @@ fn unwrap_internal<T>(zelf: &mut Compiler, r: InternalResult<T>) -> T {
229229
r.unwrap()
230230
}
231231

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

242-
fn compiler_result_unwrap<T, E>(zelf: &mut Compiler, result: Result<T, E>) -> T {
243-
if let Err(r_err) = r {
244-
eprintln!("=== CODEGEN PANIC INFO ===");
245-
eprintln!("This IS an internal error, an result was unwrapped during codegen");
246-
print_location(zelf);
247-
eprintln!("=== END PANIC INFO ===");
248-
}
249-
r.unwrap()
250-
}
242+
// fn compiler_result_unwrap<T, E: std::fmt::Debug>(zelf: &Compiler<'_>, result: Result<T, E>) -> T {
243+
// if result.is_err() {
244+
// eprintln!("=== CODEGEN PANIC INFO ===");
245+
// eprintln!("This IS an internal error, an result was unwrapped during codegen");
246+
// print_location(zelf);
247+
// eprintln!("=== END PANIC INFO ===");
248+
// }
249+
// result.unwrap()
250+
// }
251251

252252
/// The pattern context holds information about captured names and jump targets.
253253
#[derive(Clone)]
@@ -411,9 +411,9 @@ impl Compiler<'_> {
411411
fn pop_code_object(&mut self) -> CodeObject {
412412
let table = self.pop_symbol_table();
413413
assert!(table.sub_tables.is_empty());
414-
unwrap_internal(self, self.code_stack
415-
.pop())
416-
.finalize_code(self.opts.optimize)
414+
let pop = self.code_stack.pop();
415+
let stack_top = compiler_unwrap_option(self, pop);
416+
unwrap_internal(self, stack_top.finalize_code(self.opts.optimize))
417417
}
418418

419419
// could take impl Into<Cow<str>>, but everything is borrowed from ast structs; we never
@@ -520,7 +520,8 @@ impl Compiler<'_> {
520520
self.current_block().instructions.pop(); // pop Instruction::Pop
521521
}
522522
Stmt::FunctionDef(_) | Stmt::ClassDef(_) => {
523-
let store_inst = compiler_unwrap_option(zelf, self.current_block().instructions.pop()); // pop Instruction::Store
523+
let pop_instructions = self.current_block().instructions.pop();
524+
let store_inst = compiler_unwrap_option(self, pop_instructions); // pop Instruction::Store
524525
emit!(self, Instruction::Duplicate);
525526
self.current_block().instructions.push(store_inst);
526527
}
@@ -578,8 +579,8 @@ impl Compiler<'_> {
578579
self.check_forbidden_name(&name, usage)?;
579580

580581
let symbol_table = self.symbol_table_stack.last().unwrap();
581-
let symbol = unwrap_internal(symbol_table.lookup(name.as_ref()).ok_or_else(||
582-
InternalError::MissingSymbol(name.to_owned())
582+
let symbol = unwrap_internal(self, symbol_table.lookup(name.as_ref()).ok_or_else(||
583+
InternalError::MissingSymbol(name.to_string())
583584
));
584585
let info = self.code_stack.last_mut().unwrap();
585586
let mut cache = &mut info.name_cache;
@@ -1514,8 +1515,8 @@ impl Compiler<'_> {
15141515
}
15151516
for var in &*code.freevars {
15161517
let table = self.symbol_table_stack.last().unwrap();
1517-
let symbol = unwrap_internal(table.lookup(var).ok_or_else(|| {
1518-
InternalError::MissingSymbol(var)
1518+
let symbol = unwrap_internal(self, table.lookup(var).ok_or_else(|| {
1519+
InternalError::MissingSymbol(var.to_owned())
15191520
}));
15201521
let parent_code = self.code_stack.last().unwrap();
15211522
let vars = match symbol.scope {
@@ -1599,7 +1600,7 @@ impl Compiler<'_> {
15991600

16001601
// Check if the class is declared global
16011602
let symbol_table = self.symbol_table_stack.last().unwrap();
1602-
let symbol = unwrap_internal(symbol_table.lookup(name.as_ref()).ok_or_else(|| Err(InternalError::MissingSymbol(name.to_owned()))));
1603+
let symbol = unwrap_internal(self, symbol_table.lookup(name.as_ref()).ok_or_else(|| InternalError::MissingSymbol(name.to_owned())));
16031604
let mut global_path_prefix = Vec::new();
16041605
if symbol.scope == SymbolScope::GlobalExplicit {
16051606
global_path_prefix.append(&mut self.qualified_path);

‎compiler/codegen/src/ir.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ impl CodeInfo {
155155
locations.clear()
156156
}
157157

158-
CodeObject {
158+
Ok(CodeObject {
159159
flags,
160160
posonlyarg_count,
161161
arg_count,
@@ -173,7 +173,7 @@ impl CodeInfo {
173173
cellvars: cellvar_cache.into_iter().collect(),
174174
freevars: freevar_cache.into_iter().collect(),
175175
cell2arg,
176-
}
176+
})
177177
}
178178

179179
fn cell2arg(&self) -> Option<Box<[i32]>> {

0 commit comments

Comments
 (0)