8195293: Issue more comprehensive warnings for use of \"var\" in earlier source versions
authormcimadamore
Wed, 20 Jun 2018 11:47:07 +0100
changeset 50675 273183fd8246
parent 50674 9ab948f612a8
child 50676 8c0a5b51559b
child 56786 e550e715c4d3
8195293: Issue more comprehensive warnings for use of \"var\" in earlier source versions Summary: issue warnings when 'var' used as a type name in type argument positions Reviewed-by: jlahoda
src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Resolve.java
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/lvti/ParserTest.out
test/langtools/tools/javac/lvti/ParserTest9.out
test/langtools/tools/javac/lvti/badTypeReference/BadTypeReference.out
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Resolve.java	Wed Jun 20 11:33:43 2018 +0200
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Resolve.java	Wed Jun 20 11:47:07 2018 +0100
@@ -3842,7 +3842,7 @@
 
         @Override
         JCDiagnostic getDiagnostic(DiagnosticType dkind, DiagnosticPosition pos, Symbol location, Type site, Name name, List<Type> argtypes, List<Type> typeargtypes) {
-            return diags.create(dkind, log.currentSource(), pos, "illegal.ref.to.var.type", name);
+            return diags.create(dkind, log.currentSource(), pos, "illegal.ref.to.var.type");
         }
     }
 
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java	Wed Jun 20 11:33:43 2018 +0200
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java	Wed Jun 20 11:47:07 2018 +0100
@@ -751,7 +751,7 @@
     public JCExpression unannotatedType(boolean allowVar) {
         JCExpression result = term(TYPE);
 
-        if (!allowVar && isRestrictedLocalVarTypeName(result)) {
+        if (!allowVar && isRestrictedLocalVarTypeName(result, true)) {
             syntaxError(result.pos, Errors.VarNotAllowedHere);
         }
 
@@ -1688,7 +1688,7 @@
             LambdaClassifier lambdaClassifier = new LambdaClassifier();
             for (JCVariableDecl param: params) {
                 if (param.vartype != null &&
-                        isRestrictedLocalVarTypeName(param.vartype) &&
+                        isRestrictedLocalVarTypeName(param.vartype, false) &&
                         param.vartype.hasTag(TYPEARRAY)) {
                     log.error(DiagnosticFlag.SYNTAX, param.pos, Errors.VarNotAllowedArray);
                 }
@@ -1701,7 +1701,7 @@
                 log.error(DiagnosticFlag.SYNTAX, pos, Errors.InvalidLambdaParameterDeclaration(lambdaClassifier.diagFragment));
             }
             for (JCVariableDecl param: params) {
-                if (param.vartype != null && isRestrictedLocalVarTypeName(param.vartype)) {
+                if (param.vartype != null && isRestrictedLocalVarTypeName(param.vartype, true)) {
                     param.startPos = TreeInfo.getStartPos(param.vartype);
                     param.vartype = null;
                 }
@@ -1738,7 +1738,7 @@
 
         void addParameter(JCVariableDecl param) {
             if (param.vartype != null && param.name != names.empty) {
-                if (isRestrictedLocalVarTypeName(param.vartype)) {
+                if (isRestrictedLocalVarTypeName(param.vartype, false)) {
                     reduce(LambdaParameterKind.VAR);
                 } else {
                     reduce(LambdaParameterKind.EXPLICIT);
@@ -3021,13 +3021,9 @@
                                                                      T vdefs,
                                                                      boolean localDecl)
     {
-        JCVariableDecl head = variableDeclaratorRest(pos, mods, type, name, reqInit, dc, localDecl);
-        boolean implicit = Feature.LOCAL_VARIABLE_TYPE_INFERENCE.allowedInSource(source) && head.vartype == null;
+        JCVariableDecl head = variableDeclaratorRest(pos, mods, type, name, reqInit, dc, localDecl, false);
         vdefs.append(head);
         while (token.kind == COMMA) {
-            if (implicit) {
-                reportSyntaxError(pos, Errors.VarNotAllowedCompound);
-            }
             // All but last of multiple declarators subsume a comma
             storeEnd((JCTree)vdefs.last(), token.endPos);
             nextToken();
@@ -3040,7 +3036,7 @@
      *  ConstantDeclarator = Ident ConstantDeclaratorRest
      */
     JCVariableDecl variableDeclarator(JCModifiers mods, JCExpression type, boolean reqInit, Comment dc, boolean localDecl) {
-        return variableDeclaratorRest(token.pos, mods, type, ident(), reqInit, dc, localDecl);
+        return variableDeclaratorRest(token.pos, mods, type, ident(), reqInit, dc, localDecl, true);
     }
 
     /** VariableDeclaratorRest = BracketsOpt ["=" VariableInitializer]
@@ -3050,7 +3046,7 @@
      *  @param dc       The documentation comment for the variable declarations, or null.
      */
     JCVariableDecl variableDeclaratorRest(int pos, JCModifiers mods, JCExpression type, Name name,
-                                  boolean reqInit, Comment dc, boolean localDecl) {
+                                  boolean reqInit, Comment dc, boolean localDecl, boolean compound) {
         type = bracketsOpt(type);
         JCExpression init = null;
         if (token.kind == EQ) {
@@ -3060,10 +3056,13 @@
         else if (reqInit) syntaxError(token.pos, Errors.Expected(EQ));
         JCTree elemType = TreeInfo.innermostType(type, true);
         int startPos = Position.NOPOS;
-        if (Feature.LOCAL_VARIABLE_TYPE_INFERENCE.allowedInSource(source) && elemType.hasTag(IDENT)) {
+        if (elemType.hasTag(IDENT)) {
             Name typeName = ((JCIdent)elemType).name;
-            if (isRestrictedLocalVarTypeName(typeName)) {
-                if (type.hasTag(TYPEARRAY)) {
+            if (isRestrictedLocalVarTypeName(typeName, pos, !compound && localDecl)) {
+                if (compound) {
+                    //error - 'var' in compound local var decl
+                   reportSyntaxError(pos, Errors.VarNotAllowedCompound);
+                } else if (type.hasTag(TYPEARRAY)) {
                     //error - 'var' and arrays
                     reportSyntaxError(pos, Errors.VarNotAllowedArray);
                 } else {
@@ -3082,19 +3081,26 @@
         return result;
     }
 
-    boolean isRestrictedLocalVarTypeName(JCExpression e) {
+    boolean isRestrictedLocalVarTypeName(JCExpression e, boolean shouldWarn) {
         switch (e.getTag()) {
             case IDENT:
-                return isRestrictedLocalVarTypeName(((JCIdent)e).name);
+                return isRestrictedLocalVarTypeName(((JCIdent)e).name, e.pos, shouldWarn);
             case TYPEARRAY:
-                return isRestrictedLocalVarTypeName(((JCArrayTypeTree)e).elemtype);
+                return isRestrictedLocalVarTypeName(((JCArrayTypeTree)e).elemtype, shouldWarn);
             default:
                 return false;
         }
     }
 
-    boolean isRestrictedLocalVarTypeName(Name name) {
-        return Feature.LOCAL_VARIABLE_TYPE_INFERENCE.allowedInSource(source) && name == names.var;
+    boolean isRestrictedLocalVarTypeName(Name name, int pos, boolean shouldWarn) {
+        if (name == names.var) {
+            if (Feature.LOCAL_VARIABLE_TYPE_INFERENCE.allowedInSource(source)) {
+                return true;
+            } else if (shouldWarn) {
+                log.warning(pos, Warnings.VarNotAllowed);
+            }
+        }
+        return false;
     }
 
     /** VariableDeclaratorId = Ident BracketsOpt
@@ -3179,12 +3185,12 @@
         if (token.kind == FINAL || token.kind == MONKEYS_AT) {
             JCModifiers mods = optFinal(Flags.FINAL);
             JCExpression t = parseType(true);
-            return variableDeclaratorRest(token.pos, mods, t, ident(), true, null, true);
+            return variableDeclaratorRest(token.pos, mods, t, ident(), true, null, true, false);
         }
         JCExpression t = term(EXPR | TYPE);
         if ((lastmode & TYPE) != 0 && LAX_IDENTIFIER.accepts(token.kind)) {
             JCModifiers mods = toP(F.at(startPos).Modifiers(Flags.FINAL));
-            return variableDeclaratorRest(token.pos, mods, t, ident(), true, null, true);
+            return variableDeclaratorRest(token.pos, mods, t, ident(), true, null, true, false);
         } else {
             checkSourceLevel(Feature.EFFECTIVELY_FINAL_VARIABLES_IN_TRY_WITH_RESOURCES);
             if (!t.hasTag(IDENT) && !t.hasTag(SELECT)) {
@@ -3485,12 +3491,8 @@
     Name typeName() {
         int pos = token.pos;
         Name name = ident();
-        if (name == names.var) {
-            if (Feature.LOCAL_VARIABLE_TYPE_INFERENCE.allowedInSource(source)) {
-                reportSyntaxError(pos, Errors.VarNotAllowed(name));
-            } else {
-                log.warning(pos, Warnings.VarNotAllowed);
-            }
+        if (isRestrictedLocalVarTypeName(name, pos, true)) {
+            reportSyntaxError(pos, Errors.VarNotAllowed);
         }
         return name;
     }
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties	Wed Jun 20 11:33:43 2018 +0200
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties	Wed Jun 20 11:47:07 2018 +0100
@@ -1216,17 +1216,15 @@
 compiler.err.undef.label=\
     undefined label: {0}
 
-# 0: name (type)
 compiler.err.illegal.ref.to.var.type=\
-    illegal reference to restricted type ''{0}''
-
-# 0: name
+    illegal reference to restricted type ''var''
+
 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
+    ''var'' not allowed here\n\
+    as of release 10, ''var'' 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
+    as of release 10, ''var'' 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=\
--- a/test/langtools/tools/javac/lvti/ParserTest.out	Wed Jun 20 11:33:43 2018 +0200
+++ b/test/langtools/tools/javac/lvti/ParserTest.out	Wed Jun 20 11:47:07 2018 +0100
@@ -1,9 +1,9 @@
-ParserTest.java:14:18: compiler.err.var.not.allowed: var
-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:27: compiler.err.var.not.allowed: var
+ParserTest.java:14:18: compiler.err.var.not.allowed
+ParserTest.java:16:22: compiler.err.var.not.allowed
+ParserTest.java:20:19: compiler.err.var.not.allowed
+ParserTest.java:24:14: compiler.err.var.not.allowed
+ParserTest.java:28:20: compiler.err.var.not.allowed
+ParserTest.java:36:27: compiler.err.var.not.allowed
 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
@@ -11,7 +11,7 @@
 ParserTest.java:44:13: compiler.err.var.not.allowed.array
 ParserTest.java:45:15: compiler.err.var.not.allowed.array
 ParserTest.java:46:13: compiler.err.var.not.allowed.array
-ParserTest.java:49:13: compiler.err.var.not.allowed.compound
+ParserTest.java:49:24: compiler.err.var.not.allowed.compound
 ParserTest.java:54:5: compiler.err.var.not.allowed.here
 ParserTest.java:58:16: compiler.err.var.not.allowed.here
 ParserTest.java:59:14: compiler.err.var.not.allowed.here
--- a/test/langtools/tools/javac/lvti/ParserTest9.out	Wed Jun 20 11:33:43 2018 +0200
+++ b/test/langtools/tools/javac/lvti/ParserTest9.out	Wed Jun 20 11:47:07 2018 +0100
@@ -4,4 +4,28 @@
 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
+ParserTest.java:38:5: compiler.warn.var.not.allowed
+ParserTest.java:41:15: compiler.warn.var.not.allowed
+ParserTest.java:42:13: compiler.warn.var.not.allowed
+ParserTest.java:43:17: compiler.warn.var.not.allowed
+ParserTest.java:44:13: compiler.warn.var.not.allowed
+ParserTest.java:45:15: compiler.warn.var.not.allowed
+ParserTest.java:46:13: compiler.warn.var.not.allowed
+ParserTest.java:47:23: compiler.warn.var.not.allowed
+ParserTest.java:48:13: compiler.warn.var.not.allowed
+ParserTest.java:49:13: compiler.warn.var.not.allowed
+ParserTest.java:50:23: compiler.warn.var.not.allowed
+ParserTest.java:51:23: compiler.warn.var.not.allowed
+ParserTest.java:54:5: compiler.warn.var.not.allowed
+ParserTest.java:58:16: compiler.warn.var.not.allowed
+ParserTest.java:59:14: compiler.warn.var.not.allowed
+ParserTest.java:60:24: compiler.warn.var.not.allowed
+ParserTest.java:61:22: compiler.warn.var.not.allowed
+ParserTest.java:63:22: compiler.warn.var.not.allowed
+ParserTest.java:63:40: compiler.warn.var.not.allowed
+ParserTest.java:64:18: compiler.warn.var.not.allowed
+ParserTest.java:68:35: compiler.warn.var.not.allowed
+ParserTest.java:69:22: compiler.warn.var.not.allowed
+ParserTest.java:73:24: compiler.warn.var.not.allowed
+ParserTest.java:74:18: compiler.warn.var.not.allowed
+30 warnings
--- a/test/langtools/tools/javac/lvti/badTypeReference/BadTypeReference.out	Wed Jun 20 11:33:43 2018 +0200
+++ b/test/langtools/tools/javac/lvti/badTypeReference/BadTypeReference.out	Wed Jun 20 11:47:07 2018 +0100
@@ -1,3 +1,3 @@
-BadTypeReference.java:39:9: compiler.err.illegal.ref.to.var.type: var
-BadTypeReference.java:40:21: compiler.err.illegal.ref.to.var.type: var
+BadTypeReference.java:39:9: compiler.err.illegal.ref.to.var.type
+BadTypeReference.java:40:21: compiler.err.illegal.ref.to.var.type
 2 errors