# HG changeset patch # User rfield # Date 1398924134 25200 # Node ID 44f3a7b2ac71092cc5031613606fa8f5e15d385f # Parent b31b4a2305952ee4d0b6fca322274b4b28d411fa 8029852: Bad code generated (VerifyError) when lambda instantiates enclosing local class and has captured variables 8029725: Lambda reference to containing local class causes javac infinite recursion Reviewed-by: vromero, jlahoda, dlsmith diff -r b31b4a230595 -r 44f3a7b2ac71 langtools/src/share/classes/com/sun/tools/javac/comp/LambdaToMethod.java --- a/langtools/src/share/classes/com/sun/tools/javac/comp/LambdaToMethod.java Wed Apr 30 18:19:23 2014 -0700 +++ b/langtools/src/share/classes/com/sun/tools/javac/comp/LambdaToMethod.java Wed Apr 30 23:02:14 2014 -0700 @@ -36,6 +36,7 @@ import com.sun.tools.javac.code.Symbol.ClassSymbol; import com.sun.tools.javac.code.Symbol.DynamicMethodSymbol; import com.sun.tools.javac.code.Symbol.MethodSymbol; +import com.sun.tools.javac.code.Symbol.TypeSymbol; import com.sun.tools.javac.code.Symbol.VarSymbol; import com.sun.tools.javac.code.Symtab; import com.sun.tools.javac.code.Type; @@ -50,8 +51,10 @@ import java.util.EnumMap; import java.util.HashMap; +import java.util.HashSet; import java.util.LinkedHashMap; import java.util.Map; +import java.util.Set; import static com.sun.tools.javac.comp.LambdaToMethod.LambdaSymbolKind.*; import static com.sun.tools.javac.code.Flags.*; @@ -1280,7 +1283,10 @@ @Override public void visitNewClass(JCNewClass tree) { - if (lambdaNewClassFilter(context(), tree)) { + TypeSymbol def = tree.type.tsym; + boolean inReferencedClass = currentlyInClass(def); + boolean isLocal = def.isLocal(); + if ((inReferencedClass && isLocal || lambdaNewClassFilter(context(), tree))) { TranslationContext localContext = context(); while (localContext != null) { if (localContext.tree.getTag() == LAMBDA) { @@ -1290,16 +1296,16 @@ localContext = localContext.prev; } } - if (context() != null && tree.type.tsym.owner.kind == MTH) { + if (context() != null && !inReferencedClass && isLocal) { LambdaTranslationContext lambdaContext = (LambdaTranslationContext)context(); - captureLocalClassDefs(tree.type.tsym, lambdaContext); + captureLocalClassDefs(def, lambdaContext); } super.visitNewClass(tree); } //where void captureLocalClassDefs(Symbol csym, final LambdaTranslationContext lambdaContext) { JCClassDecl localCDef = localClassDefs.get(csym); - if (localCDef != null && localCDef.pos < lambdaContext.tree.pos) { + if (localCDef != null && lambdaContext.freeVarProcessedLocalClasses.add(csym)) { BasicFreeVarCollector fvc = lower.new BasicFreeVarCollector() { @Override void addFreeVars(ClassSymbol c) { @@ -1325,6 +1331,18 @@ fvc.scan(localCDef); } } + //where + boolean currentlyInClass(Symbol csym) { + for (Frame frame : frameStack) { + if (frame.tree.hasTag(JCTree.Tag.CLASSDEF)) { + JCClassDecl cdef = (JCClassDecl) frame.tree; + if (cdef.sym == csym) { + return true; + } + } + } + return false; + } /** * Method references to local class constructors, may, if the local @@ -1750,6 +1768,11 @@ List syntheticParams; + /** + * to prevent recursion, track local classes processed + */ + final Set freeVarProcessedLocalClasses; + LambdaTranslationContext(JCLambda tree) { super(tree); Frame frame = frameStack.head; @@ -1779,6 +1802,8 @@ translatedSymbols.put(CAPTURED_VAR, new LinkedHashMap()); translatedSymbols.put(CAPTURED_THIS, new LinkedHashMap()); translatedSymbols.put(TYPE_VAR, new LinkedHashMap()); + + freeVarProcessedLocalClasses = new HashSet<>(); } /** diff -r b31b4a230595 -r 44f3a7b2ac71 langtools/test/tools/javac/lambda/LambdaLocalTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/langtools/test/tools/javac/lambda/LambdaLocalTest.java Wed Apr 30 23:02:14 2014 -0700 @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2014, 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 8029725 + * @summary Lambda reference to containing local class causes javac infinite recursion + * @author Robert Field + * @run main LambdaLocalTest + */ + +public class LambdaLocalTest { + interface F {void f();} + + static F f; + static StringBuffer sb = new StringBuffer(); + + static void assertEquals(Object val, Object expected) { + if (!val.equals(expected)) { + throw new AssertionError("expected '" + expected + "' got '" + val + "'"); + } + } + + public static void main(String[] args) { + class Local { + public Local() { + f = () -> new Local(); + sb.append("+"); + } + } + new Local(); + f.f(); + assertEquals(sb.toString(), "++"); + } +} diff -r b31b4a230595 -r 44f3a7b2ac71 langtools/test/tools/javac/lambda/LambdaOuterLocalTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/langtools/test/tools/javac/lambda/LambdaOuterLocalTest.java Wed Apr 30 23:02:14 2014 -0700 @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2014, 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 8029725 + * @summary Lambda reference to containing local class causes javac infinite recursion + * @author Robert Field + * @run main LambdaOuterLocalTest + */ + +public class LambdaOuterLocalTest { + interface F {void f();} + + static F f; + static StringBuffer sb = new StringBuffer(); + + static void assertEquals(Object val, Object expected) { + if (!val.equals(expected)) { + throw new AssertionError("expected '" + expected + "' got '" + val + "'"); + } + } + + public static void main(String[] args) { + class Local1 { + public Local1() { + class Local2 { + public Local2() { + f = () -> new Local1(); + sb.append("2"); + } + } + sb.append("1"); + new Local2(); + } + } + new Local1(); + f.f(); + assertEquals(sb.toString(), "1212"); + } +} diff -r b31b4a230595 -r 44f3a7b2ac71 langtools/test/tools/javac/lambda/SingleLocalTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/langtools/test/tools/javac/lambda/SingleLocalTest.java Wed Apr 30 23:02:14 2014 -0700 @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2014, 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 8029852 + * @summary Bad code generated (VerifyError) when lambda instantiates + * enclosing local class and has captured variables + */ +public class SingleLocalTest { + interface F {void f();} + + static F f; + + public static void main(String[] args) { + StringBuffer sb = new StringBuffer(); + class Local1 { + public Local1() { + f = () -> new Local1(); + sb.append("1"); + } + } + new Local1(); + f.f(); + String s = sb.toString(); + if (!s.equals("11")) { + throw new AssertionError("Expected '11' got '" + s + "'"); + } + } +}