Skip to content

8358604: Trees for var do not have end positions #25664

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1264,14 +1264,14 @@ public void visitVarDef(JCVariableDecl tree) {
if (tree.init == null) {
//cannot use 'var' without initializer
log.error(tree, Errors.CantInferLocalVarType(tree.name, Fragments.LocalMissingInit));
tree.vartype = make.Erroneous();
tree.vartype = make.at(tree.pos()).Erroneous();
} else {
Fragment msg = canInferLocalVarType(tree);
if (msg != null) {
//cannot use 'var' with initializer which require an explicit target
//(e.g. lambda, method reference, array initializer).
log.error(tree, Errors.CantInferLocalVarType(tree.name, msg));
tree.vartype = make.Erroneous();
tree.vartype = make.at(tree.pos()).Erroneous();
}
}
}
Expand Down Expand Up @@ -5717,6 +5717,9 @@ private Type capture(Type type) {
private void setSyntheticVariableType(JCVariableDecl tree, Type type) {
if (type.isErroneous()) {
tree.vartype = make.at(tree.pos()).Erroneous();
} else if (tree.declaredUsingVar()) { // set the type's start and end positions to match the "var" keyword
Assert.check(tree.typePos != Position.NOPOS);
tree.vartype = make.at(tree.typePos, tree.typePos + names.var.length(), env.toplevel.endPositions).Type(type);
} else {
tree.vartype = make.at(tree.pos()).Type(type);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1005,10 +1005,12 @@ public JCPattern parsePattern(int pos, JCModifiers mods, JCExpression parsedType
pattern = toP(F.at(token.pos).AnyPattern());
}
else {
int varTypePos = Position.NOPOS;
if (parsedType == null) {
boolean var = token.kind == IDENTIFIER && token.name() == names.var;
e = unannotatedType(allowVar, TYPE | NOLAMBDA);
if (var) {
varTypePos = e.pos;
e = null;
}
} else {
Expand Down Expand Up @@ -1046,9 +1048,10 @@ public void visitAnnotatedType(JCAnnotatedType tree) {
if (Feature.UNNAMED_VARIABLES.allowedInSource(source) && name == names.underscore) {
name = names.empty;
}
JCVariableDecl var = toP(F.at(varPos).VarDef(mods, name, e, null));
JCVariableDecl var = toP(F.at(varPos).VarDef(mods, name, e, null,
varTypePos != Position.NOPOS ? JCVariableDecl.DeclKind.VAR : JCVariableDecl.DeclKind.EXPLICIT,
varTypePos));
if (e == null) {
var.startPos = pos;
if (var.name == names.underscore && !allowVar) {
log.error(DiagnosticFlag.SYNTAX, varPos, Errors.UseOfUnderscoreNotAllowed);
}
Expand Down Expand Up @@ -2190,7 +2193,8 @@ JCExpression lambdaExpressionOrStatement(boolean hasParens, boolean explicitPara
if (param.vartype != null
&& restrictedTypeName(param.vartype, true) != null) {
checkSourceLevel(param.pos, Feature.VAR_SYNTAX_IMPLICIT_LAMBDAS);
param.startPos = TreeInfo.getStartPos(param.vartype);
param.declKind = JCVariableDecl.DeclKind.VAR;
param.typePos = TreeInfo.getStartPos(param.vartype);
param.vartype = null;
}
}
Expand Down Expand Up @@ -3830,7 +3834,7 @@ JCVariableDecl variableDeclaratorRest(int pos, JCModifiers mods, JCExpression ty
syntaxError(token.pos, Errors.Expected(EQ));
}

int startPos = Position.NOPOS;
int varTypePos = Position.NOPOS;
JCTree elemType = TreeInfo.innermostType(type, true);
if (elemType.hasTag(IDENT)) {
Name typeName = ((JCIdent) elemType).name;
Expand All @@ -3842,19 +3846,17 @@ JCVariableDecl variableDeclaratorRest(int pos, JCModifiers mods, JCExpression ty
reportSyntaxError(elemType.pos, Errors.RestrictedTypeNotAllowedArray(typeName));
} else {
declaredUsingVar = true;
varTypePos = elemType.pos;
if (compound)
//error - 'var' in compound local var decl
reportSyntaxError(elemType.pos, Errors.RestrictedTypeNotAllowedCompound(typeName));
startPos = TreeInfo.getStartPos(mods);
if (startPos == Position.NOPOS)
startPos = TreeInfo.getStartPos(type);
//implicit type
type = null;
}
}
}
JCVariableDecl result = toP(F.at(pos).VarDef(mods, name, type, init, declaredUsingVar));
result.startPos = startPos;
JCVariableDecl result = toP(F.at(pos).VarDef(mods, name, type, init,
declaredUsingVar ? JCVariableDecl.DeclKind.VAR : JCVariableDecl.DeclKind.EXPLICIT, varTypePos));
return attach(result, dc);
}

Expand Down Expand Up @@ -3968,8 +3970,11 @@ JCVariableDecl variableDeclaratorId(JCModifiers mods, JCExpression type, boolean
name = names.empty;
}

return toP(F.at(pos).VarDef(mods, name, type, null,
type != null && type.hasTag(IDENT) && ((JCIdent)type).name == names.var));
boolean declaredUsingVar = type != null && type.hasTag(IDENT) && ((JCIdent)type).name == names.var;
JCVariableDecl.DeclKind declKind = declaredUsingVar ? JCVariableDecl.DeclKind.VAR :
type != null ? JCVariableDecl.DeclKind.EXPLICIT : JCVariableDecl.DeclKind.IMPLICIT;
int typePos = type != null ? type.pos : pos;
return toP(F.at(pos).VarDef(mods, name, type, null, declKind, typePos));
}

/** Resources = Resource { ";" Resources }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,15 @@ public String toString() {
/** Set position field and return this tree.
*/
public JCTree setPos(int pos) {
return setPos(pos, Position.NOPOS, null);
}

/** Set start and end position and return this tree.
*/
public JCTree setPos(int pos, int endPos, EndPosTable endPosTable) {
this.pos = pos;
if (endPos != Position.NOPOS && endPosTable != null)
endPosTable.storeEnd(this, endPos);
return this;
}

Expand Down Expand Up @@ -1002,6 +1010,13 @@ public Tag getTag() {
* A variable definition.
*/
public static class JCVariableDecl extends JCStatement implements VariableTree {

public enum DeclKind {
EXPLICIT, // "SomeType name"
IMPLICIT, // "name"
VAR, // "var name"
}

/** variable modifiers */
public JCModifiers mods;
/** variable name */
Expand All @@ -1014,37 +1029,39 @@ public static class JCVariableDecl extends JCStatement implements VariableTree {
public JCExpression init;
/** symbol */
public VarSymbol sym;
/** explicit start pos */
public int startPos = Position.NOPOS;
/** declared using `var` */
private boolean declaredUsingVar;
/** how the variable's type was declared */
public DeclKind declKind;
/** a source code position to use for "vartype" when null (can happen if declKind != EXPLICIT) */
public int typePos;

protected JCVariableDecl(JCModifiers mods,
Name name,
JCExpression vartype,
JCExpression init,
VarSymbol sym) {
this(mods, name, vartype, init, sym, false);
this(mods, name, vartype, init, sym, DeclKind.EXPLICIT, Position.NOPOS);
}

protected JCVariableDecl(JCModifiers mods,
Name name,
JCExpression vartype,
JCExpression init,
VarSymbol sym,
boolean declaredUsingVar) {
DeclKind declKind,
int typePos) {
this.mods = mods;
this.name = name;
this.vartype = vartype;
this.init = init;
this.sym = sym;
this.declaredUsingVar = declaredUsingVar;
this.declKind = declKind;
this.typePos = typePos;
}

protected JCVariableDecl(JCModifiers mods,
JCExpression nameexpr,
JCExpression vartype) {
this(mods, null, vartype, null, null, false);
this(mods, null, vartype, null, null, DeclKind.EXPLICIT, Position.NOPOS);
this.nameexpr = nameexpr;
if (nameexpr.hasTag(Tag.IDENT)) {
this.name = ((JCIdent)nameexpr).name;
Expand All @@ -1059,7 +1076,7 @@ public boolean isImplicitlyTyped() {
}

public boolean declaredUsingVar() {
return declaredUsingVar;
return declKind == DeclKind.VAR;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -724,7 +724,10 @@ public void visitVarDef(JCVariableDecl tree) {
print("... ");
print(tree.name);
} else {
printExpr(tree.vartype);
if (tree.vartype == null && tree.declaredUsingVar())
print("var");
else
printExpr(tree.vartype);
print(' ');
if (tree.name.isEmpty()) {
print('_');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ public JCTree visitVariable(VariableTree node, P p) {
JCExpression vartype = copy(t.vartype, p);
if (t.nameexpr == null) {
JCExpression init = copy(t.init, p);
return M.at(t.pos).VarDef(mods, t.name, vartype, init);
return M.at(t.pos).VarDef(mods, t.name, vartype, init, t.declKind, t.typePos);
} else {
JCExpression nameexpr = copy(t.nameexpr, p);
return M.at(t.pos).ReceiverVarDef(mods, nameexpr, vartype);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -610,17 +610,14 @@ public static int getStartPos(JCTree tree) {
}
case VARDEF: {
JCVariableDecl node = (JCVariableDecl)tree;
if (node.startPos != Position.NOPOS) {
return node.startPos;
} else if (node.mods.pos != Position.NOPOS) {
if (node.mods.pos != Position.NOPOS) {
return node.mods.pos;
} else if (node.vartype == null || node.vartype.pos == Position.NOPOS) {
//if there's no type (partially typed lambda parameter)
//simply return node position
return node.pos;
} else {
} else if (node.vartype != null) {
return getStartPos(node.vartype);
} else if (node.typePos != Position.NOPOS) {
return node.typePos;
}
break;
}
case BINDINGPATTERN: {
JCBindingPattern node = (JCBindingPattern)tree;
Expand Down
Loading