8189146: Have use of "var" in 9 and earlier source versions issue a warning for type declarations
authordarcy
Tue, 16 Jan 2018 17:27:06 -0800
changeset 48551 9cf44c40aa35
parent 48550 5840ed767456
child 48552 f94706337b07
8189146: Have use of "var" in 9 and earlier source versions issue a warning for type declarations Reviewed-by: mcimadamore, jjg
src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java
src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties
test/langtools/tools/javac/diags/examples/FutureVarNotAllowed.java
test/langtools/tools/javac/lvti/ParserTest.java
test/langtools/tools/javac/lvti/ParserTest.out
test/langtools/tools/javac/lvti/ParserTest9.out
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java	Tue Jan 16 14:44:04 2018 -0800
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java	Tue Jan 16 17:27:06 2018 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1999, 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1999, 2018, 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
@@ -3403,8 +3403,12 @@
     Name typeName() {
         int pos = token.pos;
         Name name = ident();
-        if (isRestrictedLocalVarTypeName(name)) {
-            reportSyntaxError(pos, "var.not.allowed", name);
+        if (name == names.var) {
+            if (Feature.LOCAL_VARIABLE_TYPE_INFERENCE.allowedInSource(source)) {
+                reportSyntaxError(pos, "var.not.allowed", name);
+            } else {
+                warning(pos, "var.not.allowed");
+            }
         }
         return name;
     }
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties	Tue Jan 16 14:44:04 2018 -0800
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties	Tue Jan 16 17:27:06 2018 -0800
@@ -1207,6 +1207,9 @@
     ''{0}'' not allowed here\n\
     as of release 10, ''{0}'' is a restricted local variable type and cannot be used for type declarations
 
+compiler.warn.var.not.allowed=\
+    as of release 10, ''var'' is a restricted local variable type and cannot be used for type declarations
+
 # 0: name (variable), 1: message segment
 compiler.err.cant.infer.local.var.type=\
     cannot infer type for local variable {0}\n\
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/langtools/tools/javac/diags/examples/FutureVarNotAllowed.java	Tue Jan 16 17:27:06 2018 -0800
@@ -0,0 +1,29 @@
+/*
+ * Copyright (c) 2016, 2018, 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.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * 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.warn.var.not.allowed
+// options: --release 9
+
+class var { }
--- a/test/langtools/tools/javac/lvti/ParserTest.java	Tue Jan 16 14:44:04 2018 -0800
+++ b/test/langtools/tools/javac/lvti/ParserTest.java	Tue Jan 16 17:27:06 2018 -0800
@@ -1,8 +1,8 @@
 /*
  * @test /nodynamiccopyright/
- * @bug 8177466
+ * @bug 8177466 8189146
+ * @compile/ref=ParserTest9.out -XDrawDiagnostics --release 9 ParserTest.java
  * @summary Add compiler support for local variable type-inference
- * @compile -source 9 ParserTest.java
  * @compile/fail/ref=ParserTest.out -XDrawDiagnostics ParserTest.java
  */
 
@@ -11,7 +11,7 @@
 import java.util.function.Function;
 import java.util.List;
 
-class ParserTest<var> {
+class ParserTest<var extends AutoCloseable> {
     static class TestClass {
         static class var { } //illegal
     }
@@ -33,7 +33,7 @@
 
     @interface DA { }
 
-    static class var extends RuntimeException { } //illegal
+    static abstract class var extends RuntimeException implements AutoCloseable { } //illegal
 
     var x = null; //illegal
 
@@ -68,4 +68,10 @@
         boolean b1 = o instanceof var; //error
         Object o2 = (var)o; //error
     }
+
+    void test4() throws Exception {
+        try (final var resource1 = null; // ok
+             var resource2 = null) {     // ok
+        }
+    }
 }
--- a/test/langtools/tools/javac/lvti/ParserTest.out	Tue Jan 16 14:44:04 2018 -0800
+++ b/test/langtools/tools/javac/lvti/ParserTest.out	Tue Jan 16 17:27:06 2018 -0800
@@ -3,7 +3,7 @@
 ParserTest.java:20:19: compiler.err.var.not.allowed: var
 ParserTest.java:24:14: compiler.err.var.not.allowed: var
 ParserTest.java:28:20: compiler.err.var.not.allowed: var
-ParserTest.java:36:18: compiler.err.var.not.allowed: var
+ParserTest.java:36:27: compiler.err.var.not.allowed: var
 ParserTest.java:38:5: compiler.err.var.not.allowed.here
 ParserTest.java:41:15: compiler.err.var.not.allowed.array
 ParserTest.java:42:13: compiler.err.var.not.allowed.array
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/langtools/tools/javac/lvti/ParserTest9.out	Tue Jan 16 17:27:06 2018 -0800
@@ -0,0 +1,7 @@
+ParserTest.java:14:18: compiler.warn.var.not.allowed
+ParserTest.java:16:22: compiler.warn.var.not.allowed
+ParserTest.java:20:19: compiler.warn.var.not.allowed
+ParserTest.java:24:14: compiler.warn.var.not.allowed
+ParserTest.java:28:20: compiler.warn.var.not.allowed
+ParserTest.java:36:27: compiler.warn.var.not.allowed
+6 warnings