8195293: Issue more comprehensive warnings for use of \"var\" in earlier source versions
Issue warnings when 'var' used as a type name in type argument positions Reviewed-by: jlahoda
This commit is contained in:
parent
d33aa17aec
commit
43248585b4
@ -3842,7 +3842,7 @@ public class Resolve {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
JCDiagnostic getDiagnostic(DiagnosticType dkind, DiagnosticPosition pos, Symbol location, Type site, Name name, List<Type> argtypes, List<Type> typeargtypes) {
|
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");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -751,7 +751,7 @@ public class JavacParser implements Parser {
|
|||||||
public JCExpression unannotatedType(boolean allowVar) {
|
public JCExpression unannotatedType(boolean allowVar) {
|
||||||
JCExpression result = term(TYPE);
|
JCExpression result = term(TYPE);
|
||||||
|
|
||||||
if (!allowVar && isRestrictedLocalVarTypeName(result)) {
|
if (!allowVar && isRestrictedLocalVarTypeName(result, true)) {
|
||||||
syntaxError(result.pos, Errors.VarNotAllowedHere);
|
syntaxError(result.pos, Errors.VarNotAllowedHere);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1688,7 +1688,7 @@ public class JavacParser implements Parser {
|
|||||||
LambdaClassifier lambdaClassifier = new LambdaClassifier();
|
LambdaClassifier lambdaClassifier = new LambdaClassifier();
|
||||||
for (JCVariableDecl param: params) {
|
for (JCVariableDecl param: params) {
|
||||||
if (param.vartype != null &&
|
if (param.vartype != null &&
|
||||||
isRestrictedLocalVarTypeName(param.vartype) &&
|
isRestrictedLocalVarTypeName(param.vartype, false) &&
|
||||||
param.vartype.hasTag(TYPEARRAY)) {
|
param.vartype.hasTag(TYPEARRAY)) {
|
||||||
log.error(DiagnosticFlag.SYNTAX, param.pos, Errors.VarNotAllowedArray);
|
log.error(DiagnosticFlag.SYNTAX, param.pos, Errors.VarNotAllowedArray);
|
||||||
}
|
}
|
||||||
@ -1701,7 +1701,7 @@ public class JavacParser implements Parser {
|
|||||||
log.error(DiagnosticFlag.SYNTAX, pos, Errors.InvalidLambdaParameterDeclaration(lambdaClassifier.diagFragment));
|
log.error(DiagnosticFlag.SYNTAX, pos, Errors.InvalidLambdaParameterDeclaration(lambdaClassifier.diagFragment));
|
||||||
}
|
}
|
||||||
for (JCVariableDecl param: params) {
|
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.startPos = TreeInfo.getStartPos(param.vartype);
|
||||||
param.vartype = null;
|
param.vartype = null;
|
||||||
}
|
}
|
||||||
@ -1738,7 +1738,7 @@ public class JavacParser implements Parser {
|
|||||||
|
|
||||||
void addParameter(JCVariableDecl param) {
|
void addParameter(JCVariableDecl param) {
|
||||||
if (param.vartype != null && param.name != names.empty) {
|
if (param.vartype != null && param.name != names.empty) {
|
||||||
if (isRestrictedLocalVarTypeName(param.vartype)) {
|
if (isRestrictedLocalVarTypeName(param.vartype, false)) {
|
||||||
reduce(LambdaParameterKind.VAR);
|
reduce(LambdaParameterKind.VAR);
|
||||||
} else {
|
} else {
|
||||||
reduce(LambdaParameterKind.EXPLICIT);
|
reduce(LambdaParameterKind.EXPLICIT);
|
||||||
@ -3021,13 +3021,9 @@ public class JavacParser implements Parser {
|
|||||||
T vdefs,
|
T vdefs,
|
||||||
boolean localDecl)
|
boolean localDecl)
|
||||||
{
|
{
|
||||||
JCVariableDecl head = variableDeclaratorRest(pos, mods, type, name, reqInit, dc, localDecl);
|
JCVariableDecl head = variableDeclaratorRest(pos, mods, type, name, reqInit, dc, localDecl, false);
|
||||||
boolean implicit = Feature.LOCAL_VARIABLE_TYPE_INFERENCE.allowedInSource(source) && head.vartype == null;
|
|
||||||
vdefs.append(head);
|
vdefs.append(head);
|
||||||
while (token.kind == COMMA) {
|
while (token.kind == COMMA) {
|
||||||
if (implicit) {
|
|
||||||
reportSyntaxError(pos, Errors.VarNotAllowedCompound);
|
|
||||||
}
|
|
||||||
// All but last of multiple declarators subsume a comma
|
// All but last of multiple declarators subsume a comma
|
||||||
storeEnd((JCTree)vdefs.last(), token.endPos);
|
storeEnd((JCTree)vdefs.last(), token.endPos);
|
||||||
nextToken();
|
nextToken();
|
||||||
@ -3040,7 +3036,7 @@ public class JavacParser implements Parser {
|
|||||||
* ConstantDeclarator = Ident ConstantDeclaratorRest
|
* ConstantDeclarator = Ident ConstantDeclaratorRest
|
||||||
*/
|
*/
|
||||||
JCVariableDecl variableDeclarator(JCModifiers mods, JCExpression type, boolean reqInit, Comment dc, boolean localDecl) {
|
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]
|
/** VariableDeclaratorRest = BracketsOpt ["=" VariableInitializer]
|
||||||
@ -3050,7 +3046,7 @@ public class JavacParser implements Parser {
|
|||||||
* @param dc The documentation comment for the variable declarations, or null.
|
* @param dc The documentation comment for the variable declarations, or null.
|
||||||
*/
|
*/
|
||||||
JCVariableDecl variableDeclaratorRest(int pos, JCModifiers mods, JCExpression type, Name name,
|
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);
|
type = bracketsOpt(type);
|
||||||
JCExpression init = null;
|
JCExpression init = null;
|
||||||
if (token.kind == EQ) {
|
if (token.kind == EQ) {
|
||||||
@ -3060,10 +3056,13 @@ public class JavacParser implements Parser {
|
|||||||
else if (reqInit) syntaxError(token.pos, Errors.Expected(EQ));
|
else if (reqInit) syntaxError(token.pos, Errors.Expected(EQ));
|
||||||
JCTree elemType = TreeInfo.innermostType(type, true);
|
JCTree elemType = TreeInfo.innermostType(type, true);
|
||||||
int startPos = Position.NOPOS;
|
int startPos = Position.NOPOS;
|
||||||
if (Feature.LOCAL_VARIABLE_TYPE_INFERENCE.allowedInSource(source) && elemType.hasTag(IDENT)) {
|
if (elemType.hasTag(IDENT)) {
|
||||||
Name typeName = ((JCIdent)elemType).name;
|
Name typeName = ((JCIdent)elemType).name;
|
||||||
if (isRestrictedLocalVarTypeName(typeName)) {
|
if (isRestrictedLocalVarTypeName(typeName, pos, !compound && localDecl)) {
|
||||||
if (type.hasTag(TYPEARRAY)) {
|
if (compound) {
|
||||||
|
//error - 'var' in compound local var decl
|
||||||
|
reportSyntaxError(pos, Errors.VarNotAllowedCompound);
|
||||||
|
} else if (type.hasTag(TYPEARRAY)) {
|
||||||
//error - 'var' and arrays
|
//error - 'var' and arrays
|
||||||
reportSyntaxError(pos, Errors.VarNotAllowedArray);
|
reportSyntaxError(pos, Errors.VarNotAllowedArray);
|
||||||
} else {
|
} else {
|
||||||
@ -3082,19 +3081,26 @@ public class JavacParser implements Parser {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean isRestrictedLocalVarTypeName(JCExpression e) {
|
boolean isRestrictedLocalVarTypeName(JCExpression e, boolean shouldWarn) {
|
||||||
switch (e.getTag()) {
|
switch (e.getTag()) {
|
||||||
case IDENT:
|
case IDENT:
|
||||||
return isRestrictedLocalVarTypeName(((JCIdent)e).name);
|
return isRestrictedLocalVarTypeName(((JCIdent)e).name, e.pos, shouldWarn);
|
||||||
case TYPEARRAY:
|
case TYPEARRAY:
|
||||||
return isRestrictedLocalVarTypeName(((JCArrayTypeTree)e).elemtype);
|
return isRestrictedLocalVarTypeName(((JCArrayTypeTree)e).elemtype, shouldWarn);
|
||||||
default:
|
default:
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean isRestrictedLocalVarTypeName(Name name) {
|
boolean isRestrictedLocalVarTypeName(Name name, int pos, boolean shouldWarn) {
|
||||||
return Feature.LOCAL_VARIABLE_TYPE_INFERENCE.allowedInSource(source) && name == names.var;
|
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
|
/** VariableDeclaratorId = Ident BracketsOpt
|
||||||
@ -3179,12 +3185,12 @@ public class JavacParser implements Parser {
|
|||||||
if (token.kind == FINAL || token.kind == MONKEYS_AT) {
|
if (token.kind == FINAL || token.kind == MONKEYS_AT) {
|
||||||
JCModifiers mods = optFinal(Flags.FINAL);
|
JCModifiers mods = optFinal(Flags.FINAL);
|
||||||
JCExpression t = parseType(true);
|
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);
|
JCExpression t = term(EXPR | TYPE);
|
||||||
if ((lastmode & TYPE) != 0 && LAX_IDENTIFIER.accepts(token.kind)) {
|
if ((lastmode & TYPE) != 0 && LAX_IDENTIFIER.accepts(token.kind)) {
|
||||||
JCModifiers mods = toP(F.at(startPos).Modifiers(Flags.FINAL));
|
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 {
|
} else {
|
||||||
checkSourceLevel(Feature.EFFECTIVELY_FINAL_VARIABLES_IN_TRY_WITH_RESOURCES);
|
checkSourceLevel(Feature.EFFECTIVELY_FINAL_VARIABLES_IN_TRY_WITH_RESOURCES);
|
||||||
if (!t.hasTag(IDENT) && !t.hasTag(SELECT)) {
|
if (!t.hasTag(IDENT) && !t.hasTag(SELECT)) {
|
||||||
@ -3485,12 +3491,8 @@ public class JavacParser implements Parser {
|
|||||||
Name typeName() {
|
Name typeName() {
|
||||||
int pos = token.pos;
|
int pos = token.pos;
|
||||||
Name name = ident();
|
Name name = ident();
|
||||||
if (name == names.var) {
|
if (isRestrictedLocalVarTypeName(name, pos, true)) {
|
||||||
if (Feature.LOCAL_VARIABLE_TYPE_INFERENCE.allowedInSource(source)) {
|
reportSyntaxError(pos, Errors.VarNotAllowed);
|
||||||
reportSyntaxError(pos, Errors.VarNotAllowed(name));
|
|
||||||
} else {
|
|
||||||
log.warning(pos, Warnings.VarNotAllowed);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
@ -1216,17 +1216,15 @@ compiler.err.io.exception=\
|
|||||||
compiler.err.undef.label=\
|
compiler.err.undef.label=\
|
||||||
undefined label: {0}
|
undefined label: {0}
|
||||||
|
|
||||||
# 0: name (type)
|
|
||||||
compiler.err.illegal.ref.to.var.type=\
|
compiler.err.illegal.ref.to.var.type=\
|
||||||
illegal reference to restricted type ''{0}''
|
illegal reference to restricted type ''var''
|
||||||
|
|
||||||
# 0: name
|
|
||||||
compiler.err.var.not.allowed=\
|
compiler.err.var.not.allowed=\
|
||||||
''{0}'' not allowed here\n\
|
''var'' not allowed here\n\
|
||||||
as of release 10, ''{0}'' 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
|
||||||
|
|
||||||
compiler.warn.var.not.allowed=\
|
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
|
# 0: name (variable), 1: message segment
|
||||||
compiler.err.cant.infer.local.var.type=\
|
compiler.err.cant.infer.local.var.type=\
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
ParserTest.java:14:18: compiler.err.var.not.allowed: var
|
ParserTest.java:14:18: compiler.err.var.not.allowed
|
||||||
ParserTest.java:16:22: compiler.err.var.not.allowed: var
|
ParserTest.java:16:22: compiler.err.var.not.allowed
|
||||||
ParserTest.java:20:19: compiler.err.var.not.allowed: var
|
ParserTest.java:20:19: compiler.err.var.not.allowed
|
||||||
ParserTest.java:24:14: compiler.err.var.not.allowed: var
|
ParserTest.java:24:14: compiler.err.var.not.allowed
|
||||||
ParserTest.java:28:20: compiler.err.var.not.allowed: var
|
ParserTest.java:28:20: compiler.err.var.not.allowed
|
||||||
ParserTest.java:36:27: compiler.err.var.not.allowed: var
|
ParserTest.java:36:27: compiler.err.var.not.allowed
|
||||||
ParserTest.java:38:5: compiler.err.var.not.allowed.here
|
ParserTest.java:38:5: compiler.err.var.not.allowed.here
|
||||||
ParserTest.java:41:15: compiler.err.var.not.allowed.array
|
ParserTest.java:41:15: compiler.err.var.not.allowed.array
|
||||||
ParserTest.java:42:13: compiler.err.var.not.allowed.array
|
ParserTest.java:42:13: compiler.err.var.not.allowed.array
|
||||||
@ -11,7 +11,7 @@ ParserTest.java:43:17: compiler.err.var.not.allowed.array
|
|||||||
ParserTest.java:44:13: compiler.err.var.not.allowed.array
|
ParserTest.java:44:13: compiler.err.var.not.allowed.array
|
||||||
ParserTest.java:45:15: 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: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:54:5: compiler.err.var.not.allowed.here
|
||||||
ParserTest.java:58:16: 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
|
ParserTest.java:59:14: compiler.err.var.not.allowed.here
|
||||||
|
@ -4,4 +4,28 @@ ParserTest.java:20:19: compiler.warn.var.not.allowed
|
|||||||
ParserTest.java:24:14: 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:28:20: compiler.warn.var.not.allowed
|
||||||
ParserTest.java:36:27: 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
|
||||||
|
@ -1,3 +1,3 @@
|
|||||||
BadTypeReference.java:39:9: 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: var
|
BadTypeReference.java:40:21: compiler.err.illegal.ref.to.var.type
|
||||||
2 errors
|
2 errors
|
||||||
|
Loading…
x
Reference in New Issue
Block a user