changeset 31296:9a8e44b62c77

8076538: Verify error at runtime due to incorrect classification of a lambda as being instance capturing Reviewed-by: mcimadamore
author sadayapalam
date Tue, 23 Jun 2015 17:46:23 +0530
parents 43d0179ee9de
children 86fe1414f12c
files langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/LambdaToMethod.java langtools/test/tools/javac/lambda/NestedCapture04.java
diffstat 2 files changed, 142 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/LambdaToMethod.java	Wed Jul 05 20:38:50 2017 +0200
+++ b/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/LambdaToMethod.java	Tue Jun 23 17:46:23 2015 +0530
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2010, 2014, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2010, 2015, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -1197,10 +1197,13 @@
                         //if a class is defined within a lambda, the lambda must capture
                         //its enclosing instance (if any)
                         TranslationContext<?> localContext = context();
-                        while (localContext != null) {
-                            if (localContext.tree.getTag() == LAMBDA) {
+                        final TypeSymbol outerInstanceSymbol = tree.sym.type.getEnclosingType().tsym;
+                        while (localContext != null && !localContext.owner.isStatic()) {
+                            if (localContext.tree.hasTag(LAMBDA)) {
+                                JCTree block = capturedDecl(localContext.depth, outerInstanceSymbol);
+                                if (block == null) break;
                                 ((LambdaTranslationContext)localContext)
-                                        .addSymbol(tree.sym.type.getEnclosingType().tsym, CAPTURED_THIS);
+                                        .addSymbol(outerInstanceSymbol, CAPTURED_THIS);
                             }
                             localContext = localContext.prev;
                         }
@@ -1236,7 +1239,7 @@
                     }
                 } else if (tree.sym.owner.kind == TYP) {
                     TranslationContext<?> localContext = context();
-                    while (localContext != null) {
+                    while (localContext != null  && !localContext.owner.isStatic()) {
                         if (localContext.tree.hasTag(LAMBDA)) {
                             JCTree block = capturedDecl(localContext.depth, tree.sym);
                             if (block == null) break;
@@ -1312,10 +1315,15 @@
             boolean isLocal = def.isLocal();
             if ((inReferencedClass && isLocal || lambdaNewClassFilter(context(), tree))) {
                 TranslationContext<?> localContext = context();
-                while (localContext != null) {
-                    if (localContext.tree.getTag() == LAMBDA) {
+                final TypeSymbol outerInstanceSymbol = tree.type.getEnclosingType().tsym;
+                while (localContext != null  && !localContext.owner.isStatic()) {
+                    if (localContext.tree.hasTag(LAMBDA)) {
+                        if (outerInstanceSymbol != null) {
+                            JCTree block = capturedDecl(localContext.depth, outerInstanceSymbol);
+                            if (block == null) break;
+                        }
                         ((LambdaTranslationContext)localContext)
-                                .addSymbol(tree.type.getEnclosingType().tsym, CAPTURED_THIS);
+                                .addSymbol(outerInstanceSymbol, CAPTURED_THIS);
                     }
                     localContext = localContext.prev;
                 }
@@ -1404,7 +1412,7 @@
                 // A select of this or super means, if we are in a lambda,
                 // we much have an instance context
                 TranslationContext<?> localContext = context();
-                while (localContext != null) {
+                while (localContext != null  && !localContext.owner.isStatic()) {
                     if (localContext.tree.hasTag(LAMBDA)) {
                         JCClassDecl clazz = (JCClassDecl)capturedDecl(localContext.depth, tree.sym);
                         if (clazz == null) break;
@@ -1579,7 +1587,7 @@
                 switch (block.tree.getTag()) {
                     case CLASSDEF:
                         ClassSymbol clazz = ((JCClassDecl)block.tree).sym;
-                        if (sym.isMemberOf(clazz, types)) {
+                        if (clazz.isSubClass(sym, types) || sym.isMemberOf(clazz, types)) {
                             return currentDepth > depth ? null : block.tree;
                         }
                         break;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/lambda/NestedCapture04.java	Tue Jun 23 17:46:23 2015 +0530
@@ -0,0 +1,124 @@
+/*
+ * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ * @test
+ * @bug 8076538
+ * @summary Verify error at runtime due to incorrect classification of a lambda as being instance capturing
+ * @run main NestedCapture04
+ */
+public class NestedCapture04 {
+
+    public static interface Ftype {
+        int get(int v);
+    }
+
+    public static class A {
+        static int counter = 0;
+    }
+    public static Ftype x0;
+    public static void main(String[] args) throws Throwable {
+        doit();
+    }
+    public static Object doit() throws Throwable {
+        Ftype x0_ =
+        (int y0) -> {
+    A.counter++;
+    Ftype x1 = (int y1) -> {
+        A.counter++;
+        class Cltype2 {
+            Cltype2 meth(Cltype2 w) {
+                A.counter++;
+                class Cltype3 {
+                    class Inclass3 {
+                        public int iv;
+                        Inclass3() { iv = 0; }
+                        Inclass3 clmeth(Inclass3 a) {
+                            A.counter++;
+                            class Cltype4 {
+                                Cltype4 (Cltype4 z) {
+                                    Ftype x5 = (int y5) -> {
+                                        A.counter++;
+                                        class Cltype6 {
+                                            Cltype6 meth(Cltype6 w) {
+                                                A.counter++;
+                                                class Cltype7 {
+                                                    class Inclass7 {
+                                                        public int iv;
+                                                        Inclass7() { iv = 0; }
+                                                        Inclass7 clmeth(Inclass7 a) {
+                                                            A.counter++;
+                                                            class Cltype8 {
+                                                                Cltype8 (Cltype8 z) {
+                                                                    Ftype x9 = (int y9) -> {
+                                                                        A.counter++;
+                                                                        return y9;
+                                                                    };
+                                                                    x9.get(2);
+                                                                    if ( z == null) {
+                                                                        A.counter++;
+                                                                        return;
+                                                                    }
+                                                                    A.counter+=100;
+                                                                }
+                                                            }
+                                                            Cltype8 v = new Cltype8(null);
+                                                            return a;
+                                                        }
+                                                    }
+                                                }
+                                                Cltype7.Inclass7 c = new Cltype7().new Inclass7();
+                                                c.clmeth((Cltype7.Inclass7)null);
+                                                return w;
+                                            }
+                                        }
+                                        Cltype6 v = new Cltype6().meth(new Cltype6());
+                                        return y5;
+                                    };
+                                    x5.get(2);
+                                    if ( z == null) {
+                                        A.counter++;
+                                        return;
+                                    }
+                                    A.counter+=100;
+                                }
+                            }
+                            Cltype4 v = new Cltype4(null);
+                            return a;
+                        }
+                    }
+                }
+                Cltype3.Inclass3 c = new Cltype3().new Inclass3();
+                c.clmeth((Cltype3.Inclass3)null);
+                return w;
+            }
+        }
+        Cltype2 v = new Cltype2().meth(new Cltype2());
+        return y1;
+    };
+    x1.get(2);
+    return y0;
+};
+        return x0 = x0_;
+    }
+}