FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Fix TypeParams, TypeAlias compile by youknowone · Pull Request #5862 · RustPython/RustPython · GitHub

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .rs  (2) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
26 changes: 22 additions & 4 deletions compiler/codegen/src/compile.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -1042,14 +1042,32 @@ impl Compiler<'_> {
)));
};
let name_string = name.id.to_string();
if type_params.is_some() {
self.push_symbol_table();
}
self.compile_expression(value)?;

// For PEP 695 syntax, we need to compile type_params first
// so that they're available when compiling the value expression
if let Some(type_params) = type_params {
self.push_symbol_table();

// Compile type params first to define T1, T2, etc.
self.compile_type_params(type_params)?;
// Stack now has type_params tuple at top

// Compile value expression (can now see T1, T2)
self.compile_expression(value)?;
// Stack: [type_params_tuple, value]

// We need [value, type_params_tuple] for TypeAlias instruction
emit!(self, Instruction::Rotate2);

self.pop_symbol_table();
} else {
// No type params - push value first, then None (not empty tuple)
self.compile_expression(value)?;
// Push None for type_params (matching CPython)
self.emit_load_const(ConstantData::None);
}

// Push name last
self.emit_load_const(ConstantData::Str {
value: name_string.clone().into(),
});
Expand Down
16 changes: 12 additions & 4 deletions vm/src/frame.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -1266,10 +1266,18 @@ impl ExecutingFrame<'_> {
}
bytecode::Instruction::TypeAlias => {
let name = self.pop_value();
let type_params: PyTupleRef = self
.pop_value()
.downcast()
.map_err(|_| vm.new_type_error("Type params must be a tuple."))?;
let type_params_obj = self.pop_value();

// CPython allows None or tuple for type_params
let type_params: PyTupleRef = if vm.is_none(&type_params_obj) {
// If None, use empty tuple (matching CPython's behavior)
vm.ctx.empty_tuple.clone()
} else {
type_params_obj
.downcast()
.map_err(|_| vm.new_type_error("Type params must be a tuple."))?
};

let value = self.pop_value();
let type_alias = typing::TypeAliasType::new(name, type_params, value);
self.push_value(type_alias.into_ref(&vm.ctx).into());
Expand Down

Back | FazBrowse Home | New Git URL