mirror of
https://github.com/RustPython/RustPython.git
synced 2026-10-29 03:04:38 +08:00
comprehension starred expression compatibility
This commit is contained in:
1 parent
09d849ab0a
commit
e4be376336
1 file changed
+15
-3
+15
-3
@@ -2027,21 +2027,33 @@ impl<O: OutputStream> Compiler<O> {
|
||||
}
|
||||
}
|
||||
|
||||
let mut compile_element = |element| {
|
||||
self.compile_expression(element).map_err(|e| {
|
||||
if matches!(e.error, CompileErrorType::InvalidStarExpr) {
|
||||
self.error(CompileErrorType::SyntaxError(
|
||||
"iterable unpacking cannot be used in comprehension".to_owned(),
|
||||
))
|
||||
} else {
|
||||
e
|
||||
}
|
||||
})
|
||||
};
|
||||
|
||||
match kind {
|
||||
ast::ComprehensionKind::GeneratorExpression { element } => {
|
||||
self.compile_expression(element)?;
|
||||
compile_element(element)?;
|
||||
self.mark_generator();
|
||||
self.emit(Instruction::YieldValue);
|
||||
self.emit(Instruction::Pop);
|
||||
}
|
||||
ast::ComprehensionKind::List { element } => {
|
||||
self.compile_expression(element)?;
|
||||
compile_element(element)?;
|
||||
self.emit(Instruction::ListAppend {
|
||||
i: 1 + generators.len(),
|
||||
});
|
||||
}
|
||||
ast::ComprehensionKind::Set { element } => {
|
||||
self.compile_expression(element)?;
|
||||
compile_element(element)?;
|
||||
self.emit(Instruction::SetAdd {
|
||||
i: 1 + generators.len(),
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user