提交 c2bfaaf2 编写于 作者: D darcy

7014734: Project Coin: Allow optional trailing semicolon to terminate...

7014734: Project Coin: Allow optional trailing semicolon to terminate resources list in try-with-resources
Reviewed-by: jjg
上级 abd92b89
...@@ -1639,7 +1639,7 @@ public class JavacParser implements Parser { ...@@ -1639,7 +1639,7 @@ public class JavacParser implements Parser {
* | WHILE ParExpression Statement * | WHILE ParExpression Statement
* | DO Statement WHILE ParExpression ";" * | DO Statement WHILE ParExpression ";"
* | TRY Block ( Catches | [Catches] FinallyPart ) * | TRY Block ( Catches | [Catches] FinallyPart )
* | TRY "(" ResourceSpecification ")" Block [Catches] [FinallyPart] * | TRY "(" ResourceSpecification ";"opt ")" Block [Catches] [FinallyPart]
* | SWITCH ParExpression "{" SwitchBlockStatementGroups "}" * | SWITCH ParExpression "{" SwitchBlockStatementGroups "}"
* | SYNCHRONIZED ParExpression Block * | SYNCHRONIZED ParExpression Block
* | RETURN [Expression] ";" * | RETURN [Expression] ";"
...@@ -2182,13 +2182,12 @@ public class JavacParser implements Parser { ...@@ -2182,13 +2182,12 @@ public class JavacParser implements Parser {
ListBuffer<JCTree> defs = new ListBuffer<JCTree>(); ListBuffer<JCTree> defs = new ListBuffer<JCTree>();
defs.append(resource()); defs.append(resource());
while (S.token() == SEMI) { while (S.token() == SEMI) {
// All but last of multiple declarators subsume a semicolon // All but last of multiple declarators must subsume a semicolon
storeEnd(defs.elems.last(), S.endPos()); storeEnd(defs.elems.last(), S.endPos());
int semiColonPos = S.pos(); int semiColonPos = S.pos();
S.nextToken(); S.nextToken();
if (S.token() == RPAREN) { // Illegal trailing semicolon if (S.token() == RPAREN) { // Optional trailing semicolon
// after last resource // after last resource
error(semiColonPos, "try.resource.trailing.semi");
break; break;
} }
defs.append(resource()); defs.append(resource());
......
...@@ -298,9 +298,6 @@ compiler.err.final.parameter.may.not.be.assigned=\ ...@@ -298,9 +298,6 @@ compiler.err.final.parameter.may.not.be.assigned=\
compiler.err.try.resource.may.not.be.assigned=\ compiler.err.try.resource.may.not.be.assigned=\
auto-closeable resource {0} may not be assigned auto-closeable resource {0} may not be assigned
compiler.err.try.resource.trailing.semi=\
illegal trailing semicolon in resources declaration
# 0: symbol # 0: symbol
compiler.err.multicatch.parameter.may.not.be.assigned=\ compiler.err.multicatch.parameter.may.not.be.assigned=\
multi-catch parameter {0} may not be assigned multi-catch parameter {0} may not be assigned
......
...@@ -4,13 +4,18 @@ ...@@ -4,13 +4,18 @@
* @author Joseph D. Darcy * @author Joseph D. Darcy
* @summary Verify bad TWRs don't compile * @summary Verify bad TWRs don't compile
* @compile/fail -source 6 BadTwrSyntax.java * @compile/fail -source 6 BadTwrSyntax.java
* @compile/fail/ref=BadTwrSyntax.out -XDrawDiagnostics BadTwrSyntax.java * @compile/fail/ref=BadTwrSyntax.out -XDrawDiagnostics BadTwrSyntax.java
*/ */
import java.io.IOException; import java.io.IOException;
public class BadTwrSyntax implements AutoCloseable { public class BadTwrSyntax implements AutoCloseable {
public static void main(String... args) throws Exception { public static void main(String... args) throws Exception {
// illegal semicolon ending resources // illegal double semicolon ending resources
try(BadTwr twrflow = new BadTwr();;) {
System.out.println(twrflow.toString());
}
// but one semicolon is fine
try(BadTwr twrflow = new BadTwr();) { try(BadTwr twrflow = new BadTwr();) {
System.out.println(twrflow.toString()); System.out.println(twrflow.toString());
} }
......
BadTwrSyntax.java:14:42: compiler.err.try.resource.trailing.semi BadTwrSyntax.java:14:43: compiler.err.illegal.start.of.type
1 error BadTwrSyntax.java:14:44: compiler.err.expected: =
BadTwrSyntax.java:14:45: compiler.err.expected: ')'
BadTwrSyntax.java:14:47: compiler.err.expected: '{'
BadTwrSyntax.java:15:19: compiler.err.illegal.start.of.expr
BadTwrSyntax.java:15:23: compiler.err.expected: ';'
6 errors
/*
* Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
// key: compiler.err.try.resource.trailing.semi
class TryResoureTrailingSemi implements AutoCloseable {
public static void main(String... args) {
try(TryResoureTrailingSemi r = new TryResoureTrailingSemi();) {
System.out.println(r.toString());
}
}
@Override
public void close() {return;}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册