diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/body/resolve/FirAbstractBodyResolveTransformer.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/body/resolve/FirAbstractBodyResolveTransformer.kt index e0b65140c7c..61e6e39e28c 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/body/resolve/FirAbstractBodyResolveTransformer.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/body/resolve/FirAbstractBodyResolveTransformer.kt @@ -5,6 +5,7 @@ package org.jetbrains.kotlin.fir.resolve.transformers.body.resolve +import kotlinx.collections.immutable.PersistentList import kotlinx.collections.immutable.persistentListOf import org.jetbrains.kotlin.fir.* import org.jetbrains.kotlin.fir.declarations.* @@ -121,18 +122,23 @@ abstract class FirAbstractBodyResolveTransformer(phase: FirResolvePhase) : FirAb @set:PrivateForInline var implicitReceiverStack: MutableImplicitReceiverStack = ImplicitReceiverStackImpl() + val containerIfAny: FirDeclaration? + get() = containers.lastOrNull() + @set:PrivateForInline - var containerIfAny: FirDeclaration? = null + var containers: PersistentList = persistentListOf() val localContextForAnonymousFunctions: MutableMap = mutableMapOf() @OptIn(PrivateForInline::class) inline fun withContainer(declaration: FirDeclaration, crossinline f: () -> T): T { - val prevContainer = containerIfAny - containerIfAny = declaration - val result = f() - containerIfAny = prevContainer - return result + val oldContainers = containers + containers = containers.add(declaration) + return try { + f() + } finally { + containers = oldContainers + } } @OptIn(PrivateForInline::class) @@ -207,7 +213,7 @@ abstract class FirAbstractBodyResolveTransformer(phase: FirResolvePhase) : FirAb file = this@BodyResolveContext.file implicitReceiverStack = this@BodyResolveContext.implicitReceiverStack localContextForAnonymousFunctions.putAll(this@BodyResolveContext.localContextForAnonymousFunctions) - containerIfAny = this@BodyResolveContext.containerIfAny + containers = this@BodyResolveContext.containers } }