Skip to content

Commit dfe23f2

Browse files
morealyouknowone
authored andcommitted
Implement vm logics related with ParamSpec, TypeVarTuple
1 parent 3de1c2a commit dfe23f2

File tree

5 files changed

+72
-7
lines changed

5 files changed

+72
-7
lines changed

compiler/codegen/src/compile.rs

+21-2
Original file line numberDiff line numberDiff line change
@@ -1095,8 +1095,27 @@ impl Compiler {
10951095
self.store_name(name.as_ref())?;
10961096
}
10971097
}
1098-
located_ast::TypeParam::ParamSpec(_) => todo!(),
1099-
located_ast::TypeParam::TypeVarTuple(_) => todo!(),
1098+
located_ast::TypeParam::ParamSpec(located_ast::TypeParamParamSpec {
1099+
name, ..
1100+
}) => {
1101+
self.emit_load_const(ConstantData::Str {
1102+
value: name.to_string(),
1103+
});
1104+
emit!(self, Instruction::ParamSpec);
1105+
emit!(self, Instruction::Duplicate);
1106+
self.store_name(name.as_ref())?;
1107+
}
1108+
located_ast::TypeParam::TypeVarTuple(located_ast::TypeParamTypeVarTuple {
1109+
name,
1110+
..
1111+
}) => {
1112+
self.emit_load_const(ConstantData::Str {
1113+
value: name.to_string(),
1114+
});
1115+
emit!(self, Instruction::TypeVarTuple);
1116+
emit!(self, Instruction::Duplicate);
1117+
self.store_name(name.as_ref())?;
1118+
}
11001119
};
11011120
}
11021121
emit!(

compiler/codegen/src/symboltable.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -1257,8 +1257,18 @@ impl SymbolTableBuilder {
12571257
self.scan_expression(binding, ExpressionContext::Load)?;
12581258
}
12591259
}
1260-
ast::located::TypeParam::ParamSpec(_) => todo!(),
1261-
ast::located::TypeParam::TypeVarTuple(_) => todo!(),
1260+
ast::located::TypeParam::ParamSpec(ast::TypeParamParamSpec {
1261+
name,
1262+
range: param_spec_range,
1263+
}) => {
1264+
self.register_name(name, SymbolUsage::Assigned, param_spec_range.start)?;
1265+
}
1266+
ast::located::TypeParam::TypeVarTuple(ast::TypeParamTypeVarTuple {
1267+
name,
1268+
range: type_var_tuple_range,
1269+
}) => {
1270+
self.register_name(name, SymbolUsage::Assigned, type_var_tuple_range.start)?;
1271+
}
12621272
}
12631273
}
12641274
Ok(())

compiler/core/src/bytecode.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -595,10 +595,12 @@ pub enum Instruction {
595595
TypeVarWithBound,
596596
TypeVarWithConstraint,
597597
TypeAlias,
598+
TypeVarTuple,
599+
ParamSpec,
598600
// If you add a new instruction here, be sure to keep LAST_INSTRUCTION updated
599601
}
600602
// This must be kept up to date to avoid marshaling errors
601-
const LAST_INSTRUCTION: Instruction = Instruction::TypeAlias;
603+
const LAST_INSTRUCTION: Instruction = Instruction::ParamSpec;
602604
const _: () = assert!(mem::size_of::<Instruction>() == 1);
603605

604606
impl From<Instruction> for u8 {
@@ -1291,6 +1293,8 @@ impl Instruction {
12911293
TypeVarWithBound => -1,
12921294
TypeVarWithConstraint => -1,
12931295
TypeAlias => -2,
1296+
ParamSpec => 0,
1297+
TypeVarTuple => 0,
12941298
}
12951299
}
12961300

@@ -1460,6 +1464,8 @@ impl Instruction {
14601464
TypeVarWithBound => w!(TypeVarWithBound),
14611465
TypeVarWithConstraint => w!(TypeVarWithConstraint),
14621466
TypeAlias => w!(TypeAlias),
1467+
ParamSpec => w!(ParamSpec),
1468+
TypeVarTuple => w!(TypeVarTuple),
14631469
}
14641470
}
14651471
}

vm/src/frame.rs

+17
Original file line numberDiff line numberDiff line change
@@ -1202,6 +1202,23 @@ impl ExecutingFrame<'_> {
12021202
self.push_value(type_alias.into_ref(&vm.ctx).into());
12031203
Ok(None)
12041204
}
1205+
bytecode::Instruction::ParamSpec => {
1206+
let param_spec_name = self.pop_value();
1207+
let param_spec: PyObjectRef = _typing::make_paramspec(param_spec_name.clone())
1208+
.into_ref(&vm.ctx)
1209+
.into();
1210+
self.push_value(param_spec);
1211+
Ok(None)
1212+
}
1213+
bytecode::Instruction::TypeVarTuple => {
1214+
let type_var_tuple_name = self.pop_value();
1215+
let type_var_tuple: PyObjectRef =
1216+
_typing::make_typevartuple(type_var_tuple_name.clone())
1217+
.into_ref(&vm.ctx)
1218+
.into();
1219+
self.push_value(type_var_tuple);
1220+
Ok(None)
1221+
}
12051222
}
12061223
}
12071224

vm/src/stdlib/typing.rs

+15-2
Original file line numberDiff line numberDiff line change
@@ -75,18 +75,31 @@ pub(crate) mod _typing {
7575
#[pyclass(name = "ParamSpec")]
7676
#[derive(Debug, PyPayload)]
7777
#[allow(dead_code)]
78-
struct ParamSpec {}
78+
pub(crate) struct ParamSpec {
79+
name: PyObjectRef,
80+
}
81+
7982
#[pyclass(flags(BASETYPE))]
8083
impl ParamSpec {}
8184

85+
pub(crate) fn make_paramspec(name: PyObjectRef) -> ParamSpec {
86+
ParamSpec { name }
87+
}
88+
8289
#[pyattr]
8390
#[pyclass(name = "TypeVarTuple")]
8491
#[derive(Debug, PyPayload)]
8592
#[allow(dead_code)]
86-
pub(crate) struct TypeVarTuple {}
93+
pub(crate) struct TypeVarTuple {
94+
name: PyObjectRef,
95+
}
8796
#[pyclass(flags(BASETYPE))]
8897
impl TypeVarTuple {}
8998

99+
pub(crate) fn make_typevartuple(name: PyObjectRef) -> TypeVarTuple {
100+
TypeVarTuple { name }
101+
}
102+
90103
#[pyattr]
91104
#[pyclass(name = "ParamSpecArgs")]
92105
#[derive(Debug, PyPayload)]

0 commit comments

Comments
 (0)