diff --git a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/generators/CallAndReferenceGenerator.kt b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/generators/CallAndReferenceGenerator.kt index 3eae14e9c10..389ab45b196 100644 --- a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/generators/CallAndReferenceGenerator.kt +++ b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/generators/CallAndReferenceGenerator.kt @@ -569,7 +569,14 @@ class CallAndReferenceGenerator( is IrPropertySymbol -> { val irProperty = symbol.owner val setter = irProperty.setter - val backingField = irProperty.backingField + var backingField = irProperty.backingField + + // If we found neither a setter nor a backing field, check if we have an override (possibly fake) of a val with + // backing field. This can happen in a class initializer where `this` was smart-casted. See KT-57105. + if (setter == null && backingField == null) { + backingField = irProperty.overriddenSymbols.firstNotNullOfOrNull { it.owner.backingField } + } + when { setter != null -> IrCallImpl( startOffset, endOffset, type, setter.symbol, diff --git a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirLightTreeBlackBoxCodegenTestGenerated.java b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirLightTreeBlackBoxCodegenTestGenerated.java index 58849613b62..3f7b4cf68ce 100644 --- a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirLightTreeBlackBoxCodegenTestGenerated.java +++ b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirLightTreeBlackBoxCodegenTestGenerated.java @@ -48995,6 +48995,12 @@ public class FirLightTreeBlackBoxCodegenTestGenerated extends AbstractFirLightTr runTest("compiler/testData/codegen/box/smartCasts/nullSmartCast.kt"); } + @Test + @TestMetadata("propertyInitializationAfterSmartCast.kt") + public void testPropertyInitializationAfterSmartCast() throws Exception { + runTest("compiler/testData/codegen/box/smartCasts/propertyInitializationAfterSmartCast.kt"); + } + @Test @TestMetadata("resolveToMemberOfSuperclass_overrideWithDifferentType.kt") public void testResolveToMemberOfSuperclass_overrideWithDifferentType() throws Exception { diff --git a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirPsiBlackBoxCodegenTestGenerated.java b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirPsiBlackBoxCodegenTestGenerated.java index 8f2064bdac8..427d7693a45 100644 --- a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirPsiBlackBoxCodegenTestGenerated.java +++ b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirPsiBlackBoxCodegenTestGenerated.java @@ -48995,6 +48995,12 @@ public class FirPsiBlackBoxCodegenTestGenerated extends AbstractFirPsiBlackBoxCo runTest("compiler/testData/codegen/box/smartCasts/nullSmartCast.kt"); } + @Test + @TestMetadata("propertyInitializationAfterSmartCast.kt") + public void testPropertyInitializationAfterSmartCast() throws Exception { + runTest("compiler/testData/codegen/box/smartCasts/propertyInitializationAfterSmartCast.kt"); + } + @Test @TestMetadata("resolveToMemberOfSuperclass_overrideWithDifferentType.kt") public void testResolveToMemberOfSuperclass_overrideWithDifferentType() throws Exception { diff --git a/compiler/testData/codegen/box/smartCasts/propertyInitializationAfterSmartCast.kt b/compiler/testData/codegen/box/smartCasts/propertyInitializationAfterSmartCast.kt new file mode 100644 index 00000000000..cdd86a66e21 --- /dev/null +++ b/compiler/testData/codegen/box/smartCasts/propertyInitializationAfterSmartCast.kt @@ -0,0 +1,16 @@ +// ISSUE: KT-57105 + +class RootBus: MessageBusImpl() + +open class MessageBusImpl { + val parentBus: Any? + + init { + this as RootBus + parentBus = "OK" + } +} + +fun box(): String { + return RootBus().parentBus as String +} diff --git a/compiler/testData/ir/irText/classes/smartCastInValInitialization.fir.ir.txt b/compiler/testData/ir/irText/classes/smartCastInValInitialization.fir.ir.txt index 1d443f7960c..d9e6caded7b 100644 --- a/compiler/testData/ir/irText/classes/smartCastInValInitialization.fir.ir.txt +++ b/compiler/testData/ir/irText/classes/smartCastInValInitialization.fir.ir.txt @@ -46,7 +46,10 @@ FILE fqName: fileName:/smartCastInValInitialization.kt TYPE_OP type=kotlin.Unit origin=IMPLICIT_COERCION_TO_UNIT typeOperand=kotlin.Unit TYPE_OP type=.RootBus origin=CAST typeOperand=.RootBus GET_VAR ': .MessageBusImpl declared in .MessageBusImpl' type=.MessageBusImpl origin=null - ERROR_CALL 'Unresolved reference: R|/MessageBusImpl.parentBus|' type=IrErrorType(null) + SET_FIELD 'FIELD PROPERTY_BACKING_FIELD name:parentBus type:kotlin.Any? visibility:private [final]' type=kotlin.Unit origin=null + receiver: TYPE_OP type=.RootBus origin=IMPLICIT_CAST typeOperand=.RootBus + GET_VAR ': .MessageBusImpl declared in .MessageBusImpl' type=.MessageBusImpl origin=null + value: CONST Null type=kotlin.Nothing? value=null FUN FAKE_OVERRIDE name:equals visibility:public modality:OPEN <> ($this:kotlin.Any, other:kotlin.Any?) returnType:kotlin.Boolean [fake_override,operator] overridden: public open fun equals (other: kotlin.Any?): kotlin.Boolean [operator] declared in kotlin.Any diff --git a/compiler/testData/ir/irText/classes/smartCastInValInitialization.fir.kt.txt b/compiler/testData/ir/irText/classes/smartCastInValInitialization.fir.kt.txt index 7413055c9a4..f2c17e59e3c 100644 --- a/compiler/testData/ir/irText/classes/smartCastInValInitialization.fir.kt.txt +++ b/compiler/testData/ir/irText/classes/smartCastInValInitialization.fir.kt.txt @@ -19,7 +19,7 @@ open class MessageBusImpl { init { as RootBus /*~> Unit */ - error("") /* ErrorCallExpression */ + /*as RootBus */.#parentBus = null } } diff --git a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/BlackBoxCodegenTestGenerated.java b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/BlackBoxCodegenTestGenerated.java index 90475cc46da..9770fc58c16 100644 --- a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/BlackBoxCodegenTestGenerated.java @@ -46625,6 +46625,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { runTest("compiler/testData/codegen/box/smartCasts/nullSmartCast.kt"); } + @Test + @TestMetadata("propertyInitializationAfterSmartCast.kt") + public void testPropertyInitializationAfterSmartCast() throws Exception { + runTest("compiler/testData/codegen/box/smartCasts/propertyInitializationAfterSmartCast.kt"); + } + @Test @TestMetadata("smartCastInsideIf.kt") public void testSmartCastInsideIf() throws Exception { diff --git a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java index af77af92a35..85f9e5f14bb 100644 --- a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java +++ b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java @@ -48995,6 +48995,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes runTest("compiler/testData/codegen/box/smartCasts/nullSmartCast.kt"); } + @Test + @TestMetadata("propertyInitializationAfterSmartCast.kt") + public void testPropertyInitializationAfterSmartCast() throws Exception { + runTest("compiler/testData/codegen/box/smartCasts/propertyInitializationAfterSmartCast.kt"); + } + @Test @TestMetadata("resolveToMemberOfSuperclass_overrideWithDifferentType.kt") public void testResolveToMemberOfSuperclass_overrideWithDifferentType() throws Exception { diff --git a/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java b/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java index 602807b4e06..bf410e10656 100644 --- a/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -37743,6 +37743,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes runTest("compiler/testData/codegen/box/smartCasts/nullSmartCast.kt"); } + @TestMetadata("propertyInitializationAfterSmartCast.kt") + public void testPropertyInitializationAfterSmartCast() throws Exception { + runTest("compiler/testData/codegen/box/smartCasts/propertyInitializationAfterSmartCast.kt"); + } + @TestMetadata("smartCastInsideIf.kt") public void testSmartCastInsideIf() throws Exception { runTest("compiler/testData/codegen/box/smartCasts/smartCastInsideIf.kt"); diff --git a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/JsCodegenBoxTestGenerated.java b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/JsCodegenBoxTestGenerated.java index 3fdad673f8e..4e9be57e9a2 100644 --- a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/JsCodegenBoxTestGenerated.java +++ b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/JsCodegenBoxTestGenerated.java @@ -34573,6 +34573,12 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest { runTest("compiler/testData/codegen/box/smartCasts/nullSmartCast.kt"); } + @Test + @TestMetadata("propertyInitializationAfterSmartCast.kt") + public void testPropertyInitializationAfterSmartCast() throws Exception { + runTest("compiler/testData/codegen/box/smartCasts/propertyInitializationAfterSmartCast.kt"); + } + @Test @TestMetadata("smartCastInsideIf.kt") public void testSmartCastInsideIf() throws Exception { diff --git a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/fir/FirJsCodegenBoxTestGenerated.java b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/fir/FirJsCodegenBoxTestGenerated.java index 2cec136283f..9e25a507086 100644 --- a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/fir/FirJsCodegenBoxTestGenerated.java +++ b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/fir/FirJsCodegenBoxTestGenerated.java @@ -34759,6 +34759,12 @@ public class FirJsCodegenBoxTestGenerated extends AbstractFirJsCodegenBoxTest { runTest("compiler/testData/codegen/box/smartCasts/nullSmartCast.kt"); } + @Test + @TestMetadata("propertyInitializationAfterSmartCast.kt") + public void testPropertyInitializationAfterSmartCast() throws Exception { + runTest("compiler/testData/codegen/box/smartCasts/propertyInitializationAfterSmartCast.kt"); + } + @Test @TestMetadata("smartCastInsideIf.kt") public void testSmartCastInsideIf() throws Exception { diff --git a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/IrJsCodegenBoxTestGenerated.java b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/IrJsCodegenBoxTestGenerated.java index dd117e346aa..90d6740cf29 100644 --- a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/IrJsCodegenBoxTestGenerated.java +++ b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/IrJsCodegenBoxTestGenerated.java @@ -34759,6 +34759,12 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest { runTest("compiler/testData/codegen/box/smartCasts/nullSmartCast.kt"); } + @Test + @TestMetadata("propertyInitializationAfterSmartCast.kt") + public void testPropertyInitializationAfterSmartCast() throws Exception { + runTest("compiler/testData/codegen/box/smartCasts/propertyInitializationAfterSmartCast.kt"); + } + @Test @TestMetadata("smartCastInsideIf.kt") public void testSmartCastInsideIf() throws Exception { diff --git a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/IrJsES6CodegenBoxTestGenerated.java b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/IrJsES6CodegenBoxTestGenerated.java index 58e1769b1d9..6ac8b9bb372 100644 --- a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/IrJsES6CodegenBoxTestGenerated.java +++ b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/IrJsES6CodegenBoxTestGenerated.java @@ -34759,6 +34759,12 @@ public class IrJsES6CodegenBoxTestGenerated extends AbstractIrJsES6CodegenBoxTes runTest("compiler/testData/codegen/box/smartCasts/nullSmartCast.kt"); } + @Test + @TestMetadata("propertyInitializationAfterSmartCast.kt") + public void testPropertyInitializationAfterSmartCast() throws Exception { + runTest("compiler/testData/codegen/box/smartCasts/propertyInitializationAfterSmartCast.kt"); + } + @Test @TestMetadata("smartCastInsideIf.kt") public void testSmartCastInsideIf() throws Exception { diff --git a/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/blackboxtest/K2NativeCodegenBoxTestGenerated.java b/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/blackboxtest/K2NativeCodegenBoxTestGenerated.java index 642a4cacc3e..5747f021abf 100644 --- a/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/blackboxtest/K2NativeCodegenBoxTestGenerated.java +++ b/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/blackboxtest/K2NativeCodegenBoxTestGenerated.java @@ -38342,6 +38342,12 @@ public class K2NativeCodegenBoxTestGenerated extends AbstractNativeCodegenBoxTes runTest("compiler/testData/codegen/box/smartCasts/nullSmartCast.kt"); } + @Test + @TestMetadata("propertyInitializationAfterSmartCast.kt") + public void testPropertyInitializationAfterSmartCast() throws Exception { + runTest("compiler/testData/codegen/box/smartCasts/propertyInitializationAfterSmartCast.kt"); + } + @Test @TestMetadata("smartCastInsideIf.kt") public void testSmartCastInsideIf() throws Exception { diff --git a/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/blackboxtest/NativeCodegenBoxTestGenerated.java b/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/blackboxtest/NativeCodegenBoxTestGenerated.java index af7e77df99c..f0a103d10f2 100644 --- a/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/blackboxtest/NativeCodegenBoxTestGenerated.java +++ b/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/blackboxtest/NativeCodegenBoxTestGenerated.java @@ -37862,6 +37862,12 @@ public class NativeCodegenBoxTestGenerated extends AbstractNativeCodegenBoxTest runTest("compiler/testData/codegen/box/smartCasts/nullSmartCast.kt"); } + @Test + @TestMetadata("propertyInitializationAfterSmartCast.kt") + public void testPropertyInitializationAfterSmartCast() throws Exception { + runTest("compiler/testData/codegen/box/smartCasts/propertyInitializationAfterSmartCast.kt"); + } + @Test @TestMetadata("smartCastInsideIf.kt") public void testSmartCastInsideIf() throws Exception { diff --git a/wasm/wasm.tests/tests-gen/org/jetbrains/kotlin/wasm/test/IrCodegenBoxWasmTestGenerated.java b/wasm/wasm.tests/tests-gen/org/jetbrains/kotlin/wasm/test/IrCodegenBoxWasmTestGenerated.java index 40aa987d27e..cf9971a6ed4 100644 --- a/wasm/wasm.tests/tests-gen/org/jetbrains/kotlin/wasm/test/IrCodegenBoxWasmTestGenerated.java +++ b/wasm/wasm.tests/tests-gen/org/jetbrains/kotlin/wasm/test/IrCodegenBoxWasmTestGenerated.java @@ -31138,6 +31138,11 @@ public class IrCodegenBoxWasmTestGenerated extends AbstractIrCodegenBoxWasmTest runTest("compiler/testData/codegen/box/smartCasts/nullSmartCast.kt"); } + @TestMetadata("propertyInitializationAfterSmartCast.kt") + public void testPropertyInitializationAfterSmartCast() throws Exception { + runTest("compiler/testData/codegen/box/smartCasts/propertyInitializationAfterSmartCast.kt"); + } + @TestMetadata("smartCastInsideIf.kt") public void testSmartCastInsideIf() throws Exception { runTest("compiler/testData/codegen/box/smartCasts/smartCastInsideIf.kt");