[LL API] Handle smart casts correctly on 'FirCodeFragment' analysis

This commit is contained in:
Yan Zhulanow
2023-08-31 15:39:40 +09:00
committed by Space Team
parent 0fbf648971
commit 5559a0754d
17 changed files with 132 additions and 10 deletions
@@ -298,6 +298,18 @@ public class FirIdeNormalAnalysisLibrarySourceModuleCompilerFacilityTestGenerate
runTest("analysis/analysis-api/testData/components/compilerFacility/compilation/codeFragments/capturing/singleClassContextReceiver.kt");
}
@Test
@TestMetadata("smartCastExtensionReceiver.kt")
public void testSmartCastExtensionReceiver() throws Exception {
runTest("analysis/analysis-api/testData/components/compilerFacility/compilation/codeFragments/capturing/smartCastExtensionReceiver.kt");
}
@Test
@TestMetadata("smartCastValueParameter.kt")
public void testSmartCastValueParameter() throws Exception {
runTest("analysis/analysis-api/testData/components/compilerFacility/compilation/codeFragments/capturing/smartCastValueParameter.kt");
}
@Test
@TestMetadata("valueParameter.kt")
public void testValueParameter() throws Exception {
@@ -298,6 +298,18 @@ public class FirIdeNormalAnalysisSourceModuleCompilerFacilityTestGenerated exten
runTest("analysis/analysis-api/testData/components/compilerFacility/compilation/codeFragments/capturing/singleClassContextReceiver.kt");
}
@Test
@TestMetadata("smartCastExtensionReceiver.kt")
public void testSmartCastExtensionReceiver() throws Exception {
runTest("analysis/analysis-api/testData/components/compilerFacility/compilation/codeFragments/capturing/smartCastExtensionReceiver.kt");
}
@Test
@TestMetadata("smartCastValueParameter.kt")
public void testSmartCastValueParameter() throws Exception {
runTest("analysis/analysis-api/testData/components/compilerFacility/compilation/codeFragments/capturing/smartCastValueParameter.kt");
}
@Test
@TestMetadata("valueParameter.kt")
public void testValueParameter() throws Exception {
@@ -0,0 +1,6 @@
ExtensionReceiver[name: dumbWith; isMutated: false; displayText: this@dumbWith]
dumbWith@fun R|kotlin/Any?|.<anonymous>(): R|kotlin/Unit| <inline=Inline, kind=UNKNOWN>
R|kotlin/Any?|
ExtensionReceiver[name: dumbWith; isMutated: false; displayText: this@dumbWith]
dumbWith@fun R|kotlin/Any?|.<anonymous>(): R|kotlin/Unit| <inline=Inline, kind=UNKNOWN>
R|kotlin/Any?|
@@ -0,0 +1,20 @@
MODULE_FRAGMENT
FILE fqName:<root> fileName:fragment.kt
CLASS CLASS name:CodeFragment modality:FINAL visibility:public superTypes:[kotlin.Any]
$this: VALUE_PARAMETER INSTANCE_RECEIVER name:<this> type:<root>.CodeFragment
CONSTRUCTOR visibility:public <> () returnType:<root>.CodeFragment [primary]
BLOCK_BODY
DELEGATING_CONSTRUCTOR_CALL 'public constructor <init> () [primary] declared in kotlin.Any'
FUN name:run visibility:public modality:FINAL <> (p0:kotlin.Any?, p1:kotlin.Any?) returnType:kotlin.Int
VALUE_PARAMETER name:p0 index:0 type:kotlin.Any?
VALUE_PARAMETER name:p1 index:1 type:kotlin.Any?
EXPRESSION_BODY
BLOCK type=kotlin.Int origin=null
CALL 'public final fun plus (other: kotlin.Int): kotlin.Int [operator] declared in kotlin.Int' type=kotlin.Int origin=PLUS
$this: CALL 'public open fun <get-length> (): kotlin.Int declared in kotlin.String' type=kotlin.Int origin=GET_PROPERTY
$this: TYPE_OP type=kotlin.String origin=IMPLICIT_CAST typeOperand=kotlin.String
GET_VAR 'p0: kotlin.Any? declared in <root>.CodeFragment.run' type=kotlin.Any? origin=null
other: CALL 'public open fun <get-length> (): kotlin.Int declared in kotlin.String' type=kotlin.Int origin=GET_PROPERTY
$this: CALL 'public final fun <get-foo> (): kotlin.String declared in <root>.Foo' type=kotlin.String origin=GET_PROPERTY
$this: TYPE_OP type=<root>.Foo origin=IMPLICIT_CAST typeOperand=<root>.Foo
GET_VAR 'p1: kotlin.Any? declared in <root>.CodeFragment.run' type=kotlin.Any? origin=null
@@ -0,0 +1,19 @@
fun test() {
dumbWith("Hello, world!") {
if (this is String) {
dumbWith(Foo()) {
if (this is Foo) {
<caret>val x = 0
}
}
}
}
}
inline fun dumbWith(obj: Any?, block: Any?.() -> Unit) {
obj.block()
}
class Foo {
val foo: String = "foo"
}
@@ -0,0 +1,5 @@
public final class CodeFragment {
// source: 'fragment.kt'
public method <init>(): void
public final static method run(p0: java.lang.Object, p1: java.lang.Object): int
}
@@ -0,0 +1,3 @@
Local[name: foo; isMutated: false; displayText: foo]
foo: R|kotlin/Any?|
R|kotlin/Any?|
@@ -0,0 +1,14 @@
MODULE_FRAGMENT
FILE fqName:<root> fileName:fragment.kt
CLASS CLASS name:CodeFragment modality:FINAL visibility:public superTypes:[kotlin.Any]
$this: VALUE_PARAMETER INSTANCE_RECEIVER name:<this> type:<root>.CodeFragment
CONSTRUCTOR visibility:public <> () returnType:<root>.CodeFragment [primary]
BLOCK_BODY
DELEGATING_CONSTRUCTOR_CALL 'public constructor <init> () [primary] declared in kotlin.Any'
FUN name:run visibility:public modality:FINAL <> (p0:kotlin.Any?) returnType:kotlin.Unit
VALUE_PARAMETER name:p0 index:0 type:kotlin.Any?
EXPRESSION_BODY
BLOCK type=kotlin.Unit origin=null
CALL 'public final fun call (): kotlin.Unit declared in <root>.Foo' type=kotlin.Unit origin=null
$this: TYPE_OP type=<root>.Foo origin=IMPLICIT_CAST typeOperand=<root>.Foo
GET_VAR 'p0: kotlin.Any? declared in <root>.CodeFragment.run' type=kotlin.Any? origin=null
@@ -0,0 +1,9 @@
fun test(foo: Any?) {
if (foo is Foo) {
<caret>val x = 0
}
}
class Foo {
fun call() {}
}
@@ -0,0 +1,5 @@
public final class CodeFragment {
// source: 'fragment.kt'
public method <init>(): void
public final static method run(p0: java.lang.Object): void
}
@@ -39,13 +39,13 @@ import org.jetbrains.kotlin.fir.resolve.FirCodeFragmentContext
import org.jetbrains.kotlin.fir.resolve.ScopeSession
import org.jetbrains.kotlin.fir.resolve.dfa.FirControlFlowGraphReferenceImpl
import org.jetbrains.kotlin.fir.resolve.codeFragmentContext
import org.jetbrains.kotlin.fir.resolve.dfa.RealVariable
import org.jetbrains.kotlin.fir.resolve.transformers.body.resolve.FirBodyResolveTransformer
import org.jetbrains.kotlin.fir.resolve.transformers.body.resolve.FirResolveContextCollector
import org.jetbrains.kotlin.fir.resolve.transformers.contracts.FirContractsDslNames
import org.jetbrains.kotlin.fir.symbols.lazyResolveToPhase
import org.jetbrains.kotlin.fir.resolve.dfa.cfg.isUsedInControlFlowGraphBuilderForClass
import org.jetbrains.kotlin.fir.resolve.dfa.cfg.isUsedInControlFlowGraphBuilderForFile
import org.jetbrains.kotlin.fir.symbols.FirBasedSymbol
import org.jetbrains.kotlin.fir.types.ConeKotlinType
import org.jetbrains.kotlin.psi.KtCodeFragment
import org.jetbrains.kotlin.psi.KtFile
@@ -225,8 +225,7 @@ private class LLFirBodyTargetResolver(
withPsiEntry("contextPsiElement", contextPsiElement)
}
val variables = elementContext.smartCasts.mapKeys { it.key.identifier.symbol }
LLFirCodeFragmentContext(elementContext.towerDataContext.withExtraScopes(), variables)
LLFirCodeFragmentContext(elementContext.towerDataContext.withExtraScopes(), elementContext.smartCasts)
} else {
val towerDataContext = FirTowerDataContext().withExtraScopes()
LLFirCodeFragmentContext(towerDataContext, emptyMap())
@@ -513,5 +512,5 @@ private fun requireSameSize(old: List<FirStatement>, new: List<FirStatement>) {
private class LLFirCodeFragmentContext(
override val towerDataContext: FirTowerDataContext,
override val variables: Map<FirBasedSymbol<*>, Set<ConeKotlinType>>
override val smartCasts: Map<RealVariable, Set<ConeKotlinType>>
) : FirCodeFragmentContext
@@ -192,6 +192,18 @@ public class CodeFragmentCapturingTestGenerated extends AbstractCodeFragmentCapt
runTest("analysis/analysis-api/testData/components/compilerFacility/compilation/codeFragments/capturing/singleClassContextReceiver.kt");
}
@Test
@TestMetadata("smartCastExtensionReceiver.kt")
public void testSmartCastExtensionReceiver() throws Exception {
runTest("analysis/analysis-api/testData/components/compilerFacility/compilation/codeFragments/capturing/smartCastExtensionReceiver.kt");
}
@Test
@TestMetadata("smartCastValueParameter.kt")
public void testSmartCastValueParameter() throws Exception {
runTest("analysis/analysis-api/testData/components/compilerFacility/compilation/codeFragments/capturing/smartCastValueParameter.kt");
}
@Test
@TestMetadata("valueParameter.kt")
public void testValueParameter() throws Exception {
@@ -255,9 +255,9 @@ abstract class FirDataFlowAnalyzer(
fun enterCodeFragment(codeFragment: FirCodeFragment) {
graphBuilder.enterCodeFragment(codeFragment).mergeIncomingFlow { _, flow ->
val realVariablesFromContext = codeFragment.codeFragmentContext?.variables.orEmpty()
for ((symbol, exactTypes) in realVariablesFromContext) {
val realVariable = variableStorage.getOrCreateIfReal(flow, symbol.fir) as? RealVariable ?: continue
val smartCasts = codeFragment.codeFragmentContext?.smartCasts.orEmpty()
for ((originalRealVariable, exactTypes) in smartCasts) {
val realVariable = variableStorage.getOrPut(originalRealVariable.identifier) { originalRealVariable }
val typeStatement = PersistentTypeStatement(realVariable, exactTypes.toPersistentSet())
flow.addTypeStatement(typeStatement)
}
@@ -9,8 +9,8 @@ import org.jetbrains.kotlin.fir.FirSession
import org.jetbrains.kotlin.fir.containingClassForLocalAttr
import org.jetbrains.kotlin.fir.declarations.*
import org.jetbrains.kotlin.fir.declarations.utils.isLocal
import org.jetbrains.kotlin.fir.resolve.dfa.RealVariable
import org.jetbrains.kotlin.fir.resolve.providers.symbolProvider
import org.jetbrains.kotlin.fir.symbols.FirBasedSymbol
import org.jetbrains.kotlin.fir.symbols.impl.FirConstructorSymbol
import org.jetbrains.kotlin.fir.symbols.impl.FirTypeAliasSymbol
import org.jetbrains.kotlin.fir.symbols.impl.FirTypeParameterSymbol
@@ -98,7 +98,7 @@ val FirConstructorSymbol.typeAliasForConstructor: FirTypeAliasSymbol?
interface FirCodeFragmentContext {
val towerDataContext: FirTowerDataContext
val variables: Map<FirBasedSymbol<*>, Set<ConeKotlinType>>
val smartCasts: Map<RealVariable, Set<ConeKotlinType>>
}
private object CodeFragmentContextDataKey : FirDeclarationDataKey()
@@ -144,7 +144,7 @@ class VariableStorageImpl(private val session: FirSession) : VariableStorage() {
extensionReceiver = if (extensionReceiver == from) to else extensionReceiver,
)
}
return _realVariables.getOrPut(newIdentifier) {
return getOrPut(newIdentifier) {
with(variable) {
RealVariable(
newIdentifier, isThisReference, if (explicitReceiverVariable == from) to else explicitReceiverVariable,
@@ -154,6 +154,10 @@ class VariableStorageImpl(private val session: FirSession) : VariableStorage() {
}
}
fun getOrPut(identifier: Identifier, factory: () -> RealVariable): RealVariable {
return _realVariables.getOrPut(identifier, factory)
}
private fun FirBasedSymbol<*>.getStability(originalFir: FirElement): PropertyStability? {
if (originalFir is FirThisReceiverExpression) return PropertyStability.STABLE_VALUE
when (this) {