8189146: Have use of "var" in 9 and earlier source versions issue a warning for type declarations

Reviewed-by: mcimadamore, jjg
This commit is contained in:
Joe Darcy 2018-01-16 17:27:06 -08:00
parent 4d5893f973
commit aa4c936f54
6 changed files with 57 additions and 8 deletions
src/jdk.compiler/share/classes/com/sun/tools/javac
test/langtools/tools/javac

@ -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 @@ public class JavacParser implements Parser {
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;
}

@ -1207,6 +1207,9 @@ compiler.err.var.not.allowed=\
''{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\

@ -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 { }

@ -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.lang.annotation.Target;
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 @@ class ParserTest<var> {
@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 @@ class ParserTest<var> {
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
}
}
}

@ -3,7 +3,7 @@ ParserTest.java:16:22: compiler.err.var.not.allowed: var
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

@ -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