@@ -1374,7 +1374,7 @@ impl<O: OutputStream> Compiler<O> {
13741374 for ( i, element) in elements. iter ( ) . enumerate ( ) {
13751375 if let ast:: ExpressionType :: Starred { .. } = & element. node {
13761376 if seen_star {
1377- return Err ( self . error ( CompileErrorType :: StarArgs ) ) ;
1377+ return Err ( self . error ( CompileErrorType :: MultipleStarArgs ) ) ;
13781378 } else {
13791379 seen_star = true ;
13801380 self . emit ( Instruction :: UnpackEx {
@@ -1399,7 +1399,14 @@ impl<O: OutputStream> Compiler<O> {
13991399 }
14001400 }
14011401 }
1402- _ => return Err ( self . error ( CompileErrorType :: Assign ( target. name ( ) ) ) ) ,
1402+ _ => {
1403+ return Err ( self . error ( match target. node {
1404+ ast:: ExpressionType :: Starred { .. } => CompileErrorType :: SyntaxError (
1405+ "starred assignment target must be in a list or tuple" . to_owned ( ) ,
1406+ ) ,
1407+ _ => CompileErrorType :: Assign ( target. name ( ) ) ,
1408+ } ) )
1409+ }
14031410 }
14041411
14051412 Ok ( ( ) )
@@ -1782,11 +1789,7 @@ impl<O: OutputStream> Compiler<O> {
17821789 self . compile_comprehension ( kind, generators) ?;
17831790 }
17841791 Starred { .. } => {
1785- return Err (
1786- self . error ( CompileErrorType :: SyntaxError ( std:: string:: String :: from (
1787- "Invalid starred expression" ,
1788- ) ) ) ,
1789- ) ;
1792+ return Err ( self . error ( CompileErrorType :: InvalidStarExpr ) ) ;
17901793 }
17911794 IfExpression { test, body, orelse } => {
17921795 let no_label = self . new_label ( ) ;
@@ -2031,21 +2034,33 @@ impl<O: OutputStream> Compiler<O> {
20312034 }
20322035 }
20332036
2037+ let mut compile_element = |element| {
2038+ self . compile_expression ( element) . map_err ( |e| {
2039+ if matches ! ( e. error, CompileErrorType :: InvalidStarExpr ) {
2040+ self . error ( CompileErrorType :: SyntaxError (
2041+ "iterable unpacking cannot be used in comprehension" . to_owned ( ) ,
2042+ ) )
2043+ } else {
2044+ e
2045+ }
2046+ } )
2047+ } ;
2048+
20342049 match kind {
20352050 ast:: ComprehensionKind :: GeneratorExpression { element } => {
2036- self . compile_expression ( element) ?;
2051+ compile_element ( element) ?;
20372052 self . mark_generator ( ) ;
20382053 self . emit ( Instruction :: YieldValue ) ;
20392054 self . emit ( Instruction :: Pop ) ;
20402055 }
20412056 ast:: ComprehensionKind :: List { element } => {
2042- self . compile_expression ( element) ?;
2057+ compile_element ( element) ?;
20432058 self . emit ( Instruction :: ListAppend {
20442059 i : 1 + generators. len ( ) ,
20452060 } ) ;
20462061 }
20472062 ast:: ComprehensionKind :: Set { element } => {
2048- self . compile_expression ( element) ?;
2063+ compile_element ( element) ?;
20492064 self . emit ( Instruction :: SetAdd {
20502065 i : 1 + generators. len ( ) ,
20512066 } ) ;
0 commit comments