diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java index b8cbed99545..0da7f511d8e 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java @@ -2084,10 +2084,19 @@ public class ExpressionCodegen extends KtVisitor impleme couldUseDirectAccessToProperty(propertyDescriptor, false, isDelegatedProperty, context, state.getShouldInlineConstVals()); if (fieldAccessorKind == AccessorKind.LATEINIT_INTRINSIC) { - skipPropertyAccessors = !isPrivateProperty || context.getClassOrPackageParentContext() == backingFieldContext; + skipPropertyAccessors = + (!isPrivateProperty || context.getClassOrPackageParentContext() == backingFieldContext) && + !isBackingFieldMovedFromCompanion; if (!skipPropertyAccessors) { - propertyDescriptor = backingFieldContext.getAccessor(propertyDescriptor, fieldAccessorKind, delegateType, superCallTarget); + if (isBackingFieldMovedFromCompanion && context.getContextDescriptor() instanceof AccessorForPropertyBackingField) { + propertyDescriptor = (PropertyDescriptor) backingFieldContext.getParentContext() + .getAccessor(propertyDescriptor, AccessorKind.IN_CLASS_COMPANION, delegateType, superCallTarget); + } + else { + propertyDescriptor = + backingFieldContext.getAccessor(propertyDescriptor, fieldAccessorKind, delegateType, superCallTarget); + } } ownerDescriptor = propertyDescriptor; } diff --git a/compiler/testData/codegen/box/properties/lateinit/isInitializedAndDeinitialize/companionObjectField.kt b/compiler/testData/codegen/box/properties/lateinit/isInitializedAndDeinitialize/companionObjectField.kt new file mode 100644 index 00000000000..abda42f1bed --- /dev/null +++ b/compiler/testData/codegen/box/properties/lateinit/isInitializedAndDeinitialize/companionObjectField.kt @@ -0,0 +1,41 @@ +// WITH_RUNTIME + +class Class { + companion object { + lateinit var public: String + private lateinit var private: String + + fun test() { + if (::public.isInitialized) throw AssertionError("Fail 1") + public = "OK" + if (public != "OK") throw AssertionError("Fail 2") + + if (::private.isInitialized) throw AssertionError("Fail 3") + private = "OK" + if (private != "OK") throw AssertionError("Fail 4") + } + } +} + +class Interface { + companion object { + lateinit var public: String + private lateinit var private: String + + fun test() { + if (::public.isInitialized) throw AssertionError("Fail 5") + public = "OK" + if (public != "OK") throw AssertionError("Fail 6") + + if (::private.isInitialized) throw AssertionError("Fail 7") + private = "OK" + if (private != "OK") throw AssertionError("Fail 8") + } + } +} + +fun box(): String { + Class.test() + Interface.test() + return "OK" +} diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java index ce7f10fa67b..0897758fc34 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java @@ -17860,6 +17860,11 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/properties/lateinit/isInitializedAndDeinitialize"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JVM, true); } + @TestMetadata("companionObjectField.kt") + public void testCompanionObjectField() throws Exception { + runTest("compiler/testData/codegen/box/properties/lateinit/isInitializedAndDeinitialize/companionObjectField.kt"); + } + @TestMetadata("emptyLhs.kt") public void testEmptyLhs() throws Exception { runTest("compiler/testData/codegen/box/properties/lateinit/isInitializedAndDeinitialize/emptyLhs.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java index d16f7494c96..6163dec3437 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -17860,6 +17860,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/properties/lateinit/isInitializedAndDeinitialize"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JVM, true); } + @TestMetadata("companionObjectField.kt") + public void testCompanionObjectField() throws Exception { + runTest("compiler/testData/codegen/box/properties/lateinit/isInitializedAndDeinitialize/companionObjectField.kt"); + } + @TestMetadata("emptyLhs.kt") public void testEmptyLhs() throws Exception { runTest("compiler/testData/codegen/box/properties/lateinit/isInitializedAndDeinitialize/emptyLhs.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java index 907c0cea537..eed2d8fee65 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java @@ -17865,6 +17865,11 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/properties/lateinit/isInitializedAndDeinitialize"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JVM_IR, true); } + @TestMetadata("companionObjectField.kt") + public void testCompanionObjectField() throws Exception { + runTest("compiler/testData/codegen/box/properties/lateinit/isInitializedAndDeinitialize/companionObjectField.kt"); + } + @TestMetadata("emptyLhs.kt") public void testEmptyLhs() throws Exception { runTest("compiler/testData/codegen/box/properties/lateinit/isInitializedAndDeinitialize/emptyLhs.kt"); diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/IrJsCodegenBoxTestGenerated.java b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/IrJsCodegenBoxTestGenerated.java index a26d30a1816..12746b3990e 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/IrJsCodegenBoxTestGenerated.java +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/IrJsCodegenBoxTestGenerated.java @@ -15350,6 +15350,11 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest { KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/properties/lateinit/isInitializedAndDeinitialize"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JS_IR, true); } + @TestMetadata("companionObjectField.kt") + public void testCompanionObjectField() throws Exception { + runTest("compiler/testData/codegen/box/properties/lateinit/isInitializedAndDeinitialize/companionObjectField.kt"); + } + @TestMetadata("emptyLhs.kt") public void testEmptyLhs() throws Exception { runTest("compiler/testData/codegen/box/properties/lateinit/isInitializedAndDeinitialize/emptyLhs.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 48ffe3a9232..d455159de2f 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 @@ -16410,6 +16410,11 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest { KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/properties/lateinit/isInitializedAndDeinitialize"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JS, true); } + @TestMetadata("companionObjectField.kt") + public void testCompanionObjectField() throws Exception { + runTest("compiler/testData/codegen/box/properties/lateinit/isInitializedAndDeinitialize/companionObjectField.kt"); + } + @TestMetadata("emptyLhs.kt") public void testEmptyLhs() throws Exception { runTest("compiler/testData/codegen/box/properties/lateinit/isInitializedAndDeinitialize/emptyLhs.kt");