From f53cd222535c4934e063b461a440189e5cace04f Mon Sep 17 00:00:00 2001 From: Mikhail Glukhikh Date: Thu, 23 Dec 2021 10:20:08 +0300 Subject: [PATCH] FIR: copy builder inference session when creating snapshot for locals This commit fixes one builder inference case from KT-49925 --- .../inference/problems/buildSetWithVisitor.fir.txt | 4 ++-- .../inference/problems/buildSetWithVisitor.kt | 2 +- .../transformers/body/resolve/BodyResolveContext.kt | 8 ++++++++ .../builderCallAsReturnTypeInLocalClass.kt | 2 -- .../codegen/box/inference/builderInference/kt48633.kt | 2 -- 5 files changed, 11 insertions(+), 7 deletions(-) diff --git a/compiler/fir/analysis-tests/testData/resolveWithStdlib/inference/problems/buildSetWithVisitor.fir.txt b/compiler/fir/analysis-tests/testData/resolveWithStdlib/inference/problems/buildSetWithVisitor.fir.txt index b5d0c2442ef..e384c9932d0 100644 --- a/compiler/fir/analysis-tests/testData/resolveWithStdlib/inference/problems/buildSetWithVisitor.fir.txt +++ b/compiler/fir/analysis-tests/testData/resolveWithStdlib/inference/problems/buildSetWithVisitor.fir.txt @@ -19,8 +19,8 @@ FILE: buildSetWithVisitor.kt public final fun R|Wrapper|.accept(visitor: R|AnyVisitor|): R|kotlin/Unit| { R|/visitor|.R|/AnyVisitor.visit|(this@R|/accept|) } - public final fun bar(wrapper: R|Wrapper|): R|kotlin/collections/Set| { - ^bar #( = buildSet@fun R|kotlin/collections/MutableSet|.(): R|kotlin/Unit| { + public final fun bar(wrapper: R|Wrapper|): R|kotlin/collections/Set| { + ^bar R|kotlin/collections/buildSet|( = buildSet@fun R|kotlin/collections/MutableSet|.(): R|kotlin/Unit| { R|/wrapper|.R|/accept|(object : R|AnyVisitor| { private constructor(): R|| { super() diff --git a/compiler/fir/analysis-tests/testData/resolveWithStdlib/inference/problems/buildSetWithVisitor.kt b/compiler/fir/analysis-tests/testData/resolveWithStdlib/inference/problems/buildSetWithVisitor.kt index 3dd0aa5fd14..72e7b3f4265 100644 --- a/compiler/fir/analysis-tests/testData/resolveWithStdlib/inference/problems/buildSetWithVisitor.kt +++ b/compiler/fir/analysis-tests/testData/resolveWithStdlib/inference/problems/buildSetWithVisitor.kt @@ -8,7 +8,7 @@ fun Wrapper.accept(visitor: AnyVisitor) { visitor.visit(this) } -fun bar(wrapper: Wrapper) = buildSet { +fun bar(wrapper: Wrapper) = buildSet { wrapper.accept(object : AnyVisitor() { override fun visit(arg: Wrapper) { add(arg.tag) diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/body/resolve/BodyResolveContext.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/body/resolve/BodyResolveContext.kt index 06e2b735f73..1ee91a60879 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/body/resolve/BodyResolveContext.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/body/resolve/BodyResolveContext.kt @@ -23,6 +23,7 @@ import org.jetbrains.kotlin.fir.resolve.calls.InaccessibleImplicitReceiverValue import org.jetbrains.kotlin.fir.resolve.calls.ResolutionContext import org.jetbrains.kotlin.fir.resolve.dfa.DataFlowAnalyzerContext import org.jetbrains.kotlin.fir.resolve.dfa.PersistentFlow +import org.jetbrains.kotlin.fir.resolve.inference.FirBuilderInferenceSession import org.jetbrains.kotlin.fir.resolve.inference.FirCallCompleter import org.jetbrains.kotlin.fir.resolve.inference.FirDelegatedPropertyInferenceSession import org.jetbrains.kotlin.fir.resolve.inference.FirInferenceSession @@ -348,6 +349,13 @@ class BodyResolveContext( containingClass = this@BodyResolveContext.containingClass replaceTowerDataContext(this@BodyResolveContext.towerDataContext) anonymousFunctionsAnalyzedInDependentContext.addAll(this@BodyResolveContext.anonymousFunctionsAnalyzedInDependentContext) + // Looks like we should copy this session only for builder inference to be able + // to use information from local class inside it. + // However, we should not copy other kinds of inference sessions, + // otherwise we can "inherit" type variables from there provoking inference problems + if (this@BodyResolveContext.inferenceSession is FirBuilderInferenceSession) { + inferenceSession = this@BodyResolveContext.inferenceSession + } } // withElement PUBLIC API diff --git a/compiler/testData/codegen/box/inference/builderInference/builderCallAsReturnTypeInLocalClass.kt b/compiler/testData/codegen/box/inference/builderInference/builderCallAsReturnTypeInLocalClass.kt index 4868380e76b..e46d9e093e7 100644 --- a/compiler/testData/codegen/box/inference/builderInference/builderCallAsReturnTypeInLocalClass.kt +++ b/compiler/testData/codegen/box/inference/builderInference/builderCallAsReturnTypeInLocalClass.kt @@ -1,6 +1,4 @@ // WITH_STDLIB -// IGNORE_BACKEND_FIR: JVM_IR -// FIR status: NEW_INFERENCE_NO_INFORMATION_FOR_PARAMETER on buildList calls (E) @OptIn(ExperimentalStdlibApi::class) fun foo1() { buildList { diff --git a/compiler/testData/codegen/box/inference/builderInference/kt48633.kt b/compiler/testData/codegen/box/inference/builderInference/kt48633.kt index 9a4c2377be5..fc7fbca112f 100644 --- a/compiler/testData/codegen/box/inference/builderInference/kt48633.kt +++ b/compiler/testData/codegen/box/inference/builderInference/kt48633.kt @@ -1,6 +1,4 @@ // DONT_TARGET_EXACT_BACKEND: WASM -// IGNORE_BACKEND_FIR: JVM_IR -// FIR status: NEW_INFERENCE_NO_INFORMATION_FOR_PARAMETER on buildList call (E) // WITH_STDLIB class TowerDataElementsForName() {