7014734: Project Coin: Allow optional trailing semicolon to terminate resources list in try-with-resources
Reviewed-by: jjg
--- a/langtools/src/share/classes/com/sun/tools/javac/parser/JavacParser.java Fri Jan 28 16:54:18 2011 -0800
+++ b/langtools/src/share/classes/com/sun/tools/javac/parser/JavacParser.java Mon Jan 31 19:06:32 2011 -0800
@@ -1639,7 +1639,7 @@
* | WHILE ParExpression Statement
* | DO Statement WHILE ParExpression ";"
* | TRY Block ( Catches | [Catches] FinallyPart )
- * | TRY "(" ResourceSpecification ")" Block [Catches] [FinallyPart]
+ * | TRY "(" ResourceSpecification ";"opt ")" Block [Catches] [FinallyPart]
* | SWITCH ParExpression "{" SwitchBlockStatementGroups "}"
* | SYNCHRONIZED ParExpression Block
* | RETURN [Expression] ";"
@@ -2182,13 +2182,12 @@
ListBuffer<JCTree> defs = new ListBuffer<JCTree>();
defs.append(resource());
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());
int semiColonPos = S.pos();
S.nextToken();
- if (S.token() == RPAREN) { // Illegal trailing semicolon
+ if (S.token() == RPAREN) { // Optional trailing semicolon
// after last resource
- error(semiColonPos, "try.resource.trailing.semi");
break;
}
defs.append(resource());
--- a/langtools/src/share/classes/com/sun/tools/javac/resources/compiler.properties Fri Jan 28 16:54:18 2011 -0800
+++ b/langtools/src/share/classes/com/sun/tools/javac/resources/compiler.properties Mon Jan 31 19:06:32 2011 -0800
@@ -298,9 +298,6 @@
compiler.err.try.resource.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
compiler.err.multicatch.parameter.may.not.be.assigned=\
multi-catch parameter {0} may not be assigned
--- a/langtools/test/tools/javac/TryWithResources/BadTwrSyntax.java Fri Jan 28 16:54:18 2011 -0800
+++ b/langtools/test/tools/javac/TryWithResources/BadTwrSyntax.java Mon Jan 31 19:06:32 2011 -0800
@@ -4,13 +4,18 @@
* @author Joseph D. Darcy
* @summary Verify bad TWRs don't compile
* @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;
public class BadTwrSyntax implements AutoCloseable {
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();) {
System.out.println(twrflow.toString());
}
--- a/langtools/test/tools/javac/TryWithResources/BadTwrSyntax.out Fri Jan 28 16:54:18 2011 -0800
+++ b/langtools/test/tools/javac/TryWithResources/BadTwrSyntax.out Mon Jan 31 19:06:32 2011 -0800
@@ -1,2 +1,7 @@
-BadTwrSyntax.java:14:42: compiler.err.try.resource.trailing.semi
-1 error
+BadTwrSyntax.java:14:43: compiler.err.illegal.start.of.type
+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
--- a/langtools/test/tools/javac/diags/examples/TryResourceTrailingSemi.java Fri Jan 28 16:54:18 2011 -0800
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,35 +0,0 @@
-/*
- * 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;}
-}