[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.
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 f18e5f9..8d3e052 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
@@ -1085,7 +1085,7 @@ 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 @@ -1154,6 +1154,19 @@ } } + // In partial module compilation, 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)