Skip to content

Commit fb3b617

Browse files
committed
phase5
1 parent 26767d0 commit fb3b617

2 files changed

Lines changed: 26 additions & 4 deletions

File tree

crates/codegen/src/compile.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3572,8 +3572,10 @@ impl Compiler {
35723572
.annotation_block = Some(Box::new(annotation_table));
35733573
// Exit code scope
35743574
let pop = self.code_stack.pop();
3575-
let annotate_code =
3576-
unwrap_internal(self, compiler_unwrap_option(self, pop).finalize_code(&self.opts));
3575+
let annotate_code = unwrap_internal(
3576+
self,
3577+
compiler_unwrap_option(self, pop).finalize_code(&self.opts),
3578+
);
35773579

35783580
// Make a closure from the code object
35793581
self.make_closure(annotate_code, bytecode::MakeFunctionFlags::empty())?;

crates/codegen/src/symboltable.rs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -947,6 +947,20 @@ impl SymbolTableBuilder {
947947
}) => {
948948
self.scan_decorators(decorator_list, ExpressionContext::Load)?;
949949
self.register_ident(name, SymbolUsage::Assigned)?;
950+
951+
// When in class scope, save the class's annotation_block before scanning
952+
// function annotations, so method annotations don't interfere with class annotations
953+
let parent_is_class = self
954+
.tables
955+
.last()
956+
.map(|t| t.typ == CompilerScope::Class)
957+
.unwrap_or(false);
958+
let saved_annotation_block = if parent_is_class {
959+
self.tables.last_mut().unwrap().annotation_block.take()
960+
} else {
961+
None
962+
};
963+
950964
let has_return_annotation = if let Some(expression) = returns {
951965
self.scan_annotation(expression)?;
952966
true
@@ -971,6 +985,11 @@ impl SymbolTableBuilder {
971985
if type_params.is_some() {
972986
self.leave_scope();
973987
}
988+
989+
// Restore class's annotation_block after processing the function
990+
if let Some(block) = saved_annotation_block {
991+
self.tables.last_mut().unwrap().annotation_block = Some(block);
992+
}
974993
}
975994
Stmt::ClassDef(StmtClassDef {
976995
name,
@@ -1912,8 +1931,9 @@ impl SymbolTableBuilder {
19121931

19131932
let has_any_annotations = has_param_annotations || has_return_annotation;
19141933

1915-
// Only take annotation_block if this function has any annotations
1916-
// Otherwise, leave the parent's annotation_block for module-level annotations
1934+
// Take annotation_block if this function has any annotations.
1935+
// When in class scope, the class's annotation_block was saved before scanning
1936+
// function annotations, so the current annotation_block belongs to this function.
19171937
let annotation_block = if has_any_annotations {
19181938
self.tables.last_mut().unwrap().annotation_block.take()
19191939
} else {

0 commit comments

Comments
 (0)