8181911: Lambda Analyzer causes compile-time error
Summary: When copying Env<AttrContext> for Analyzer, detach returnResult from the outer context.
Reviewed-by: mcimadamore
--- a/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Analyzer.java Mon Jun 12 12:25:40 2017 -0700
+++ b/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Analyzer.java Mon Jun 19 11:41:21 2017 +0200
@@ -336,12 +336,28 @@
};
/**
+ * Create a copy of Env if needed.
+ */
+ Env<AttrContext> copyEnvIfNeeded(JCTree tree, Env<AttrContext> env) {
+ if (!analyzerModes.isEmpty() &&
+ !env.info.isSpeculative &&
+ TreeInfo.isStatement(tree)) {
+ Env<AttrContext> analyzeEnv =
+ env.dup(env.tree, env.info.dup(env.info.scope.dupUnshared(env.info.scope.owner)));
+ analyzeEnv.info.returnResult = analyzeEnv.info.returnResult != null ?
+ attr.new ResultInfo(analyzeEnv.info.returnResult.pkind,
+ analyzeEnv.info.returnResult.pt) : null;
+ return analyzeEnv;
+ } else {
+ return null;
+ }
+ }
+
+ /**
* Analyze an AST node if needed.
*/
void analyzeIfNeeded(JCTree tree, Env<AttrContext> env) {
- if (!analyzerModes.isEmpty() &&
- !env.info.isSpeculative &&
- TreeInfo.isStatement(tree)) {
+ if (env != null) {
JCStatement stmt = (JCStatement)tree;
analyze(stmt, env);
}
--- a/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java Mon Jun 12 12:25:40 2017 -0700
+++ b/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java Mon Jun 19 11:41:21 2017 +0200
@@ -718,8 +718,7 @@
/** Derived visitor method: attribute a statement or definition tree.
*/
public Type attribStat(JCTree tree, Env<AttrContext> env) {
- Env<AttrContext> analyzeEnv =
- env.dup(tree, env.info.dup(env.info.scope.dupUnshared(env.info.scope.owner)));
+ Env<AttrContext> analyzeEnv = analyzer.copyEnvIfNeeded(tree, env);
try {
return attribTree(tree, env, statInfo);
} finally {
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/lambda/LambdaConv28.java Mon Jun 19 11:41:21 2017 +0200
@@ -0,0 +1,32 @@
+/*
+ * @test /nodynamiccopyright/
+ * @bug 8181911
+ * @summary Verify that the analyzer does not affect ordinary compilation.
+ * @compile/ref=LambdaConv28.out -XDrawDiagnostics -XDfind=lambda LambdaConv28.java
+ */
+
+class LambdaConv28 {
+
+ public void test(A a) {
+ test(()-> {
+ return new I() {
+ public <T> void t() {
+ }
+ };
+ });
+ test(new A() {
+ public I get() {
+ return null;
+ }
+ });
+ }
+
+ public interface I {
+ public <T> void t();
+ }
+
+ public interface A {
+ public I get();
+ }
+
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/lambda/LambdaConv28.out Mon Jun 19 11:41:21 2017 +0200
@@ -0,0 +1,2 @@
+LambdaConv28.java:17:22: compiler.warn.potential.lambda.found
+1 warning