From 0c13d3197ce213c9e10e1958003b860df282373a Mon Sep 17 00:00:00 2001 From: Mikhail Glukhikh Date: Mon, 14 Sep 2020 19:05:18 +0300 Subject: [PATCH] [FIR] Fix non-serializable type argument at the end of resolve --- .../resolveWithStdlib/withInInitializer.txt | 4 +-- .../FirDeclarationsResolveTransformer.kt | 35 +++++++++++-------- .../commonSupertypeContravariant2.kt | 1 - ...initelyNotNullTypeInvariantPosition.fir.kt | 2 +- 4 files changed, 24 insertions(+), 18 deletions(-) diff --git a/compiler/fir/analysis-tests/testData/resolveWithStdlib/withInInitializer.txt b/compiler/fir/analysis-tests/testData/resolveWithStdlib/withInInitializer.txt index ff724bdb98d..c12874a2167 100644 --- a/compiler/fir/analysis-tests/testData/resolveWithStdlib/withInInitializer.txt +++ b/compiler/fir/analysis-tests/testData/resolveWithStdlib/withInInitializer.txt @@ -13,8 +13,8 @@ FILE: withInInitializer.kt super() } - public final val list: R|kotlin/collections/List & java/io/Serializable)>| = R|kotlin/collections/listOf| & java/io/Serializable)|>(vararg(Int(1), Int(2), Int(3), String())) - public get(): R|kotlin/collections/List & java/io/Serializable)>| + public final val list: R|kotlin/collections/List| = R|kotlin/collections/listOf| & java/io/Serializable)|>(vararg(Int(1), Int(2), Int(3), String())) + public get(): R|kotlin/collections/List| public final val data: R|First| = R|/First.First|(Int(42)) public get(): R|First| diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/body/resolve/FirDeclarationsResolveTransformer.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/body/resolve/FirDeclarationsResolveTransformer.kt index af6ed4f9a79..13c4ffb76fe 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/body/resolve/FirDeclarationsResolveTransformer.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/body/resolve/FirDeclarationsResolveTransformer.kt @@ -968,6 +968,19 @@ open class FirDeclarationsResolveTransformer(transformer: FirBodyResolveTransfor } } + private fun ConeKotlinType.requiresApproximationInPublicPosition(): Boolean { + return when (this) { + is ConeIntegerLiteralType, + is ConeCapturedType, + is ConeDefinitelyNotNullType, + is ConeIntersectionType -> true + is ConeClassLikeType -> typeArguments.any { + it is ConeKotlinTypeProjection && it.type.requiresApproximationInPublicPosition() + } + else -> false + } + } + private fun FirTypeRef.approximateTypeIfNeeded( containingCallableVisibility: Visibility?, isInlineFunction: Boolean = false @@ -975,20 +988,14 @@ open class FirDeclarationsResolveTransformer(transformer: FirBodyResolveTransfor val approximatedType = if (this is FirResolvedTypeRef && (containingCallableVisibility == Public || containingCallableVisibility == Protected) ) { - when (this.type) { - is ConeIntegerLiteralType, - is ConeCapturedType, - is ConeDefinitelyNotNullType, - is ConeIntersectionType -> { - this.withReplacedConeType( - inferenceComponents.approximator.approximateToSuperType( - this.type, TypeApproximatorConfiguration.PublicDeclaration - ) as ConeKotlinType - ) - } - else -> { - this - } + if (type.requiresApproximationInPublicPosition()) { + this.withReplacedConeType( + inferenceComponents.approximator.approximateToSuperType( + this.type, TypeApproximatorConfiguration.PublicDeclaration + ) as? ConeKotlinType + ) + } else { + this } } else { this diff --git a/compiler/testData/codegen/box/regressions/commonSupertypeContravariant2.kt b/compiler/testData/codegen/box/regressions/commonSupertypeContravariant2.kt index 15b8f2e9c6e..959eaf527aa 100644 --- a/compiler/testData/codegen/box/regressions/commonSupertypeContravariant2.kt +++ b/compiler/testData/codegen/box/regressions/commonSupertypeContravariant2.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND_FIR: JVM_IR interface In class A : In class B : In diff --git a/compiler/testData/diagnostics/tests/inference/constraints/definitelyNotNullTypeInvariantPosition.fir.kt b/compiler/testData/diagnostics/tests/inference/constraints/definitelyNotNullTypeInvariantPosition.fir.kt index 59f44e66b4e..99bd63b2afa 100644 --- a/compiler/testData/diagnostics/tests/inference/constraints/definitelyNotNullTypeInvariantPosition.fir.kt +++ b/compiler/testData/diagnostics/tests/inference/constraints/definitelyNotNullTypeInvariantPosition.fir.kt @@ -20,5 +20,5 @@ fun test(i: Int, s: S) { c - takeInvInt(create(i)) + takeInvInt(create(i)) }