@@ -382,7 +382,7 @@ impl Compiler<'_> {
382
382
kwonlyarg_count : u32 ,
383
383
obj_name : String ,
384
384
) {
385
- self . push_output_with_symbol_table (
385
+ self . push_output_inner (
386
386
flags,
387
387
posonlyarg_count,
388
388
arg_count,
@@ -400,7 +400,7 @@ impl Compiler<'_> {
400
400
kwonlyarg_count : u32 ,
401
401
obj_name : String ,
402
402
) {
403
- self . push_output_with_symbol_table (
403
+ self . push_output_inner (
404
404
flags,
405
405
posonlyarg_count,
406
406
arg_count,
@@ -410,7 +410,7 @@ impl Compiler<'_> {
410
410
)
411
411
}
412
412
413
- fn push_output_with_symbol_table (
413
+ fn push_output_inner (
414
414
& mut self ,
415
415
flags : bytecode:: CodeFlags ,
416
416
posonlyarg_count : u32 ,
@@ -474,14 +474,14 @@ impl Compiler<'_> {
474
474
}
475
475
476
476
fn pop_code_object ( & mut self ) -> CodeObject {
477
- self . pop_code_object_with_symbol_table ( true )
477
+ self . pop_code_object_inner ( true )
478
478
}
479
479
480
480
fn pop_code_object_without_symbol_table ( & mut self ) -> CodeObject {
481
- self . pop_code_object_with_symbol_table ( false )
481
+ self . pop_code_object_inner ( false )
482
482
}
483
483
484
- fn pop_code_object_with_symbol_table ( & mut self , pop_symbol_table : bool ) -> CodeObject {
484
+ fn pop_code_object_inner ( & mut self , pop_symbol_table : bool ) -> CodeObject {
485
485
if pop_symbol_table {
486
486
let table = self . pop_symbol_table ( ) ;
487
487
assert ! ( table. sub_tables. is_empty( ) ) ;
@@ -1753,7 +1753,7 @@ impl Compiler<'_> {
1753
1753
let _type_params_table = self . push_symbol_table ( ) ;
1754
1754
1755
1755
// Create wrapper function
1756
- let wrapper_name = format ! ( "<generic parameters of {}>" , name ) ;
1756
+ let wrapper_name = format ! ( "<generic parameters of {name }>" ) ;
1757
1757
1758
1758
// Save current context and set function context for wrapper
1759
1759
let prev_ctx = self . ctx ;
@@ -1806,10 +1806,8 @@ impl Compiler<'_> {
1806
1806
) ;
1807
1807
}
1808
1808
1809
- // Make cell and store as deref
1810
- let name_idx = self . varname ( name. as_str ( ) ) ?;
1811
- emit ! ( self , Instruction :: MakeCell ( name_idx) ) ;
1812
- emit ! ( self , Instruction :: StoreDeref ( name_idx) ) ;
1809
+ // Store as cell variable
1810
+ self . store_name ( name. as_str ( ) ) ?;
1813
1811
}
1814
1812
TypeParam :: ParamSpec ( TypeParamParamSpec { name, default, .. } ) => {
1815
1813
self . emit_load_const ( ConstantData :: Str {
@@ -1827,10 +1825,8 @@ impl Compiler<'_> {
1827
1825
) ;
1828
1826
}
1829
1827
1830
- // Make cell and store as deref
1831
- let name_idx = self . varname ( name. as_str ( ) ) ?;
1832
- emit ! ( self , Instruction :: MakeCell ( name_idx) ) ;
1833
- emit ! ( self , Instruction :: StoreDeref ( name_idx) ) ;
1828
+ // Store as cell variable
1829
+ self . store_name ( name. as_str ( ) ) ?;
1834
1830
}
1835
1831
TypeParam :: TypeVarTuple ( TypeParamTypeVarTuple { name, default, .. } ) => {
1836
1832
self . emit_load_const ( ConstantData :: Str {
@@ -1848,29 +1844,18 @@ impl Compiler<'_> {
1848
1844
) ;
1849
1845
}
1850
1846
1851
- // Make cell and store as deref
1852
- let name_idx = self . varname ( name. as_str ( ) ) ?;
1853
- emit ! ( self , Instruction :: MakeCell ( name_idx) ) ;
1854
- emit ! ( self , Instruction :: StoreDeref ( name_idx) ) ;
1847
+ // Store as cell variable
1848
+ self . store_name ( name. as_str ( ) ) ?;
1855
1849
}
1856
1850
}
1857
1851
}
1858
1852
1859
- // Build tuple of type parameters - use LoadDeref for cell variables
1853
+ // Build tuple of type parameters
1860
1854
for type_param in & type_params. type_params {
1861
1855
match type_param {
1862
- TypeParam :: TypeVar ( tv) => {
1863
- let idx = self . varname ( tv. name . as_str ( ) ) ?;
1864
- emit ! ( self , Instruction :: LoadDeref ( idx) ) ;
1865
- }
1866
- TypeParam :: ParamSpec ( ps) => {
1867
- let idx = self . varname ( ps. name . as_str ( ) ) ?;
1868
- emit ! ( self , Instruction :: LoadDeref ( idx) ) ;
1869
- }
1870
- TypeParam :: TypeVarTuple ( tvt) => {
1871
- let idx = self . varname ( tvt. name . as_str ( ) ) ?;
1872
- emit ! ( self , Instruction :: LoadDeref ( idx) ) ;
1873
- }
1856
+ TypeParam :: TypeVar ( tv) => self . load_name ( tv. name . as_str ( ) ) ?,
1857
+ TypeParam :: ParamSpec ( ps) => self . load_name ( ps. name . as_str ( ) ) ?,
1858
+ TypeParam :: TypeVarTuple ( tvt) => self . load_name ( tvt. name . as_str ( ) ) ?,
1874
1859
}
1875
1860
}
1876
1861
let num_params = type_params. type_params . len ( ) ;
@@ -1881,17 +1866,15 @@ impl Compiler<'_> {
1881
1866
}
1882
1867
) ;
1883
1868
1884
- // Make .type_params a cell variable and store it
1885
- let type_params_idx = self . varname ( ".type_params" ) ?;
1886
- emit ! ( self , Instruction :: MakeCell ( type_params_idx) ) ;
1887
- emit ! ( self , Instruction :: StoreDeref ( type_params_idx) ) ;
1869
+ // Store .type_params as cell variable
1870
+ self . store_name ( ".type_params" ) ?;
1888
1871
1889
1872
// Now compile the actual class
1890
1873
emit ! ( self , Instruction :: LoadBuildClass ) ;
1891
1874
1892
1875
// Create class body code object
1893
1876
// Need to push symbol table for class
1894
- let class_table = self . push_symbol_table ( ) ;
1877
+ let _class_table = self . push_symbol_table ( ) ;
1895
1878
1896
1879
let prev_ctx_inner = self . ctx ;
1897
1880
self . ctx = CompileContext {
@@ -1903,6 +1886,7 @@ impl Compiler<'_> {
1903
1886
let prev_class_name = self . class_name . replace ( name. to_owned ( ) ) ;
1904
1887
self . push_qualified_path ( name) ;
1905
1888
1889
+ // Create class body function that takes .type_params as argument
1906
1890
self . push_output_without_symbol_table (
1907
1891
bytecode:: CodeFlags :: empty ( ) ,
1908
1892
0 ,
@@ -1933,14 +1917,8 @@ impl Compiler<'_> {
1933
1917
let doc = self . name ( "__doc__" ) ;
1934
1918
emit ! ( self , Instruction :: StoreLocal ( doc) ) ;
1935
1919
1936
- // Load .type_params and store as __type_params__
1937
- let type_params_cache_idx = self . varname ( ".type_params" ) ?;
1938
- emit ! (
1939
- self ,
1940
- Instruction :: LoadFromDictOrDeref ( type_params_cache_idx)
1941
- ) ;
1942
- let dunder_type_params_local = self . name ( "__type_params__" ) ;
1943
- emit ! ( self , Instruction :: StoreLocal ( dunder_type_params_local) ) ;
1920
+ // Skip loading .type_params for now - it's causing issues
1921
+ // We'll handle __type_params__ differently
1944
1922
1945
1923
// setup annotations
1946
1924
if Self :: find_ann ( body) {
@@ -1999,9 +1977,8 @@ impl Compiler<'_> {
1999
1977
}
2000
1978
}
2001
1979
2002
- // Create Generic[*type_params] as base - use LoadDeref
2003
- let type_params_idx = self . varname ( ".type_params" ) ?;
2004
- emit ! ( self , Instruction :: LoadDeref ( type_params_idx) ) ;
1980
+ // Create Generic[*type_params] as base
1981
+ self . load_name ( ".type_params" ) ?;
2005
1982
emit ! (
2006
1983
self ,
2007
1984
Instruction :: CallIntrinsic1 {
@@ -2022,6 +1999,19 @@ impl Compiler<'_> {
2022
1999
}
2023
2000
) ;
2024
2001
2002
+ // Set __type_params__ on the created class
2003
+ // Stack: [class]
2004
+ emit ! ( self , Instruction :: Duplicate ) ; // Stack: [class, class]
2005
+ self . load_name ( ".type_params" ) ?; // Stack: [class, class, type_params]
2006
+ emit ! ( self , Instruction :: Rotate2 ) ; // Stack: [class, type_params, class]
2007
+ let type_params_attr = self . name ( "__type_params__" ) ;
2008
+ emit ! (
2009
+ self ,
2010
+ Instruction :: StoreAttr {
2011
+ idx: type_params_attr
2012
+ }
2013
+ ) ; // Stack: [class]
2014
+
2025
2015
// Return the created class from the wrapper function
2026
2016
self . emit_return_value ( ) ;
2027
2017
0 commit comments