[FIR] FirFakeOverrideGenerator: generate fake declaration with STATUS phase

The requirement for original declarations is to have resolved status,
so we can set at least `STATUS` phase for copied ones. This fixes the
problem that fake declarations for local declarations can have `RAW_FIR`
phase due to the copy from the original declarations, as during body
resolution all local declarations have only this phase.
Another issue: we cannot set the phase from the original declaration
as it is not guaranteed that we will have everything in the resolved
state. E.g., we can have the original declaration in `BODY_RESOLVE`
phase, but at the same time a containing class for fake declaration
can be in `STATUS` phase, for example. This means that the fake
declaration can have unresolved type annotation as it has them
from both places – from the original declaration and from the class
super type.
To simplify the code, we can just always set STATUS phase to be sure
that everything is resolved correctly.

Also, so we can optimize the logic for all phases above on Low Level FIR
level

^KT-64243
This commit is contained in:
Dmitrii Gridin
2023-12-14 16:45:32 +01:00
committed by Space Team
parent 835a9632b9
commit c3e6dd12a2
6 changed files with 206 additions and 181 deletions
@@ -59,37 +59,27 @@ internal object LLFirResolveMultiDesignationCollector {
else -> throwUnexpectedFirElementError(this)
}
private fun FirDeclaration.shouldBeResolved(): Boolean = when (origin) {
is FirDeclarationOrigin.Source,
is FirDeclarationOrigin.ImportedFromObjectOrStatic,
is FirDeclarationOrigin.Delegated,
is FirDeclarationOrigin.Synthetic,
is FirDeclarationOrigin.SubstitutionOverride,
is FirDeclarationOrigin.SamConstructor,
is FirDeclarationOrigin.WrappedIntegerOperator,
is FirDeclarationOrigin.IntersectionOverride,
is FirDeclarationOrigin.ScriptCustomization,
-> {
when (this) {
is FirFile -> true
is FirSyntheticProperty, is FirSyntheticPropertyAccessor -> false
is FirSimpleFunction,
is FirProperty,
is FirPropertyAccessor,
is FirField,
is FirTypeAlias,
is FirConstructor,
-> true
else -> true
}
}
else -> {
private fun FirDeclaration.shouldBeResolved(): Boolean {
if (!origin.isLazyResolvable) {
@OptIn(ResolveStateAccess::class)
check(resolvePhase == FirResolvePhase.BODY_RESOLVE) {
"Expected body resolve phase for origin $origin but found $resolveState"
}
false
return false
}
return when (this) {
is FirFile -> true
is FirSyntheticProperty, is FirSyntheticPropertyAccessor -> false
is FirSimpleFunction,
is FirProperty,
is FirPropertyAccessor,
is FirField,
is FirTypeAlias,
is FirConstructor,
-> true
else -> true
}
}
}
@@ -153,6 +153,9 @@ private class LLFirAnnotationArgumentsTargetResolver(
}
override fun doLazyResolveUnderLock(target: FirElementWithResolveState) {
// There is no sense to resolve such declarations as they do not have their own annotations
if (target is FirCallableDeclaration && target.isCopyCreatedInScope) return
resolveWithKeeper(
target,
target.llFirSession,
@@ -18,6 +18,7 @@ import org.jetbrains.kotlin.fir.FirSession
import org.jetbrains.kotlin.fir.contracts.FirRawContractDescription
import org.jetbrains.kotlin.fir.declarations.*
import org.jetbrains.kotlin.fir.declarations.impl.FirPrimaryConstructor
import org.jetbrains.kotlin.fir.isCopyCreatedInScope
import org.jetbrains.kotlin.fir.resolve.ScopeSession
import org.jetbrains.kotlin.fir.resolve.transformers.body.resolve.BodyResolveContext
import org.jetbrains.kotlin.fir.resolve.transformers.body.resolve.FirResolveContextCollector
@@ -64,6 +65,9 @@ private class LLFirContractsTargetResolver(
override fun doLazyResolveUnderLock(target: FirElementWithResolveState) {
collectTowerDataContext(target)
// There is no sense to resolve such declarations as they do not have contracts
if (target is FirCallableDeclaration && target.isCopyCreatedInScope) return
when (target) {
is FirPrimaryConstructor, is FirErrorPrimaryConstructor -> {
// No contracts here
@@ -13,6 +13,7 @@ import org.jetbrains.kotlin.fir.FirElementWithResolveState
import org.jetbrains.kotlin.fir.FirSession
import org.jetbrains.kotlin.fir.declarations.*
import org.jetbrains.kotlin.fir.expressions.FirStatement
import org.jetbrains.kotlin.fir.isCopyCreatedInScope
import org.jetbrains.kotlin.fir.languageVersionSettings
import org.jetbrains.kotlin.fir.resolve.ScopeSession
import org.jetbrains.kotlin.fir.resolve.transformers.body.resolve.FirResolveContextCollector
@@ -73,12 +74,14 @@ private class LLFirExpectActualMatchingTargetResolver(
}
}
private fun FirMemberDeclaration.canHaveExpectCounterPart(): Boolean = when (this) {
is FirEnumEntry -> true
is FirProperty -> true
is FirConstructor -> true
is FirSimpleFunction -> true
is FirRegularClass -> true
is FirTypeAlias -> true
private fun FirMemberDeclaration.canHaveExpectCounterPart(): Boolean = when {
// We shouldn't try to calculate expect/actual mapping for fake declarations
this is FirCallableDeclaration && isCopyCreatedInScope -> false
this is FirEnumEntry -> true
this is FirProperty -> true
this is FirConstructor -> true
this is FirSimpleFunction -> true
this is FirRegularClass -> true
this is FirTypeAlias -> true
else -> false
}