changeset 902:7f8794f9cc14

7013420: Project Coin: remove general expression support from try-with-resources statement Reviewed-by: mcimadamore, jjg
author darcy
date Tue, 25 Jan 2011 17:02:56 -0800
parents a8437c34fdc7
children df371fd16386
files src/share/classes/com/sun/tools/javac/parser/JavacParser.java src/share/classes/com/sun/tools/javac/resources/compiler.properties test/tools/javac/TryWithResources/BadTwrSyntax.out test/tools/javac/TryWithResources/DuplicateResource.java test/tools/javac/TryWithResources/ExplicitFinal.java test/tools/javac/TryWithResources/ImplicitFinal.java test/tools/javac/TryWithResources/ImplicitFinal.out test/tools/javac/TryWithResources/TwrFlow.java test/tools/javac/TryWithResources/TwrFlow.out test/tools/javac/TryWithResources/TwrInference.java test/tools/javac/TryWithResources/TwrIntersection.java test/tools/javac/TryWithResources/TwrIntersection02.java test/tools/javac/TryWithResources/TwrIntersection02.out test/tools/javac/TryWithResources/TwrMultiCatch.java test/tools/javac/TryWithResources/TwrOnNonResource.java test/tools/javac/TryWithResources/TwrOnNonResource.out test/tools/javac/diags/examples/TryResourceTrailingSemi.java
diffstat 17 files changed, 145 insertions(+), 199 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/com/sun/tools/javac/parser/JavacParser.java	Mon Jan 24 16:38:56 2011 -0800
+++ b/src/share/classes/com/sun/tools/javac/parser/JavacParser.java	Tue Jan 25 17:02:56 2011 -0800
@@ -142,7 +142,7 @@
      */
     boolean allowAnnotations;
 
-    /** Switch: should we recognize automatic resource management?
+    /** Switch: should we recognize try-with-resources?
      */
     boolean allowTWR;
 
@@ -2184,29 +2184,23 @@
         while (S.token() == SEMI) {
             // All but last of multiple declarators subsume a semicolon
             storeEnd(defs.elems.last(), S.endPos());
+            int semiColonPos = S.pos();
             S.nextToken();
+            if (S.token() == RPAREN) { // Illegal trailing semicolon
+                                       // after last resource
+                error(semiColonPos, "try.resource.trailing.semi");
+                break;
+            }
             defs.append(resource());
         }
         return defs.toList();
     }
 
-    /** Resource =
-     *    VariableModifiers Type VariableDeclaratorId = Expression
-     *  | Expression
+    /** Resource = VariableModifiersOpt Type VariableDeclaratorId = Expression
      */
     JCTree resource() {
-        int pos = S.pos();
-        if (S.token() == FINAL || S.token() == MONKEYS_AT) {
-            return variableDeclaratorRest(pos, optFinal(0), parseType(),
-                                          ident(), true, null);
-        } else {
-            JCExpression t = term(EXPR | TYPE);
-            if ((lastmode & TYPE) != 0 && S.token() == IDENTIFIER)
-                return variableDeclaratorRest(pos, toP(F.at(pos).Modifiers(Flags.FINAL)), t,
-                                              ident(), true, null);
-            else
-                return t;
-        }
+        return variableDeclaratorRest(S.pos(), optFinal(Flags.FINAL),
+                                      parseType(), ident(), true, null);
     }
 
     /** CompilationUnit = [ { "@" Annotation } PACKAGE Qualident ";"] {ImportDeclaration} {TypeDeclaration}
--- a/src/share/classes/com/sun/tools/javac/resources/compiler.properties	Mon Jan 24 16:38:56 2011 -0800
+++ b/src/share/classes/com/sun/tools/javac/resources/compiler.properties	Tue Jan 25 17:02:56 2011 -0800
@@ -177,6 +177,8 @@
     final parameter {0} may not be assigned
 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
 compiler.err.multicatch.parameter.may.not.be.assigned=\
     multi-catch parameter {0} may not be assigned
 compiler.err.finally.without.try=\
--- a/test/tools/javac/TryWithResources/BadTwrSyntax.out	Mon Jan 24 16:38:56 2011 -0800
+++ b/test/tools/javac/TryWithResources/BadTwrSyntax.out	Tue Jan 25 17:02:56 2011 -0800
@@ -1,2 +1,2 @@
-BadTwrSyntax.java:14:43: compiler.err.illegal.start.of.expr
+BadTwrSyntax.java:14:42: compiler.err.try.resource.trailing.semi
 1 error
--- a/test/tools/javac/TryWithResources/DuplicateResource.java	Mon Jan 24 16:38:56 2011 -0800
+++ b/test/tools/javac/TryWithResources/DuplicateResource.java	Tue Jan 25 17:02:56 2011 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2010, 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
@@ -23,9 +23,9 @@
 
 /*
  * @test
- * @bug 6911256 6964740 6965277
+ * @bug 6911256 6964740 6965277 7013420
  * @author Maurizio Cimadamore
- * @summary Check that lowered arm block does not end up creating resource twice
+ * @summary Check that lowered try-with-resources block does not end up creating resource twice
  */
 
 import java.util.ArrayList;
