Skip to content

Commit 66c4c87

Browse files
committed
push_output based on enter_scope
1 parent 5071bba commit 66c4c87

File tree

1 file changed

+20
-85
lines changed

1 file changed

+20
-85
lines changed

compiler/codegen/src/compile.rs

Lines changed: 20 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -593,95 +593,32 @@ impl Compiler<'_> {
593593
kwonlyarg_count: u32,
594594
obj_name: String,
595595
) {
596-
let source_path = self.source_code.path.to_owned();
597-
let first_line_number = self.get_source_line_number();
598-
599-
// Get the private name from current scope if exists
600-
let private = self.code_stack.last().and_then(|info| info.private.clone());
601-
596+
// First push the symbol table
602597
let table = self.push_symbol_table();
598+
let scope_type = table.typ;
603599

604-
// Build cellvars in sorted order (like CPython's dictbytype)
605-
let mut cell_names: Vec<_> = table
606-
.symbols
607-
.iter()
608-
.filter(|(_, s)| s.scope == SymbolScope::Cell)
609-
.map(|(name, _)| name.clone())
610-
.collect();
611-
cell_names.sort(); // Sort for deterministic order
612-
let mut cellvar_cache: IndexSet<String> = cell_names.into_iter().collect();
600+
// The key is the current position in the symbol table stack
601+
let key = self.symbol_table_stack.len() - 1;
613602

614-
// Handle implicit __class__ cell if needed (like CPython)
615-
if table.needs_class_closure {
616-
cellvar_cache.insert("__class__".to_string());
617-
}
603+
// Get the line number
604+
let lineno = self.get_source_line_number().get();
618605

619-
// Handle implicit __classdict__ cell if needed (like CPython)
620-
if table.needs_classdict {
621-
cellvar_cache.insert("__classdict__".to_string());
606+
// Call enter_scope which does most of the work
607+
if let Err(e) = self.enter_scope(&obj_name, scope_type, key, lineno as u32) {
608+
// In the current implementation, push_output doesn't return an error,
609+
// so we panic here. This maintains the same behavior.
610+
panic!("enter_scope failed: {:?}", e);
622611
}
623612

624-
// Build freevars in sorted order (like CPython's dictbytype)
625-
let mut free_names: Vec<_> = table
626-
.symbols
627-
.iter()
628-
.filter(|(_, s)| {
629-
s.scope == SymbolScope::Free || s.flags.contains(SymbolFlags::FREE_CLASS)
630-
})
631-
.map(|(name, _)| name.clone())
632-
.collect();
633-
free_names.sort(); // Sort for deterministic order
634-
let freevar_cache: IndexSet<String> = free_names.into_iter().collect();
635-
636-
// Initialize varname_cache from SymbolTable::varnames
637-
let varname_cache: IndexSet<String> = table.varnames.iter().cloned().collect();
638-
639-
// Qualname will be set later by set_qualname
640-
let qualname = None;
641-
642-
// Check if this is a class scope
643-
let is_class_scope = table.typ == SymbolTableType::Class;
644-
645-
let info = ir::CodeInfo {
646-
flags,
647-
source_path,
648-
private,
649-
blocks: vec![ir::Block::default()],
650-
current_block: ir::BlockIdx(0),
651-
metadata: ir::CodeUnitMetadata {
652-
name: obj_name,
653-
qualname,
654-
consts: IndexSet::default(),
655-
names: IndexSet::default(),
656-
varnames: varname_cache,
657-
cellvars: cellvar_cache,
658-
freevars: freevar_cache,
659-
fast_hidden: IndexMap::default(),
660-
argcount: arg_count,
661-
posonlyargcount: posonlyarg_count,
662-
kwonlyargcount: kwonlyarg_count,
663-
firstlineno: first_line_number,
664-
},
665-
static_attributes: if is_class_scope {
666-
Some(IndexSet::default())
667-
} else {
668-
None
669-
},
670-
in_inlined_comp: false,
671-
fblock: Vec::with_capacity(MAXBLOCKS),
672-
};
673-
self.code_stack.push(info);
674-
675-
// We just pushed a code object, and need the qualname
676-
self.set_qualname();
677-
678-
// Add RESUME instruction just like CPython's compiler_enter_scope
679-
emit!(
680-
self,
681-
Instruction::Resume {
682-
arg: bytecode::ResumeType::AtFuncStart as u32
683-
}
684-
);
613+
// Override the values that push_output sets explicitly
614+
// enter_scope sets default values based on scope_type, but push_output
615+
// allows callers to specify exact values
616+
if let Some(info) = self.code_stack.last_mut() {
617+
info.flags = flags;
618+
info.metadata.argcount = arg_count;
619+
info.metadata.posonlyargcount = posonlyarg_count;
620+
info.metadata.kwonlyargcount = kwonlyarg_count;
621+
}
685622
}
686623

687624
// compiler_exit_scope
@@ -1968,8 +1905,6 @@ impl Compiler<'_> {
19681905
.consts
19691906
.insert_full(ConstantData::None);
19701907

1971-
// RESUME instruction is already emitted in push_output
1972-
19731908
self.compile_statements(body)?;
19741909

19751910
// Emit None at end:

0 commit comments

Comments
 (0)