8204674: Inconsistent lambda parameter span
authorjlahoda
Tue, 12 Jun 2018 12:14:46 +0200
changeset 50518 e775444c555e
parent 50517 618526574f8b
child 50520 12be4323b0a5
8204674: Inconsistent lambda parameter span Summary: Correcting parameter spans for lambda parameters. Reviewed-by: mcimadamore
src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java
src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java
test/langtools/tools/javac/tree/VarTree.java
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java	Sun Jun 10 20:53:10 2018 +0100
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java	Tue Jun 12 12:14:46 2018 +0200
@@ -2562,7 +2562,9 @@
                     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 :
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java	Sun Jun 10 20:53:10 2018 +0100
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java	Tue Jun 12 12:14:46 2018 +0200
@@ -1702,6 +1702,7 @@
             }
             for (JCVariableDecl param: params) {
                 if (param.vartype != null && isRestrictedLocalVarTypeName(param.vartype)) {
+                    param.startPos = TreeInfo.getStartPos(param.vartype);
                     param.vartype = null;
                 }
             }
--- a/test/langtools/tools/javac/tree/VarTree.java	Sun Jun 10 20:53:10 2018 +0100
+++ b/test/langtools/tools/javac/tree/VarTree.java	Tue Jun 12 12:14:46 2018 +0200
@@ -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.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 @@
                  "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 @@
                                                         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 @@
                         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 @@
                         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;