Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fasthidden & static attributes
  • Loading branch information
youknowone committed Jul 11, 2025
commit 2725d42acce41a7c45d9025a9e808f78a131ee2f
25 changes: 16 additions & 9 deletions compiler/codegen/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
#![deny(clippy::cast_possible_truncation)]

use crate::{
IndexSet, ToPythonName,
IndexMap, IndexSet, ToPythonName,
error::{CodegenError, CodegenErrorType, PatternUnreachableReason},
ir::{self, BlockIdx},
symboltable::{self, SymbolFlags, SymbolScope, SymbolTable},
symboltable::{self, SymbolFlags, SymbolScope, SymbolTable, SymbolTableType},
unparse::unparse_expr,
};
use itertools::Itertools;
Expand Down Expand Up @@ -318,6 +318,8 @@ impl<'src> Compiler<'src> {
varname_cache: IndexSet::default(),
cellvar_cache: IndexSet::default(),
freevar_cache: IndexSet::default(),
fasthidden_cache: IndexMap::default(),
static_attributes: None,
};
Compiler {
code_stack: vec![module_code],
Expand Down Expand Up @@ -382,6 +384,9 @@ impl Compiler<'_> {
let source_path = self.source_code.path.to_owned();
let first_line_number = self.get_source_line_number();

// Get the private name from current scope if exists
let private = self.code_stack.last().and_then(|info| info.private.clone());

let table = self.push_symbol_table();

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

// Initialize varname_cache from SymbolTable::varnames
let varname_cache: IndexSet<String> = table
.varnames
.iter()
.cloned()
.collect();
let varname_cache: IndexSet<String> = table.varnames.iter().cloned().collect();

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

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

let info = ir::CodeInfo {
flags,
Expand All @@ -430,6 +431,12 @@ impl Compiler<'_> {
varname_cache,
cellvar_cache,
freevar_cache,
fasthidden_cache: IndexMap::default(),
static_attributes: if is_class_scope {
Some(IndexSet::default())
} else {
None
},
};
self.code_stack.push(info);
}
Expand Down
11 changes: 10 additions & 1 deletion compiler/codegen/src/ir.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::ops;

use crate::IndexSet;
use crate::error::InternalError;
use crate::{IndexMap, IndexSet};
use ruff_source_file::{OneIndexed, SourceLocation};
use rustpython_compiler_core::bytecode::{
CodeFlags, CodeObject, CodeUnit, ConstantData, InstrDisplayContext, Instruction, Label, OpArg,
Expand Down Expand Up @@ -83,6 +83,13 @@ pub struct CodeInfo {
pub varname_cache: IndexSet<String>,
pub cellvar_cache: IndexSet<String>,
pub freevar_cache: IndexSet<String>,

// For tracking hidden fast locals (used in comprehensions)
// Maps variable name to bool (true if currently fast, false if was fast)
pub fast_hidden_cache: IndexMap<String, bool>,

// For class scopes: attributes accessed via self.X
pub static_attributes: Option<IndexSet<String>>,
}
impl CodeInfo {
pub fn finalize_code(mut self, optimize: u8) -> crate::InternalResult<CodeObject> {
Expand Down Expand Up @@ -111,6 +118,8 @@ impl CodeInfo {
varname_cache,
cellvar_cache,
freevar_cache,
fast_hidden_cache: _,
static_attributes: _,
} = self;

let mut instructions = Vec::new();
Expand Down