changeset 2642:f7f3f96999ba

8062747: Compiler error when anonymous class uses method with parametrized exception Summary: When inferring lambda's thrown types, avoid tracking variables that are not under the lambda to avoid crashes. Reviewed-by: vromero
author jlahoda
date Fri, 14 Nov 2014 20:27:08 +0100
parents 682a6c1aefd7
children 10e9228e77b0
files src/share/classes/com/sun/tools/javac/comp/Flow.java test/tools/javac/flow/T8062747.java
diffstat 2 files changed, 31 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/com/sun/tools/javac/comp/Flow.java	Fri Nov 14 09:48:02 2014 -0800
+++ b/src/share/classes/com/sun/tools/javac/comp/Flow.java	Fri Nov 14 20:27:08 2014 +0100
@@ -242,9 +242,15 @@
         Log.DiagnosticHandler diagHandler = new Log.DiscardDiagnosticHandler(log);
         try {
             new AssignAnalyzer() {
+                Scope enclosedSymbols = new Scope(env.enclClass.sym);
+                @Override
+                public void visitVarDef(JCVariableDecl tree) {
+                    enclosedSymbols.enter(tree.sym);
+                    super.visitVarDef(tree);
+                }
                 @Override
                 protected boolean trackable(VarSymbol sym) {
-                    return !env.info.scope.includes(sym) &&
+                    return enclosedSymbols.includes(sym) &&
                            sym.owner.kind == MTH;
                 }
             }.analyzeTree(env, that);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/tools/javac/flow/T8062747.java	Fri Nov 14 20:27:08 2014 +0100
@@ -0,0 +1,24 @@
+/**
+ * @test
+ * @bug 8062747
+ * @summary Avoiding an error for lambdas with thrown types inference inside an anonymous class.
+ * @compile T8062747.java
+ */
+public class T8062747 {
+
+    public interface Throwing<Y extends Exception> {
+        void canThrow() throws Y;
+    }
+
+    public static <Y extends Exception> void wrap(Throwing<Y> action) {
+    }
+
+    public static void invoke(String a) {
+        Runnable r = new Runnable() {
+            @Override
+            public void run() {
+                wrap(() -> System.out.println(a));
+            }
+        };
+    }
+}