Skip to content

Commit 8f79b4e

Browse files
committed
fasthidden & static attributes
1 parent db374ae commit 8f79b4e

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed

compiler/codegen/src/compile.rs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
#![deny(clippy::cast_possible_truncation)]
99

1010
use crate::{
11-
IndexSet, ToPythonName,
11+
IndexMap, IndexSet, ToPythonName,
1212
error::{CodegenError, CodegenErrorType, PatternUnreachableReason},
1313
ir::{self, BlockIdx},
14-
symboltable::{self, SymbolFlags, SymbolScope, SymbolTable},
14+
symboltable::{self, SymbolFlags, SymbolScope, SymbolTable, SymbolTableType},
1515
unparse::unparse_expr,
1616
};
1717
use itertools::Itertools;
@@ -318,6 +318,8 @@ impl<'src> Compiler<'src> {
318318
varname_cache: IndexSet::default(),
319319
cellvar_cache: IndexSet::default(),
320320
freevar_cache: IndexSet::default(),
321+
fasthidden_cache: IndexMap::default(),
322+
static_attributes: None,
321323
};
322324
Compiler {
323325
code_stack: vec![module_code],
@@ -382,6 +384,9 @@ impl Compiler<'_> {
382384
let source_path = self.source_code.path.to_owned();
383385
let first_line_number = self.get_source_line_number();
384386

387+
// Get the private name from current scope if exists
388+
let private = self.code_stack.last().and_then(|info| info.private.clone());
389+
385390
let table = self.push_symbol_table();
386391

387392
let cellvar_cache = table
@@ -400,17 +405,13 @@ impl Compiler<'_> {
400405
.collect();
401406

402407
// Initialize varname_cache from SymbolTable::varnames
403-
let varname_cache: IndexSet<String> = table
404-
.varnames
405-
.iter()
406-
.cloned()
407-
.collect();
408+
let varname_cache: IndexSet<String> = table.varnames.iter().cloned().collect();
408409

409410
// Qualname will be set later by set_qualname
410411
let qualname = None;
411412

412-
// Get the private name from current scope if exists
413-
let private = self.code_stack.last().and_then(|info| info.private.clone());
413+
// Check if this is a class scope
414+
let is_class_scope = table.typ == SymbolTableType::Class;
414415

415416
let info = ir::CodeInfo {
416417
flags,
@@ -430,6 +431,12 @@ impl Compiler<'_> {
430431
varname_cache,
431432
cellvar_cache,
432433
freevar_cache,
434+
fasthidden_cache: IndexMap::default(),
435+
static_attributes: if is_class_scope {
436+
Some(IndexSet::default())
437+
} else {
438+
None
439+
},
433440
};
434441
self.code_stack.push(info);
435442
}

compiler/codegen/src/ir.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::ops;
22

3-
use crate::IndexSet;
43
use crate::error::InternalError;
4+
use crate::{IndexMap, IndexSet};
55
use ruff_source_file::{OneIndexed, SourceLocation};
66
use rustpython_compiler_core::bytecode::{
77
CodeFlags, CodeObject, CodeUnit, ConstantData, InstrDisplayContext, Instruction, Label, OpArg,
@@ -83,6 +83,13 @@ pub struct CodeInfo {
8383
pub varname_cache: IndexSet<String>,
8484
pub cellvar_cache: IndexSet<String>,
8585
pub freevar_cache: IndexSet<String>,
86+
87+
// For tracking hidden fast locals (used in comprehensions)
88+
// Maps variable name to bool (true if currently fast, false if was fast)
89+
pub fasthidden_cache: IndexMap<String, bool>,
90+
91+
// For class scopes: attributes accessed via self.X
92+
pub static_attributes: Option<IndexSet<String>>,
8693
}
8794
impl CodeInfo {
8895
pub fn finalize_code(mut self, optimize: u8) -> crate::InternalResult<CodeObject> {
@@ -111,6 +118,8 @@ impl CodeInfo {
111118
varname_cache,
112119
cellvar_cache,
113120
freevar_cache,
121+
fasthidden_cache: _,
122+
static_attributes: _,
114123
} = self;
115124

116125
let mut instructions = Vec::new();

0 commit comments

Comments
 (0)