Mon, 05 Nov 2012 16:26:09 +0000
8000484: Bad error recovery when 'catch' without 'try' is found
Reviewed-by: jjg, mcimadamore
1.1 --- a/src/share/classes/com/sun/tools/javac/parser/JavacParser.java Tue Nov 06 18:41:56 2012 -0800 1.2 +++ b/src/share/classes/com/sun/tools/javac/parser/JavacParser.java Mon Nov 05 16:26:09 2012 +0000 1.3 @@ -88,6 +88,15 @@ 1.4 /** End position mappings container */ 1.5 private final AbstractEndPosTable endPosTable; 1.6 1.7 + interface ErrorRecoveryAction { 1.8 + JCTree doRecover(JavacParser parser); 1.9 + } 1.10 + 1.11 + enum BasicErrorRecoveryAction implements ErrorRecoveryAction { 1.12 + BLOCK_STMT {public JCTree doRecover(JavacParser parser) { return parser.parseStatementAsBlock(); }}, 1.13 + CATCH_CLAUSE {public JCTree doRecover(JavacParser parser) { return parser.catchClause(); }} 1.14 + } 1.15 + 1.16 /** Construct a parser from a given scanner, tree factory and log. 1.17 */ 1.18 protected JavacParser(ParserFactory fac, 1.19 @@ -2102,11 +2111,15 @@ 1.20 nextToken(); 1.21 return toP(F.at(pos).Skip()); 1.22 case ELSE: 1.23 - return toP(F.Exec(syntaxError("else.without.if"))); 1.24 + int elsePos = token.pos; 1.25 + nextToken(); 1.26 + return doRecover(elsePos, BasicErrorRecoveryAction.BLOCK_STMT, "else.without.if"); 1.27 case FINALLY: 1.28 - return toP(F.Exec(syntaxError("finally.without.try"))); 1.29 + int finallyPos = token.pos; 1.30 + nextToken(); 1.31 + return doRecover(finallyPos, BasicErrorRecoveryAction.BLOCK_STMT, "finally.without.try"); 1.32 case CATCH: 1.33 - return toP(F.Exec(syntaxError("catch.without.try"))); 1.34 + return doRecover(token.pos, BasicErrorRecoveryAction.CATCH_CLAUSE, "catch.without.try"); 1.35 case ASSERT: { 1.36 if (allowAsserts && token.kind == ASSERT) { 1.37 nextToken(); 1.38 @@ -2139,6 +2152,13 @@ 1.39 } 1.40 } 1.41 1.42 + private JCStatement doRecover(int startPos, ErrorRecoveryAction action, String key) { 1.43 + int errPos = S.errPos(); 1.44 + JCTree stm = action.doRecover(this); 1.45 + S.errPos(errPos); 1.46 + return toP(F.Exec(syntaxError(startPos, List.<JCTree>of(stm), key))); 1.47 + } 1.48 + 1.49 /** CatchClause = CATCH "(" FormalParameter ")" Block 1.50 */ 1.51 protected JCCatch catchClause() {
2.1 --- a/test/tools/javac/diags/examples/CatchWithoutTry.java Tue Nov 06 18:41:56 2012 -0800 2.2 +++ b/test/tools/javac/diags/examples/CatchWithoutTry.java Mon Nov 05 16:26:09 2012 +0000 2.3 @@ -22,9 +22,6 @@ 2.4 */ 2.5 2.6 // key: compiler.err.catch.without.try 2.7 -// key: compiler.err.expected 2.8 -// key: compiler.err.not.stmt 2.9 -// key: compiler.err.lambda.not.supported.in.source 2.10 2.11 class CatchWithoutTry { 2.12 void m() {
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 3.2 +++ b/test/tools/javac/incompleteStatements/T8000484.java Mon Nov 05 16:26:09 2012 +0000 3.3 @@ -0,0 +1,17 @@ 3.4 +/* 3.5 + * @test /nodynamiccopyright/ 3.6 + * @bug 8000484 3.7 + * @summary Bad error recovery when 'catch' without 'try' is found 3.8 + * @compile/fail/ref=T8000484.out -XDrawDiagnostics T8000484.java 3.9 + */ 3.10 + 3.11 +public class T8000484 { 3.12 + void m() { 3.13 + catch (Exception e){} 3.14 + else{} 3.15 + finally{} 3.16 + catch (Exception e) {catch (Exception e){}} 3.17 + else{else{}} 3.18 + finally{finally{}} 3.19 + } 3.20 +}
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 4.2 +++ b/test/tools/javac/incompleteStatements/T8000484.out Mon Nov 05 16:26:09 2012 +0000 4.3 @@ -0,0 +1,10 @@ 4.4 +T8000484.java:10:9: compiler.err.catch.without.try 4.5 +T8000484.java:11:9: compiler.err.else.without.if 4.6 +T8000484.java:12:9: compiler.err.finally.without.try 4.7 +T8000484.java:13:30: compiler.err.catch.without.try 4.8 +T8000484.java:13:9: compiler.err.catch.without.try 4.9 +T8000484.java:14:14: compiler.err.else.without.if 4.10 +T8000484.java:14:9: compiler.err.else.without.if 4.11 +T8000484.java:15:17: compiler.err.finally.without.try 4.12 +T8000484.java:15:9: compiler.err.finally.without.try 4.13 +9 errors