Refactoring: Improve createExpectActualTypeParameterSubstitutor API

Review: https://jetbrains.team/p/kt/reviews/12279/files

Motivation: make sure that cases like KT-62027 won't happen again
Review: https://jetbrains.team/p/kt/reviews/12279/files

Now it's responsibility of the
`createExpectActualTypeParameterSubstitutor` calller to think about the
case when parameters size isn't equal. You must not be able to create a
substitutor if type parameters sizes are not equal

Improvement in `createExpectActualTypeParameterSubstitutor` API also
improves
`AbstractExpectActualCompatibilityChecker.getCallablesCompatibility`
API.

Because suppose that you accidentally created a redundant wrapping
substitutor => you need to handle the case of not equal type parameters
size on the call site => you start thinking why you should do that on
the call site? It must be a responsibility of
`getCallablesCompatibility` => you realize that you created a redundant
wrapping substitutor
This commit is contained in:
Nikita Bobko
2023-09-20 14:17:59 +02:00
committed by Space Team
parent 211102569c
commit 91a337074e
12 changed files with 48 additions and 62 deletions
@@ -36,8 +36,6 @@ import org.jetbrains.kotlin.types.model.KotlinTypeMarker
import org.jetbrains.kotlin.types.model.SimpleTypeMarker
import org.jetbrains.kotlin.types.model.TypeSubstitutorMarker
import org.jetbrains.kotlin.types.model.TypeSystemContext
import org.jetbrains.kotlin.utils.addToStdlib.UnsafeCastFunction
import org.jetbrains.kotlin.utils.addToStdlib.castAll
class FirExpectActualMatchingContextImpl private constructor(
private val actualSession: FirSession,
@@ -147,15 +145,13 @@ class FirExpectActualMatchingContextImpl private constructor(
override val PropertySymbolMarker.setter: FunctionSymbolMarker?
get() = asSymbol().setterSymbol
@OptIn(UnsafeCastFunction::class)
override fun createExpectActualTypeParameterSubstitutor(
expectTypeParameters: List<TypeParameterSymbolMarker>,
actualTypeParameters: List<TypeParameterSymbolMarker>,
expectActualTypeParameters: List<Pair<TypeParameterSymbolMarker, TypeParameterSymbolMarker>>,
parentSubstitutor: TypeSubstitutorMarker?,
): TypeSubstitutorMarker {
@Suppress("UNCHECKED_CAST")
return createExpectActualTypeParameterSubstitutor(
expectTypeParameters.castAll<FirTypeParameterSymbol>(),
actualTypeParameters.castAll<FirTypeParameterSymbol>(),
expectActualTypeParameters as List<Pair<FirTypeParameterSymbol, FirTypeParameterSymbol>>,
actualSession,
parentSubstitutor as ConeSubstitutor?
)
@@ -19,6 +19,7 @@ import org.jetbrains.kotlin.fir.types.*
import org.jetbrains.kotlin.mpp.CallableSymbolMarker
import org.jetbrains.kotlin.resolve.calls.mpp.AbstractExpectActualCompatibilityChecker
import org.jetbrains.kotlin.resolve.multiplatform.ExpectActualCompatibility
import org.jetbrains.kotlin.utils.zipIfSizesAreEqual
object FirExpectActualResolver {
fun findExpectForActual(
@@ -46,14 +47,8 @@ object FirExpectActualResolver {
val expectTypeParameters = expectContainingClass?.typeParameterSymbols.orEmpty()
val actualTypeParameters = actualContainingClass?.typeParameterSymbols.orEmpty()
parentSubstitutor = when (expectTypeParameters.size == actualTypeParameters.size) {
true -> createExpectActualTypeParameterSubstitutor(
expectTypeParameters,
actualTypeParameters,
useSiteSession,
)
false -> null
}
parentSubstitutor = (expectTypeParameters zipIfSizesAreEqual actualTypeParameters)
?.let { createExpectActualTypeParameterSubstitutor(it, useSiteSession) }
when (actualSymbol) {
is FirConstructorSymbol -> expectContainingClass?.getConstructors(scopeSession)