Visitar URL original
compile_type_param_bound_or_default by youknowone · Pull Request #6005 · RustPython/RustPython · GitHub
Skip to content
Merged
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
Next Next commit
enter_type_param_block
  • Loading branch information
youknowone committed Jul 20, 2025
commit a1b3520df02b02fdb4a90d9834463a197d3204fe
48 changes: 36 additions & 12 deletions compiler/codegen/src/symboltable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ pub struct SymbolTable {

/// Whether this class scope needs an implicit __classdict__ cell
pub needs_classdict: bool,

/// Whether this type param scope can see the parent class scope
pub can_see_class_scope: bool,
}

impl SymbolTable {
Expand All @@ -68,6 +71,7 @@ impl SymbolTable {
varnames: Vec::new(),
needs_class_closure: false,
needs_classdict: false,
can_see_class_scope: false,
}
}

Expand Down Expand Up @@ -665,6 +669,31 @@ impl SymbolTableBuilder<'_> {
self.current_varnames.clear();
}

fn enter_type_param_block(&mut self, name: &str, line_number: u32) -> SymbolTableResult {
// Check if we're in a class scope
let in_class = self
.tables
.last()
.map_or(false, |t| t.typ == CompilerScope::Class);

self.enter_scope(name, CompilerScope::TypeParams, line_number);

// If we're in a class, mark that this type param scope can see the class scope
if let Some(table) = self.tables.last_mut() {
table.can_see_class_scope = in_class;

// Add __classdict__ as a USE symbol in type param scope if in class
if in_class {
self.register_name("__classdict__", SymbolUsage::Used, TextRange::default())?;
}
}

// Register .type_params as a SET symbol (it will be converted to cell variable later)
self.register_name(".type_params", SymbolUsage::Assigned, TextRange::default())?;

Ok(())
}

/// Pop symbol table and add to sub table of parent table.
fn leave_scope(&mut self) {
let mut table = self.tables.pop().unwrap();
Expand Down Expand Up @@ -746,12 +775,10 @@ impl SymbolTableBuilder<'_> {
self.scan_annotation(expression)?;
}
if let Some(type_params) = type_params {
self.enter_scope(
self.enter_type_param_block(
&format!("<generic parameters of {}>", name.as_str()),
CompilerScope::TypeParams,
// FIXME: line no
self.line_index_start(*range),
);
self.line_index_start(type_params.range),
)?;
self.scan_type_params(type_params)?;
}
self.enter_scope_with_parameters(
Expand All @@ -774,11 +801,10 @@ impl SymbolTableBuilder<'_> {
range,
}) => {
if let Some(type_params) = type_params {
self.enter_scope(
self.enter_type_param_block(
&format!("<generic parameters of {}>", name.as_str()),
CompilerScope::TypeParams,
self.line_index_start(type_params.range),
);
)?;
self.scan_type_params(type_params)?;
}
self.enter_scope(
Expand Down Expand Up @@ -965,12 +991,10 @@ impl SymbolTableBuilder<'_> {
..
}) => {
if let Some(type_params) = type_params {
self.enter_scope(
// &name.to_string(),
self.enter_type_param_block(
"TypeAlias",
CompilerScope::TypeParams,
self.line_index_start(type_params.range),
);
)?;
self.scan_type_params(type_params)?;
self.scan_expression(value, ExpressionContext::Load)?;
self.leave_scope();
Expand Down