FIR IDE: collect snapshot of FirTowerDataContext for statements

ImplicitReceiverValue is mutable and FIR body resolve could alter it
while analysing code with smartcast. Hence, previously the IDE may see
inconsistent receiver values for a local scope. For example

```
open class A
interface Foo {
    fun foo()
}
fun A.bar() {
    if (this is Foo) {
        // scope here has implicit receiver type to be `A` rather than `it<A, Foo>`
    }
}
```

This change creates snapshots for local statements so later changes
during body resolve won't affect the collected context.
This commit is contained in:
Tianyu Geng
2021-08-19 11:56:46 -07:00
committed by Ilya Kirillov
parent aad02c1259
commit e5b9d667c0
4 changed files with 49 additions and 12 deletions
@@ -37,7 +37,8 @@ internal class FirTowerDataContextAllElementsCollector : FirTowerDataContextColl
override fun addStatementContext(statement: FirStatement, context: FirTowerDataContext) {
val closestStatementInBlock = statement.psi?.closestBlockLevelOrInitializerExpression() ?: return
elementsToContext[closestStatementInBlock] = context
// FIR body transform may alter the context if there are implicit receivers with smartcast
elementsToContext[closestStatementInBlock] = context.createSnapshot()
}
override fun addDeclarationContext(declaration: FirDeclaration, context: FirTowerDataContext) {