From dae0bda9d0096c25d6378561ab2d09df05f381cf Mon Sep 17 00:00:00 2001 From: Archie Cobbs Date: Fri, 14 Jun 2024 14:53:05 +0000 Subject: [PATCH] 8334252: Verifier error for lambda declared in early construction context Reviewed-by: mcimadamore --- .../sun/tools/javac/comp/LambdaToMethod.java | 24 +++++----- .../javac/SuperInit/LambdaOuterCapture.java | 46 +++++++++++++++++++ 2 files changed, 57 insertions(+), 13 deletions(-) create mode 100644 test/langtools/tools/javac/SuperInit/LambdaOuterCapture.java diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/LambdaToMethod.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/LambdaToMethod.java index 5b70a376f2a..7e5dcee0e8d 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/LambdaToMethod.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/LambdaToMethod.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2010, 2022, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2010, 2024, 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 @@ -1239,7 +1239,7 @@ public class LambdaToMethod extends TreeTranslator { private int lambdaCount = 0; /** - * List of types undergoing construction via explicit constructor chaining. + * List of types undergoing construction, i.e., in an early construction context. */ private List typesUnderConstruction; @@ -1280,15 +1280,10 @@ public class LambdaToMethod extends TreeTranslator { @Override public void visitApply(JCMethodInvocation tree) { - List previousNascentTypes = typesUnderConstruction; - try { - Name methName = TreeInfo.name(tree.meth); - if (methName == names._this || methName == names._super) { - typesUnderConstruction = typesUnderConstruction.prepend(currentClass()); - } - super.visitApply(tree); - } finally { - typesUnderConstruction = previousNascentTypes; + super.visitApply(tree); + if (TreeInfo.isConstructorCall(tree)) { + Assert.check(typesUnderConstruction.head == currentClass()); + typesUnderConstruction = typesUnderConstruction.tail; // end of early construction context } } // where @@ -1439,13 +1434,16 @@ public class LambdaToMethod extends TreeTranslator { @Override public void visitMethodDef(JCMethodDecl tree) { + List prevTypesUnderConstruction = typesUnderConstruction; List prevStack = frameStack; try { + if (TreeInfo.isConstructor(tree)) // start early construction context (Object() notwithstanding) + typesUnderConstruction = typesUnderConstruction.prepend(currentClass()); frameStack = frameStack.prepend(new Frame(tree)); super.visitMethodDef(tree); - } - finally { + } finally { frameStack = prevStack; + typesUnderConstruction = prevTypesUnderConstruction; } } diff --git a/test/langtools/tools/javac/SuperInit/LambdaOuterCapture.java b/test/langtools/tools/javac/SuperInit/LambdaOuterCapture.java new file mode 100644 index 00000000000..092e2916c69 --- /dev/null +++ b/test/langtools/tools/javac/SuperInit/LambdaOuterCapture.java @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2024, 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 8194743 + * @summary Test lambda declared in early construction context + * @enablePreview + */ + +public class LambdaOuterCapture { + + public class Inner { + + public Inner() { + Runnable r = () -> System.out.println(LambdaOuterCapture.this); + this(r); + } + + public Inner(Runnable r) { + } + } + + public static void main(String[] args) { + new LambdaOuterCapture().new Inner(); + } +}