Skip to content

Commit 3113d84

Browse files
committed
hm:wqm
1 parent f525c6b commit 3113d84

File tree

4 files changed

+44
-85
lines changed

4 files changed

+44
-85
lines changed

Lib/test/test_typing.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3888,8 +3888,6 @@ def test_pep695_generic_class_with_future_annotations(self):
38883888
# should not have changed as a result of the get_type_hints() calls!
38893889
self.assertEqual(ann_module695.__dict__, original_globals)
38903890

3891-
# TODO: RUSTPYTHON
3892-
@unittest.expectedFailure
38933891
def test_pep695_generic_class_with_future_annotations_and_local_shadowing(self):
38943892
hints_for_B = get_type_hints(ann_module695.B)
38953893
self.assertEqual(hints_for_B, {"x": int, "y": str, "z": bytes})
@@ -3935,8 +3933,6 @@ def test_pep_695_generic_method_with_future_annotations_name_clash_with_global_v
39353933
set(ann_module695.D.generic_method_2.__type_params__)
39363934
)
39373935

3938-
# TODO: RUSTPYTHON
3939-
@unittest.expectedFailure
39403936
def test_pep_695_generics_with_future_annotations_nested_in_function(self):
39413937
results = ann_module695.nested()
39423938

compiler/codegen/src/compile.rs

Lines changed: 38 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ impl Compiler<'_> {
382382
kwonlyarg_count: u32,
383383
obj_name: String,
384384
) {
385-
self.push_output_with_symbol_table(
385+
self.push_output_inner(
386386
flags,
387387
posonlyarg_count,
388388
arg_count,
@@ -400,7 +400,7 @@ impl Compiler<'_> {
400400
kwonlyarg_count: u32,
401401
obj_name: String,
402402
) {
403-
self.push_output_with_symbol_table(
403+
self.push_output_inner(
404404
flags,
405405
posonlyarg_count,
406406
arg_count,
@@ -410,7 +410,7 @@ impl Compiler<'_> {
410410
)
411411
}
412412

413-
fn push_output_with_symbol_table(
413+
fn push_output_inner(
414414
&mut self,
415415
flags: bytecode::CodeFlags,
416416
posonlyarg_count: u32,
@@ -474,14 +474,14 @@ impl Compiler<'_> {
474474
}
475475

476476
fn pop_code_object(&mut self) -> CodeObject {
477-
self.pop_code_object_with_symbol_table(true)
477+
self.pop_code_object_inner(true)
478478
}
479479

480480
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)
482482
}
483483

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 {
485485
if pop_symbol_table {
486486
let table = self.pop_symbol_table();
487487
assert!(table.sub_tables.is_empty());
@@ -1753,7 +1753,7 @@ impl Compiler<'_> {
17531753
let _type_params_table = self.push_symbol_table();
17541754

17551755
// Create wrapper function
1756-
let wrapper_name = format!("<generic parameters of {}>", name);
1756+
let wrapper_name = format!("<generic parameters of {name}>");
17571757

17581758
// Save current context and set function context for wrapper
17591759
let prev_ctx = self.ctx;
@@ -1806,10 +1806,8 @@ impl Compiler<'_> {
18061806
);
18071807
}
18081808

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())?;
18131811
}
18141812
TypeParam::ParamSpec(TypeParamParamSpec { name, default, .. }) => {
18151813
self.emit_load_const(ConstantData::Str {
@@ -1827,10 +1825,8 @@ impl Compiler<'_> {
18271825
);
18281826
}
18291827

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())?;
18341830
}
18351831
TypeParam::TypeVarTuple(TypeParamTypeVarTuple { name, default, .. }) => {
18361832
self.emit_load_const(ConstantData::Str {
@@ -1848,29 +1844,18 @@ impl Compiler<'_> {
18481844
);
18491845
}
18501846

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())?;
18551849
}
18561850
}
18571851
}
18581852

1859-
// Build tuple of type parameters - use LoadDeref for cell variables
1853+
// Build tuple of type parameters
18601854
for type_param in &type_params.type_params {
18611855
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())?,
18741859
}
18751860
}
18761861
let num_params = type_params.type_params.len();
@@ -1881,17 +1866,15 @@ impl Compiler<'_> {
18811866
}
18821867
);
18831868

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")?;
18881871

18891872
// Now compile the actual class
18901873
emit!(self, Instruction::LoadBuildClass);
18911874

18921875
// Create class body code object
18931876
// Need to push symbol table for class
1894-
let class_table = self.push_symbol_table();
1877+
let _class_table = self.push_symbol_table();
18951878