@@ -45,7 +45,7 @@
     static ArrayList<TestResource> resources = new ArrayList<TestResource>();
 
     public static void main(String[] args) {
-        try(new TestResource()) {
+        try(TestResource tr = new TestResource()) {
            //do something
         } catch (Exception e) {
             throw new AssertionError("Shouldn't reach here", e);
@@ -59,7 +59,7 @@
        }
        TestResource resource = resources.get(0);
        if (!resource.isClosed) {
-           throw new AssertionError("Resource used in ARM block has not been automatically closed");
+           throw new AssertionError("Resource used in try-with-resources block has not been automatically closed");
        }
     }
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/tools/javac/TryWithResources/ExplicitFinal.java	Tue Jan 25 17:02:56 2011 -0800
@@ -0,0 +1,56 @@
+/*
+ * 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.
+ */
+
+/*
+ * @test
+ * @bug 7013420
+ * @author Joseph D. Darcy
+ * @summary Test that resource variables are accepted as explicitly final.
+ */
+
+import java.io.IOException;
+
+public class ExplicitFinal implements AutoCloseable {
+    public static void main(String... args) {
+        try(final ExplicitFinal r2 = new ExplicitFinal()) {
+            r2.toString();
+        } catch (IOException ioe) {
+            throw new AssertionError("Shouldn't reach here", ioe);
+        }
+
+        try(final @SuppressWarnings("unchecked") ExplicitFinal r3 = new ExplicitFinal()) {
+            r3.toString();
+        } catch (IOException ioe) {
+            throw new AssertionError("Shouldn't reach here", ioe);
+        }
+
+        try(@SuppressWarnings("unchecked") ExplicitFinal r4 = new ExplicitFinal()) {
+            r4.toString();
+        } catch (IOException ioe) {
+            throw new AssertionError("Shouldn't reach here", ioe);
+        }
+    }
+    public void close() throws IOException {
+        System.out.println("Calling close on " + this);
+    }
+}
--- a/test/tools/javac/TryWithResources/ImplicitFinal.java	Mon Jan 24 16:38:56 2011 -0800
+++ b/test/tools/javac/TryWithResources/ImplicitFinal.java	Tue Jan 25 17:02:56 2011 -0800
@@ -1,6 +1,6 @@
 /*
  * @test  /nodynamiccopyright/
- * @bug 6911256 6964740 6965277
+ * @bug 6911256 6964740 6965277 7013420
  * @author Maurizio Cimadamore
  * @summary Test that resource variables are implicitly final
  * @compile/fail/ref=ImplicitFinal.out -XDrawDiagnostics ImplicitFinal.java
@@ -15,12 +15,25 @@
         } catch (IOException ioe) { // Not reachable
             throw new AssertionError("Shouldn't reach here", ioe);
         }
+
+        try(@SuppressWarnings("unchecked") ImplicitFinal r1 = new ImplicitFinal()) {
+            r1 = null; //disallowed
+        } catch (IOException ioe) { // Not reachable
+            throw new AssertionError("Shouldn't reach here", ioe);
+        }
+
+        try(final ImplicitFinal r2 = new ImplicitFinal()) {
+            r2 = null; //disallowed
+        } catch (IOException ioe) { // Not reachable
+            throw new AssertionError("Shouldn't reach here", ioe);
+        }
+
+        try(final @SuppressWarnings("unchecked") ImplicitFinal r3 = new ImplicitFinal()) {
+            r3 = null; //disallowed
+        } catch (IOException ioe) { // Not reachable
+            throw new AssertionError("Shouldn't reach here", ioe);
+        }
     }
-
-
-     // A close method, but the class is <em>not</em> Closeable or
-     // AutoCloseable.
-
     public void close() throws IOException {
         throw new IOException();
     }
--- a/test/tools/javac/TryWithResources/ImplicitFinal.out	Mon Jan 24 16:38:56 2011 -0800
+++ b/test/tools/javac/TryWithResources/ImplicitFinal.out	Tue Jan 25 17:02:56 2011 -0800
@@ -1,2 +1,5 @@
 ImplicitFinal.java:14:13: compiler.err.try.resource.may.not.be.assigned: r
-1 error
+ImplicitFinal.java:20:13: compiler.err.try.resource.may.not.be.assigned: r1
+ImplicitFinal.java:26:13: compiler.err.try.resource.may.not.be.assigned: r2
+ImplicitFinal.java:32:13: compiler.err.try.resource.may.not.be.assigned: r3
+4 errors
--- a/test/tools/javac/TryWithResources/TwrFlow.java	Mon Jan 24 16:38:56 2011 -0800
+++ b/test/tools/javac/TryWithResources/TwrFlow.java	Tue Jan 25 17:02:56 2011 -0800
@@ -1,26 +1,16 @@
 /*
  * @test  /nodynamiccopyright/
- * @bug 6911256 6964740
+ * @bug 6911256 6964740 7013420
  * @author Joseph D. Darcy
- * @summary Test exception analysis of ARM blocks
+ * @summary Test exception analysis of try-with-resources blocks
  * @compile/fail/ref=TwrFlow.out -XDrawDiagnostics TwrFlow.java
  */
 
 import java.io.IOException;
 public class TwrFlow implements AutoCloseable {
     public static void main(String... args) {
-        try(TwrFlow armflow = new TwrFlow()) {
-            System.out.println(armflow.toString());
-        } catch (IOException ioe) { // Not reachable
-            throw new AssertionError("Shouldn't reach here", ioe);
-        }
-        // CustomCloseException should be caught or added to throws clause
-
-        // Also check behavior on a resource expression rather than a
-        // declaration.
-        TwrFlow armflowexpr = new TwrFlow();
-        try(armflowexpr) {
-            System.out.println(armflowexpr.toString());
+        try(TwrFlow twrFlow = new TwrFlow()) {
+            System.out.println(twrFlow.toString());
         } catch (IOException ioe) { // Not reachable
             throw new AssertionError("Shouldn't reach here", ioe);
         }
--- a/test/tools/javac/TryWithResources/TwrFlow.out	Mon Jan 24 16:38:56 2011 -0800
+++ b/test/tools/javac/TryWithResources/TwrFlow.out	Tue Jan 25 17:02:56 2011 -0800
@@ -1,5 +1,3 @@
 TwrFlow.java:14:11: compiler.err.except.never.thrown.in.try: java.io.IOException
-TwrFlow.java:24:11: compiler.err.except.never.thrown.in.try: java.io.IOException
 TwrFlow.java:12:46: compiler.err.unreported.exception.need.to.catch.or.throw: CustomCloseException
-TwrFlow.java:22:26: compiler.err.unreported.exception.need.to.catch.or.throw: CustomCloseException
-4 errors
+2 errors
--- a/test/tools/javac/TryWithResources/TwrInference.java	Mon Jan 24 16:38:56 2011 -0800
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,43 +0,0 @@
-/*
- * Copyright (c) 2010, 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.
- */
-
-/*
- * @test
- * @bug 6911256 6964740 6965277
- * @author Maurizio Cimadamore
- * @summary Verify that method type-inference works as expected in TWR context
- * @compile TwrInference.java
- */
-
-class TwrInference {
-
-    public void test() {
-        try(getX()) {
-            //do something
-        } catch (Exception e) { // Not reachable
-            throw new AssertionError("Shouldn't reach here", e);
-        }
-    }
-
-    <X> X getX() { return null; }
-}
--- a/test/tools/javac/TryWithResources/TwrIntersection.java	Mon Jan 24 16:38:56 2011 -0800
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,47 +0,0 @@
-/*
- * Copyright (c) 2010, 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.
- */
-
-/*
- * @test
- * @bug 6911256 6964740 6965277
- * @author Maurizio Cimadamore
- * @summary Resource of an intersection type crashes Flow
- * @compile TwrIntersection.java
- */
-
-interface MyCloseable extends AutoCloseable {
-   void close() throws java.io.IOException;
-}
-
-class ResourceTypeVar {
-
-    public void test() {
-        try(getX()) {
-            //do something
-        } catch (java.io.IOException e) { // Not reachable
-            throw new AssertionError("Shouldn't reach here", e);
-        }
-    }
-
-    <X extends Number & MyCloseable> X getX() { return null; }
-}
--- a/test/tools/javac/TryWithResources/TwrIntersection02.java	Mon Jan 24 16:38:56 2011 -0800
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,37 +0,0 @@
-/*
- * @test  /nodynamiccopyright/
- * @bug 6911256 6964740 6965277
- * @author Maurizio Cimadamore
- * @summary Check that resources of an intersection type forces union of exception types
- *          to be caught outside twr block
- * @compile/fail/ref=TwrIntersection02.out -XDrawDiagnostics TwrIntersection02.java
- */
-
-class TwrIntersection02 {
-
-    static class Exception1 extends Exception {}
-    static class Exception2 extends Exception {}
-
-
-    interface MyResource1 extends AutoCloseable {
-       void close() throws Exception1;
-    }
-
-    interface MyResource2 extends AutoCloseable {
-       void close() throws Exception2;
-    }
-
-    public void test1() throws Exception1 {
-        try(getX()) {
-            //do something
-        }
-    }
-
-    public void test2() throws Exception2 {
-        try(getX()) {
-            //do something
-        }
-    }
-
-    <X extends MyResource1 & MyResource2> X getX() { return null; }
-}
--- a/test/tools/javac/TryWithResources/TwrIntersection02.out	Mon Jan 24 16:38:56 2011 -0800
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,3 +0,0 @@
-TwrIntersection02.java:25:21: compiler.err.unreported.exception.need.to.catch.or.throw: TwrIntersection02.Exception2
-TwrIntersection02.java:31:21: compiler.err.unreported.exception.need.to.catch.or.throw: TwrIntersection02.Exception1
-2 errors
--- a/test/tools/javac/TryWithResources/TwrMultiCatch.java	Mon Jan 24 16:38:56 2011 -0800
+++ b/test/tools/javac/TryWithResources/TwrMultiCatch.java	Tue Jan 25 17:02:56 2011 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2010, 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
@@ -23,7 +23,7 @@
 
 /*
  * @test
- * @bug 6911256 6964740
+ * @bug 6911256 6964740 7013420
  * @author Joseph D. Darcy
  * @summary Test that TWR and multi-catch play well together
  * @compile TwrMultiCatch.java
@@ -48,9 +48,9 @@
 
     private static void test(TwrMultiCatch twrMultiCatch,
                      Class<? extends Exception> expected) {
-        try(twrMultiCatch) {
-            System.out.println(twrMultiCatch.toString());
-        } catch (final CustomCloseException1 |
+        try(TwrMultiCatch tmc = twrMultiCatch) {
+            System.out.println(tmc.toString());
+        } catch (CustomCloseException1 |
                  CustomCloseException2 exception) {
             if (!exception.getClass().equals(expected) ) {
                 throw new RuntimeException("Unexpected catch!");
@@ -68,7 +68,7 @@
 
         try {
             throw t;
-        } catch (final CustomCloseException1 |
+        } catch (CustomCloseException1 |
                  CustomCloseException2 exception) {
             throw exception;
         } catch (Throwable throwable) {
--- a/test/tools/javac/TryWithResources/TwrOnNonResource.java	Mon Jan 24 16:38:56 2011 -0800
+++ b/test/tools/javac/TryWithResources/TwrOnNonResource.java	Tue Jan 25 17:02:56 2011 -0800
@@ -1,6 +1,6 @@
 /*
  * @test  /nodynamiccopyright/
- * @bug 6911256 6964740
+ * @bug 6911256 6964740 7013420
  * @author Joseph D. Darcy
  * @summary Verify invalid TWR block is not accepted.
  * @compile/fail -source 6 TwrOnNonResource.java
@@ -18,18 +18,6 @@
         try(TwrOnNonResource aonr = new TwrOnNonResource()) {
             System.out.println(aonr.toString());
         } catch (Exception e) {;}
-
-        // Also check expression form
-        TwrOnNonResource aonr = new TwrOnNonResource();
-        try(aonr) {
-            System.out.println(aonr.toString());
-        }
-        try(aonr) {
-            System.out.println(aonr.toString());
-        } finally {;}
-        try(aonr) {
-            System.out.println(aonr.toString());
-        } catch (Exception e) {;}
     }
 
     /*
--- a/test/tools/javac/TryWithResources/TwrOnNonResource.out	Mon Jan 24 16:38:56 2011 -0800
+++ b/test/tools/javac/TryWithResources/TwrOnNonResource.out	Tue Jan 25 17:02:56 2011 -0800
@@ -1,7 +1,4 @@
 TwrOnNonResource.java:12:13: compiler.err.prob.found.req: (compiler.misc.try.not.applicable.to.type), TwrOnNonResource, java.lang.AutoCloseable
 TwrOnNonResource.java:15:13: compiler.err.prob.found.req: (compiler.misc.try.not.applicable.to.type), TwrOnNonResource, java.lang.AutoCloseable
 TwrOnNonResource.java:18:13: compiler.err.prob.found.req: (compiler.misc.try.not.applicable.to.type), TwrOnNonResource, java.lang.AutoCloseable
-TwrOnNonResource.java:24:13: compiler.err.prob.found.req: (compiler.misc.try.not.applicable.to.type), TwrOnNonResource, java.lang.AutoCloseable
-TwrOnNonResource.java:27:13: compiler.err.prob.found.req: (compiler.misc.try.not.applicable.to.type), TwrOnNonResource, java.lang.AutoCloseable
-TwrOnNonResource.java:30:13: compiler.err.prob.found.req: (compiler.misc.try.not.applicable.to.type), TwrOnNonResource, java.lang.AutoCloseable
-6 errors
+3 errors
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/tools/javac/diags/examples/TryResourceTrailingSemi.java	Tue Jan 25 17:02:56 2011 -0800
@@ -0,0 +1,35 @@
+/*
+ * 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;}
+}