--- old/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java 2018-01-11 14:46:38.935903218 -0800 +++ new/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java 2018-01-11 14:46:38.559715202 -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 @@ -761,7 +761,7 @@ public JCExpression unannotatedType(boolean allowVar) { JCExpression result = term(TYPE); - if (!allowVar && isRestrictedLocalVarTypeName(result)) { + if (isRestrictedLocalVarTypeName(result) && !allowVar) { syntaxError(result.pos, "var.not.allowed.here"); } @@ -2955,6 +2955,12 @@ T vdefs, boolean localDecl) { + JCTree elemType = TreeInfo.innermostType(type, true); // bracketsOpt needed? + if (elemType.hasTag(IDENT)) { + Name typeName = ((JCIdent)elemType).name; + boolean typeNameVarCheck = isRestrictedLocalVarTypeName(typeName, elemType.pos); + } + JCVariableDecl head = variableDeclaratorRest(pos, mods, type, name, reqInit, dc, localDecl); boolean implicit = Feature.LOCAL_VARIABLE_TYPE_INFERENCE.allowedInSource(source) && head.vartype == null; vdefs.append(head); @@ -2996,7 +3002,7 @@ int startPos = Position.NOPOS; if (Feature.LOCAL_VARIABLE_TYPE_INFERENCE.allowedInSource(source) && elemType.hasTag(IDENT)) { Name typeName = ((JCIdent)elemType).name; - if (isRestrictedLocalVarTypeName(typeName)) { + if (isRestrictedLocalVarTypeName(typeName, elemType.pos)) { if (type.hasTag(TYPEARRAY)) { //error - 'var' and arrays reportSyntaxError(pos, "var.not.allowed.array"); @@ -3019,7 +3025,7 @@ boolean isRestrictedLocalVarTypeName(JCExpression e) { switch (e.getTag()) { case IDENT: - return isRestrictedLocalVarTypeName(((JCIdent)e).name); + return isRestrictedLocalVarTypeName(((JCIdent)e).name, e.pos); case TYPEARRAY: return isRestrictedLocalVarTypeName(((JCArrayTypeTree)e).elemtype); default: @@ -3027,8 +3033,15 @@ } } - boolean isRestrictedLocalVarTypeName(Name name) { - return Feature.LOCAL_VARIABLE_TYPE_INFERENCE.allowedInSource(source) && name == names.var; + boolean isRestrictedLocalVarTypeName(Name name, int pos) { + if (name == names.var) { + if (Feature.LOCAL_VARIABLE_TYPE_INFERENCE.allowedInSource(source)) { + return true; + } else { + warning(pos, "var.not.allowed", name); + } + } + return false; } /** VariableDeclaratorId = Ident BracketsOpt @@ -3403,7 +3416,7 @@ Name typeName() { int pos = token.pos; Name name = ident(); - if (isRestrictedLocalVarTypeName(name)) { + if (isRestrictedLocalVarTypeName(name, pos)) { reportSyntaxError(pos, "var.not.allowed", name); } return name; --- old/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties 2018-01-11 14:46:39.700285252 -0800 +++ new/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties 2018-01-11 14:46:39.336103236 -0800 @@ -1204,6 +1204,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, ''{0}'' is a restricted local variable type and cannot be used for type declarations or as the element type of an array + # 0: name (variable), 1: message segment compiler.err.cant.infer.local.var.type=\ cannot infer type for local variable {0}\n\ --- old/test/langtools/tools/javac/lvti/ParserTest.java 2018-01-11 14:46:40.472671286 -0800 +++ new/test/langtools/tools/javac/lvti/ParserTest.java 2018-01-11 14:46:40.084477269 -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 { +class ParserTest { 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 + } + } } --- old/test/langtools/tools/javac/lvti/ParserTest.out 2018-01-11 14:46:41.377123325 -0800 +++ new/test/langtools/tools/javac/lvti/ParserTest.out 2018-01-11 14:46:41.020945310 -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 2017-11-14 18:40:26.820374259 -0800 +++ new/test/langtools/tools/javac/diags/examples/FutureVarNotAllowed.java 2018-01-11 14:46:41.873371348 -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 { } --- /dev/null 2017-11-14 18:40:26.820374259 -0800 +++ new/test/langtools/tools/javac/lvti/ParserTest9.out 2018-01-11 14:46:42.581725379 -0800 @@ -0,0 +1,33 @@ +ParserTest.java:14:18: compiler.warn.var.not.allowed: var +ParserTest.java:16:22: compiler.warn.var.not.allowed: var +ParserTest.java:20:19: compiler.warn.var.not.allowed: var +ParserTest.java:24:14: compiler.warn.var.not.allowed: var +ParserTest.java:28:20: compiler.warn.var.not.allowed: var +ParserTest.java:36:27: compiler.warn.var.not.allowed: var +ParserTest.java:38:5: compiler.warn.var.not.allowed: var +ParserTest.java:38:5: compiler.warn.var.not.allowed: var +ParserTest.java:41:9: compiler.warn.var.not.allowed: var +ParserTest.java:42:9: compiler.warn.var.not.allowed: var +ParserTest.java:43:9: compiler.warn.var.not.allowed: var +ParserTest.java:44:9: compiler.warn.var.not.allowed: var +ParserTest.java:45:9: compiler.warn.var.not.allowed: var +ParserTest.java:46:9: compiler.warn.var.not.allowed: var +ParserTest.java:47:9: compiler.warn.var.not.allowed: var +ParserTest.java:48:9: compiler.warn.var.not.allowed: var +ParserTest.java:49:9: compiler.warn.var.not.allowed: var +ParserTest.java:50:19: compiler.warn.var.not.allowed: var +ParserTest.java:50:19: compiler.warn.var.not.allowed: var +ParserTest.java:51:19: compiler.warn.var.not.allowed: var +ParserTest.java:51:19: compiler.warn.var.not.allowed: var +ParserTest.java:54:5: compiler.warn.var.not.allowed: var +ParserTest.java:58:16: compiler.warn.var.not.allowed: var +ParserTest.java:59:14: compiler.warn.var.not.allowed: var +ParserTest.java:60:24: compiler.warn.var.not.allowed: var +ParserTest.java:61:22: compiler.warn.var.not.allowed: var +ParserTest.java:63:22: compiler.warn.var.not.allowed: var +ParserTest.java:63:40: compiler.warn.var.not.allowed: var +ParserTest.java:64:18: compiler.warn.var.not.allowed: var +ParserTest.java:68:35: compiler.warn.var.not.allowed: var +ParserTest.java:69:22: compiler.warn.var.not.allowed: var +ParserTest.java:73:20: compiler.warn.var.not.allowed: var +32 warnings