From 7af43176ce059288faa39757cc41dae3c9b6fc7d Mon Sep 17 00:00:00 2001 From: Dmitriy Novozhilov Date: Fri, 28 Oct 2022 14:23:20 +0300 Subject: [PATCH] [FIR] Add optin on API of updating type of receiver values. KT-54708 --- .../kotlin/fir/resolve/calls/FirReceivers.kt | 13 +++++++++++-- .../resolve/inference/FirBuilderInferenceSession.kt | 3 ++- .../fir/resolve/PersistentImplicitReceiverStack.kt | 3 ++- .../box/inference/builderInference/kt51988.kt | 1 + .../coroutines/inference/kt35684.fir.kt | 1 + .../testsWithStdLib/coroutines/inference/kt35684.kt | 1 + 6 files changed, 18 insertions(+), 4 deletions(-) diff --git a/compiler/fir/providers/src/org/jetbrains/kotlin/fir/resolve/calls/FirReceivers.kt b/compiler/fir/providers/src/org/jetbrains/kotlin/fir/resolve/calls/FirReceivers.kt index 5751c41b7ee..5d75d2d49d9 100644 --- a/compiler/fir/providers/src/org/jetbrains/kotlin/fir/resolve/calls/FirReceivers.kt +++ b/compiler/fir/providers/src/org/jetbrains/kotlin/fir/resolve/calls/FirReceivers.kt @@ -19,7 +19,6 @@ import org.jetbrains.kotlin.fir.resolve.ScopeSession import org.jetbrains.kotlin.fir.resolve.constructType import org.jetbrains.kotlin.fir.resolve.scope import org.jetbrains.kotlin.fir.resolve.smartcastScope -import org.jetbrains.kotlin.fir.resolvedTypeFromPrototype import org.jetbrains.kotlin.fir.scopes.FakeOverrideTypeCalculator import org.jetbrains.kotlin.fir.scopes.FirTypeScope import org.jetbrains.kotlin.fir.symbols.FirBasedSymbol @@ -100,10 +99,20 @@ sealed class ImplicitReceiverValue>( final override var receiverExpression: FirExpression = originalReceiverExpression private set + @RequiresOptIn + annotation class ImplicitReceiverInternals + + @OptIn(ImplicitReceiverInternals::class) + @Deprecated(level = DeprecationLevel.ERROR, message = "Builder inference should not modify implicit receivers. KT-54708") + fun updateTypeInBuilderInference(type: ConeKotlinType) { + updateTypeFromSmartcast(type) + } + /* * Should be called only in ImplicitReceiverStack */ - fun replaceType(type: ConeKotlinType) { + @ImplicitReceiverInternals + fun updateTypeFromSmartcast(type: ConeKotlinType) { if (type == this.type) return if (!mutable) throw IllegalStateException("Cannot mutate an immutable ImplicitReceiverValue") this.type = type diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/FirBuilderInferenceSession.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/FirBuilderInferenceSession.kt index 0ecdb3e83df..9195ddc96aa 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/FirBuilderInferenceSession.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/FirBuilderInferenceSession.kt @@ -242,7 +242,8 @@ class FirBuilderInferenceSession( lambda.transformSingle(stubTypeSubstitutor, null) for (receiver in lambdaImplicitReceivers) { - receiver.replaceType(substitutor.substituteOrSelf(receiver.type)) + @Suppress("DEPRECATION_ERROR") + receiver.updateTypeInBuilderInference(substitutor.substituteOrSelf(receiver.type)) } // TODO: support diagnostics, see [CoroutineInferenceSession#updateCalls] diff --git a/compiler/fir/semantics/src/org/jetbrains/kotlin/fir/resolve/PersistentImplicitReceiverStack.kt b/compiler/fir/semantics/src/org/jetbrains/kotlin/fir/resolve/PersistentImplicitReceiverStack.kt index dcdd1f5a3e0..90eda405bd1 100644 --- a/compiler/fir/semantics/src/org/jetbrains/kotlin/fir/resolve/PersistentImplicitReceiverStack.kt +++ b/compiler/fir/semantics/src/org/jetbrains/kotlin/fir/resolve/PersistentImplicitReceiverStack.kt @@ -100,9 +100,10 @@ class PersistentImplicitReceiverStack private constructor( // This method is only used from DFA and it's in some sense breaks persistence contracts of the data structure // But it's ok since DFA handles everything properly yet, but still may be it should be rewritten somehow + @OptIn(ImplicitReceiverValue.ImplicitReceiverInternals::class) fun replaceReceiverType(index: Int, type: ConeKotlinType) { assert(index >= 0 && index < stack.size) - stack[index].replaceType(type) + stack[index].updateTypeFromSmartcast(type) } fun createSnapshot(): PersistentImplicitReceiverStack { diff --git a/compiler/testData/codegen/box/inference/builderInference/kt51988.kt b/compiler/testData/codegen/box/inference/builderInference/kt51988.kt index cf3af2230b9..d47e24cd968 100644 --- a/compiler/testData/codegen/box/inference/builderInference/kt51988.kt +++ b/compiler/testData/codegen/box/inference/builderInference/kt51988.kt @@ -1,3 +1,4 @@ +// IGNORE_LEAKED_INTERNAL_TYPES: KT-54708 // WITH_STDLIB import kotlin.experimental.ExperimentalTypeInference diff --git a/compiler/testData/diagnostics/testsWithStdLib/coroutines/inference/kt35684.fir.kt b/compiler/testData/diagnostics/testsWithStdLib/coroutines/inference/kt35684.fir.kt index 2ec35f51572..49460a455d1 100644 --- a/compiler/testData/diagnostics/testsWithStdLib/coroutines/inference/kt35684.fir.kt +++ b/compiler/testData/diagnostics/testsWithStdLib/coroutines/inference/kt35684.fir.kt @@ -1,3 +1,4 @@ +// IGNORE_LEAKED_INTERNAL_TYPES: KT-54708 // !OPT_IN: kotlin.RequiresOptIn // !DIAGNOSTICS: -UNUSED_PARAMETER // ISSUE: KT-35684 diff --git a/compiler/testData/diagnostics/testsWithStdLib/coroutines/inference/kt35684.kt b/compiler/testData/diagnostics/testsWithStdLib/coroutines/inference/kt35684.kt index cde719d0e78..1af4850a359 100644 --- a/compiler/testData/diagnostics/testsWithStdLib/coroutines/inference/kt35684.kt +++ b/compiler/testData/diagnostics/testsWithStdLib/coroutines/inference/kt35684.kt @@ -1,3 +1,4 @@ +// IGNORE_LEAKED_INTERNAL_TYPES: KT-54708 // !OPT_IN: kotlin.RequiresOptIn // !DIAGNOSTICS: -UNUSED_PARAMETER // ISSUE: KT-35684