--- 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;
}