@@ -78,7 +78,6 @@ struct Compiler<'src> {
78
78
done_with_future_stmts : DoneWithFuture ,
79
79
future_annotations : bool ,
80
80
ctx : CompileContext ,
81
- class_name : Option < String > ,
82
81
opts : CompileOpts ,
83
82
in_annotation : bool ,
84
83
}
@@ -312,6 +311,7 @@ impl<'src> Compiler<'src> {
312
311
first_line_number : OneIndexed :: MIN ,
313
312
obj_name : code_name. clone ( ) ,
314
313
qualname : Some ( code_name) ,
314
+ private : None ,
315
315
blocks : vec ! [ ir:: Block :: default ( ) ] ,
316
316
current_block : ir:: BlockIdx ( 0 ) ,
317
317
constants : IndexSet :: default ( ) ,
@@ -334,7 +334,6 @@ impl<'src> Compiler<'src> {
334
334
in_class : false ,
335
335
func : FunctionContext :: NoFunction ,
336
336
} ,
337
- class_name : None ,
338
337
opts,
339
338
in_annotation : false ,
340
339
}
@@ -409,6 +408,9 @@ impl Compiler<'_> {
409
408
Some ( self . qualified_path . join ( "." ) )
410
409
} ;
411
410
411
+ // Get the private name from current scope if exists
412
+ let private = self . code_stack . last ( ) . and_then ( |info| info. private . clone ( ) ) ;
413
+
412
414
let info = ir:: CodeInfo {
413
415
flags,
414
416
posonlyarg_count,
@@ -418,6 +420,7 @@ impl Compiler<'_> {
418
420
first_line_number,
419
421
obj_name,
420
422
qualname,
423
+ private,
421
424
422
425
blocks : vec ! [ ir:: Block :: default ( ) ] ,
423
426
current_block : ir:: BlockIdx ( 0 ) ,
@@ -587,7 +590,12 @@ impl Compiler<'_> {
587
590
}
588
591
589
592
fn mangle < ' a > ( & self , name : & ' a str ) -> Cow < ' a , str > {
590
- symboltable:: mangle_name ( self . class_name . as_deref ( ) , name)
593
+ // Use u_private from current code unit for name mangling
594
+ let private = self
595
+ . code_stack
596
+ . last ( )
597
+ . and_then ( |info| info. private . as_deref ( ) ) ;
598
+ symboltable:: mangle_name ( private, name)
591
599
}
592
600
593
601
fn check_forbidden_name ( & mut self , name : & str , usage : NameUsage ) -> CompileResult < ( ) > {
@@ -1709,8 +1717,6 @@ impl Compiler<'_> {
1709
1717
loop_data : None ,
1710
1718
} ;
1711
1719
1712
- let prev_class_name = self . class_name . replace ( name. to_owned ( ) ) ;
1713
-
1714
1720
// Check if the class is declared global
1715
1721
let symbol_table = self . symbol_table_stack . last ( ) . unwrap ( ) ;
1716
1722
let symbol = unwrap_internal (
@@ -1729,8 +1735,14 @@ impl Compiler<'_> {
1729
1735
// If there are type params, we need to push a special symbol table just for them
1730
1736
if let Some ( type_params) = type_params {
1731
1737
self . push_symbol_table ( ) ;
1738
+ // Save current private name to restore later
1739
+ let saved_private = self . code_stack . last ( ) . and_then ( |info| info. private . clone ( ) ) ;
1732
1740
// Compile type parameters and store as .type_params
1733
1741
self . compile_type_params ( type_params) ?;
1742
+ // Restore private name after type param scope
1743
+ if let Some ( private) = saved_private {
1744
+ self . code_stack . last_mut ( ) . unwrap ( ) . private = Some ( private) ;
1745
+ }
1734
1746
let dot_type_params = self . name ( ".type_params" ) ;
1735
1747
emit ! ( self , Instruction :: StoreLocal ( dot_type_params) ) ;
1736
1748
}
@@ -1740,6 +1752,9 @@ impl Compiler<'_> {
1740
1752
// Update the qualname in the current code info
1741
1753
self . code_stack . last_mut ( ) . unwrap ( ) . qualname = Some ( qualified_name. clone ( ) ) ;
1742
1754
1755
+ // For class scopes, set u_private to the class name for name mangling
1756
+ self . code_stack . last_mut ( ) . unwrap ( ) . private = Some ( name. to_owned ( ) ) ;
1757
+
1743
1758
let ( doc_str, body) = split_doc ( body, & self . opts ) ;
1744
1759
1745
1760
let dunder_name = self . name ( "__name__" ) ;
@@ -1793,7 +1808,6 @@ impl Compiler<'_> {
1793
1808
1794
1809
let code = self . pop_code_object ( ) ;
1795
1810
1796
- self . class_name = prev_class_name;
1797
1811
self . qualified_path . pop ( ) ;
1798
1812
self . qualified_path . append ( global_path_prefix. as_mut ( ) ) ;
1799
1813
self . ctx = prev_ctx;
0 commit comments