From ee7377e249b6d3338f08575eb1128eeb46ab5a44 Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Wed, 3 Dec 2025 16:03:04 +0200 Subject: [PATCH 1/3] Use rust idioms for accessing a `Vec` --- crates/codegen/src/compile.rs | 35 ++++++++++++++++------------------- 1 file changed, 16 insertions(+), 19 deletions(-) diff --git a/crates/codegen/src/compile.rs b/crates/codegen/src/compile.rs index 67f5ddd4df..1eb9f4a63e 100644 --- a/crates/codegen/src/compile.rs +++ b/crates/codegen/src/compile.rs @@ -557,11 +557,9 @@ impl Compiler { /// Get the SymbolTable for the current scope. fn current_symbol_table(&self) -> &SymbolTable { - if self.symbol_table_stack.is_empty() { - panic!("symbol_table_stack is empty! This is a compiler bug."); - } - let index = self.symbol_table_stack.len() - 1; - &self.symbol_table_stack[index] + self.symbol_table_stack + .last() + .expect("symbol_table_stack is empty! This is a compiler bug.") } /// Get the index of a free variable. @@ -626,9 +624,9 @@ impl Compiler { let table = current_table.sub_tables.remove(0); // Push the next table onto the stack - let last_idx = self.symbol_table_stack.len(); self.symbol_table_stack.push(table); - &self.symbol_table_stack[last_idx] + // SAFETY: We just pushed, so it can't be empty + unsafe { &self.symbol_table_stack.last().unwrap_unchecked() } } /// Pop the current symbol table off the stack @@ -657,12 +655,13 @@ impl Compiler { let source_path = self.source_file.name().to_owned(); // Lookup symbol table entry using key (_PySymtable_Lookup) - let ste = if key < self.symbol_table_stack.len() { - &self.symbol_table_stack[key] - } else { - return Err(self.error(CodegenErrorType::SyntaxError( - "unknown symbol table entry".to_owned(), - ))); + let ste = match self.symbol_table_stack.get(key) { + Some(v) => v, + None => { + return Err(self.error(CodegenErrorType::SyntaxError( + "unknown symbol table entry".to_owned(), + ))); + } }; // Use varnames from symbol table (already collected in definition order) @@ -1199,12 +1198,10 @@ impl Compiler { // If not found and we're in TypeParams scope, try parent scope let symbol = if symbol.is_none() && is_typeparams { - if self.symbol_table_stack.len() > 1 { - let parent_idx = self.symbol_table_stack.len() - 2; - self.symbol_table_stack[parent_idx].lookup(name.as_ref()) - } else { - None - } + self.symbol_table_stack + .get(self.symbol_table_stack.len() - 2) // Try to get parent index + .expect("Symbol has no parent! This is a compiler bug.") + .lookup(name.as_ref()) } else { symbol }; From e24221b940ded85e348dcb73a692519e70737b77 Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Wed, 3 Dec 2025 16:41:38 +0200 Subject: [PATCH 2/3] clippy --- crates/codegen/src/compile.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/codegen/src/compile.rs b/crates/codegen/src/compile.rs index 1eb9f4a63e..f371967584 100644 --- a/crates/codegen/src/compile.rs +++ b/crates/codegen/src/compile.rs @@ -626,7 +626,7 @@ impl Compiler { // Push the next table onto the stack self.symbol_table_stack.push(table); // SAFETY: We just pushed, so it can't be empty - unsafe { &self.symbol_table_stack.last().unwrap_unchecked() } + unsafe { self.symbol_table_stack.last().unwrap_unchecked() } } /// Pop the current symbol table off the stack From 83c1ff1b783a08c4093d488241d8e466b04318cf Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Wed, 3 Dec 2025 17:53:14 +0200 Subject: [PATCH 3/3] Remove `unsafe` --- crates/codegen/src/compile.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/crates/codegen/src/compile.rs b/crates/codegen/src/compile.rs index f371967584..5e2c7b81b3 100644 --- a/crates/codegen/src/compile.rs +++ b/crates/codegen/src/compile.rs @@ -625,8 +625,7 @@ impl Compiler { // Push the next table onto the stack self.symbol_table_stack.push(table); - // SAFETY: We just pushed, so it can't be empty - unsafe { self.symbol_table_stack.last().unwrap_unchecked() } + self.current_symbol_table() } /// Pop the current symbol table off the stack