From 7a6a3a0b1d6bde426a4362dc24162e51530a7868 Mon Sep 17 00:00:00 2001 From: Kirill Rakhman Date: Wed, 15 Mar 2023 10:49:57 +0100 Subject: [PATCH] [FIR2IR] Fix generation of val initialization after smart-cast, again #KT-57284 --- .../generators/CallAndReferenceGenerator.kt | 9 ++++++++- .../propertyInitializationAfterSmartCast.kt | 17 +++++++++++++++-- 2 files changed, 23 insertions(+), 3 deletions(-) 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 40c1a05b6b0..3afb1c6c003 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 @@ -572,7 +572,7 @@ class CallAndReferenceGenerator( // 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 } + backingField = irProperty.overriddenBackingFieldOrNull() } when { @@ -624,6 +624,13 @@ class CallAndReferenceGenerator( } } + private fun IrProperty.overriddenBackingFieldOrNull(): IrField? { + return overriddenSymbols.firstNotNullOfOrNull { + val owner = it.owner + owner.backingField ?: owner.overriddenBackingFieldOrNull() + } + } + fun convertToIrConstructorCall(annotation: FirAnnotation): IrExpression { val coneType = annotation.annotationTypeRef.coneTypeSafe() ?.fullyExpandedType(session) as? ConeLookupTagBasedType diff --git a/compiler/testData/codegen/box/smartCasts/propertyInitializationAfterSmartCast.kt b/compiler/testData/codegen/box/smartCasts/propertyInitializationAfterSmartCast.kt index cdd86a66e21..9f55118fdcb 100644 --- a/compiler/testData/codegen/box/smartCasts/propertyInitializationAfterSmartCast.kt +++ b/compiler/testData/codegen/box/smartCasts/propertyInitializationAfterSmartCast.kt @@ -7,10 +7,23 @@ open class MessageBusImpl { init { this as RootBus - parentBus = "OK" + parentBus = "O" + } +} + +class RootBus2: CompositeMessageBus2() + +open class CompositeMessageBus2: MessageBusImpl2() + +open class MessageBusImpl2 { + val parentBus: Any? + + init { + this as RootBus2 + parentBus = "K" } } fun box(): String { - return RootBus().parentBus as String + return "" + RootBus().parentBus + RootBus2().parentBus }