18961879
let prev_ctx_inner = self.ctx;
18971880
self.ctx = CompileContext {
@@ -1903,6 +1886,7 @@ impl Compiler<'_> {
19031886
let prev_class_name = self.class_name.replace(name.to_owned());
19041887
self.push_qualified_path(name);
19051888

1889+
// Create class body function that takes .type_params as argument
19061890
self.push_output_without_symbol_table(
19071891
bytecode::CodeFlags::empty(),
19081892
0,
@@ -1933,14 +1917,8 @@ impl Compiler<'_> {
19331917
let doc = self.name("__doc__");
19341918
emit!(self, Instruction::StoreLocal(doc));
19351919

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
19441922

19451923
// setup annotations
19461924
if Self::find_ann(body) {
@@ -1999,9 +1977,8 @@ impl Compiler<'_> {
19991977
}
20001978
}
20011979

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")?;
20051982
emit!(
20061983
self,
20071984
Instruction::CallIntrinsic1 {
@@ -2022,6 +1999,19 @@ impl Compiler<'_> {
20221999
}
20232000
);
20242001

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+
20252015
// Return the created class from the wrapper function
20262016
self.emit_return_value();
20272017

compiler/core/src/bytecode.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -456,8 +456,6 @@ pub enum Instruction {
456456
DeleteGlobal(Arg<NameIdx>),
457457
DeleteDeref(Arg<NameIdx>),
458458
LoadClosure(Arg<NameIdx>),
459-
MakeCell(Arg<NameIdx>),
460-
CopyFreeVars(Arg<u32>),
461459
Subscript,
462460
StoreSubscript,
463461
DeleteSubscript,
@@ -1275,8 +1273,6 @@ impl Instruction {
12751273
StoreFastLoadFast { .. } => 0, // Stores then loads, net effect 0
12761274
DeleteFast(_) | DeleteLocal(_) | DeleteGlobal(_) | DeleteDeref(_) => 0,
12771275
LoadClosure(_) => 1,
1278-
MakeCell(_) => 0,
1279-
CopyFreeVars(_n) => 0, // Copies from closure to locals, no stack effect
12801276
Subscript => -1,
12811277
StoreSubscript => -3,
12821278
DeleteSubscript => -2,
@@ -1595,8 +1591,6 @@ impl Instruction {
15951591
TypeAlias => w!(TypeAlias),
15961592
ParamSpec => w!(ParamSpec),
15971593
TypeVarTuple => w!(TypeVarTuple),
1598-
MakeCell(idx) => w!(MakeCell, cell_name = idx),
1599-
CopyFreeVars(count) => w!(CopyFreeVars, count),
16001594
}
16011595
}
16021596
}

vm/src/frame.rs

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -596,7 +596,12 @@ impl ExecutingFrame<'_> {
596596
}
597597
bytecode::Instruction::LoadClassDeref(i) => {
598598
let i = i.get(arg) as usize;
599-
let name = self.code.freevars[i - self.code.cellvars.len()];
599+
// Check if this is a cellvar or freevar
600+
let name = if i < self.code.cellvars.len() {
601+
self.code.cellvars[i]
602+
} else {
603+
self.code.freevars[i - self.code.cellvars.len()]
604+
};
600605
let value = self.locals.mapping().subscript(name, vm).ok();
601606
self.push_value(match value {
602607
Some(v) => v,
@@ -727,32 +732,6 @@ impl ExecutingFrame<'_> {
727732
self.push_value(value.into());
728733
Ok(None)
729734
}
730-
bytecode::Instruction::MakeCell(i) => {
731-
// Create a new cell for the given name
732-
let idx = i.get(arg) as usize;
733-
// Get the variable name from varnames
734-
if idx < self.code.varnames.len() {
735-
// Create a new cell for this variable
736-
// In CPython, MAKE_CELL creates cells for variables that will be used in closures
737-
// For now, we'll just create an unbound cell
738-
// The actual value will be set by StoreDeref
739-
}
740-
Ok(None)
741-
}
742-
bytecode::Instruction::CopyFreeVars(n) => {
743-
// Copy free variables from the closure to fast locals
744-
let n = n.get(arg) as usize;
745-
// In CPython, this copies free variables from the function's closure
746-
// to the frame's fast locals at the beginning of the code object
747-
let mut fastlocals = self.fastlocals.lock();
748-
for i in 0..n.min(self.cells_frees.len()) {
749-
let value = self.cells_frees[i].get();
750-
if i < fastlocals.len() {
751-
fastlocals[i] = value;
752-
}
753-
}
754-
Ok(None)
755-
}
756735
bytecode::Instruction::Subscript => self.execute_subscript(vm),
757736
bytecode::Instruction::StoreSubscript => self.execute_store_subscript(vm),
758737
bytecode::Instruction::DeleteSubscript => self.execute_delete_subscript(vm),

0 commit comments

Comments
 (0)