[FIR2IR] Fix generation of val initialization after smart-cast

If neither setter nor backing field were found for an assignment call,
search overridden properties for backing fields.

^KT-57105 Fixed
This commit is contained in:
Kirill Rakhman
2023-03-07 11:16:44 +01:00
committed by Space Team
parent 2706e5502f
commit 0e721c8fc2
16 changed files with 99 additions and 3 deletions
@@ -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,
@@ -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 {
@@ -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 {
@@ -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
}
@@ -46,7 +46,10 @@ FILE fqName:<root> fileName:/smartCastInValInitialization.kt
TYPE_OP type=kotlin.Unit origin=IMPLICIT_COERCION_TO_UNIT typeOperand=kotlin.Unit
TYPE_OP type=<root>.RootBus origin=CAST typeOperand=<root>.RootBus
GET_VAR '<this>: <root>.MessageBusImpl declared in <root>.MessageBusImpl' type=<root>.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=<root>.RootBus origin=IMPLICIT_CAST typeOperand=<root>.RootBus
GET_VAR '<this>: <root>.MessageBusImpl declared in <root>.MessageBusImpl' type=<root>.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
@@ -19,7 +19,7 @@ open class MessageBusImpl {
init {
<this> as RootBus /*~> Unit */
error("") /* ErrorCallExpression */
<this> /*as RootBus */.#parentBus = null
}
}
@@ -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 {
@@ -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 {
@@ -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");
@@ -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 {
@@ -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 {
@@ -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 {
@@ -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 {
@@ -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 {
@@ -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 {
@@ -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");