Skip to content

Commit e1f5b61

Browse files
committed
Fix error messages for assignments to yield
1 parent 6273cbd commit e1f5b61

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/parser/sst/FactorySSTVisitor.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@
9292
import com.oracle.graal.python.nodes.function.FunctionDefinitionNode;
9393
import com.oracle.graal.python.nodes.function.FunctionRootNode;
9494
import com.oracle.graal.python.nodes.function.GeneratorFunctionDefinitionNode;
95+
import com.oracle.graal.python.nodes.generator.AbstractYieldNode;
9596
import com.oracle.graal.python.nodes.generator.GeneratorBlockNode;
9697
import com.oracle.graal.python.nodes.generator.GeneratorReturnTargetNode;
9798
import com.oracle.graal.python.nodes.generator.ReadGeneratorFrameVariableNode;
@@ -331,6 +332,7 @@ public PNode visit(AssignmentSSTNode node) {
331332
public PNode visit(AugAssignmentSSTNode node) {
332333
checkCannotAssignTo(node.lhs);
333334
ExpressionNode lhs = (ExpressionNode) node.lhs.accept(this);
335+
checkExpressionAssignable(lhs);
334336
if (!(lhs instanceof ReadNode)) {
335337
throw errors.raiseInvalidSyntax(source, createSourceSection(node.startOffset, node.endOffset), ErrorMessages.ILLEGAL_EXPRESSION_FOR_AUGMENTED_ASSIGNEMNT);
336338
}
@@ -1366,7 +1368,7 @@ protected StatementNode makeWriteNode(ExpressionNode accept) {
13661368
}
13671369
}
13681370

1369-
private StatementNode createAssignment(ExpressionNode lhs, ExpressionNode rhs) {
1371+
private void checkExpressionAssignable(ExpressionNode lhs) {
13701372
if (lhs instanceof ObjectLiteralNode) {
13711373
if (((ObjectLiteralNode) lhs).getObject() == PEllipsis.INSTANCE) {
13721374
throw errors.raiseInvalidSyntax(source, lhs.getSourceSection(), ErrorMessages.CANNOT_ASSIGN_TO, "Ellipsis");
@@ -1387,7 +1389,14 @@ private StatementNode createAssignment(ExpressionNode lhs, ExpressionNode rhs) {
13871389
throw errors.raiseInvalidSyntax(source, lhs.getSourceSection(), ErrorMessages.CANNOT_ASSIGN_TO, "set display");
13881390
} else if (lhs instanceof FormatStringLiteralNode) {
13891391
throw errors.raiseInvalidSyntax(source, lhs.getSourceSection(), ErrorMessages.CANNOT_ASSIGN_TO, "f-string expression");
1390-
} else if (lhs instanceof TupleLiteralNode) {
1392+
} else if (lhs instanceof AbstractYieldNode) {
1393+
throw errors.raiseInvalidSyntax(source, lhs.getSourceSection(), ErrorMessages.CANNOT_ASSIGN_TO, "yield expression");
1394+
}
1395+
}
1396+
1397+
private StatementNode createAssignment(ExpressionNode lhs, ExpressionNode rhs) {
1398+
checkExpressionAssignable(lhs);
1399+
if (lhs instanceof TupleLiteralNode) {
13911400
return createDestructuringAssignment(((TupleLiteralNode) lhs).getValues(), rhs);
13921401
} else if (lhs instanceof ListLiteralNode) {
13931402
return createDestructuringAssignment(((ListLiteralNode) lhs).getValues(), rhs);

graalpython/lib-python/3/test/test_generators.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1858,7 +1858,9 @@ def printsolution(self, x):
18581858
...
18591859
SyntaxError: 'yield' outside function
18601860
1861-
>>> def f(): x = yield = y
1861+
1862+
# XXX Truffle change: relax the message, CPython error messages are inconsistent and arbitrary here
1863+
>>> def f(): x = yield = y # doctest:+IGNORE_EXCEPTION_DETAIL
18621864
Traceback (most recent call last):
18631865
...
18641866
SyntaxError: assignment to yield expression not possible

0 commit comments

Comments
 (0)