Skip to content

Commit c43b1ff

Browse files
committed
[GR-27201] Wrong Syntax Error message, when a function call is annotated.
1 parent 3b14977 commit c43b1ff

File tree

5 files changed

+52
-17
lines changed

5 files changed

+52
-17
lines changed

graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/test/parser/AssignmentTests.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,4 +193,29 @@ public void annotationType02() throws Exception {
193193
public void annotationType03() throws Exception {
194194
checkTreeResult("j = 1\n" + "ahoj.__annotations__['j'] = float");
195195
}
196+
197+
@Test
198+
public void annotationType04() throws Exception {
199+
checkSyntaxErrorMessage("lambda: x:x", "SyntaxError: illegal target for annotation");
200+
}
201+
202+
@Test
203+
public void annotationType05() throws Exception {
204+
checkSyntaxErrorMessage("{}: int", "SyntaxError: illegal target for annotation");
205+
}
206+
207+
@Test
208+
public void annotationType06() throws Exception {
209+
checkSyntaxErrorMessage("(1,2): int", "SyntaxError: only single target (not tuple) can be annotated");
210+
}
211+
212+
@Test
213+
public void annotationType07() throws Exception {
214+
checkSyntaxErrorMessage("[1,2]: int", "SyntaxError: only single target (not list) can be annotated");
215+
}
216+
217+
@Test
218+
public void annotationType08() throws Exception {
219+
checkSyntaxErrorMessage("list(): int", "SyntaxError: illegal target for annotation");
220+
}
196221
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ public abstract class ErrorMessages {
112112
public static final String CAN_ONLY_CONCAT_S_NOT_P_TO_S = "can only concatenate %s (not \"%p\") to %s";
113113
public static final String CAN_ONLY_JOIN_ITERABLE = "can only join an iterable";
114114
public static final String CANNOT_ASSIGN_TO = "cannot assign to %s";
115+
public static final String CANNOT_ASSIGN_TO_COMPREHENSION = "cannot assign to %s comprehension";
115116
public static final String CANNOT_BE_INTEPRETED_AS_LONG = "%s cannot be interpreted as long (type %p)";
116117
public static final String CANNOT_BE_NEGATIVE = "%s cannot be negative";
117118
public static final String CANNOT_CALL_CTOR_OF = "cannot call constructor of %s";

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,15 @@
6666
import com.oracle.graal.python.parser.sst.ForSSTNode;
6767
import com.oracle.graal.python.parser.sst.FunctionDefSSTNode;
6868
import com.oracle.graal.python.parser.sst.GeneratorFactorySSTVisitor;
69+
import com.oracle.graal.python.parser.sst.GetAttributeSSTNode;
6970
import com.oracle.graal.python.parser.sst.ImportFromSSTNode;
7071
import com.oracle.graal.python.parser.sst.ImportSSTNode;
7172
import com.oracle.graal.python.parser.sst.SSTNode;
7273
import com.oracle.graal.python.parser.sst.SimpleSSTNode;
7374
import com.oracle.graal.python.parser.sst.StarSSTNode;
7475
import com.oracle.graal.python.parser.sst.StringLiteralSSTNode;
7576
import com.oracle.graal.python.parser.sst.StringUtils;
77+
import com.oracle.graal.python.parser.sst.SubscriptSSTNode;
7678
import com.oracle.graal.python.parser.sst.VarLookupSSTNode;
7779
import com.oracle.graal.python.parser.sst.WithSSTNode;
7880
import com.oracle.graal.python.parser.sst.YieldExpressionSSTNode;
@@ -218,6 +220,18 @@ public SSTNode createAnnAssignment(SSTNode lhs, SSTNode type, SSTNode rhs, int s
218220
if (!scopeEnvironment.getCurrentScope().hasAnnotations()) {
219221
scopeEnvironment.getCurrentScope().setHasAnnotations(true);
220222
}
223+
// checking if the annotation has the right target
224+
if (!(lhs instanceof VarLookupSSTNode || lhs instanceof GetAttributeSSTNode || lhs instanceof SubscriptSSTNode)) {
225+
if (lhs instanceof CollectionSSTNode) {
226+
CollectionSSTNode collectionNode = (CollectionSSTNode) lhs;
227+
if (collectionNode.getType() == PythonBuiltinClassType.PList) {
228+
throw errors.raiseInvalidSyntax(source, createSourceSection(lhs.getStartOffset(), lhs.getEndOffset()), ErrorMessages.ONLY_SINGLE_TARGET_CAN_BE_ANNOTATED, "list");
229+
} else if (collectionNode.getType() == PythonBuiltinClassType.PTuple) {
230+
throw errors.raiseInvalidSyntax(source, createSourceSection(lhs.getStartOffset(), lhs.getEndOffset()), ErrorMessages.ONLY_SINGLE_TARGET_CAN_BE_ANNOTATED, "tuple");
231+
}
232+
}
233+
throw errors.raiseInvalidSyntax(source, createSourceSection(lhs.getStartOffset(), lhs.getEndOffset()), ErrorMessages.ILLEGAL_TARGET_FOR_ANNOTATION);
234+
}
221235
return new AnnAssignmentSSTNode(lhs, type, rhs, start, end);
222236
}
223237

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2019, 2020, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -62,4 +62,8 @@ public SSTNode[] getValues() {
6262
return values;
6363
}
6464

65+
public PythonBuiltinClassType getType() {
66+
return type;
67+
}
68+
6569
}

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

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ public PNode visit(AndSSTNode node) {
273273

274274
@Override
275275
public PNode visit(AnnAssignmentSSTNode node) {
276-
PNode assignmentNode = createAssignment(node, true);
276+
PNode assignmentNode = createAssignment(node);
277277
if (!scopeEnvironment.isInFunctionScope() && node.type != null && node.lhs.length == 1 && node.lhs[0] instanceof VarLookupSSTNode) {
278278
// annotations in a function we ignore at all. Even there are not evalueated, whether
279279
// the type is wrong
@@ -301,25 +301,16 @@ public PNode visit(AssertSSTNode node) {
301301

302302
@Override
303303
public PNode visit(AssignmentSSTNode node) {
304-
return createAssignment(node, false);
304+
return createAssignment(node);
305305
}
306306

307-
public StatementNode createAssignment(AssignmentSSTNode node, boolean checkAnnotationPermitted) {
307+
private StatementNode createAssignment(AssignmentSSTNode node) {
308308
ExpressionNode[] lhs = new ExpressionNode[node.lhs.length];
309309
int numYields = getCurrentNumberOfYields();
310310
for (int i = 0; i < node.lhs.length; i++) {
311311
SSTNode sstLhs = node.lhs[i];
312312
checkCannotAssignTo(sstLhs);
313313
lhs[i] = (ExpressionNode) sstLhs.accept(this);
314-
if (checkAnnotationPermitted) {
315-
if (lhs[i] instanceof TupleLiteralNode) {
316-
throw errors.raiseInvalidSyntax(source, createSourceSection(sstLhs.getStartOffset(), sstLhs.getEndOffset()), ErrorMessages.ONLY_SINGLE_TARGET_CAN_BE_ANNOTATED, "tuple");
317-
} else if (lhs[i] instanceof ListLiteralNode) {
318-
throw errors.raiseInvalidSyntax(source, createSourceSection(sstLhs.getStartOffset(), sstLhs.getEndOffset()), ErrorMessages.ONLY_SINGLE_TARGET_CAN_BE_ANNOTATED, "list");
319-
} else if (!(lhs[i] instanceof ReadNode)) {
320-
throw errors.raiseInvalidSyntax(source, createSourceSection(sstLhs.getStartOffset(), sstLhs.getEndOffset()), ErrorMessages.ILLEGAL_TARGET_FOR_ANNOTATION);
321-
}
322-
}
323314
}
324315
ExpressionNode rhs = (ExpressionNode) node.rhs.accept(this);
325316

@@ -361,7 +352,7 @@ private void checkCannotAssignTo(SSTNode lhs) throws RuntimeException {
361352
if (lhs instanceof ForComprehensionSSTNode) {
362353
PythonBuiltinClassType resultType = ((ForComprehensionSSTNode) lhs).resultType;
363354
if (resultType == PythonBuiltinClassType.PGenerator) {
364-
throw errors.raiseInvalidSyntax(source, createSourceSection(lhs.startOffset, lhs.endOffset), "cannot assign to generator expression");
355+
throw errors.raiseInvalidSyntax(source, createSourceSection(lhs.startOffset, lhs.endOffset), ErrorMessages.CANNOT_ASSIGN_TO, "generator expression");
365356
}
366357
String calleeName;
367358
switch (resultType) {
@@ -378,12 +369,12 @@ private void checkCannotAssignTo(SSTNode lhs) throws RuntimeException {
378369
calleeName = null;
379370
}
380371
if (calleeName == null) {
381-
throw errors.raiseInvalidSyntax(source, createSourceSection(lhs.startOffset, lhs.endOffset), "cannot assign to comprehension");
372+
throw errors.raiseInvalidSyntax(source, createSourceSection(lhs.startOffset, lhs.endOffset), ErrorMessages.CANNOT_ASSIGN_TO, "comprehension");
382373
} else {
383-
throw errors.raiseInvalidSyntax(source, createSourceSection(lhs.startOffset, lhs.endOffset), "cannot assign to %s comprehension", calleeName);
374+
throw errors.raiseInvalidSyntax(source, createSourceSection(lhs.startOffset, lhs.endOffset), ErrorMessages.CANNOT_ASSIGN_TO_COMPREHENSION, calleeName);
384375
}
385376
} else if (lhs instanceof CallSSTNode) {
386-
throw errors.raiseInvalidSyntax(source, createSourceSection(lhs.startOffset, lhs.endOffset), "cannot assign to function call");
377+
throw errors.raiseInvalidSyntax(source, createSourceSection(lhs.startOffset, lhs.endOffset), ErrorMessages.CANNOT_ASSIGN_TO, "function call");
387378
} else if (lhs instanceof CollectionSSTNode) {
388379
for (SSTNode n : ((CollectionSSTNode) lhs).getValues()) {
389380
checkCannotAssignTo(n);

0 commit comments

Comments
 (0)