From 2bd807bb4ee35ce47524d3b6fd51c555add1cddb Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Mon, 30 May 2016 11:13:39 +0300 Subject: [PATCH] KT-11960: add test for case of instantiating inner class of a local class --- .../localClasses/closureOfInnerLocalClass.kt | 28 +++++++++++++ .../closureOfLambdaInLocalClass.kt | 38 +++++++++++++++++ .../ownClosureOfInnerLocalClass.kt | 27 ++++++++++++ .../codegen/BlackBoxCodegenTestGenerated.java | 18 ++++++++ .../kotlin/js/test/semantics/ClosureTest.java | 4 ++ .../semantics/LocalClassesTestGenerated.java | 18 ++++++++ .../closure/cases/lambdaInLocalFun.kt | 41 +++++++++++++++++++ 7 files changed, 174 insertions(+) create mode 100644 compiler/testData/codegen/box/localClasses/closureOfInnerLocalClass.kt create mode 100644 compiler/testData/codegen/box/localClasses/closureOfLambdaInLocalClass.kt create mode 100644 compiler/testData/codegen/box/localClasses/ownClosureOfInnerLocalClass.kt create mode 100644 js/js.translator/testData/closure/cases/lambdaInLocalFun.kt diff --git a/compiler/testData/codegen/box/localClasses/closureOfInnerLocalClass.kt b/compiler/testData/codegen/box/localClasses/closureOfInnerLocalClass.kt new file mode 100644 index 00000000000..c01ad313420 --- /dev/null +++ b/compiler/testData/codegen/box/localClasses/closureOfInnerLocalClass.kt @@ -0,0 +1,28 @@ +// Enable for JVM backend when KT-8120 gets fixed +// TARGET_BACKEND: JS + +fun box(): String { + var log = "" + + var s: Any? = null + for (t in arrayOf("1", "2", "3")) { + class C() { + val y = t + + inner class D() { + fun copyOuter() = C() + } + } + + if (s == null) { + s = C() + } + + val c = (s as C).D().copyOuter() + log += c.y + } + + if (log != "111") return "fail: ${log}" + + return "OK" +} \ No newline at end of file diff --git a/compiler/testData/codegen/box/localClasses/closureOfLambdaInLocalClass.kt b/compiler/testData/codegen/box/localClasses/closureOfLambdaInLocalClass.kt new file mode 100644 index 00000000000..3cae7289280 --- /dev/null +++ b/compiler/testData/codegen/box/localClasses/closureOfLambdaInLocalClass.kt @@ -0,0 +1,38 @@ +fun box(): String { + var log = "" + + var s: Any? = null + for (t in arrayOf("1", "2", "3")) { + class A() { + fun foo() = { t } + } + + if (s == null) { + s = A() + } + + log += (s as A).foo()() + } + + if (log != "111") return "fail1: ${log}" + + s = null + log = "" + for (t in arrayOf("1", "2", "3")) { + class B() { + val y = t + + fun foo() = { y } + } + + if (s == null) { + s = B() + } + + log += (s as B).foo()() + } + + if (log != "111") return "fail2: ${log}" + + return "OK" +} \ No newline at end of file diff --git a/compiler/testData/codegen/box/localClasses/ownClosureOfInnerLocalClass.kt b/compiler/testData/codegen/box/localClasses/ownClosureOfInnerLocalClass.kt new file mode 100644 index 00000000000..5b004242049 --- /dev/null +++ b/compiler/testData/codegen/box/localClasses/ownClosureOfInnerLocalClass.kt @@ -0,0 +1,27 @@ +// TARGET_BACKEND: JVM +// Enable when KT-12566 gets fixed + +fun box(): String { + var log = "" + + var s: Any? = null + for (t in arrayOf("1", "2", "3")) { + class C() { + val y = t + + inner class D() { + fun foo() = "($y;$t)" + } + } + + if (s == null) { + s = C() + } + + log += (s as C).D().foo() + } + + if (log != "(1;1)(1;1)(1;1)") return "fail: ${log}" + + return "OK" +} \ No newline at end of file diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java index a1dad801d40..8302fc2bed2 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java @@ -7863,6 +7863,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class) public static class LocalClasses extends AbstractBlackBoxCodegenTest { + @TestMetadata("closureOfInnerLocalClass.kt") + public void ignoredClosureOfInnerLocalClass() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/localClasses/closureOfInnerLocalClass.kt"); + doTest(fileName); + } + @TestMetadata("closureWithSelfInstantiation.kt") public void ignoredClosureWithSelfInstantiation() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/localClasses/closureWithSelfInstantiation.kt"); @@ -7885,6 +7891,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { doTest(fileName); } + @TestMetadata("closureOfLambdaInLocalClass.kt") + public void testClosureOfLambdaInLocalClass() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/localClasses/closureOfLambdaInLocalClass.kt"); + doTest(fileName); + } + @TestMetadata("inExtensionFunction.kt") public void testInExtensionFunction() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/localClasses/inExtensionFunction.kt"); @@ -8005,6 +8017,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { doTest(fileName); } + @TestMetadata("ownClosureOfInnerLocalClass.kt") + public void testOwnClosureOfInnerLocalClass() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/localClasses/ownClosureOfInnerLocalClass.kt"); + doTest(fileName); + } + @TestMetadata("withclosure.kt") public void testWithclosure() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/localClasses/withclosure.kt"); diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/ClosureTest.java b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/ClosureTest.java index 1a9687502a1..d424c24ddc4 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/ClosureTest.java +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/ClosureTest.java @@ -187,4 +187,8 @@ public final class ClosureTest extends SingleFileTranslationTest { public void testClosureThisInLambdaInsideObject() throws Exception { checkFooBoxIsOk(); } + + public void testLambdaInLocalFun() throws Exception { + checkFooBoxIsOk(); + } } diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/LocalClassesTestGenerated.java b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/LocalClassesTestGenerated.java index 341842c3217..f874f34718d 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/LocalClassesTestGenerated.java +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/LocalClassesTestGenerated.java @@ -31,6 +31,12 @@ import java.util.regex.Pattern; @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class) public class LocalClassesTestGenerated extends AbstractLocalClassesTest { + @TestMetadata("ownClosureOfInnerLocalClass.kt") + public void ignoredOwnClosureOfInnerLocalClass() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/localClasses/ownClosureOfInnerLocalClass.kt"); + doTest(fileName); + } + public void testAllFilesPresentInLocalClasses() throws Exception { KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/localClasses"), Pattern.compile("^(.+)\\.kt$"), true); } @@ -47,6 +53,18 @@ public class LocalClassesTestGenerated extends AbstractLocalClassesTest { doTest(fileName); } + @TestMetadata("closureOfInnerLocalClass.kt") + public void testClosureOfInnerLocalClass() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/localClasses/closureOfInnerLocalClass.kt"); + doTest(fileName); + } + + @TestMetadata("closureOfLambdaInLocalClass.kt") + public void testClosureOfLambdaInLocalClass() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/localClasses/closureOfLambdaInLocalClass.kt"); + doTest(fileName); + } + @TestMetadata("closureWithSelfInstantiation.kt") public void testClosureWithSelfInstantiation() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/localClasses/closureWithSelfInstantiation.kt"); diff --git a/js/js.translator/testData/closure/cases/lambdaInLocalFun.kt b/js/js.translator/testData/closure/cases/lambdaInLocalFun.kt new file mode 100644 index 00000000000..2673fc55049 --- /dev/null +++ b/js/js.translator/testData/closure/cases/lambdaInLocalFun.kt @@ -0,0 +1,41 @@ +package foo + +fun box(): String { + var log = "" + + var s: Any? = null + for (t in arrayOf("1", "2", "3")) { + fun foo() = { + fun q() = { t } + q() + } + + if (s == null) { + s = foo() + } + + log += (s as (() -> (() -> String)))()() + } + + if (log != "111") return "fail1: ${log}" + + s = null + log = "" + for (t in arrayOf("1", "2", "3")) { + fun foo() = { + val y = t + fun q() = { y } + q() + } + + if (s == null) { + s = foo() + } + + log += (s as (() -> (() -> String)))()() + } + + if (log != "111") return "fail2: ${log}" + + return "OK" +} \ No newline at end of file