changeset 2235:1b91796976f6

8020147: Spurious errors when compiling nested stuck lambdas
author mcimadamore
date Wed, 10 Jul 2013 11:42:06 +0100
parents 2059cf03d755
children 1f469f8884c3
files src/share/classes/com/sun/tools/javac/comp/Attr.java src/share/classes/com/sun/tools/javac/comp/DeferredAttr.java src/share/classes/com/sun/tools/javac/comp/Flow.java test/tools/javac/lambda/8020147/T8020147.java test/tools/javac/lambda/8020147/T8020147.out
diffstat 5 files changed, 77 insertions(+), 29 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/com/sun/tools/javac/comp/Attr.java	Thu Jul 04 16:39:45 2013 +0100
+++ b/src/share/classes/com/sun/tools/javac/comp/Attr.java	Wed Jul 10 11:42:06 2013 +0100
@@ -475,31 +475,7 @@
         static final long serialVersionUID = -6924771130405446405L;
         private Env<AttrContext> env;
         private BreakAttr(Env<AttrContext> env) {
-            this.env = copyEnv(env);
-        }
-
-        private Env<AttrContext> copyEnv(Env<AttrContext> env) {
-            Env<AttrContext> newEnv =
-                    env.dup(env.tree, env.info.dup(copyScope(env.info.scope)));
-            if (newEnv.outer != null) {
-                newEnv.outer = copyEnv(newEnv.outer);
-            }
-            return newEnv;
-        }
-
-        private Scope copyScope(Scope sc) {
-            Scope newScope = new Scope(sc.owner);
-            List<Symbol> elemsList = List.nil();
-            while (sc != null) {
-                for (Scope.Entry e = sc.elems ; e != null ; e = e.sibling) {
-                    elemsList = elemsList.prepend(e.sym);
-                }
-                sc = sc.next;
-            }
-            for (Symbol s : elemsList) {
-                newScope.enter(s);
-            }
-            return newScope;
+            this.env = env;
         }
     }
 
@@ -598,7 +574,7 @@
             tree.accept(this);
             if (tree == breakTree &&
                     resultInfo.checkContext.deferredAttrContext().mode == AttrMode.CHECK) {
-                throw new BreakAttr(env);
+                throw new BreakAttr(copyEnv(env));
             }
             return result;
         } catch (CompletionFailure ex) {
@@ -609,6 +585,30 @@
             this.resultInfo = prevResult;
         }
     }
+    
+    Env<AttrContext> copyEnv(Env<AttrContext> env) {
+        Env<AttrContext> newEnv =
+                env.dup(env.tree, env.info.dup(copyScope(env.info.scope)));
+        if (newEnv.outer != null) {
+            newEnv.outer = copyEnv(newEnv.outer);
+        }
+        return newEnv;
+    }
+
+    Scope copyScope(Scope sc) {
+        Scope newScope = new Scope(sc.owner);
+        List<Symbol> elemsList = List.nil();
+        while (sc != null) {
+            for (Scope.Entry e = sc.elems ; e != null ; e = e.sibling) {
+                elemsList = elemsList.prepend(e.sym);
+            }
+            sc = sc.next;
+        }
+        for (Symbol s : elemsList) {
+            newScope.enter(s);
+        }
+        return newScope;
+    }
 
     /** Derived visitor method: attribute an expression tree.
      */
@@ -2423,7 +2423,7 @@
             boolean isSpeculativeRound =
                     resultInfo.checkContext.deferredAttrContext().mode == DeferredAttr.AttrMode.SPECULATIVE;
 
-            postAttr(that);
+            preFlow(that);
             flow.analyzeLambda(env, that, make, isSpeculativeRound);
 
             checkLambdaCompatible(that, lambdaType, resultInfo.checkContext, isSpeculativeRound);
@@ -2445,6 +2445,21 @@
         }
     }
     //where
+        void preFlow(JCLambda tree) {
+            new PostAttrAnalyzer() {
+                @Override
+                public void scan(JCTree tree) {
+                    if (tree == null ||
+                            (tree.type != null &&
+                            tree.type == Type.stuckType)) {
+                        //don't touch stuck expressions!
+                        return;
+                    }
+                    super.scan(tree);
+                }
+            }.scan(tree);
+        }
+    
         Types.MapVisitor<DiagnosticPosition> targetChecker = new Types.MapVisitor<DiagnosticPosition>() {
 
             @Override
--- a/src/share/classes/com/sun/tools/javac/comp/DeferredAttr.java	Thu Jul 04 16:39:45 2013 +0100
+++ b/src/share/classes/com/sun/tools/javac/comp/DeferredAttr.java	Wed Jul 10 11:42:06 2013 +0100
@@ -127,7 +127,7 @@
         DeferredType(JCExpression tree, Env<AttrContext> env) {
             super(DEFERRED, null);
             this.tree = tree;
-            this.env = env.dup(tree, env.info.dup());
+            this.env = attr.copyEnv(env);
             this.speculativeCache = new SpeculativeCache();
         }
 
@@ -258,7 +258,7 @@
     DeferredTypeCompleter dummyCompleter = new DeferredTypeCompleter() {
         public Type complete(DeferredType dt, ResultInfo resultInfo, DeferredAttrContext deferredAttrContext) {
             Assert.check(deferredAttrContext.mode == AttrMode.CHECK);
-            return dt.tree.type = Type.noType;
+            return dt.tree.type = Type.stuckType;
         }
     };
 
--- a/src/share/classes/com/sun/tools/javac/comp/Flow.java	Thu Jul 04 16:39:45 2013 +0100
+++ b/src/share/classes/com/sun/tools/javac/comp/Flow.java	Wed Jul 10 11:42:06 2013 +0100
@@ -373,6 +373,15 @@
         boolean resolveBreaks(JCTree tree, ListBuffer<P> oldPendingExits) {
             return resolveJump(tree, oldPendingExits, JumpKind.BREAK);
         }
+
+        @Override
+        public void scan(JCTree tree) {
+            if (tree != null && (
+                    tree.type == null ||
+                    tree.type != Type.stuckType)) {
+                super.scan(tree);
+            }
+        }
     }
 
     /**
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/tools/javac/lambda/8020147/T8020147.java	Wed Jul 10 11:42:06 2013 +0100
@@ -0,0 +1,19 @@
+/*
+ * @test /nodynamiccopyright/
+ * @bug 8020147
+ * @summary Spurious errors when compiling nested stuck lambdas
+ * @compile/fail/ref=T8020147.out -Werror -Xlint:cast -XDrawDiagnostics T8020147.java
+ */
+class T8020147 {
+    interface Function<X, Y> { 
+        Y apply(X x); 
+    }
+
+    <T> void g(Function<String, T> f) { } 
+    <U> String m(U u, Function<U, U> fuu) { return null; } 
+
+    void test() { 
+        g(x->m("", i->(String)i)); 
+        g(x->m("", i->(String)x)); 
+    } 
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/tools/javac/lambda/8020147/T8020147.out	Wed Jul 10 11:42:06 2013 +0100
@@ -0,0 +1,5 @@
+T8020147.java:16:23: compiler.warn.redundant.cast: java.lang.String
+T8020147.java:17:23: compiler.warn.redundant.cast: java.lang.String
+- compiler.err.warnings.and.werror
+1 error
+2 warnings