@@ -557,11 +557,9 @@ impl Compiler {
557557
558558 /// Get the SymbolTable for the current scope.
559559 fn current_symbol_table ( & self ) -> & SymbolTable {
560- if self . symbol_table_stack . is_empty ( ) {
561- panic ! ( "symbol_table_stack is empty! This is a compiler bug." ) ;
562- }
563- let index = self . symbol_table_stack . len ( ) - 1 ;
564- & self . symbol_table_stack [ index]
560+ self . symbol_table_stack
561+ . last ( )
562+ . expect ( "symbol_table_stack is empty! This is a compiler bug." )
565563 }
566564
567565 /// Get the index of a free variable.
@@ -626,9 +624,9 @@ impl Compiler {
626624 let table = current_table. sub_tables . remove ( 0 ) ;
627625
628626 // Push the next table onto the stack
629- let last_idx = self . symbol_table_stack . len ( ) ;
630627 self . symbol_table_stack . push ( table) ;
631- & self . symbol_table_stack [ last_idx]
628+ // SAFETY: We just pushed, so it can't be empty
629+ unsafe { & self . symbol_table_stack . last ( ) . unwrap_unchecked ( ) }
632630 }
633631
634632 /// Pop the current symbol table off the stack
@@ -657,12 +655,13 @@ impl Compiler {
657655 let source_path = self . source_file . name ( ) . to_owned ( ) ;
658656
659657 // Lookup symbol table entry using key (_PySymtable_Lookup)
660- let ste = if key < self . symbol_table_stack . len ( ) {
661- & self . symbol_table_stack [ key]
662- } else {
663- return Err ( self . error ( CodegenErrorType :: SyntaxError (
664- "unknown symbol table entry" . to_owned ( ) ,
665- ) ) ) ;
658+ let ste = match self . symbol_table_stack . get ( key) {
659+ Some ( v) => v,
660+ None => {
661+ return Err ( self . error ( CodegenErrorType :: SyntaxError (
662+ "unknown symbol table entry" . to_owned ( ) ,
663+ ) ) ) ;
664+ }
666665 } ;
667666
668667 // Use varnames from symbol table (already collected in definition order)
@@ -1199,12 +1198,10 @@ impl Compiler {
11991198
12001199 // If not found and we're in TypeParams scope, try parent scope
12011200 let symbol = if symbol. is_none ( ) && is_typeparams {
1202- if self . symbol_table_stack . len ( ) > 1 {
1203- let parent_idx = self . symbol_table_stack . len ( ) - 2 ;
1204- self . symbol_table_stack [ parent_idx] . lookup ( name. as_ref ( ) )
1205- } else {
1206- None
1207- }
1201+ self . symbol_table_stack
1202+ . get ( self . symbol_table_stack . len ( ) - 2 ) // Try to get parent index
1203+ . expect ( "Symbol has no parent! This is a compiler bug." )
1204+ . lookup ( name. as_ref ( ) )
12081205 } else {
12091206 symbol
12101207 } ;
0 commit comments