Skip to content

Commit 5e37557

Browse files
committed
[GR-25621] Fix cast exception on syntax error
PullRequest: graalpython/1202
2 parents 45fd476 + 91f5891 commit 5e37557

File tree

4 files changed

+25
-4
lines changed

4 files changed

+25
-4
lines changed

graalpython/com.oracle.graal.python.test/src/tests/test_parser.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,3 +158,12 @@ def assert_raise(s, msg):
158158
assert_raise("''", "cannot assign to literal")
159159
assert_raise("f''", "cannot assign to f-string expression")
160160
assert_raise("(None,)", "cannot assign to None")
161+
162+
163+
def test_invalid_ann_assignment():
164+
try:
165+
exec("lambda: x:x")
166+
except SyntaxError as e:
167+
assert e.msg == 'illegal target for annotation'
168+
return
169+
assert False, "SyntaxError not raised"

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/ErrorMessages.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,4 +546,6 @@ public abstract class ErrorMessages {
546546
public static final String CANNOT_CONVERT_NEGATIVE_VALUE_TO_UNSIGNED_INT = "can't convert negative value to unsigned int";
547547
public static final String SEND_NON_NONE_TO_UNSTARTED_GENERATOR = "can't send non-None value to a just-started generator";
548548
public static final String UNSUPPORTED_FORMAT_STRING_PASSED_TO_P_FORMAT = "unsupported format string passed to %p.__format__";
549+
public static final String ONLY_SINGLE_TARGET_CAN_BE_ANNOTATED = "only single target (not %s) can be annotated";
550+
public static final String ILLEGAL_TARGET_FOR_ANNOTATION = "illegal target for annotation";
549551
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/parser/PythonSSTNodeFactory.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -210,9 +210,6 @@ public SSTNode createAssignment(SSTNode[] lhs, SSTNode rhs, int start, int stop)
210210
}
211211

212212
public SSTNode createAnnAssignment(SSTNode lhs, SSTNode type, SSTNode rhs, int start, int end) {
213-
if (lhs instanceof CollectionSSTNode) {
214-
throw errors.raiseInvalidSyntax(source, createSourceSection(lhs.getStartOffset(), lhs.getEndOffset()), "only single target (not tuple) can be annotated");
215-
}
216213
declareVar(lhs);
217214
if (!scopeEnvironment.getCurrentScope().hasAnnotations()) {
218215
scopeEnvironment.getCurrentScope().setHasAnnotations(true);

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ public PNode visit(AndSSTNode node) {
275275

276276
@Override
277277
public PNode visit(AnnAssignmentSSTNode node) {
278-
PNode assignmentNode = visit((AssignmentSSTNode) node);
278+
PNode assignmentNode = createAssignment(node, true);
279279
if (!scopeEnvironment.isInFunctionScope() && node.type != null && node.lhs.length == 1 && node.lhs[0] instanceof VarLookupSSTNode) {
280280
// annotations in a function we ignore at all. Even there are not evalueated, whether
281281
// the type is wrong
@@ -303,12 +303,25 @@ public PNode visit(AssertSSTNode node) {
303303

304304
@Override
305305
public PNode visit(AssignmentSSTNode node) {
306+
return createAssignment(node, false);
307+
}
308+
309+
public StatementNode createAssignment(AssignmentSSTNode node, boolean checkAnnotationPermitted) {
306310
ExpressionNode[] lhs = new ExpressionNode[node.lhs.length];
307311
int numYields = getCurrentNumberOfYields();
308312
for (int i = 0; i < node.lhs.length; i++) {
309313
SSTNode sstLhs = node.lhs[i];
310314
checkCannotAssignTo(sstLhs);
311315
lhs[i] = (ExpressionNode) sstLhs.accept(this);
316+
if (checkAnnotationPermitted) {
317+
if (lhs[i] instanceof TupleLiteralNode) {
318+
throw errors.raiseInvalidSyntax(source, createSourceSection(sstLhs.getStartOffset(), sstLhs.getEndOffset()), ErrorMessages.ONLY_SINGLE_TARGET_CAN_BE_ANNOTATED, "tuple");
319+
} else if (lhs[i] instanceof ListLiteralNode) {
320+
throw errors.raiseInvalidSyntax(source, createSourceSection(sstLhs.getStartOffset(), sstLhs.getEndOffset()), ErrorMessages.ONLY_SINGLE_TARGET_CAN_BE_ANNOTATED, "list");
321+
} else if (!(lhs[i] instanceof ReadNode)) {
322+
throw errors.raiseInvalidSyntax(source, createSourceSection(sstLhs.getStartOffset(), sstLhs.getEndOffset()), ErrorMessages.ILLEGAL_TARGET_FOR_ANNOTATION);
323+
}
324+
}
312325
}
313326
ExpressionNode rhs = (ExpressionNode) node.rhs.accept(this);
314327

0 commit comments

Comments
 (0)