From bb301f8aca239a2991d8e6bdc2161c898199bf5b Mon Sep 17 00:00:00 2001 From: Georgy Bronnikov Date: Wed, 11 Dec 2019 18:41:57 +0300 Subject: [PATCH] JVM_IR: fix wrong check in skipping captured vars --- .../kotlin/backend/common/lower/ClosureAnnotator.kt | 3 ++- .../testData/codegen/box/closures/localFunInInit.kt | 10 ++++++++++ .../kotlin/codegen/BlackBoxCodegenTestGenerated.java | 5 +++++ .../kotlin/codegen/LightAnalysisModeTestGenerated.java | 5 +++++ .../codegen/ir/FirBlackBoxCodegenTestGenerated.java | 5 +++++ .../codegen/ir/IrBlackBoxCodegenTestGenerated.java | 5 +++++ .../test/ir/semantics/IrJsCodegenBoxTestGenerated.java | 5 +++++ .../js/test/semantics/JsCodegenBoxTestGenerated.java | 5 +++++ 8 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 compiler/testData/codegen/box/closures/localFunInInit.kt diff --git a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/ClosureAnnotator.kt b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/ClosureAnnotator.kt index b60b2840bbd..7d3c16694c4 100644 --- a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/ClosureAnnotator.kt +++ b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/ClosureAnnotator.kt @@ -20,6 +20,7 @@ import org.jetbrains.kotlin.backend.common.ir.ir2string import org.jetbrains.kotlin.backend.common.peek import org.jetbrains.kotlin.backend.common.pop import org.jetbrains.kotlin.backend.common.push +import org.jetbrains.kotlin.descriptors.Visibilities import org.jetbrains.kotlin.ir.IrElement import org.jetbrains.kotlin.ir.declarations.* import org.jetbrains.kotlin.ir.expressions.IrFunctionAccessExpression @@ -189,7 +190,7 @@ class ClosureAnnotator(irFile: IrFile) { private fun processMemberAccess(declaration: IrDeclaration) { if (declaration.isLocal) { - if (declaration is IrSimpleFunction && declaration.parent is IrClass) { + if (declaration is IrSimpleFunction && declaration.visibility != Visibilities.LOCAL) { return } diff --git a/compiler/testData/codegen/box/closures/localFunInInit.kt b/compiler/testData/codegen/box/closures/localFunInInit.kt new file mode 100644 index 00000000000..0f81d73138b --- /dev/null +++ b/compiler/testData/codegen/box/closures/localFunInInit.kt @@ -0,0 +1,10 @@ +// IGNORE_BACKEND_FIR: JVM_IR +class A { + val result: String + init { + val flag = "OK" + fun getFlag(): String = flag + result = { getFlag() } () + } +} +fun box(): String = A().result diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java index 982be1fd81a..e63b36e3f2f 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java @@ -3966,6 +3966,11 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { runTest("compiler/testData/codegen/box/closures/localClassLambdaClosure.kt"); } + @TestMetadata("localFunInInit.kt") + public void testLocalFunInInit() throws Exception { + runTest("compiler/testData/codegen/box/closures/localFunInInit.kt"); + } + @TestMetadata("localFunctionInFunction.kt") public void testLocalFunctionInFunction() throws Exception { runTest("compiler/testData/codegen/box/closures/localFunctionInFunction.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java index 43857c3b40f..ceb50fab7df 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -3966,6 +3966,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes runTest("compiler/testData/codegen/box/closures/localClassLambdaClosure.kt"); } + @TestMetadata("localFunInInit.kt") + public void testLocalFunInInit() throws Exception { + runTest("compiler/testData/codegen/box/closures/localFunInInit.kt"); + } + @TestMetadata("localFunctionInFunction.kt") public void testLocalFunctionInFunction() throws Exception { runTest("compiler/testData/codegen/box/closures/localFunctionInFunction.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/ir/FirBlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/ir/FirBlackBoxCodegenTestGenerated.java index 5990373fadc..818dabe3363 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/ir/FirBlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/ir/FirBlackBoxCodegenTestGenerated.java @@ -3946,6 +3946,11 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT runTest("compiler/testData/codegen/box/closures/localClassLambdaClosure.kt"); } + @TestMetadata("localFunInInit.kt") + public void testLocalFunInInit() throws Exception { + runTest("compiler/testData/codegen/box/closures/localFunInInit.kt"); + } + @TestMetadata("localFunctionInFunction.kt") public void testLocalFunctionInFunction() throws Exception { runTest("compiler/testData/codegen/box/closures/localFunctionInFunction.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java index af8cf518445..17a810b2b91 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java @@ -3946,6 +3946,11 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes runTest("compiler/testData/codegen/box/closures/localClassLambdaClosure.kt"); } + @TestMetadata("localFunInInit.kt") + public void testLocalFunInInit() throws Exception { + runTest("compiler/testData/codegen/box/closures/localFunInInit.kt"); + } + @TestMetadata("localFunctionInFunction.kt") public void testLocalFunctionInFunction() throws Exception { runTest("compiler/testData/codegen/box/closures/localFunctionInFunction.kt"); diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java b/js/js.tests/test/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java index b72c26bcad0..f9631ffdc68 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java @@ -3291,6 +3291,11 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest { runTest("compiler/testData/codegen/box/closures/localClassLambdaClosure.kt"); } + @TestMetadata("localFunInInit.kt") + public void testLocalFunInInit() throws Exception { + runTest("compiler/testData/codegen/box/closures/localFunInInit.kt"); + } + @TestMetadata("localFunctionInFunction.kt") public void testLocalFunctionInFunction() throws Exception { runTest("compiler/testData/codegen/box/closures/localFunctionInFunction.kt"); diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java index 0307d176924..cd6d6491c13 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java @@ -3291,6 +3291,11 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest { runTest("compiler/testData/codegen/box/closures/localClassLambdaClosure.kt"); } + @TestMetadata("localFunInInit.kt") + public void testLocalFunInInit() throws Exception { + runTest("compiler/testData/codegen/box/closures/localFunInInit.kt"); + } + @TestMetadata("localFunctionInFunction.kt") public void testLocalFunctionInFunction() throws Exception { runTest("compiler/testData/codegen/box/closures/localFunctionInFunction.kt");