60
60
import com .oracle .graal .python .builtins .objects .PNone ;
61
61
import com .oracle .graal .python .builtins .objects .complex .PComplex ;
62
62
import com .oracle .graal .python .builtins .objects .function .Signature ;
63
+ import com .oracle .graal .python .nodes .BuiltinNames ;
63
64
import com .oracle .graal .python .nodes .EmptyNode ;
64
65
import com .oracle .graal .python .nodes .NoValueNode ;
65
66
import com .oracle .graal .python .nodes .NodeFactory ;
@@ -244,6 +245,7 @@ public PNode visit(AssignmentSSTNode node) {
244
245
ExpressionNode [] lhs = new ExpressionNode [node .lhs .length ];
245
246
for (int i = 0 ; i < node .lhs .length ; i ++) {
246
247
SSTNode sstLhs = node .lhs [i ];
248
+ checkCannotAssignTo (sstLhs );
247
249
lhs [i ] = (ExpressionNode ) sstLhs .accept (this );
248
250
}
249
251
ExpressionNode rhs = (ExpressionNode ) node .rhs .accept (this );
@@ -268,6 +270,7 @@ public PNode visit(AssignmentSSTNode node) {
268
270
269
271
@ Override
270
272
public PNode visit (AugAssignmentSSTNode node ) {
273
+ checkCannotAssignTo (node .lhs );
271
274
ExpressionNode lhs = (ExpressionNode ) node .lhs .accept (this );
272
275
if (!(lhs instanceof ReadNode )) {
273
276
throw errors .raiseInvalidSyntax (source , createSourceSection (node .startOffset , node .endOffset ), "illegal expression for augmented assignment" );
@@ -280,6 +283,36 @@ public PNode visit(AugAssignmentSSTNode node) {
280
283
return result ;
281
284
}
282
285
286
+ private void checkCannotAssignTo (SSTNode lhs ) throws RuntimeException {
287
+ if (lhs instanceof ForComprehensionSSTNode ) {
288
+ String calleeName ;
289
+ switch (((ForComprehensionSSTNode ) lhs ).resultType ) {
290
+ case PList :
291
+ calleeName = BuiltinNames .LIST ;
292
+ break ;
293
+ case PSet :
294
+ calleeName = BuiltinNames .SET ;
295
+ break ;
296
+ case PDict :
297
+ calleeName = BuiltinNames .DICT ;
298
+ break ;
299
+ default :
300
+ calleeName = null ;
301
+ }
302
+ if (calleeName == null ) {
303
+ throw errors .raiseInvalidSyntax (source , createSourceSection (lhs .startOffset , lhs .endOffset ), "cannot assign to comprehension" );
304
+ } else {
305
+ throw errors .raiseInvalidSyntax (source , createSourceSection (lhs .startOffset , lhs .endOffset ), "cannot assign to %s comprehension" , calleeName );
306
+ }
307
+ } else if (lhs instanceof CallSSTNode ) {
308
+ throw errors .raiseInvalidSyntax (source , createSourceSection (lhs .startOffset , lhs .endOffset ), "cannot assign to function call" );
309
+ } else if (lhs instanceof CollectionSSTNode ) {
310
+ for (SSTNode n : ((CollectionSSTNode ) lhs ).getValues ()) {
311
+ checkCannotAssignTo (n );
312
+ }
313
+ }
314
+ }
315
+
283
316
@ Override
284
317
public PNode visit (BinaryArithmeticSSTNode node ) {
285
318
ExpressionNode left = (ExpressionNode ) node .left .accept (this );
0 commit comments