8204674: Inconsistent lambda parameter span

Correcting parameter spans for lambda parameters.

Reviewed-by: mcimadamore
This commit is contained in:
Jan Lahoda 2018-06-12 12:14:46 +02:00
parent dff9265409
commit 62a6e04db0
3 changed files with 55 additions and 4 deletions

View File

@ -2562,7 +2562,9 @@ public class Attr extends JCTree.Visitor {
Type argType = arityMismatch ?
syms.errType :
actuals.head;
setSyntheticVariableType(params.head, argType);
if (params.head.isImplicitlyTyped()) {
setSyntheticVariableType(params.head, argType);
}
params.head.sym = null;
actuals = actuals.isEmpty() ?
actuals :

View File

@ -1702,6 +1702,7 @@ public class JavacParser implements Parser {
}
for (JCVariableDecl param: params) {
if (param.vartype != null && isRestrictedLocalVarTypeName(param.vartype)) {
param.startPos = TreeInfo.getStartPos(param.vartype);
param.vartype = null;
}
}

View File

@ -23,7 +23,7 @@
/*
* @test
* @bug 8188225
* @bug 8188225 8204674
* @summary Check that variables of type var have a consistent model
* @modules jdk.compiler/com.sun.tools.javac.api
*/
@ -42,6 +42,7 @@ import javax.tools.SimpleJavaFileObject;
import javax.tools.ToolProvider;
import com.sun.source.tree.VariableTree;
import com.sun.source.util.JavacTask;
import com.sun.source.util.TreeScanner;
import com.sun.source.util.Trees;
@ -64,6 +65,10 @@ public class VarTree {
"java.lang.String testVar");
test.run("java.util.function.Consumer<String> c = (|testVar|) -> {};",
"java.lang.String testVar");
test.run("java.util.function.Consumer<String> c = (|var testVar|) -> {};",
"java.lang.String testVar");
test.run("java.util.function.IntBinaryOperator c = (var x, |testType|) -> 1;",
"testType ");
}
void run(String code, String expected) throws IOException {
@ -81,9 +86,12 @@ public class VarTree {
null, Arrays.asList(new MyFileObject(src)));
Iterable<? extends CompilationUnitTree> units = ct.parse();
runSpanCheck(ct, units, src, prefix.length() + parts[0].length(), prefix.length() + parts[0].length() + parts[1].length());
ct.analyze();
Trees trees = Trees.instance(ct);
runSpanCheck(ct, units, src, prefix.length() + parts[0].length(), prefix.length() + parts[0].length() + parts[1].length());
for (CompilationUnitTree cut : units) {
new TreeScanner<Void, Void>() {
@ -93,13 +101,34 @@ public class VarTree {
if (!expected.equals(node.toString())) {
throw new AssertionError("Unexpected tree: " + node.toString());
}
}
if (String.valueOf(node.getType()).equals("testType")) {
if (!expected.equals(node.toString())) {
throw new AssertionError("Unexpected tree: " + node.toString());
}
}
return super.visitVariable(node, p);
}
}.scan(cut, null);
}
}
private void runSpanCheck(JavacTask ct, Iterable<? extends CompilationUnitTree> units, String src, int spanStart, int spanEnd) {
Trees trees = Trees.instance(ct);
boolean[] found = new boolean[1];
for (CompilationUnitTree cut : units) {
new TreeScanner<Void, Void>() {
@Override
public Void visitVariable(VariableTree node, Void p) {
if (node.getName().contentEquals("testVar")) {
int start = (int) trees.getSourcePositions().getStartPosition(cut, node);
int end = (int) trees.getSourcePositions().getEndPosition(cut, node);
String snip = src.substring(start, end);
if (start != prefix.length() + parts[0].length() || end != prefix.length() + parts[0].length() + parts[1].length()) {
if (start != spanStart || end != spanEnd) {
throw new AssertionError("Unexpected span: " + snip);
}
@ -109,13 +138,32 @@ public class VarTree {
if (typeStart != (-1) && typeEnd != (-1)) {
throw new AssertionError("Unexpected type position: " + typeStart + ", " + typeEnd);
}
found[0] = true;
}
if (String.valueOf(node.getType()).equals("testType")) {
int start = (int) trees.getSourcePositions().getStartPosition(cut, node);
int end = (int) trees.getSourcePositions().getEndPosition(cut, node);
String snip = src.substring(start, end);
if (start != spanStart || end != spanEnd) {
throw new AssertionError("Unexpected span: " + snip);
}
found[0] = true;
}
return super.visitVariable(node, p);
}
}.scan(cut, null);
}
if (!found[0]) {
throw new AssertionError("Didn't find the test variable.");
}
}
class MyFileObject extends SimpleJavaFileObject {
private String text;