From 01cce59c35480f369cb2572c6369080fca7ba2fd Mon Sep 17 00:00:00 2001 From: Dmitry Petrov Date: Thu, 1 Jun 2017 15:21:39 +0300 Subject: [PATCH] Make sure lateinit top-level properties work in JVM BE --- .../lateinit/topLevel/accessorException.kt | 27 +++++++++ .../topLevel/accessorForTopLevelLateinit.kt | 13 +++++ .../lateinit/topLevel/topLevelLateinit.kt | 8 +++ .../topLevel/uninitializedMemberAccess.kt | 20 +++++++ .../lateinit/topLevel/uninitializedRead.kt | 20 +++++++ .../ir/IrBlackBoxCodegenTestGenerated.java | 39 +++++++++++++ .../codegen/BlackBoxCodegenTestGenerated.java | 39 +++++++++++++ .../LightAnalysisModeTestGenerated.java | 39 +++++++++++++ .../semantics/JsCodegenBoxTestGenerated.java | 57 +++++++++++++++++++ 9 files changed, 262 insertions(+) create mode 100644 compiler/testData/codegen/box/properties/lateinit/topLevel/accessorException.kt create mode 100644 compiler/testData/codegen/box/properties/lateinit/topLevel/accessorForTopLevelLateinit.kt create mode 100644 compiler/testData/codegen/box/properties/lateinit/topLevel/topLevelLateinit.kt create mode 100644 compiler/testData/codegen/box/properties/lateinit/topLevel/uninitializedMemberAccess.kt create mode 100644 compiler/testData/codegen/box/properties/lateinit/topLevel/uninitializedRead.kt diff --git a/compiler/testData/codegen/box/properties/lateinit/topLevel/accessorException.kt b/compiler/testData/codegen/box/properties/lateinit/topLevel/accessorException.kt new file mode 100644 index 00000000000..18c498ecbd7 --- /dev/null +++ b/compiler/testData/codegen/box/properties/lateinit/topLevel/accessorException.kt @@ -0,0 +1,27 @@ +// WITH_REFLECT +// IGNORE_BACKEND: JS, NATIVE +// FILE: lateinit.kt +private lateinit var s: String + +object C { + fun setS(value: String) { s = value } + fun getS() = s +} + +// FILE: test.kt +import kotlin.UninitializedPropertyAccessException + +fun box(): String { + var str2: String = "" + try { + str2 = C.getS() + return "Should throw an exception" + } + catch (e: UninitializedPropertyAccessException) { + return "OK" + } + catch (e: Throwable) { + return "Unexpected exception: ${e::class.qualifiedName}" + } + +} \ No newline at end of file diff --git a/compiler/testData/codegen/box/properties/lateinit/topLevel/accessorForTopLevelLateinit.kt b/compiler/testData/codegen/box/properties/lateinit/topLevel/accessorForTopLevelLateinit.kt new file mode 100644 index 00000000000..251409c9e5c --- /dev/null +++ b/compiler/testData/codegen/box/properties/lateinit/topLevel/accessorForTopLevelLateinit.kt @@ -0,0 +1,13 @@ +// FILE: lateinit.kt +private lateinit var s: String + +object C { + fun setS(value: String) { s = value } + fun getS() = s +} + +// FILE: test.kt +fun box(): String { + C.setS("OK") + return C.getS() +} \ No newline at end of file diff --git a/compiler/testData/codegen/box/properties/lateinit/topLevel/topLevelLateinit.kt b/compiler/testData/codegen/box/properties/lateinit/topLevel/topLevelLateinit.kt new file mode 100644 index 00000000000..9a07c673ee0 --- /dev/null +++ b/compiler/testData/codegen/box/properties/lateinit/topLevel/topLevelLateinit.kt @@ -0,0 +1,8 @@ +lateinit var ok: String + +fun box(): String { + run { + ok = "OK" + } + return ok +} \ No newline at end of file diff --git a/compiler/testData/codegen/box/properties/lateinit/topLevel/uninitializedMemberAccess.kt b/compiler/testData/codegen/box/properties/lateinit/topLevel/uninitializedMemberAccess.kt new file mode 100644 index 00000000000..30c7ec622bf --- /dev/null +++ b/compiler/testData/codegen/box/properties/lateinit/topLevel/uninitializedMemberAccess.kt @@ -0,0 +1,20 @@ +// WITH_REFLECT +// IGNORE_BACKEND: JS, NATIVE + +import kotlin.UninitializedPropertyAccessException + +lateinit var str: String + +fun box(): String { + var i: Int = 0 + try { + i = str.length + return "Should throw an exception" + } + catch (e: UninitializedPropertyAccessException) { + return "OK" + } + catch (e: Throwable) { + return "Unexpected exception: ${e::class.qualifiedName}" + } +} \ No newline at end of file diff --git a/compiler/testData/codegen/box/properties/lateinit/topLevel/uninitializedRead.kt b/compiler/testData/codegen/box/properties/lateinit/topLevel/uninitializedRead.kt new file mode 100644 index 00000000000..594dd3ccd9d --- /dev/null +++ b/compiler/testData/codegen/box/properties/lateinit/topLevel/uninitializedRead.kt @@ -0,0 +1,20 @@ +// WITH_REFLECT +// IGNORE_BACKEND: JS, NATIVE + +import kotlin.UninitializedPropertyAccessException + +lateinit var str: String + +fun box(): String { + var str2: String = "" + try { + str2 = str + return "Should throw an exception" + } + catch (e: UninitializedPropertyAccessException) { + return "OK" + } + catch (e: Throwable) { + return "Unexpected exception: ${e::class.qualifiedName}" + } +} \ No newline at end of file diff --git a/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java b/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java index 55352f90ef4..f95735a3585 100644 --- a/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java +++ b/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java @@ -13419,6 +13419,45 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes doTest(fileName); } } + + @TestMetadata("compiler/testData/codegen/box/properties/lateinit/topLevel") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class TopLevel extends AbstractIrBlackBoxCodegenTest { + @TestMetadata("accessorException.kt") + public void testAccessorException() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/properties/lateinit/topLevel/accessorException.kt"); + doTest(fileName); + } + + @TestMetadata("accessorForTopLevelLateinit.kt") + public void testAccessorForTopLevelLateinit() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/properties/lateinit/topLevel/accessorForTopLevelLateinit.kt"); + doTest(fileName); + } + + public void testAllFilesPresentInTopLevel() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/properties/lateinit/topLevel"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JVM, true); + } + + @TestMetadata("topLevelLateinit.kt") + public void testTopLevelLateinit() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/properties/lateinit/topLevel/topLevelLateinit.kt"); + doTest(fileName); + } + + @TestMetadata("uninitializedMemberAccess.kt") + public void testUninitializedMemberAccess() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/properties/lateinit/topLevel/uninitializedMemberAccess.kt"); + doTest(fileName); + } + + @TestMetadata("uninitializedRead.kt") + public void testUninitializedRead() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/properties/lateinit/topLevel/uninitializedRead.kt"); + doTest(fileName); + } + } } } diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java index 88efb4e3ab2..9e04bcd6821 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java @@ -13419,6 +13419,45 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { doTest(fileName); } } + + @TestMetadata("compiler/testData/codegen/box/properties/lateinit/topLevel") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class TopLevel extends AbstractBlackBoxCodegenTest { + @TestMetadata("accessorException.kt") + public void testAccessorException() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/properties/lateinit/topLevel/accessorException.kt"); + doTest(fileName); + } + + @TestMetadata("accessorForTopLevelLateinit.kt") + public void testAccessorForTopLevelLateinit() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/properties/lateinit/topLevel/accessorForTopLevelLateinit.kt"); + doTest(fileName); + } + + public void testAllFilesPresentInTopLevel() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/properties/lateinit/topLevel"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JVM, true); + } + + @TestMetadata("topLevelLateinit.kt") + public void testTopLevelLateinit() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/properties/lateinit/topLevel/topLevelLateinit.kt"); + doTest(fileName); + } + + @TestMetadata("uninitializedMemberAccess.kt") + public void testUninitializedMemberAccess() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/properties/lateinit/topLevel/uninitializedMemberAccess.kt"); + doTest(fileName); + } + + @TestMetadata("uninitializedRead.kt") + public void testUninitializedRead() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/properties/lateinit/topLevel/uninitializedRead.kt"); + doTest(fileName); + } + } } } diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java index f97e0089bf6..6add202f7b8 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -13419,6 +13419,45 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes doTest(fileName); } } + + @TestMetadata("compiler/testData/codegen/box/properties/lateinit/topLevel") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class TopLevel extends AbstractLightAnalysisModeTest { + @TestMetadata("accessorException.kt") + public void testAccessorException() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/properties/lateinit/topLevel/accessorException.kt"); + doTest(fileName); + } + + @TestMetadata("accessorForTopLevelLateinit.kt") + public void testAccessorForTopLevelLateinit() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/properties/lateinit/topLevel/accessorForTopLevelLateinit.kt"); + doTest(fileName); + } + + public void testAllFilesPresentInTopLevel() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/properties/lateinit/topLevel"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JVM, true); + } + + @TestMetadata("topLevelLateinit.kt") + public void testTopLevelLateinit() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/properties/lateinit/topLevel/topLevelLateinit.kt"); + doTest(fileName); + } + + @TestMetadata("uninitializedMemberAccess.kt") + public void testUninitializedMemberAccess() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/properties/lateinit/topLevel/uninitializedMemberAccess.kt"); + doTest(fileName); + } + + @TestMetadata("uninitializedRead.kt") + public void testUninitializedRead() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/properties/lateinit/topLevel/uninitializedRead.kt"); + doTest(fileName); + } + } } } 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 e4f77c7acbe..5dd9601b228 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 @@ -15069,6 +15069,63 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest { throw new AssertionError("Looks like this test can be unmuted. Remove IGNORE_BACKEND directive for that."); } } + + @TestMetadata("compiler/testData/codegen/box/properties/lateinit/topLevel") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class TopLevel extends AbstractJsCodegenBoxTest { + @TestMetadata("accessorException.kt") + public void testAccessorException() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/properties/lateinit/topLevel/accessorException.kt"); + try { + doTest(fileName); + } + catch (Throwable ignore) { + return; + } + throw new AssertionError("Looks like this test can be unmuted. Remove IGNORE_BACKEND directive for that."); + } + + @TestMetadata("accessorForTopLevelLateinit.kt") + public void testAccessorForTopLevelLateinit() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/properties/lateinit/topLevel/accessorForTopLevelLateinit.kt"); + doTest(fileName); + } + + public void testAllFilesPresentInTopLevel() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/properties/lateinit/topLevel"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JS, true); + } + + @TestMetadata("topLevelLateinit.kt") + public void testTopLevelLateinit() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/properties/lateinit/topLevel/topLevelLateinit.kt"); + doTest(fileName); + } + + @TestMetadata("uninitializedMemberAccess.kt") + public void testUninitializedMemberAccess() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/properties/lateinit/topLevel/uninitializedMemberAccess.kt"); + try { + doTest(fileName); + } + catch (Throwable ignore) { + return; + } + throw new AssertionError("Looks like this test can be unmuted. Remove IGNORE_BACKEND directive for that."); + } + + @TestMetadata("uninitializedRead.kt") + public void testUninitializedRead() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/properties/lateinit/topLevel/uninitializedRead.kt"); + try { + doTest(fileName); + } + catch (Throwable ignore) { + return; + } + throw new AssertionError("Looks like this test can be unmuted. Remove IGNORE_BACKEND directive for that."); + } + } } }