Skip to content

Commit 647f540

Browse files
committed
Remove SymbolScope::TypeParams
1 parent f6869f7 commit 647f540

File tree

3 files changed

+19
-28
lines changed

3 files changed

+19
-28
lines changed

compiler/codegen/src/compile.rs

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::{
1111
IndexMap, IndexSet, ToPythonName,
1212
error::{CodegenError, CodegenErrorType, PatternUnreachableReason},
1313
ir::{self, BlockIdx},
14-
symboltable::{self, SymbolFlags, SymbolScope, SymbolTable, SymbolTableType},
14+
symboltable::{self, CompilerScope, SymbolFlags, SymbolScope, SymbolTable},
1515
unparse::unparse_expr,
1616
};
1717

@@ -409,7 +409,7 @@ impl Compiler<'_> {
409409
fn enter_scope(
410410
&mut self,
411411
name: &str,
412-
scope_type: SymbolTableType,
412+
scope_type: CompilerScope,
413413
key: usize, // In RustPython, we use the index in symbol_table_stack as key
414414
lineno: u32,
415415
) -> CompileResult<()> {
@@ -452,14 +452,14 @@ impl Compiler<'_> {
452452
// Handle implicit __class__ cell if needed
453453
if ste.needs_class_closure {
454454
// Cook up an implicit __class__ cell
455-
debug_assert_eq!(scope_type, SymbolTableType::Class);
455+
debug_assert_eq!(scope_type, CompilerScope::Class);
456456
cellvar_cache.insert("__class__".to_string());
457457
}
458458

459459
// Handle implicit __classdict__ cell if needed
460460
if ste.needs_classdict {
461461
// Cook up an implicit __classdict__ cell
462-
debug_assert_eq!(scope_type, SymbolTableType::Class);
462+
debug_assert_eq!(scope_type, CompilerScope::Class);
463463
cellvar_cache.insert("__classdict__".to_string());
464464
}
465465

@@ -480,21 +480,21 @@ impl Compiler<'_> {
480480

481481
// Initialize u_metadata fields
482482
let (flags, posonlyarg_count, arg_count, kwonlyarg_count) = match scope_type {
483-
SymbolTableType::Module => (bytecode::CodeFlags::empty(), 0, 0, 0),
484-
SymbolTableType::Class => (bytecode::CodeFlags::empty(), 0, 0, 0),
485-
SymbolTableType::Function | SymbolTableType::Lambda => (
483+
CompilerScope::Module => (bytecode::CodeFlags::empty(), 0, 0, 0),
484+
CompilerScope::Class => (bytecode::CodeFlags::empty(), 0, 0, 0),
485+
CompilerScope::Function | CompilerScope::AsyncFunction | CompilerScope::Lambda => (
486486
bytecode::CodeFlags::NEW_LOCALS | bytecode::CodeFlags::IS_OPTIMIZED,
487487
0, // Will be set later in enter_function
488488
0, // Will be set later in enter_function
489489
0, // Will be set later in enter_function
490490
),
491-
SymbolTableType::Comprehension => (
491+
CompilerScope::Comprehension => (
492492
bytecode::CodeFlags::NEW_LOCALS | bytecode::CodeFlags::IS_OPTIMIZED,
493493
0,
494494
1, // comprehensions take one argument (.0)
495495
0,
496496
),
497-
SymbolTableType::TypeParams => (
497+
CompilerScope::TypeParams => (
498498
bytecode::CodeFlags::NEW_LOCALS | bytecode::CodeFlags::IS_OPTIMIZED,
499499
0,
500500
0,
@@ -530,7 +530,7 @@ impl Compiler<'_> {
530530
kwonlyargcount: kwonlyarg_count,
531531
firstlineno: OneIndexed::new(lineno as usize).unwrap_or(OneIndexed::MIN),
532532
},
533-
static_attributes: if scope_type == SymbolTableType::Class {
533+
static_attributes: if scope_type == CompilerScope::Class {
534534
Some(IndexSet::default())
535535
} else {
536536
None
@@ -544,12 +544,12 @@ impl Compiler<'_> {
544544
self.code_stack.push(code_info);
545545

546546
// Set qualname after pushing (uses compiler_set_qualname logic)
547-
if scope_type != SymbolTableType::Module {
547+
if scope_type != CompilerScope::Module {
548548
self.set_qualname();
549549
}
550550

551551
// Emit RESUME instruction
552-
let _resume_loc = if scope_type == SymbolTableType::Module {
552+
let _resume_loc = if scope_type == CompilerScope::Module {
553553
// Module scope starts with lineno 0
554554
ruff_source_file::SourceLocation {
555555
row: OneIndexed::MIN,
@@ -569,7 +569,7 @@ impl Compiler<'_> {
569569
}
570570
);
571571

572-
if scope_type == SymbolTableType::Module {
572+
if scope_type == CompilerScope::Module {
573573
// This would be loc.lineno = -1 in CPython
574574
// We handle this differently in RustPython
575575
}
@@ -950,11 +950,7 @@ impl Compiler<'_> {
950950
cache = &mut info.metadata.cellvars;
951951
NameOpType::Deref
952952
} // TODO: is this right?
953-
SymbolScope::TypeParams => {
954-
// Type parameters are always cell variables
955-
cache = &mut info.metadata.cellvars;
956-
NameOpType::Deref
957-
} // SymbolScope::Unknown => NameOpType::Global,
953+
// SymbolScope::Unknown => NameOpType::Global,
958954
};
959955

960956
if NameUsage::Load == usage && name == "__debug__" {
@@ -1999,7 +1995,7 @@ impl Compiler<'_> {
19991995
let table = self.symbol_table_stack.last().unwrap();
20001996
match table.lookup(name) {
20011997
Some(symbol) => match symbol.scope {
2002-
SymbolScope::Cell | SymbolScope::TypeParams => Ok(SymbolScope::Cell),
1998+
SymbolScope::Cell => Ok(SymbolScope::Cell),
20031999
SymbolScope::Free => Ok(SymbolScope::Free),
20042000
_ if symbol.flags.contains(SymbolFlags::FREE_CLASS) => Ok(SymbolScope::Free),
20052001
_ => Err(CodegenErrorType::SyntaxError(format!(
@@ -2204,7 +2200,7 @@ impl Compiler<'_> {
22042200
// Use enter_scope instead of push_output to match CPython
22052201
let key = self.symbol_table_stack.len();
22062202
self.push_symbol_table();
2207-
self.enter_scope(name, SymbolTableType::Class, key, firstlineno)?;
2203+
self.enter_scope(name, CompilerScope::Class, key, firstlineno)?;
22082204

22092205
// Set qualname using the new method
22102206
let qualname = self.set_qualname();

compiler/codegen/src/symboltable.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ impl fmt::Display for CompilerScope {
104104
Self::Module => write!(f, "module"),
105105
Self::Class => write!(f, "class"),
106106
Self::Function => write!(f, "function"),
107+
Self::AsyncFunction => write!(f, "async function"),
107108
Self::Lambda => write!(f, "lambda"),
108109
Self::Comprehension => write!(f, "comprehension"),
109110
Self::TypeParams => write!(f, "type parameter"),
@@ -128,8 +129,6 @@ pub enum SymbolScope {
128129
GlobalImplicit,
129130
Free,
130131
Cell,
131-
// TODO: wrong place. not a symbol scope, but a COMPILER_SCOPE_TYPEPARAMS
132-
TypeParams,
133132
}
134133

135134
bitflags! {
@@ -406,10 +405,6 @@ impl SymbolTableAnalyzer {
406405
SymbolScope::Local | SymbolScope::Cell => {
407406
// all is well
408407
}
409-
SymbolScope::TypeParams => {
410-
// Type parameters are always cell variables in their scope
411-
symbol.scope = SymbolScope::Cell;
412-
}
413408
SymbolScope::Unknown => {
414409
// Try hard to figure out what the scope of this symbol is.
415410
let scope = if symbol.is_bound() {
@@ -539,7 +534,7 @@ impl SymbolTableAnalyzer {
539534
location: None,
540535
});
541536
}
542-
CompilerScope::Function | CompilerScope::Lambda => {
537+
CompilerScope::Function | CompilerScope::AsyncFunction | CompilerScope::Lambda => {
543538
if let Some(parent_symbol) = symbols.get_mut(&symbol.name) {
544539
if let SymbolScope::Unknown = parent_symbol.scope {
545540
// this information is new, as the assignment is done in inner scope

vm/src/stdlib/symtable.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ mod symtable {
66
PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine, builtins::PyStrRef, compiler,
77
};
88
use rustpython_codegen::symboltable::{
9-
Symbol, SymbolFlags, SymbolScope, SymbolTable, CompilerScope,
9+
CompilerScope, Symbol, SymbolFlags, SymbolScope, SymbolTable,
1010
};
1111
use std::fmt;
1212

0 commit comments

Comments
 (0)