From e4be376336981c43eb5d65ae76edee50d22e6302 Mon Sep 17 00:00:00 2001 From: Jeong YunWon Date: Wed, 13 May 2020 01:43:09 +0900 Subject: [PATCH] comprehension starred expression compatibility --- compiler/src/compile.rs | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/compiler/src/compile.rs b/compiler/src/compile.rs index df543eb7c..4a8ee9025 100644 --- a/compiler/src/compile.rs +++ b/compiler/src/compile.rs @@ -2027,21 +2027,33 @@ impl Compiler { } } + 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(), });