From 3e12693f9480ff3c3d4bbf6743218b3ed70d4062 Mon Sep 17 00:00:00 2001 From: Yan Zhulanow Date: Tue, 22 Aug 2023 00:18:30 +0900 Subject: [PATCH] [fir2ir] Ensure the property is resolved before mapping its initializer In partial module compilation, especially inside the IDE, only a few files from the project are passed to fir2ir. FIR in these files might contain references to declarations from other files. As the FE doesn't usually care about called property initializers, called properties might be resolved to 'CONTRACTS' or even 'STATUS'. The backend, however, might need to inline constant expressions from properties. --- .../fir/backend/Fir2IrDeclarationStorage.kt | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrDeclarationStorage.kt b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrDeclarationStorage.kt index 9ff39afc5ba..defc78662ba 100644 --- a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrDeclarationStorage.kt +++ b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrDeclarationStorage.kt @@ -1088,7 +1088,7 @@ class Fir2IrDeclarationStorage( NameUtils.propertyDelegateName(property.name), true, delegate ) } else { - val initializer = property.backingField?.initializer ?: property.initializer + val initializer = getEffectivePropertyInitializer(property, resolveIfNeeded = true) // There are cases when we get here for properties // that have no backing field. For example, in the // funExpression.kt test there's an attempt @@ -1157,6 +1157,22 @@ class Fir2IrDeclarationStorage( } } + /** + * In partial module compilation (see [org.jetbrains.kotlin.analysis.api.fir.components.KtFirCompilerFacility]), + * referenced properties might be resolved only up to [FirResolvePhase.CONTRACTS], + * however the backend requires the exact initializer type. + */ + private fun getEffectivePropertyInitializer(property: FirProperty, resolveIfNeeded: Boolean): FirExpression? { + val initializer = property.backingField?.initializer ?: property.initializer + + if (resolveIfNeeded && initializer is FirConstExpression<*>) { + property.lazyResolveToPhase(FirResolvePhase.BODY_RESOLVE) + return getEffectivePropertyInitializer(property, resolveIfNeeded = false) + } + + return initializer + } + fun getCachedIrProperty(property: FirProperty): IrProperty? { return getCachedIrProperty(property, fakeOverrideOwnerLookupTag = null) { signatureComposer.composeSignature(property)