8191981: javac crash when detecting lambda candidates
Summary: Ensuring proper positions on the testing AST nodes created by Analyzer.
Reviewed-by: mcimadamore
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Analyzer.java Wed Nov 29 14:45:41 2017 +0100
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Analyzer.java Wed Nov 29 14:41:09 2017 +0100
@@ -63,6 +63,7 @@
import com.sun.tools.javac.util.ListBuffer;
import com.sun.tools.javac.util.Log;
import com.sun.tools.javac.util.Options;
+import com.sun.tools.javac.util.Position;
import java.util.EnumSet;
import java.util.HashMap;
@@ -305,7 +306,7 @@
JCMethodDecl md = (JCMethodDecl)decls(oldTree.def).head;
List<JCVariableDecl> params = md.params;
JCBlock body = md.body;
- JCLambda newTree = make.Lambda(params, body);
+ JCLambda newTree = make.at(oldTree).Lambda(params, body);
return List.of(newTree);
}
@@ -418,7 +419,7 @@
List<JCEnhancedForLoop> rewrite(JCEnhancedForLoop oldTree) {
JCEnhancedForLoop newTree = copier.copy(oldTree);
newTree.var = rewriteVarType(oldTree.var);
- newTree.body = make.Block(0, List.nil());
+ newTree.body = make.at(oldTree.body).Block(0, List.nil());
return List.of(newTree);
}
@Override
@@ -551,7 +552,8 @@
JCStatement treeToAnalyze = (JCStatement)rewriting.originalTree;
if (rewriting.env.info.scope.owner.kind == Kind.TYP) {
//add a block to hoist potential dangling variable declarations
- treeToAnalyze = make.Block(Flags.SYNTHETIC, List.of((JCStatement)rewriting.originalTree));
+ treeToAnalyze = make.at(Position.NOPOS)
+ .Block(Flags.SYNTHETIC, List.of((JCStatement)rewriting.originalTree));
}
//TODO: to further refine the analysis, try all rewriting combinations
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/test/langtools/tools/javac/analyzer/LambdaWithMethod.java Wed Nov 29 14:41:09 2017 +0100
@@ -0,0 +1,17 @@
+/**
+ * @test /nodynamiccopyright/
+ * @bug 8191981
+ * @compile/fail/ref=LambdaWithMethod.out -Werror -XDrawDiagnostics -XDfind=lambda LambdaWithMethod.java
+ */
+
+public class LambdaWithMethod {
+ public static void run(Runnable r) {
+ run(new Runnable() {
+ public void run() {
+ put(get());
+ }
+ });
+ }
+ private static String get() { return null; }
+ private static void put(String i) {}
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/test/langtools/tools/javac/analyzer/LambdaWithMethod.out Wed Nov 29 14:41:09 2017 +0100
@@ -0,0 +1,4 @@
+LambdaWithMethod.java:9:28: compiler.warn.potential.lambda.found
+- compiler.err.warnings.and.werror
+1 error
+1 warning