[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.
This commit is contained in:
Yan Zhulanow
2023-08-22 00:18:30 +09:00
committed by Space Team
parent 981dde26ea
commit 3e12693f94
@@ -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)