Skip to content

Commit 31f582d

Browse files
msimacektomasstupka
authored andcommitted
Add more error handling to the compiler
1 parent 1231e10 commit 31f582d

File tree

1 file changed

+41
-11
lines changed
  • graalpython/com.oracle.graal.python/src/com/oracle/graal/python/compiler

1 file changed

+41
-11
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/compiler/Compiler.java

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ private void checkForbiddenName(String id, ExprContext context) {
255255
}
256256
if (context == ExprContext.Delete) {
257257
if (id.equals("__debug__")) {
258-
errorCallback.onError(ErrorCallback.ErrorType.Syntax, unit.currentLocation, "cannot assign to __debug__");
258+
errorCallback.onError(ErrorCallback.ErrorType.Syntax, unit.currentLocation, "cannot delete __debug__");
259259
}
260260
}
261261
}
@@ -547,7 +547,7 @@ private void collectIntoArray(ExprTy[] nodes, int bits, int alreadyOnStack) {
547547
if (e instanceof ExprTy.Starred) {
548548
// splat
549549
collector.flushStackIfNecessary();
550-
e.accept(this);
550+
((ExprTy.Starred) e).value.accept(this);
551551
collector.appendCollection();
552552
} else {
553553
e.accept(this);
@@ -584,7 +584,21 @@ private void collectIntoDict(ExprTy[] keys, ExprTy[] values) {
584584
collector.finishCollection();
585585
}
586586

587+
private void validateKeywords(KeywordTy[] keywords) {
588+
for (int i = 0; i < keywords.length; i++) {
589+
if (keywords[i].arg != null) {
590+
checkForbiddenName(keywords[i].arg, ExprContext.Store);
591+
for (int j = i + 1; j < keywords.length; j++) {
592+
if (keywords[i].arg.equals(keywords[j].arg)) {
593+
errorCallback.onError(ErrorCallback.ErrorType.Syntax, unit.currentLocation, "keyword argument repeated: " + keywords[i].arg);
594+
}
595+
}
596+
}
597+
}
598+
}
599+
587600
private void collectKeywords(KeywordTy[] keywords, OpCodes callOp) {
601+
validateKeywords(keywords);
588602
boolean hasSplat = false;
589603
for (KeywordTy k : keywords) {
590604
if (k.arg == null) {
@@ -751,7 +765,7 @@ public Void visit(ExprTy.Await node) {
751765
errorCallback.onError(ErrorCallback.ErrorType.Syntax, unit.currentLocation, "'await' outside function");
752766
}
753767
if (unit.scopeType != CompilationScope.AsyncFunction && unit.scopeType != CompilationScope.Comprehension) {
754-
errorCallback.onError(ErrorCallback.ErrorType.Syntax, unit.currentLocation, "'await' outside function");
768+
errorCallback.onError(ErrorCallback.ErrorType.Syntax, unit.currentLocation, "'await' outside async function");
755769
}
756770
try {
757771
node.value.accept(this);
@@ -1336,14 +1350,13 @@ public Void visit(ExprTy.Slice node) {
13361350

13371351
@Override
13381352
public Void visit(ExprTy.Starred node) {
1339-
SourceRange savedLocation = setLocation(node);
1340-
try {
1341-
// TODO context?
1342-
node.value.accept(this);
1343-
return null;
1344-
} finally {
1345-
setLocation(savedLocation);
1353+
// Valid occurrences are handled by other visitors
1354+
if (node.context == ExprContext.Store) {
1355+
errorCallback.onError(ErrorCallback.ErrorType.Syntax, unit.currentLocation, "starred assignment target must be in a list or tuple");
1356+
} else {
1357+
errorCallback.onError(ErrorCallback.ErrorType.Syntax, unit.currentLocation, "can't use starred expression here");
13461358
}
1359+
return null;
13471360
}
13481361

13491362
@Override
@@ -1436,6 +1449,9 @@ public Void visit(ExprTy.UnaryOp node) {
14361449
public Void visit(ExprTy.Yield node) {
14371450
SourceRange savedLocation = setLocation(node);
14381451
try {
1452+
if (!unit.scope.isFunction()) {
1453+
errorCallback.onError(ErrorCallback.ErrorType.Syntax, unit.currentLocation, "'yield' outside function");
1454+
}
14391455
if (node.value != null) {
14401456
node.value.accept(this);
14411457
} else {
@@ -1453,6 +1469,12 @@ public Void visit(ExprTy.Yield node) {
14531469
public Void visit(ExprTy.YieldFrom node) {
14541470
SourceRange savedLocation = setLocation(node);
14551471
try {
1472+
if (!unit.scope.isFunction()) {
1473+
errorCallback.onError(ErrorCallback.ErrorType.Syntax, unit.currentLocation, "'yield' outside function");
1474+
}
1475+
if (unit.scopeType == CompilationScope.AsyncFunction) {
1476+
errorCallback.onError(ErrorCallback.ErrorType.Syntax, unit.currentLocation, "'yield from' inside async function");
1477+
}
14561478
node.value.accept(this);
14571479
// TODO GET_YIELD_FROM_ITER
14581480
addOp(GET_ITER);
@@ -1966,6 +1988,12 @@ public Void visit(StmtTy.Raise node) {
19661988
@Override
19671989
public Void visit(StmtTy.Return node) {
19681990
setLocation(node);
1991+
if (!unit.scope.isFunction()) {
1992+
errorCallback.onError(ErrorCallback.ErrorType.Syntax, unit.currentLocation, "'return' outside function");
1993+
}
1994+
if (node.value != null && unit.scope.isGenerator() && unit.scope.isCoroutine()) {
1995+
errorCallback.onError(ErrorCallback.ErrorType.Syntax, unit.currentLocation, "'return' with value in async generator");
1996+
}
19691997
if (node.value == null) {
19701998
unwindBlockStack(UnwindType.RETURN_CONST);
19711999
addOp(LOAD_NONE);
@@ -2042,8 +2070,10 @@ public Void visit(StmtTy.Try node) {
20422070
*/
20432071
commonCleanupHandler.unwindOffset = -1;
20442072
for (int i = 0; i < node.handlers.length; i++) {
2045-
assert !hasBareExcept;
20462073
setLocation(node.handlers[i]);
2074+
if (hasBareExcept) {
2075+
errorCallback.onError(ErrorCallback.ErrorType.Syntax, unit.currentLocation, "default 'except:' must be last");
2076+
}
20472077
unit.useNextBlock(nextHandler);
20482078

20492079
if (i < node.handlers.length - 1) {

0 commit comments

Comments
 (0)