[LL FIR] LLFirContractsLazyResolver: avoid transformation for functions with implicit type

Legacy (current) contracts can exist only on functions with bodies.
This change also should improve memory consumption because we will
less frequently calculate bodies.

^KT-55750
This commit is contained in:
Dmitrii Gridin
2023-09-20 13:55:19 +02:00
committed by Space Team
parent 3c865bdddd
commit 3e9e66493f
9 changed files with 19 additions and 63 deletions
@@ -20,6 +20,7 @@ import org.jetbrains.kotlin.fir.declarations.*
import org.jetbrains.kotlin.fir.resolve.ScopeSession import org.jetbrains.kotlin.fir.resolve.ScopeSession
import org.jetbrains.kotlin.fir.resolve.transformers.body.resolve.FirResolveContextCollector import org.jetbrains.kotlin.fir.resolve.transformers.body.resolve.FirResolveContextCollector
import org.jetbrains.kotlin.fir.resolve.transformers.contracts.FirContractResolveTransformer import org.jetbrains.kotlin.fir.resolve.transformers.contracts.FirContractResolveTransformer
import org.jetbrains.kotlin.fir.types.FirImplicitTypeRef
internal object LLFirContractsLazyResolver : LLFirLazyResolver(FirResolvePhase.CONTRACTS) { internal object LLFirContractsLazyResolver : LLFirLazyResolver(FirResolvePhase.CONTRACTS) {
override fun resolve( override fun resolve(
@@ -54,7 +55,13 @@ private class LLFirContractsTargetResolver(
override fun doLazyResolveUnderLock(target: FirElementWithResolveState) { override fun doLazyResolveUnderLock(target: FirElementWithResolveState) {
when (target) { when (target) {
is FirSimpleFunction -> resolve(target, ContractStateKeepers.SIMPLE_FUNCTION) is FirSimpleFunction -> {
// There is no sense to try to transform functions without a block body and without a raw contract
if (target.returnTypeRef !is FirImplicitTypeRef || target.contractDescription is FirRawContractDescription) {
resolve(target, ContractStateKeepers.SIMPLE_FUNCTION)
}
}
is FirConstructor -> resolve(target, ContractStateKeepers.CONSTRUCTOR) is FirConstructor -> resolve(target, ContractStateKeepers.CONSTRUCTOR)
is FirProperty -> resolve(target, ContractStateKeepers.PROPERTY) is FirProperty -> resolve(target, ContractStateKeepers.PROPERTY)
is FirRegularClass, is FirRegularClass,
@@ -36,9 +36,7 @@ FILE: [ResolvedTo(IMPORTS)] main.kt
CONTRACTS: CONTRACTS:
FILE: [ResolvedTo(IMPORTS)] main.kt FILE: [ResolvedTo(IMPORTS)] main.kt
public final [ResolvedTo(CONTRACTS)] fun resolveMe([ResolvedTo(CONTRACTS)] i: R|JavaInterface|): <implicit> { public final [ResolvedTo(CONTRACTS)] fun resolveMe([ResolvedTo(CONTRACTS)] i: R|JavaInterface|): <implicit> { LAZY_BLOCK }
^resolveMe i#.id#
}
IMPLICIT_TYPES_BODY_RESOLVE: IMPLICIT_TYPES_BODY_RESOLVE:
FILE: [ResolvedTo(IMPORTS)] main.kt FILE: [ResolvedTo(IMPORTS)] main.kt
@@ -36,9 +36,7 @@ FILE: [ResolvedTo(IMPORTS)] main.kt
CONTRACTS: CONTRACTS:
FILE: [ResolvedTo(IMPORTS)] main.kt FILE: [ResolvedTo(IMPORTS)] main.kt
public final [ResolvedTo(CONTRACTS)] fun resolveMe([ResolvedTo(CONTRACTS)] i: R|JavaInterface|): <implicit> { public final [ResolvedTo(CONTRACTS)] fun resolveMe([ResolvedTo(CONTRACTS)] i: R|JavaInterface|): <implicit> { LAZY_BLOCK }
^resolveMe i#.id#
}
IMPLICIT_TYPES_BODY_RESOLVE: IMPLICIT_TYPES_BODY_RESOLVE:
FILE: [ResolvedTo(IMPORTS)] main.kt FILE: [ResolvedTo(IMPORTS)] main.kt
@@ -56,9 +56,7 @@ CONTRACTS:
FILE: [ResolvedTo(IMPORTS)] functionWithParameter.kt FILE: [ResolvedTo(IMPORTS)] functionWithParameter.kt
public? final? [ResolvedTo(RAW_FIR)] interface I : R|kotlin/Any| { public? final? [ResolvedTo(RAW_FIR)] interface I : R|kotlin/Any| {
} }
public final [ResolvedTo(CONTRACTS)] fun resolveMe([ResolvedTo(CONTRACTS)] param: R|I|): <implicit> { public final [ResolvedTo(CONTRACTS)] fun resolveMe([ResolvedTo(CONTRACTS)] param: R|I|): <implicit> { LAZY_BLOCK }
^resolveMe Unit#
}
IMPLICIT_TYPES_BODY_RESOLVE: IMPLICIT_TYPES_BODY_RESOLVE:
FILE: [ResolvedTo(IMPORTS)] functionWithParameter.kt FILE: [ResolvedTo(IMPORTS)] functionWithParameter.kt
@@ -115,9 +115,7 @@ FILE: [ResolvedTo(IMPORTS)] functionWithParameterScript.kts
public? final? [ResolvedTo(RAW_FIR)] interface I : R|kotlin/Any| { public? final? [ResolvedTo(RAW_FIR)] interface I : R|kotlin/Any| {
} }
public final [ResolvedTo(CONTRACTS)] fun resolveMe([ResolvedTo(CONTRACTS)] param: R|I|): <implicit> { public final [ResolvedTo(CONTRACTS)] fun resolveMe([ResolvedTo(CONTRACTS)] param: R|I|): <implicit> { LAZY_BLOCK }
^resolveMe Unit#
}
IMPLICIT_TYPES_BODY_RESOLVE: IMPLICIT_TYPES_BODY_RESOLVE:
@@ -174,3 +172,4 @@ FILE: [ResolvedTo(BODY_RESOLVE)] functionWithParameterScript.kts
public final [ResolvedTo(BODY_RESOLVE)] fun resolveMe([ResolvedTo(BODY_RESOLVE)] param: R|I|): R|kotlin/Unit| { public final [ResolvedTo(BODY_RESOLVE)] fun resolveMe([ResolvedTo(BODY_RESOLVE)] param: R|I|): R|kotlin/Unit| {
^resolveMe Q|kotlin/Unit| ^resolveMe Q|kotlin/Unit|
} }
@@ -36,9 +36,7 @@ FILE: [ResolvedTo(IMPORTS)] functionParameter.kt
CONTRACTS: CONTRACTS:
FILE: [ResolvedTo(IMPORTS)] functionParameter.kt FILE: [ResolvedTo(IMPORTS)] functionParameter.kt
public final [ResolvedTo(CONTRACTS)] fun foo([ResolvedTo(CONTRACTS)] param: R|kotlin/Int|): <implicit> { public final [ResolvedTo(CONTRACTS)] fun foo([ResolvedTo(CONTRACTS)] param: R|kotlin/Int|): <implicit> { LAZY_BLOCK }
^foo String(boo)
}
IMPLICIT_TYPES_BODY_RESOLVE: IMPLICIT_TYPES_BODY_RESOLVE:
FILE: [ResolvedTo(IMPORTS)] functionParameter.kt FILE: [ResolvedTo(IMPORTS)] functionParameter.kt
@@ -85,9 +85,7 @@ FILE: [ResolvedTo(IMPORTS)] functionParameterScript.kts
SCRIPT: [ResolvedTo(TYPES)] <script-functionParameterScript.kts> SCRIPT: [ResolvedTo(TYPES)] <script-functionParameterScript.kts>
[ResolvedTo(RAW_FIR)] lval args: R|kotlin/Array<kotlin/String>| [ResolvedTo(RAW_FIR)] lval args: R|kotlin/Array<kotlin/String>|
public final [ResolvedTo(CONTRACTS)] fun foo([ResolvedTo(CONTRACTS)] param: R|kotlin/Int|): <implicit> { public final [ResolvedTo(CONTRACTS)] fun foo([ResolvedTo(CONTRACTS)] param: R|kotlin/Int|): <implicit> { LAZY_BLOCK }
^foo String(boo)
}
IMPLICIT_TYPES_BODY_RESOLVE: IMPLICIT_TYPES_BODY_RESOLVE:
@@ -132,3 +130,4 @@ FILE: [ResolvedTo(BODY_RESOLVE)] functionParameterScript.kts
public final [ResolvedTo(BODY_RESOLVE)] fun foo([ResolvedTo(BODY_RESOLVE)] param: R|kotlin/Int|): R|kotlin/String| { public final [ResolvedTo(BODY_RESOLVE)] fun foo([ResolvedTo(BODY_RESOLVE)] param: R|kotlin/Int|): R|kotlin/String| {
^foo String(boo) ^foo String(boo)
} }
@@ -114,28 +114,7 @@ FILE: [ResolvedTo(IMPORTS)] localConstructor.kt
LAZY_super<R|kotlin/Any|> LAZY_super<R|kotlin/Any|>
} }
public final [ResolvedTo(CONTRACTS)] fun <[ResolvedTo(CONTRACTS)] E, [ResolvedTo(CONTRACTS)] F> foo(): <implicit> { public final [ResolvedTo(CONTRACTS)] fun <[ResolvedTo(CONTRACTS)] E, [ResolvedTo(CONTRACTS)] F> foo(): <implicit> { LAZY_BLOCK }
^foo [ResolvedTo(RAW_FIR)] fun <implicit>.<anonymous>(): <implicit> <inline=Unknown> {
local final? [ResolvedTo(RAW_FIR)] class C<[ResolvedTo(RAW_FIR)] G, [ResolvedTo(CONTRACTS)] Outer(E), [ResolvedTo(CONTRACTS)] Outer(F)> : R|kotlin/Any| {
public? [ResolvedTo(RAW_FIR)] [ContainingClassKey=C] constructor<[ResolvedTo(RAW_FIR)] G>(): R|<local>/C<G, E, F>| {
super<R|kotlin/Any|>()
}
public? final? [ResolvedTo(RAW_FIR)] val e: E = magic#()
public? [ResolvedTo(RAW_FIR)] [ContainingClassKey=C] get(): E
public? final? [ResolvedTo(RAW_FIR)] val f: F = magic#()
public? [ResolvedTo(RAW_FIR)] [ContainingClassKey=C] get(): F
public? final? [ResolvedTo(RAW_FIR)] val g: G = magic#()
public? [ResolvedTo(RAW_FIR)] [ContainingClassKey=C] get(): G
}
C#<F>()
}
}
} }
@@ -173,28 +173,7 @@ FILE: [ResolvedTo(IMPORTS)] localConstructorScript.kts
LAZY_super<R|kotlin/Any|> LAZY_super<R|kotlin/Any|>
} }
public final [ResolvedTo(CONTRACTS)] fun <[ResolvedTo(CONTRACTS)] E, [ResolvedTo(CONTRACTS)] F> foo(): <implicit> { public final [ResolvedTo(CONTRACTS)] fun <[ResolvedTo(CONTRACTS)] E, [ResolvedTo(CONTRACTS)] F> foo(): <implicit> { LAZY_BLOCK }
^foo [ResolvedTo(RAW_FIR)] fun <implicit>.<anonymous>(): <implicit> <inline=Unknown> {
local final? [ResolvedTo(RAW_FIR)] class C<[ResolvedTo(RAW_FIR)] G, [ResolvedTo(CONTRACTS)] Outer(E), [ResolvedTo(CONTRACTS)] Outer(F)> : R|kotlin/Any| {
public? [ResolvedTo(RAW_FIR)] [ContainingClassKey=C] constructor<[ResolvedTo(RAW_FIR)] G>(): R|<local>/C<G, E, F>| {
super<R|kotlin/Any|>()
}
public? final? [ResolvedTo(RAW_FIR)] val e: E = magic#()
public? [ResolvedTo(RAW_FIR)] [ContainingClassKey=C] get(): E
public? final? [ResolvedTo(RAW_FIR)] val f: F = magic#()
public? [ResolvedTo(RAW_FIR)] [ContainingClassKey=C] get(): F
public? final? [ResolvedTo(RAW_FIR)] val g: G = magic#()
public? [ResolvedTo(RAW_FIR)] [ContainingClassKey=C] get(): G
}
C#<F>()
}
}
} }
@@ -361,3 +340,4 @@ FILE: [ResolvedTo(BODY_RESOLVE)] localConstructorScript.kts
} }
} }