# HG changeset patch # User darcy # Date 1296529592 28800 # Node ID 2ab47c4cd618f731a7d618598d887412110aa001 # Parent 7a75a1803c7af0de9e62fd53adadea18d2912027 7014734: Project Coin: Allow optional trailing semicolon to terminate resources list in try-with-resources Reviewed-by: jjg diff -r 7a75a1803c7a -r 2ab47c4cd618 src/share/classes/com/sun/tools/javac/parser/JavacParser.java --- a/src/share/classes/com/sun/tools/javac/parser/JavacParser.java Fri Jan 28 16:54:18 2011 -0800 +++ b/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 defs = new ListBuffer(); 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()); diff -r 7a75a1803c7a -r 2ab47c4cd618 src/share/classes/com/sun/tools/javac/resources/compiler.properties --- a/src/share/classes/com/sun/tools/javac/resources/compiler.properties Fri Jan 28 16:54:18 2011 -0800 +++ b/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 diff -r 7a75a1803c7a -r 2ab47c4cd618 test/tools/javac/TryWithResources/BadTwrSyntax.java --- a/test/tools/javac/TryWithResources/BadTwrSyntax.java Fri Jan 28 16:54:18 2011 -0800 +++ b/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()); } diff -r 7a75a1803c7a -r 2ab47c4cd618 test/tools/javac/TryWithResources/BadTwrSyntax.out --- a/test/tools/javac/TryWithResources/BadTwrSyntax.out Fri Jan 28 16:54:18 2011 -0800 +++ b/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 diff -r 7a75a1803c7a -r 2ab47c4cd618 test/tools/javac/diags/examples/TryResourceTrailingSemi.java --- a/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;} -}